#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ proxy(scalar_t__, scalar_t__);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t gdb_pid, child_pid;
  gdb_pid = fork();
  if (gdb_pid == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    execlp("gdb", "gdb", NULL);
    exit(1);
  }
  ptrace(PTRACE_SYSCALL, gdb_pid, 0, 0);
  child_pid = fork();
  if (child_pid == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    execl("child", "child", NULL);
    exit(1);
  }
  proxy(gdb_pid, child_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)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <main+0x5f>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <ptrace@plt>
lea    0xe57(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1060 <execlp@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <ptrace@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121e <main+0xbe>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1040 <ptrace@plt>
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execl@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <proxy@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11a0 <main+0x40>
mov    %rax,%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11cf <main+0x6f>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <proxy@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xe45(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1060 <execlp@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
lea    0xe1a(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PTRACE_TRACEME = global i32 0
@.str = [4 x i8] c"gdb\00"
@PTRACE_SYSCALL = global i32 0
@.str.1 = [6 x i8] c"child\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @PTRACE_TRACEME
%13 = call i32 @ptrace(i32 %12, i64 0, i32 0, i32 0)
%14 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* null)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @PTRACE_SYSCALL
%18 = load i64, i64* %6
%19 = call i32 @ptrace(i32 %17, i64 %18, i32 0, i32 0)
%20 = call i64 (...) @fork()
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = load i32, i32* @PTRACE_TRACEME
%25 = call i32 @ptrace(i32 %24, i64 0, i32 0, i32 0)
%26 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* null)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i64, i64* %6
%30 = load i64, i64* %7
%31 = call i32 @proxy(i64 %29, i64 %30)
ret i32 0
}

LLVM IR (-O2):
@PTRACE_TRACEME = global i32 0
@.str = [4 x i8] c"gdb\00"
@PTRACE_SYSCALL = global i32 0
@.str.1 = [6 x i8] c"child\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @PTRACE_TRACEME
%7 = tail call i32 @ptrace(i32 %6, i64 0, i32 0, i32 0)
%8 = tail call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* null)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @PTRACE_SYSCALL
%12 = tail call i32 @ptrace(i32 %11, i64 %3, i32 0, i32 0)
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @PTRACE_TRACEME
%17 = tail call i32 @ptrace(i32 %16, i64 0, i32 0, i32 0)
%18 = tail call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* null)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = tail call i32 @proxy(i64 %3, i64 %13)
ret i32 0
}