#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 PTRACE_O_TRACECLONE;
int PTRACE_O_TRACESYSGOOD;
int /*<<< orphan*/ PTRACE_SETOPTIONS;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ SIGSTOP;
int execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ process_signals(int);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  int status;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <prog> <arg1> ... <argN>\n", argv[0]);
    return 1;
  }
  if ((pid = fork()) == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    kill(getpid(), SIGSTOP);
    return execvp(argv[1], argv + 1);
  } else {
    printf("tracing %i\n", pid);
    waitpid(pid, &status, 0);
    ptrace(PTRACE_SETOPTIONS, pid, 0,
           PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACECLONE);
    process_signals(pid);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d8 <main+0x48>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1284 <main+0xf4>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1233 <main+0xa3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1070 <ptrace@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1090 <execvp@plt>
mov    %eax,-0x4(%rbp)
jmp    1284 <main+0xf4>
mov    -0x14(%rbp),%esi
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%ecx
mov    0x2d44(%rip),%rax
or     (%rax),%ecx
xor    %edx,%edx
call   1070 <ptrace@plt>
mov    -0x14(%rbp),%edi
call   10a0 <process_signals@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11c0 <main+0x30>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r14d
jmp    1217 <main+0x87>
xor    %r14d,%r14d
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1223 <main+0x93>
mov    %eax,%ebp
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    0x2dad(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ebp,%esi
xor    %edx,%edx
call   1070 <ptrace@plt>
mov    %ebp,%edi
call   10a0 <process_signals@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1060 <kill@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <execvp@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: %s <prog> <arg1> ... <argN>\0A\00"
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i32 0
@.str.1 = [12 x i8] c"tracing %i\0A\00"
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global i32 0
@PTRACE_O_TRACECLONE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %3
br label %44
16: ; preds = %2
%17 = call i32 (...) @fork()
store i32 %17, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = load i32, i32* @PTRACE_TRACEME
%21 = call i32 @ptrace(i32 %20, i32 0, i32 0, i32 0)
%22 = call i32 (...) @getpid()
%23 = load i32, i32* @SIGSTOP
%24 = call i32 @kill(i32 %22, i32 %23)
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = call i32 @execvp(i8* %27, i8** %29)
store i32 %30, i32* %3
br label %44
31: ; preds = %16
%32 = load i32, i32* %6
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @waitpid(i32 %34, i32* %7, i32 0)
%36 = load i32, i32* @PTRACE_SETOPTIONS
%37 = load i32, i32* %6
%38 = load i32, i32* @PTRACE_O_TRACESYSGOOD
%39 = load i32, i32* @PTRACE_O_TRACECLONE
%40 = or i32 %38, %39
%41 = call i32 @ptrace(i32 %36, i32 %37, i32 0, i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @process_signals(i32 %42)
store i32 0, i32* %3
br label %44
44: ; preds = %31, %19, %10
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: %s <prog> <arg1> ... <argN>\0A\00"
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i32 0
@.str.1 = [12 x i8] c"tracing %i\0A\00"
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global i32 0
@PTRACE_O_TRACECLONE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %31
10: ; preds = %2
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i32, i32* @PTRACE_TRACEME
%15 = tail call i32 @ptrace(i32 %14, i32 0, i32 0, i32 0)
%16 = tail call i32 (...) @getpid()
%17 = load i32, i32* @SIGSTOP
%18 = tail call i32 @kill(i32 %16, i32 %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @execvp(i8* %20, i8** %19)
br label %31
22: ; preds = %10
%23 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%24 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%25 = load i32, i32* @PTRACE_SETOPTIONS
%26 = load i32, i32* @PTRACE_O_TRACESYSGOOD
%27 = load i32, i32* @PTRACE_O_TRACECLONE
%28 = or i32 %27, %26
%29 = call i32 @ptrace(i32 %25, i32 %11, i32 0, i32 %28)
%30 = call i32 @process_signals(i32 %11)
br label %31
31: ; preds = %22, %13, %6
%32 = phi i32 [ 1, %6 ], [ %21, %13 ], [ 0, %22 ]
ret i32 %32
}