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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_CONT;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int run_debugger(pid_t pid) {
  int status;
  printf("debugger start...\n");
  if (0 > (pid = waitpid(pid, &status, 0))) {
    perror("waitpid");
    return -1;
  }
  while (1) {
    if (WIFEXITED(status)) {
      printf("[+] child process exit!\n");
      break;
    }
    if (WIFSTOPPED(status)) {
      printf("[+] child process send signal %d \n", WSTOPSIG(status));
      if (-1 == ptrace(PTRACE_CONT, pid, 0, 0)) {
        perror("PTRACE_CONT");
        return -1;
      }
    }
    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)
lea    0xe7e(%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    11be <run_debugger+0x4e>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <run_debugger+0x124>
jmp    11c3 <run_debugger+0x53>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e8 <run_debugger+0x78>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    128d <run_debugger+0x11d>
mov    -0xc(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1253 <run_debugger+0xe3>
mov    -0xc(%rbp),%edi
call   1090 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <ptrace@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    124e <run_debugger+0xde>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <run_debugger+0x124>
jmp    1253 <run_debugger+0xe3>
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    1288 <run_debugger+0x118>
lea    0xd9c(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <run_debugger+0x124>
jmp    11c3 <run_debugger+0x53>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_debugger>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %eax,%eax
js     124a <run_debugger+0xca>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
lea    0xe4e(%rip),%r15
mov    0x2e1f(%rip),%r12
jmp    11e2 <run_debugger+0x62>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,%ebx
test   %eax,%eax
js     124a <run_debugger+0xca>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    1230 <run_debugger+0xb0>
mov    0x4(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     11d0 <run_debugger+0x50>
mov    0x4(%rsp),%edi
call   10a0 <WSTOPSIG@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r12),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    11d0 <run_debugger+0x50>
lea    0xdfd(%rip),%rdi
jmp    1251 <run_debugger+0xd1>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    123e <run_debugger+0xbe>

LLVM IR (-O0):
@.str = [19 x i8] c"debugger start...\0A\00"
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [25 x i8] c"[+] child process exit!\0A\00"
@.str.3 = [35 x i8] c"[+] child process send signal %d \0A\00"
@PTRACE_CONT = global i32 0
@.str.4 = [12 x i8] c"PTRACE_CONT\00"
define i32 @run_debugger(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* %3
%7 = call i32 @waitpid(i32 %6, i32* %4, i32 0)
store i32 %7, i32* %3
%8 = icmp sgt i32 0, %7
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = 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 %41
11: ; preds = %1
br label %12
12: ; preds = %11, %39
%13 = load i32, i32* %4
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %40
18: ; preds = %12
%19 = load i32, i32* %4
%20 = call i64 @WIFSTOPPED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i32, i32* %4
%24 = call i32 @WSTOPSIG(i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @PTRACE_CONT
%27 = load i32, i32* %3
%28 = call i32 @ptrace(i32 %26, i32 %27, i32 0, i32 0)
%29 = icmp eq i32 -1, %28
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
32: ; preds = %22
br label %33
33: ; preds = %32, %18
%34 = load i32, i32* %3
%35 = call i32 @waitpid(i32 %34, i32* %4, i32 0)
store i32 %35, i32* %3
%36 = icmp sgt i32 0, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = 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 %41
39: ; preds = %33
br label %12
40: ; preds = %16
store i32 0, i32* %2
br label %41
41: ; preds = %40, %37, %30, %9
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"waitpid\00"
@.str.3 = [35 x i8] c"[+] child process send signal %d \0A\00"
@PTRACE_CONT = global i32 0
@.str.4 = [12 x i8] c"PTRACE_CONT\00"
@str = [18 x i8] c"debugger start...\00"
@str.5 = [24 x i8] c"[+] child process exit!\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 %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %34
9: ; preds = %1, %29
%10 = phi i32 [ %30, %29 ], [ %5, %1 ]
%11 = load i32, i32* %2
%12 = call i64 @WIFEXITED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
br label %34
16: ; preds = %9
%17 = load i32, i32* %2
%18 = call i64 @WIFSTOPPED(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = load i32, i32* %2
%22 = call i32 @WSTOPSIG(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @PTRACE_CONT
%25 = call i32 @ptrace(i32 %24, i32 %10, i32 0, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %34
29: ; preds = %20, %16
%30 = call i32 @waitpid(i32 %10, i32* %2, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %9
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %14, %32, %27, %7
%35 = phi i32 [ -1, %7 ], [ 0, %14 ], [ -1, %27 ], [ -1, %32 ]
ret i32 %35
}