#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 ECHILD;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ SIGSTOP;
int /*<<< orphan*/ __WALL;
int /*<<< orphan*/ die(int, char *, ...);
int errno;
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *);
int strerror(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 2)
    die(1, "Not enough args: %d wanted, %d supplied\n", 2, argc - 1);
  pid_t res = fork();
  if (res < 0)
    die(1, "Fork failed\n");
  else if (res == 0) {
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    kill(getpid(), SIGSTOP);
    execve(argv[1], argv + 1, NULL);
  } else {
    for (;;) {
      int status;
      pid_t child = waitpid(-1, &status, __WALL);
      if (errno == ECHILD) {
        errno = 0;
        break;
      }
      if (errno != 0)
        die(1, "Encountered unexpected error %d (%s)\n", errno,
            strerror(errno));
      ptrace(PTRACE_SYSCALL, child, NULL, NULL);
    }
  }
}


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)
jge    11be <main+0x3e>
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%edi
lea    0xe4e(%rip),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1080 <die@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ec <main+0x6c>
mov    $0x1,%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1080 <die@plt>
jmp    12f8 <main+0x178>
cmpq   $0x0,-0x18(%rbp)
jne    1244 <main+0xc4>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <ptrace@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execve@plt>
jmp    12f3 <main+0x173>
jmp    1249 <main+0xc9>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%eax
jne    128e <main+0x10e>
mov    0x2d55(%rip),%rax
movl   $0x0,(%rax)
jmp    12ee <main+0x16e>
mov    0x2d43(%rip),%rax
cmpl   $0x0,(%rax)
je     12d0 <main+0x150>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
mov    $0x1,%edi
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1080 <die@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <ptrace@plt>
jmp    1249 <main+0xc9>
jmp    12f3 <main+0x173>
jmp    12f8 <main+0x178>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11b0 <main+0x30>
mov    %edi,%ecx
add    $0xffffffff,%ecx
lea    0xe61(%rip),%rsi
mov    $0x1,%edi
mov    $0x2,%edx
xor    %eax,%eax
call   1080 <die@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11f7 <main+0x77>
je     120c <main+0x8c>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1070 <waitpid@plt>
mov    0x2dfb(%rip),%r13
mov    0x0(%r13),%ebp
mov    0x2de0(%rip),%r15
cmp    (%r15),%ebp
jne    125a <main+0xda>
movl   $0x0,0x0(%r13)
jmp    1249 <main+0xc9>
lea    0xe2b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <die@plt>
jmp    1249 <main+0xc9>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2d92(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1050 <kill@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rax,%rbx
mov    0x2d4c(%rip),%r12
lea    0x4(%rsp),%r14
jmp    12a6 <main+0x126>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <ptrace@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
mov    %r14,%rsi
call   1070 <waitpid@plt>
mov    %rax,%rbx
mov    0x0(%r13),%ebp
cmp    (%r15),%ebp
je     11ed <main+0x6d>
test   %ebp,%ebp
je     1270 <main+0xf0>
mov    %ebp,%edi
call   1090 <strerror@plt>
mov    $0x1,%edi
lea    0xd79(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <die@plt>
jmp    1270 <main+0xf0>

LLVM IR (-O0):
@.str = [41 x i8] c"Not enough args: %d wanted, %d supplied\0A\00"
@.str.1 = [13 x i8] c"Fork failed\0A\00"
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i32 0
@__WALL = global i32 0
@errno = global i32 0
@ECHILD = global i32 0
@.str.2 = [38 x i8] c"Encountered unexpected error %d (%s)\0A\00"
@PTRACE_SYSCALL = global i32 0
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 slt i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* %4
%13 = sub nsw i32 %12, 1
%14 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 2, i32 %13)
br label %15
15: ; preds = %11, %2
%16 = call i64 (...) @fork()
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %58
21: ; preds = %15
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %21
%25 = load i32, i32* @PTRACE_TRACEME
%26 = call i32 @ptrace(i32 %25, i64 0, i32* null, i32* null)
%27 = call i32 (...) @getpid()
%28 = load i32, i32* @SIGSTOP
%29 = call i32 @kill(i32 %27, i32 %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = call i32 @execve(i8* %32, i8** %34, i32* null)
br label %57
36: ; preds = %21
br label %37
37: ; preds = %52, %36
%38 = load i32, i32* @__WALL
%39 = call i64 @waitpid(i32 -1, i32* %7, i32 %38)
store i64 %39, i64* %8
%40 = load i32, i32* @errno
%41 = load i32, i32* @ECHILD
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %37
store i32 0, i32* @errno
br label %56
44: ; preds = %37
%45 = load i32, i32* @errno
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %50)
br label %52
52: ; preds = %47, %44
%53 = load i32, i32* @PTRACE_SYSCALL
%54 = load i64, i64* %8
%55 = call i32 @ptrace(i32 %53, i64 %54, i32* null, i32* null)
br label %37
56: ; preds = %43
br label %57
57: ; preds = %56, %24
br label %58
58: ; preds = %57, %19
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [41 x i8] c"Not enough args: %d wanted, %d supplied\0A\00"
@.str.1 = [13 x i8] c"Fork failed\0A\00"
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i32 0
@__WALL = global i32 0
@errno = global i32 0
@ECHILD = global i32 0
@.str.2 = [38 x i8] c"Encountered unexpected error %d (%s)\0A\00"
@PTRACE_SYSCALL = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = add nsw i32 %0, -1
%7 = tail call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 2, i32 %6)
br label %8
8: ; preds = %5, %2
%9 = tail call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %47
13: ; preds = %8
%14 = icmp eq i64 %9, 0
br i1 %14, label %22, label %15
15: ; preds = %13
%16 = bitcast i32* %3 to i8*
%17 = load i32, i32* @__WALL
%18 = call i64 @waitpid(i32 -1, i32* %3, i32 %17)
%19 = load i32, i32* @errno
%20 = load i32, i32* @ECHILD
%21 = icmp eq i32 %19, %20
br i1 %21, label %31, label %32
22: ; preds = %13
%23 = load i32, i32* @PTRACE_TRACEME
%24 = tail call i32 @ptrace(i32 %23, i64 0, i32* null, i32* null)
%25 = tail call i32 (...) @getpid()
%26 = load i32, i32* @SIGSTOP
%27 = tail call i32 @kill(i32 %25, i32 %26)
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = tail call i32 @execve(i8* %29, i8** %28, i32* null)
br label %47
31: ; preds = %39, %15
store i32 0, i32* @errno
br label %47
32: ; preds = %15, %39
%33 = phi i32 [ %44, %39 ], [ %19, %15 ]
%34 = phi i64 [ %43, %39 ], [ %18, %15 ]
%35 = icmp eq i32 %33, 0
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = call i32 @strerror(i32 %33)
%38 = call i32 (i32, i8*, ...) @die(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %37)
br label %39
39: ; preds = %32, %36
%40 = load i32, i32* @PTRACE_SYSCALL
%41 = call i32 @ptrace(i32 %40, i64 %34, i32* null, i32* null)
%42 = load i32, i32* @__WALL
%43 = call i64 @waitpid(i32 -1, i32* %3, i32 %42)
%44 = load i32, i32* @errno
%45 = load i32, i32* @ECHILD
%46 = icmp eq i32 %44, %45
br i1 %46, label %31, label %32
47: ; preds = %31, %22, %11
ret i32 0
}