#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*/ EXIT_FAILURE;
long PTRACE_O_TRACEEXEC;
long PTRACE_O_TRACESYSGOOD;
int /*<<< orphan*/ PTRACE_SETOPTIONS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ proc_start(char *);
scalar_t__ proc_syscall(int /*<<< orphan*/);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *, long);
int /*<<< orphan*/ stderr;
scalar_t__ trace(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  long trace_options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC;
  pid_t pid = proc_start(argv[1]);
  long ret = ptrace(PTRACE_SETOPTIONS, pid, NULL, trace_options);
  if (ret == -1) {
    perror("ptrace setoptions");
    goto fail;
  }
  if (proc_syscall(pid))
    return -1;
  fprintf(stderr, "[+] Initialization done! Entering trace loop\n");
  while (trace(pid))
    ;
  return 0;
fail:
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bc <main+0x4c>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
or     (%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <proc_start@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
call   1050 <ptrace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    121d <main+0xad>
lea    0xe03(%rip),%rdi
call   1080 <perror@plt>
jmp    1275 <main+0x105>
mov    -0x1c(%rbp),%edi
call   1040 <proc_syscall@plt>
cmp    $0x0,%rax
je     123b <main+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <main+0x113>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <trace@plt>
cmp    $0x0,%rax
je     1269 <main+0xf9>
jmp    1252 <main+0xe2>
movl   $0x0,-0x4(%rbp)
jmp    1283 <main+0x113>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11f5 <main+0x85>
mov    0x2e69(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%rbp
or     (%rax),%rbp
mov    0x8(%rsi),%rdi
call   1060 <proc_start@plt>
mov    %eax,%ebx
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
mov    %rbp,%rcx
call   1050 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
je     1211 <main+0xa1>
mov    %ebx,%edi
call   1040 <proc_syscall@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11ec <main+0x7c>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <fprintf@plt>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1070 <trace@plt>
test   %rax,%rax
jne    11e0 <main+0x70>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdf8(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    121d <main+0xad>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@PTRACE_O_TRACESYSGOOD = global i64 0
@PTRACE_O_TRACEEXEC = global i64 0
@PTRACE_SETOPTIONS = global i32 0
@.str.1 = [18 x i8] c"ptrace setoptions\00"
@.str.2 = [46 x i8] c"[+] Initialization done! Entering trace loop\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i64, i64* @PTRACE_O_TRACESYSGOOD
%21 = load i64, i64* @PTRACE_O_TRACEEXEC
%22 = or i64 %20, %21
store i64 %22, i64* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @proc_start(i8* %25)
store i32 %26, i32* %7
%27 = load i32, i32* @PTRACE_SETOPTIONS
%28 = load i32, i32* %7
%29 = load i64, i64* %6
%30 = call i64 @ptrace(i32 %27, i32 %28, i32* null, i64 %29)
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = icmp eq i64 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %49
35: ; preds = %19
%36 = load i32, i32* %7
%37 = call i64 @proc_syscall(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %3
br label %52
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %47, %40
%44 = load i32, i32* %7
%45 = call i64 @trace(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
br label %43
48: ; preds = %43
store i32 0, i32* %3
br label %52
49: ; preds = %33
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %48, %39
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@PTRACE_O_TRACESYSGOOD = global i64 0
@PTRACE_O_TRACEEXEC = global i64 0
@PTRACE_SETOPTIONS = global i32 0
@.str.1 = [18 x i8] c"ptrace setoptions\00"
@.str.2 = [46 x i8] c"[+] Initialization done! Entering trace loop\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = load i64, i64* @PTRACE_O_TRACESYSGOOD
%12 = load i64, i64* @PTRACE_O_TRACEEXEC
%13 = or i64 %12, %11
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @proc_start(i8* %15)
%17 = load i32, i32* @PTRACE_SETOPTIONS
%18 = tail call i64 @ptrace(i32 %17, i32 %16, i32* null, i64 %13)
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %10
%21 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = tail call i64 @proc_syscall(i32 %16)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %30, %27
%31 = tail call i64 @trace(i32 %16)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %30
33: ; preds = %30, %24
%34 = phi i32 [ -1, %24 ], [ 0, %30 ]
ret i32 %34
}