#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_SINGLESTEP;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int run_debugger(pid_t pid) {
  int status;
  int counter = 0;
  printf("debugger start...\n");
  if (0 > (pid = waitpid(pid, &status, 0))) {
    perror("waitpid");
    return -1;
  }
  while (1) {
    if (WIFEXITED(status)) {
      printf("\n[+] the child process has  executed %u instructions\n",
             counter++);
      printf("\n[+] detect rocess %d exit\n", pid);
      break;
    }
    if (WIFSTOPPED(status)) {
      counter++;
      if (ptrace(PTRACE_SINGLESTEP, pid, 0, 0) < 0) {
        perror("ptrace PTRACE_SINGLESTEP");
        return -1;
      }
      printf("[+] child process execute No.%u instruction!\n", counter);
      if (0 > (pid = waitpid(pid, &status, 0))) {
        perror("waitpid");
        return -1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_debugger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    11b5 <run_debugger+0x55>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <run_debugger+0x144>
jmp    11ba <run_debugger+0x5a>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11fb <run_debugger+0x9b>
mov    -0x10(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129d <run_debugger+0x13d>
mov    -0xc(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1298 <run_debugger+0x138>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <ptrace@plt>
cmp    $0x0,%rax
jge    124d <run_debugger+0xed>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <run_debugger+0x144>
mov    -0x10(%rbp),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1293 <run_debugger+0x133>
lea    0xd91(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <run_debugger+0x144>
jmp    1298 <run_debugger+0x138>
jmp    11ba <run_debugger+0x5a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_debugger>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xf1c(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %eax,%eax
js     1251 <run_debugger+0xe1>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     11e5 <run_debugger+0x75>
lea    0xe4f(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe72(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df4(%rip),%r12
lea    0xe80(%rip),%r14
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
jmp    120e <run_debugger+0x9e>
nopl   0x0(%rax)
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    11b2 <run_debugger+0x42>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     1200 <run_debugger+0x90>
mov    (%r12),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
test   %rax,%rax
js     1268 <run_debugger+0xf8>
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1200 <run_debugger+0x90>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11d5 <run_debugger+0x65>
lea    0xdeb(%rip),%rdi
jmp    1258 <run_debugger+0xe8>

LLVM IR (-O0):
@.str = [19 x i8] c"debugger start...\0A\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [54 x i8] c"\0A[+] the child process has executed %u instructions\0A\00"
@.str.3 = [28 x i8] c"\0A[+] detect rocess %d exit\0A\00"
@PTRACE_SINGLESTEP = global i32 0
@.str.4 = [25 x i8] c"ptrace PTRACE_SINGLESTEP\00"
@.str.5 = [46 x i8] c"[+] child process execute No.%u instruction!\0A\00"
define i32 @run_debugger(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* %3
%8 = call i32 @waitpid(i32 %7, i32* %4, i32 0)
store i32 %8, i32* %3
%9 = icmp sgt i32 0, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
12: ; preds = %1
br label %13
13: ; preds = %12, %45
%14 = load i32, i32* %4
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%21 = load i32, i32* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %46
23: ; preds = %13
%24 = load i32, i32* %4
%25 = call i64 @WIFSTOPPED(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %23
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
%30 = load i32, i32* @PTRACE_SINGLESTEP
%31 = load i32, i32* %3
%32 = call i64 @ptrace(i32 %30, i32 %31, i32 0, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
36: ; preds = %27
%37 = load i32, i32* %5
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %3
%40 = call i32 @waitpid(i32 %39, i32* %4, i32 0)
store i32 %40, i32* %3
%41 = icmp sgt i32 0, %40
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
44: ; preds = %36
br label %45
45: ; preds = %44, %23
br label %13
46: ; preds = %17
store i32 0, i32* %2
br label %47
47: ; preds = %46, %42, %34, %10
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [54 x i8] c"\0A[+] the child process has executed %u instructions\0A\00"
@.str.3 = [28 x i8] c"\0A[+] detect rocess %d exit\0A\00"
@PTRACE_SINGLESTEP = global i32 0
@.str.4 = [25 x i8] c"ptrace PTRACE_SINGLESTEP\00"
@.str.5 = [46 x i8] c"[+] child process execute No.%u instruction!\0A\00"
@str = [18 x i8] c"debugger start...\00"
define i32 @run_debugger(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%5 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* %2
%9 = call i64 @WIFEXITED(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %43
13: ; preds = %37, %7
%14 = phi i32 [ %5, %7 ], [ %38, %37 ]
%15 = phi i32 [ 0, %7 ], [ %39, %37 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %43
18: ; preds = %7, %37
%19 = phi i32 [ %39, %37 ], [ 0, %7 ]
%20 = phi i32 [ %38, %37 ], [ %5, %7 ]
%21 = load i32, i32* %2
%22 = call i64 @WIFSTOPPED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %18
%25 = add nsw i32 %19, 1
%26 = load i32, i32* @PTRACE_SINGLESTEP
%27 = call i64 @ptrace(i32 %26, i32 %20, i32 0, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %43
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%33 = call i32 @waitpid(i32 %20, i32* %2, i32 0)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %43
37: ; preds = %31, %18
%38 = phi i32 [ %33, %31 ], [ %20, %18 ]
%39 = phi i32 [ %25, %31 ], [ %19, %18 ]
%40 = load i32, i32* %2
%41 = call i64 @WIFEXITED(i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %18, label %13
43: ; preds = %13, %35, %29, %11
%44 = phi i32 [ -1, %11 ], [ 0, %13 ], [ -1, %29 ], [ -1, %35 ]
ret i32 %44
}