#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PTRACE_TRACEME;
scalar_t__ SIGSTOP;
int /*<<< orphan*/ WIFSTOPPED(int);
int /*<<< orphan*/ WSTOPPED;
scalar_t__ WSTOPSIG(int);
scalar_t__ errno;
int /*<<< orphan*/ execv(char const *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ perr_exit(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
pid_t proc_start(const char *filename) {
  int status;
  pid_t pid;
  char *args[2] = {NULL};
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  } else if (!pid) {
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    kill(getpid(), SIGSTOP);
    args[0] = (char *)filename;
    execv(filename, args);
    exit(EXIT_FAILURE);
  }
  while (waitpid(pid, &status, WSTOPPED) < 0) {
    if (errno == EINTR)
      continue;
    perr_exit("waitpid");
  }
  if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
    perr_exit("Unexpected wait status");
  return pid;
}


x86 assembly (clang, -O0):
<proc_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    120a <proc_start+0x4a>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1266 <proc_start+0xa6>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1070 <ptrace@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%rsi
call   1050 <kill@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <execv@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    126b <proc_start+0xab>
jmp    1270 <proc_start+0xb0>
mov    -0x10(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jge    12bf <proc_start+0xff>
mov    0x2d3a(%rip),%rax
mov    (%rax),%rax
mov    0x2d20(%rip),%rcx
cmp    (%rcx),%rax
jne    12ae <proc_start+0xee>
jmp    1270 <proc_start+0xb0>
lea    0xd50(%rip),%rdi
call   10e0 <perr_exit@plt>
jmp    1270 <proc_start+0xb0>
mov    -0xc(%rbp),%edi
call   1060 <WIFSTOPPED@plt>
cmp    $0x0,%eax
je     12e8 <proc_start+0x128>
mov    -0xc(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    0x2cd9(%rip),%rcx
cmp    (%rcx),%rax
je     12f4 <proc_start+0x134>
lea    0xd1e(%rip),%rdi
call   10e0 <perr_exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_start>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     1287 <proc_start+0xd7>
mov    %eax,%ebx
test   %eax,%eax
je     1295 <proc_start+0xe5>
mov    0x2e06(%rip),%r12
mov    (%r12),%edx
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1070 <waitpid@plt>
test   %rax,%rax
js     123e <proc_start+0x8e>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %eax,%eax
je     1221 <proc_start+0x71>
mov    0xc(%rsp),%edi
call   10a0 <WSTOPSIG@plt>
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
je     122d <proc_start+0x7d>
lea    0xde5(%rip),%rdi
call   10d0 <perr_exit@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8b(%rip),%r13
mov    0x2d74(%rip),%rbp
lea    0xc(%rsp),%r14
lea    0xdad(%rip),%r15
jmp    1273 <proc_start+0xc3>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <waitpid@plt>
test   %rax,%rax
jns    11ff <proc_start+0x4f>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1260 <proc_start+0xb0>
mov    %r15,%rdi
call   10d0 <perr_exit@plt>
jmp    1260 <proc_start+0xb0>
lea    0xd72(%rip),%rdi
call   1080 <perror@plt>
jmp    12d3 <proc_start+0x123>
mov    0x2d3c(%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    0x2d01(%rip),%rcx
mov    (%rcx),%rsi
mov    %eax,%edi
call   1040 <kill@plt>
mov    %r14,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   10b0 <execv@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i64 0
@WSTOPPED = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [23 x i8] c"Unexpected wait status\00"
define i32 @proc_start(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8*]
store i8* %0, i8** %2
%6 = bitcast [2 x i8*]* %5 to i8*
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %14
%18 = load i32, i32* @PTRACE_TRACEME
%19 = call i32 @ptrace(i32 %18, i32 0, i32* null, i32* null)
%20 = call i32 (...) @getpid()
%21 = load i64, i64* @SIGSTOP
%22 = call i32 @kill(i32 %20, i64 %21)
%23 = load i8*, i8** %2
%24 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
store i8* %23, i8** %24
%25 = load i8*, i8** %2
%26 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%27 = call i32 @execv(i8* %25, i8** %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
br label %31
31: ; preds = %30
br label %32
32: ; preds = %42, %41, %31
%33 = load i32, i32* %4
%34 = load i32, i32* @WSTOPPED
%35 = call i64 @waitpid(i32 %33, i32* %3, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %32
42: ; preds = %37
%43 = call i32 @perr_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %32
44: ; preds = %32
%45 = load i32, i32* %3
%46 = call i32 @WIFSTOPPED(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = load i32, i32* %3
%50 = call i64 @WSTOPSIG(i32 %49)
%51 = load i64, i64* @SIGSTOP
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %48, %44
%54 = call i32 @perr_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %48
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@PTRACE_TRACEME = global i32 0
@SIGSTOP = global i64 0
@WSTOPPED = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [23 x i8] c"Unexpected wait status\00"
define i32 @proc_start(i8* %0) {
%2 = alloca i32
%3 = alloca [2 x i8*]
%4 = bitcast i32* %2 to i8*
%5 = bitcast [2 x i8*]* %3 to i8*
%6 = tail call i32 (...) @fork()
switch i32 %6, label %7 [
i32 -1, label %11
i32 0, label %15
]
7: ; preds = %1
%8 = load i32, i32* @WSTOPPED
%9 = call i64 @waitpid(i32 %6, i32* %2, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %25, label %35
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* @PTRACE_TRACEME
%17 = tail call i32 @ptrace(i32 %16, i32 0, i32* null, i32* null)
%18 = tail call i32 (...) @getpid()
%19 = load i64, i64* @SIGSTOP
%20 = tail call i32 @kill(i32 %18, i64 %19)
%21 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
store i8* %0, i8** %21
%22 = call i32 @execv(i8* %0, i8** %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %7, %31
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @perr_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* @WSTOPPED
%33 = call i64 @waitpid(i32 %6, i32* %2, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %25, label %35
35: ; preds = %31, %7
%36 = load i32, i32* %2
%37 = call i32 @WIFSTOPPED(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* %2
%41 = call i64 @WSTOPSIG(i32 %40)
%42 = load i64, i64* @SIGSTOP
%43 = icmp eq i64 %41, %42
br i1 %43, label %46, label %44
44: ; preds = %39, %35
%45 = call i32 @perr_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
ret i32 %6
}