#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 */

/* Variables and functions */
int PTRACE_O_TRACESYSGOOD;
int /*<<< orphan*/ PTRACE_SETOPTIONS;
int /*<<< orphan*/ PTRACE_SYSCALL;
int SIGTRAP;
int /*<<< orphan*/ WCONTINUED;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ parent_syscall(int);
int /*<<< orphan*/ perror(char *);
int ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void parent_loop(int pid) {
  int r;
  waitpid(pid, NULL, WCONTINUED);
  int sysgood = 0;
  r = ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD);
  if (r == 0) {
    sysgood = 0x80;
  } else {
    perror("ptrace(PTRACE_O_TRACESYSGOOD)");
  }
  int within_syscall = 0;
  int inject_signal = 0;
  while (1) {
    int status;
    ptrace(PTRACE_SYSCALL, pid, 0, inject_signal);
    inject_signal = 0;
    waitpid(pid, &status, 0);
    if (WIFEXITED(status)) {
      int exit_status = WEXITSTATUS(status);
      fprintf(stderr, "[.] exit with status = %i\n", exit_status);
      exit(exit_status);
    }
    if (WIFSIGNALED(status)) {
      int signal = WTERMSIG(status);
      fprintf(stderr, "[.] terminated by a signal %i\n", signal);
      break;
    }
    if (WIFSTOPPED(status)) {
      int signal = WSTOPSIG(status);
      if (signal != (SIGTRAP | sysgood)) {
        if (signal != SIGTRAP) {
          inject_signal = signal;
          fprintf(stderr, "[.] got a signal %i\n", signal);
        } else {
          fprintf(stderr, "[.] got a SIGTRAP, execve in action\n");
        }
        continue;
      } else {
        if (!within_syscall) {
          parent_syscall(pid);
        } else {
          parent_syscall(pid);
        }
        within_syscall = !within_syscall;
      }
      continue;
    }
    fprintf(stderr, "[.] status %x not understood!\n", status);
  }
}


