#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 {
  int /*<<< orphan*/ rip;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ PTRACE_SINGLESTEP;
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ procmsg(char *, ...);
unsigned int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                    struct user_regs_struct *);
int /*<<< orphan*/ wait(int *);
void run_debugger(pid_t child_pid) {
  int wait_status;
  unsigned icounter = 0;
  procmsg("debugger started\n");
  wait(&wait_status);
  while (WIFSTOPPED(wait_status)) {
    icounter++;
    struct user_regs_struct regs;
    ptrace(PTRACE_GETREGS, child_pid, 0, &regs);
    unsigned instr = ptrace(PTRACE_PEEKTEXT, child_pid, regs.rip, 0);
    procmsg("icounter = %u.  RIP = 0x%08x.  instr = 0x%08x\n", icounter,
            regs.rip, instr);
    if (ptrace(PTRACE_SINGLESTEP, child_pid, 0, 0) < 0) {
      perror("ptrace");
      return;
    }
    wait(&wait_status);
  }
  procmsg("the child executed %u instructions\n", icounter);
}


x86 assembly (clang, -O0):
<run_debugger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1030 <procmsg@plt>
lea    -0x8(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x8(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     121c <run_debugger+0xcc>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
lea    -0x10(%rbp),%rcx
call   1050 <ptrace@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <ptrace@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <procmsg@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <ptrace@plt>
cmp    $0x0,%eax
jae    120e <run_debugger+0xbe>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
jmp    122d <run_debugger+0xdd>
lea    -0x8(%rbp),%rdi
call   1070 <wait@plt>
jmp    1179 <run_debugger+0x29>
mov    -0xc(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <procmsg@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_debugger>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0xea9(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <procmsg@plt>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
mov    0xc(%rsp),%edi
call   1040 <WIFSTOPPED@plt>
test   %rax,%rax
je     1201 <run_debugger+0xc1>
mov    0x2e4d(%rip),%r13
lea    0xe88(%rip),%r15
mov    0x2e47(%rip),%r14
lea    0xc(%rsp),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
lea    0x10(%rsp),%rcx
call   1050 <ptrace@plt>
mov    0x0(%r13),%edi
mov    0x10(%rsp),%edx
mov    %ebx,%esi
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    0x10(%rsp),%edx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <procmsg@plt>
mov    (%r14),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    %r12,%rdi
call   1060 <wait@plt>
mov    0xc(%rsp),%edi
call   1040 <WIFSTOPPED@plt>
test   %rax,%rax
jne    11a0 <run_debugger+0x60>
lea    0xe39(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <procmsg@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@.str = [18 x i8] c"debugger started\0A\00"
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [47 x i8] c"icounter = %u. RIP = 0x%08x. instr = 0x%08x\0A\00"
@PTRACE_SINGLESTEP = global i32 0
@.str.2 = [7 x i8] c"ptrace\00"
@.str.3 = [36 x i8] c"the child executed %u instructions\0A\00"
define void @run_debugger(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.user_regs_struct
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%7 = call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @wait(i32* %3)
br label %9
9: ; preds = %35, %1
%10 = load i32, i32* %3
%11 = call i64 @WIFSTOPPED(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i32, i32* %4
%15 = add i32 %14, 1
store i32 %15, i32* %4
%16 = load i32, i32* @PTRACE_GETREGS
%17 = load i32, i32* %2
%18 = call i32 @ptrace(i32 %16, i32 %17, i32 0, %struct.user_regs_struct* %5)
%19 = load i32, i32* @PTRACE_PEEKTEXT
%20 = load i32, i32* %2
%21 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @ptrace(i32 %19, i32 %20, i32 %22, %struct.user_regs_struct* null)
store i32 %23, i32* %6
%24 = load i32, i32* %4
%25 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %26, i32 %27)
%29 = load i32, i32* @PTRACE_SINGLESTEP
%30 = load i32, i32* %2
%31 = call i32 @ptrace(i32 %29, i32 %30, i32 0, %struct.user_regs_struct* null)
%32 = icmp ult i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %13
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %40
35: ; preds = %13
%36 = call i32 @wait(i32* %3)
br label %9
37: ; preds = %9
%38 = load i32, i32* %4
%39 = call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %37, %33
ret void
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@.str = [18 x i8] c"debugger started\0A\00"
@PTRACE_GETREGS = global i32 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [47 x i8] c"icounter = %u. RIP = 0x%08x. instr = 0x%08x\0A\00"
@PTRACE_SINGLESTEP = global i32 0
@.str.3 = [36 x i8] c"the child executed %u instructions\0A\00"
define void @run_debugger(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.user_regs_struct
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @wait(i32* %2)
%7 = load i32, i32* %2
%8 = call i64 @WIFSTOPPED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %1
%11 = bitcast %struct.user_regs_struct* %3 to i8*
%12 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %3, i64 0, i32 0
br label %13
13: ; preds = %10, %13
%14 = phi i32 [ 0, %10 ], [ %15, %13 ]
%15 = add i32 %14, 1
%16 = load i32, i32* @PTRACE_GETREGS
%17 = call i32 @ptrace(i32 %16, i32 %0, i32 0, %struct.user_regs_struct* %3)
%18 = load i32, i32* @PTRACE_PEEKTEXT
%19 = load i32, i32* %12
%20 = call i32 @ptrace(i32 %18, i32 %0, i32 %19, %struct.user_regs_struct* null)
%21 = load i32, i32* %12
%22 = call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %21, i32 %20)
%23 = load i32, i32* @PTRACE_SINGLESTEP
%24 = call i32 @ptrace(i32 %23, i32 %0, i32 0, %struct.user_regs_struct* null)
%25 = call i32 @wait(i32* %2)
%26 = load i32, i32* %2
%27 = call i64 @WIFSTOPPED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %13
29: ; preds = %13, %1
%30 = phi i32 [ 0, %1 ], [ %15, %13 ]
%31 = call i32 (i8*, ...) @procmsg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %30)
ret void
}