#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct user_regs_struct {
  scalar_t__ orig_rax;
  int /*<<< orphan*/ *rip;
};
struct u {
  long val;
  char *chars;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ PTRACE_SINGLESTEP;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
scalar_t__ SYS_write;
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int /*<<< orphan*/ *,
                           long);
long ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/ *,
            struct user_regs_struct *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait(int *);
int main(void) {
  pid_t child;
  const int long_size = sizeof(long);
  child = fork();
  if (child == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    execl("/bin/ls", "ls", NULL);
  } else {
    int status;
    union u {
      long val;
      char chars[long_size];
    } data;
    struct user_regs_struct regs;
    int start = 0;
    long ins;
    while (1) {
      wait(&status);
      if (WIFEXITED(status)) {
        break;
      }
      ptrace(PTRACE_GETREGS, child, NULL, &regs);
      if (start == 1) {
        start = 0;
        ins = ptrace(PTRACE_PEEKTEXT, child, regs.rip, NULL);
        fprintf(stdout,
                "RIP: %lx Instruction "
                "executed : %lx \n",
                regs.rip, ins);
      }
      if (regs.orig_rax == SYS_write) {
        start = 1;
        ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
      } else {
        ptrace(PTRACE_SYSCALL, child, NULL, NULL);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x8,-0x14(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <main+0x60>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <ptrace@plt>
lea    0xe55(%rip),%rdi
lea    0xe53(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <execl@plt>
jmp    12a9 <main+0x149>
movl   $0x0,-0x34(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x18(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e7 <main+0x87>
jmp    12a4 <main+0x144>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x30(%rbp),%rcx
call   1040 <ptrace@plt>
cmpl   $0x1,-0x34(%rbp)
jne    124d <main+0xed>
movl   $0x0,-0x34(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xdc0(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
jne    1286 <main+0x126>
movl   $0x1,-0x34(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
jmp    129f <main+0x13f>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <ptrace@plt>
jmp    11c7 <main+0x67>
jmp    12a9 <main+0x149>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1260 <main+0x100>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    1289 <main+0x129>
xor    %r13d,%r13d
lea    0x8(%rsp),%r14
mov    0x2e3c(%rip),%rbp
mov    0x2df5(%rip),%r15
lea    0x4(%rsp),%r12
jmp    11eb <main+0x8b>
nopw   0x0(%rax,%rax,1)
mov    %cl,%r13b
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %r12,%rdi
call   1070 <wait@plt>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    1289 <main+0x129>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
mov    %r14,%rcx
call   1040 <ptrace@plt>
cmp    $0x1,%r13d
jne    123c <main+0xdc>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rdx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    0x2d9a(%rip),%rcx
mov    (%rcx),%edi
mov    0x10(%rsp),%rdx
lea    0xdd4(%rip),%rsi
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    0x8(%rsp),%rcx
xor    %r13d,%r13d
mov    %r15,%rax
cmp    0x0(%rbp),%rcx
sete   %cl
jne    11c0 <main+0x60>
mov    0x2d6d(%rip),%rax
jmp    11c0 <main+0x60>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xd85(%rip),%rdi
lea    0xd83(%rip),%rsi
xor    %edx,%edx
call   1060 <execl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.u = type { i64 }
%struct.user_regs_struct = type { i64, i32* }
@PTRACE_TRACEME = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@stdout = global i32 0
@.str.2 = [38 x i8] c"RIP: %lx Instruction executed : %lx \0A\00"
@SYS_write = global i64 0
@PTRACE_SINGLESTEP = global i32 0
@PTRACE_SYSCALL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca %union.u
%6 = alloca %struct.user_regs_struct
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %1
store i32 8, i32* %3
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @PTRACE_TRACEME
%14 = call i64 @ptrace(i32 %13, i64 0, i32* null, %struct.user_regs_struct* null)
%15 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %55
16: ; preds = %0
store i32 0, i32* %7
br label %17
17: ; preds = %16, %53
%18 = call i32 @wait(i32* %4)
%19 = load i32, i32* %4
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %54
23: ; preds = %17
%24 = load i32, i32* @PTRACE_GETREGS
%25 = load i64, i64* %2
%26 = call i64 @ptrace(i32 %24, i64 %25, i32* null, %struct.user_regs_struct* %6)
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %40
29: ; preds = %23
store i32 0, i32* %7
%30 = load i32, i32* @PTRACE_PEEKTEXT
%31 = load i64, i64* %2
%32 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 1
%33 = load i32*, i32** %32
%34 = call i64 @ptrace(i32 %30, i64 %31, i32* %33, %struct.user_regs_struct* null)
store i64 %34, i64* %8
%35 = load i32, i32* @stdout
%36 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 1
%37 = load i32*, i32** %36
%38 = load i64, i64* %8
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32* %37, i64 %38)
br label %40
40: ; preds = %29, %23
%41 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load i64, i64* @SYS_write
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %49
45: ; preds = %40
store i32 1, i32* %7
%46 = load i32, i32* @PTRACE_SINGLESTEP
%47 = load i64, i64* %2
%48 = call i64 @ptrace(i32 %46, i64 %47, i32* null, %struct.user_regs_struct* null)
br label %53
49: ; preds = %40
%50 = load i32, i32* @PTRACE_SYSCALL
%51 = load i64, i64* %2
%52 = call i64 @ptrace(i32 %50, i64 %51, i32* null, %struct.user_regs_struct* null)
br label %53
53: ; preds = %49, %45
br label %17
54: ; preds = %22
br label %55
55: ; preds = %54, %12
ret i32 0
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i64, i32* }
@PTRACE_TRACEME = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@stdout = global i32 0
@.str.2 = [38 x i8] c"RIP: %lx Instruction executed : %lx \0A\00"
@SYS_write = global i64 0
@PTRACE_SINGLESTEP = global i32 0
@PTRACE_SYSCALL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.user_regs_struct
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @PTRACE_TRACEME
%7 = tail call i64 @ptrace(i32 %6, i64 0, i32* null, %struct.user_regs_struct* null)
%8 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %45
9: ; preds = %0
%10 = bitcast i32* %1 to i8*
%11 = bitcast %struct.user_regs_struct* %2 to i8*
%12 = call i32 @wait(i32* %1)
%13 = load i32, i32* %1
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %44
16: ; preds = %9
%17 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %2, i64 0, i32 1
%18 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %2, i64 0, i32 0
br label %19
19: ; preds = %16, %31
%20 = phi i32 [ 0, %16 ], [ %35, %31 ]
%21 = load i32, i32* @PTRACE_GETREGS
%22 = call i64 @ptrace(i32 %21, i64 %3, i32* null, %struct.user_regs_struct* %2)
%23 = icmp eq i32 %20, 1
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i32, i32* @PTRACE_PEEKTEXT
%26 = load i32*, i32** %17
%27 = call i64 @ptrace(i32 %25, i64 %3, i32* %26, %struct.user_regs_struct* null)
%28 = load i32, i32* @stdout
%29 = load i32*, i32** %17
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32* %29, i64 %27)
br label %31
31: ; preds = %24, %19
%32 = load i64, i64* %18
%33 = load i64, i64* @SYS_write
%34 = icmp eq i64 %32, %33
%35 = zext i1 %34 to i32
%36 = load i32, i32* @PTRACE_SINGLESTEP
%37 = load i32, i32* @PTRACE_SYSCALL
%38 = select i1 %34, i32 %36, i32 %37
%39 = call i64 @ptrace(i32 %38, i64 %3, i32* null, %struct.user_regs_struct* null)
%40 = call i32 @wait(i32* %1)
%41 = load i32, i32* %1
%42 = call i64 @WIFEXITED(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %19, label %44
44: ; preds = %31, %9
br label %45
45: ; preds = %44, %5
ret i32 0
}