set test "register_x86"
set testpath "$srcdir/$subdir"

if {! [installtest_p]} { untested $test; return }
if {! [uretprobes_p]} { untested $test; return }
set arch [exec uname -i]
if {$arch ne "i386" && $arch ne "x86_64"} { untested $test; return }

# --- TEST 1 ---

set subtest1 "TEST 1: 8-bit and 16-bit registers for eax"

set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
    "additional_flags=-O additional_flags=-g additional_flags=-O0"]
if {$res ne ""} {
    verbose "target_compile failed: $res" 2
    fail "$test: $subtest1: unable to compile ${test}_1.c"
} else {
    foreach runtime [get_runtime_list] {
        if {$runtime eq ""} {
            set runtime "kernel"
        }
        set test_name "$test: $subtest1 ($runtime)"
        set cmd "stap --runtime=$runtime -c ./a.out '$srcdir/$subdir/${test}_1.stp'"
        set exit_code [run_cmd_2way $cmd out stderr]
        set exp_out "eax = 0xffffffffbeefdead
u eax = 0xbeefdead

ax = 0xffffffffffffdead
u ax = 0xdead

al = 0xffffffffffffffad
u al = 0xad

ah = 0xffffffffffffffde
u ah = 0xde
"
        is "${test_name}: stdout" $out $exp_out
        is "${test_name}: exit code" $exit_code 0
        if {$stderr ne ""} {
            send_log "stderr:\n$stderr"
        }
    }
}

# --- TEST 2 ---

set subtest2 "TEST 2: 8-bit and 16-bit registers for edx"

set res [target_compile ${testpath}/${test}_2.c ./a.out executable \
    "additional_flags=-O additional_flags=-g additional_flags=-O0"]
if {$res ne ""} {
    verbose "target_compile failed: $res" 2
    fail "$test: $subtest2: unable to compile ${test}_2.c"
} else {
    foreach runtime [get_runtime_list] {
        if {$runtime eq ""} {
            set runtime "kernel"
        }
        set test_name "$test: $subtest2 ($runtime)"
        set cmd "stap --runtime=$runtime -c ./a.out '$srcdir/$subdir/${test}_2.stp'"
        set exit_code [run_cmd_2way $cmd out stderr]
        set exp_out "edx = 0xffffffffbeefdead
u edx = 0xbeefdead

dx = 0xffffffffffffdead
u dx = 0xdead

dl = 0xffffffffffffffad
u dl = 0xad

dh = 0xffffffffffffffde
u dh = 0xde
"
        is "${test_name}: stdout" $out $exp_out
        is "${test_name}: exit code" $exit_code 0
        if {$stderr ne ""} {
            send_log "stderr:\n$stderr"
        }
    }
}

# --- TEST 3 ---

set subtest3 "TEST 3: 8-bit and 16-bit registers for ecx"

set res [target_compile ${testpath}/${test}_3.c ./a.out executable \
    "additional_flags=-O additional_flags=-g additional_flags=-O0"]
if {$res ne ""} {
    verbose "target_compile failed: $res" 2
    fail "$test: $subtest3: unable to compile ${test}_3.c"
} else {
    foreach runtime [get_runtime_list] {
        if {$runtime eq ""} {
            set runtime "kernel"
        }
        set test_name "$test: $subtest3 ($runtime)"
        set cmd "stap --runtime=$runtime -c ./a.out '$srcdir/$subdir/${test}_3.stp'"
        set exit_code [run_cmd_2way $cmd out stderr]
        set exp_out "ecx = 0xffffffffbeefdead
u ecx = 0xbeefdead

cx = 0xffffffffffffdead
u cx = 0xdead

cl = 0xffffffffffffffad
u cl = 0xad

ch = 0xffffffffffffffde
u ch = 0xde
"
        is "${test_name}: stdout" $out $exp_out
        is "${test_name}: exit code" $exit_code 0
        if {$stderr ne ""} {
            send_log "stderr:\n$stderr"
        }
    }
}

# --- TEST 4 ---

set subtest4 "TEST 4: 8-bit and 16-bit registers for eax"

set res [target_compile ${testpath}/${test}_4.c ./a.out executable \
    "additional_flags=-O additional_flags=-g additional_flags=-O0"]
if {$res ne ""} {
    verbose "target_compile failed: $res" 2
    fail "$test: $subtest4: unable to compile ${test}_4.c"
} else {
    foreach runtime [get_runtime_list] {
        if {$runtime eq ""} {
            set runtime "kernel"
        }
        set test_name "$test: $subtest4 ($runtime)"
        set cmd "stap --runtime=$runtime -c ./a.out '$srcdir/$subdir/${test}_4.stp'"
        set exit_code [run_cmd_2way $cmd out stderr]
        set exp_out "ebx = 0xffffffffbeefdead
u ebx = 0xbeefdead

bx = 0xffffffffffffdead
u bx = 0xdead

bl = 0xffffffffffffffad
u bl = 0xad

bh = 0xffffffffffffffde
u bh = 0xde
"
        is "${test_name}: stdout" $out $exp_out
        is "${test_name}: exit code" $exit_code 0
        if {$stderr ne ""} {
            send_log "stderr:\n$stderr"
        }
    }
}