x86 assembly (clang, -O0):
<parent_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <waitpid@plt>
movl   $0x0,-0xc(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
call   1070 <ptrace@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    121c <parent_loop+0x5c>
movl   $0x80,-0xc(%rbp)
jmp    1228 <parent_loop+0x68>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x14(%rbp),%ecx
xor    %edx,%edx
call   1070 <ptrace@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x18(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12a0 <parent_loop+0xe0>
mov    -0x18(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12dc <parent_loop+0x11c>
mov    -0x18(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    13b7 <parent_loop+0x1f7>
mov    -0x18(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1398 <parent_loop+0x1d8>
mov    -0x18(%rbp),%edi
call   10d0 <WSTOPSIG@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2cc5(%rip),%rcx
mov    (%rcx),%ecx
or     -0xc(%rbp),%ecx
cmp    %ecx,%eax
je     1363 <parent_loop+0x1a3>
mov    -0x24(%rbp),%eax
mov    0x2cae(%rip),%rcx
cmp    (%rcx),%eax
je     1347 <parent_loop+0x187>
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    135e <parent_loop+0x19e>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1236 <parent_loop+0x76>
cmpl   $0x0,-0x10(%rbp)
jne    137a <parent_loop+0x1ba>
mov    -0x4(%rbp),%edi
call   10e0 <parent_syscall@plt>
jmp    1382 <parent_loop+0x1c2>
mov    -0x4(%rbp),%edi
call   10e0 <parent_syscall@plt>
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
jmp    1236 <parent_loop+0x76>
mov    0x2c31(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xce7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1236 <parent_loop+0x76>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r13d
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <waitpid@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%ecx
mov    %r13d,%esi
xor    %edx,%edx
call   1070 <ptrace@plt>
mov    $0x80,%r12d
test   %eax,%eax
jne    134d <parent_loop+0x18d>
mov    0x2da1(%rip),%r14
mov    (%r14),%edi
mov    %r13d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
mov    %rsp,%rsi
mov    %r13d,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    (%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
jne    1361 <parent_loop+0x1a1>
mov    0x2d8d(%rip),%r15
mov    %rsp,%rbx
movl   $0x0,0x4(%rsp)
jmp    12ad <parent_loop+0xed>
mov    %r12d,%ebx
mov    %r15,%r12
mov    %r14,%r15
xor    %r14d,%r14d
cmpl   $0x0,0x4(%rsp)
sete   %r14b
mov    %r13d,%edi
call   10e0 <parent_syscall@plt>
xor    %ebp,%ebp
mov    %r14d,0x4(%rsp)
mov    %r15,%r14
mov    %r12,%r15
mov    %ebx,%r12d
mov    %rsp,%rbx
mov    (%r14),%edi
mov    %r13d,%esi
xor    %edx,%edx
mov    %ebp,%ecx
call   1070 <ptrace@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    (%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
jne    1361 <parent_loop+0x1a1>
mov    (%rsp),%edi
call   1030 <WIFSIGNALED@plt>
mov    (%rsp),%edi
test   %rax,%rax
jne    1326 <parent_loop+0x166>
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     1300 <parent_loop+0x140>
mov    (%rsp),%edi
call   10d0 <WSTOPSIG@plt>
mov    %eax,%ebp
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
or     %r12d,%ecx
cmp    %ecx,%ebp
je     1250 <parent_loop+0x90>
mov    (%r15),%edi
cmp    %eax,%ebp
jne    1311 <parent_loop+0x151>
xor    %ebp,%ebp
lea    0xd76(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1280 <parent_loop+0xc0>
mov    (%r15),%edi
mov    (%rsp),%edx
xor    %ebp,%ebp
lea    0xd83(%rip),%rsi
jmp    131a <parent_loop+0x15a>
lea    0xd40(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1280 <parent_loop+0xc0>
call   1060 <WTERMSIG@plt>
mov    (%r15),%edi
lea    0xd04(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcac(%rip),%rdi
call   10a0 <perror@plt>
xor    %r12d,%r12d
jmp    1208 <parent_loop+0x48>
mov    (%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
lea    0xca3(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@WCONTINUED = global i32 0
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global i32 0
@.str = [30 x i8] c"ptrace(PTRACE_O_TRACESYSGOOD)\00"
@PTRACE_SYSCALL = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"[.] exit with status = %i\0A\00"
@.str.2 = [31 x i8] c"[.] terminated by a signal %i\0A\00"
@SIGTRAP = global i32 0
@.str.3 = [21 x i8] c"[.] got a signal %i\0A\00"
@.str.4 = [37 x i8] c"[.] got a SIGTRAP, execve in action\0A\00"
@.str.5 = [31 x i8] c"[.] status %x not understood!\0A\00"
define void @parent_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
%11 = load i32, i32* %2
%12 = load i32, i32* @WCONTINUED
%13 = call i32 @waitpid(i32 %11, i32* null, i32 %12)
store i32 0, i32* %4
%14 = load i32, i32* @PTRACE_SETOPTIONS
%15 = load i32, i32* %2
%16 = load i32, i32* @PTRACE_O_TRACESYSGOOD
%17 = call i32 @ptrace(i32 %14, i32 %15, i32 0, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i32 128, i32* %4
br label %23
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %20
store i32 0, i32* %5
store i32 0, i32* %6
br label %24
24: ; preds = %23, %76, %91, %92
%25 = load i32, i32* @PTRACE_SYSCALL
%26 = load i32, i32* %2
%27 = load i32, i32* %6
%28 = call i32 @ptrace(i32 %25, i32 %26, i32 0, i32 %27)
store i32 0, i32* %6
%29 = load i32, i32* %2
%30 = call i32 @waitpid(i32 %29, i32* %7, i32 0)
%31 = load i32, i32* %7
%32 = call i64 @WIFEXITED(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %24
%35 = load i32, i32* %7
%36 = call i32 @WEXITSTATUS(i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* @stderr
%38 = load i32, i32* %8
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %8
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %24
%43 = load i32, i32* %7
%44 = call i64 @WIFSIGNALED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* %7
%48 = call i32 @WTERMSIG(i32 %47)
store i32 %48, i32* %9
%49 = load i32, i32* @stderr
%50 = load i32, i32* %9
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %50)
br label %96
52: ; preds = %42
%53 = load i32, i32* %7
%54 = call i64 @WIFSTOPPED(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %92
56: ; preds = %52
%57 = load i32, i32* %7
%58 = call i32 @WSTOPSIG(i32 %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = load i32, i32* @SIGTRAP
%61 = load i32, i32* %4
%62 = or i32 %60, %61
%63 = icmp ne i32 %59, %62
br i1 %63, label %64, label %77
64: ; preds = %56
%65 = load i32, i32* %10
%66 = load i32, i32* @SIGTRAP
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %73
68: ; preds = %64
%69 = load i32, i32* %10
store i32 %69, i32* %6
%70 = load i32, i32* @stderr
%71 = load i32, i32* %10
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %76
73: ; preds = %64
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %76
76: ; preds = %73, %68
br label %24
77: ; preds = %56
%78 = load i32, i32* %5
%79 = icmp ne i32 %78, 0
br i1 %79, label %83, label %80
80: ; preds = %77
%81 = load i32, i32* %2
%82 = call i32 @parent_syscall(i32 %81)
br label %86
83: ; preds = %77
%84 = load i32, i32* %2
%85 = call i32 @parent_syscall(i32 %84)
br label %86
86: ; preds = %83, %80
%87 = load i32, i32* %5
%88 = icmp ne i32 %87, 0
%89 = xor i1 %88, true
%90 = zext i1 %89 to i32
store i32 %90, i32* %5
br label %91
91: ; preds = %86
br label %24
92: ; preds = %52
%93 = load i32, i32* @stderr
%94 = load i32, i32* %7
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %94)
br label %24
96: ; preds = %46
ret void
}

LLVM IR (-O2):
@WCONTINUED = global i32 0
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global i32 0
@.str = [30 x i8] c"ptrace(PTRACE_O_TRACESYSGOOD)\00"
@PTRACE_SYSCALL = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"[.] exit with status = %i\0A\00"
@.str.2 = [31 x i8] c"[.] terminated by a signal %i\0A\00"
@SIGTRAP = global i32 0
@.str.3 = [21 x i8] c"[.] got a signal %i\0A\00"
@.str.4 = [37 x i8] c"[.] got a SIGTRAP, execve in action\0A\00"
@.str.5 = [31 x i8] c"[.] status %x not understood!\0A\00"
define void @parent_loop(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @WCONTINUED
%4 = tail call i32 @waitpid(i32 %0, i32* null, i32 %3)
%5 = load i32, i32* @PTRACE_SETOPTIONS
%6 = load i32, i32* @PTRACE_O_TRACESYSGOOD
%7 = tail call i32 @ptrace(i32 %5, i32 %0, i32 0, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %1, %9
%12 = phi i32 [ 0, %9 ], [ 128, %1 ]
%13 = bitcast i32* %2 to i8*
%14 = load i32, i32* @PTRACE_SYSCALL
%15 = call i32 @ptrace(i32 %14, i32 %0, i32 0, i32 0)
%16 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%17 = load i32, i32* %2
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %56, %11
%21 = load i32, i32* %2
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%25 = call i32 @exit(i32 %22)
unreachable
26: ; preds = %11, %56
%27 = phi i32 [ %58, %56 ], [ 0, %11 ]
%28 = load i32, i32* %2
%29 = call i64 @WIFSIGNALED(i32 %28)
%30 = icmp eq i64 %29, 0
%31 = load i32, i32* %2
br i1 %30, label %32, label %65
32: ; preds = %26
%33 = call i64 @WIFSTOPPED(i32 %31)
%34 = icmp eq i64 %33, 0
br i1 %34, label %52, label %35
35: ; preds = %32
%36 = load i32, i32* %2
%37 = call i32 @WSTOPSIG(i32 %36)
%38 = load i32, i32* @SIGTRAP
%39 = or i32 %38, %12
%40 = icmp eq i32 %37, %39
br i1 %40, label %48, label %41
41: ; preds = %35
%42 = icmp eq i32 %37, %38
%43 = load i32, i32* @stderr
br i1 %42, label %46, label %44
44: ; preds = %41
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %56
46: ; preds = %41
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %56
48: ; preds = %35
%49 = icmp eq i32 %27, 0
%50 = call i32 @parent_syscall(i32 %0)
%51 = zext i1 %49 to i32
br label %56
52: ; preds = %32
%53 = load i32, i32* @stderr
%54 = load i32, i32* %2
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %48, %46, %44, %52
%57 = phi i32 [ 0, %52 ], [ 0, %48 ], [ %37, %44 ], [ 0, %46 ]
%58 = phi i32 [ %27, %52 ], [ %51, %48 ], [ %27, %44 ], [ %27, %46 ]
%59 = load i32, i32* @PTRACE_SYSCALL
%60 = call i32 @ptrace(i32 %59, i32 %0, i32 0, i32 %57)
%61 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%62 = load i32, i32* %2
%63 = call i64 @WIFEXITED(i32 %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %26, label %20
65: ; preds = %26
%66 = call i32 @WTERMSIG(i32 %31)
%67 = load i32, i32* @stderr
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %66)
ret void
}