#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*/ O_WRONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int execl(char const *, char const *, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int pipe(int *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysdie(char *);
pid_t test_start(const char *path, int *fd) {
  int fds[2], errfd;
  pid_t child;
  if (pipe(fds) == -1) {
    puts("ABORTED");
    fflush(stdout);
    sysdie("can't create pipe");
  }
  child = fork();
  if (child == (pid_t)-1) {
    puts("ABORTED");
    fflush(stdout);
    sysdie("can't fork");
  } else if (child == 0) {
    errfd = open("/dev/null", O_WRONLY);
    if (errfd < 0)
      _exit(102);
    if (dup2(errfd, 2) == -1)
      _exit(100);
    close(fds[0]);
    if (dup2(fds[1], 1) == -1)
      _exit(100);
    if (execl(path, path, (char *)0) == -1)
      _exit(101);
  } else {
    close(fds[1]);
  }
  *fd = fds[0];
  return child;
}


x86 assembly (clang, -O0):
<test_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e8 <test_start+0x48>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xe25(%rip),%rdi
call   1080 <sysdie@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1229 <test_start+0x89>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xdfb(%rip),%rdi
call   1080 <sysdie@plt>
jmp    12de <test_start+0x13e>
cmpq   $0x0,-0x28(%rbp)
jne    12d1 <test_start+0x131>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1260 <test_start+0xc0>
mov    $0x66,%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1280 <test_start+0xe0>
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12a8 <test_start+0x108>
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
jne    12cc <test_start+0x12c>
mov    $0x65,%edi
call   1030 <_exit@plt>
jmp    12d9 <test_start+0x139>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12de <test_start+0x13e>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_start>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e4 <test_start+0x44>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xe29(%rip),%rdi
call   1080 <sysdie@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1225 <test_start+0x85>
cmp    $0xffffffffffffffff,%rbx
jne    1287 <test_start+0xe7>
lea    0xdfc(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xdfc(%rip),%rdi
call   1080 <sysdie@plt>
jmp    1290 <test_start+0xf0>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xdf0(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12ae <test_start+0x10e>
mov    %eax,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12a4 <test_start+0x104>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12a4 <test_start+0x104>
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1290 <test_start+0xf0>
mov    $0x65,%edi
call   1030 <_exit@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    $0x66,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"ABORTED\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"can't create pipe\00"
@.str.2 = [11 x i8] c"can't fork\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define i64 @test_start(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
%15 = call i32 @sysdie(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %11, %2
%17 = call i64 (...) @fork()
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = call i32 @sysdie(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %64
25: ; preds = %16
%26 = load i64, i64* %7
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %59
28: ; preds = %25
%29 = load i32, i32* @O_WRONLY
%30 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @_exit(i32 102)
unreachable
35: ; preds = %28
%36 = load i32, i32* %6
%37 = call i32 @dup2(i32 %36, i32 2)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @_exit(i32 100)
unreachable
41: ; preds = %35
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @dup2(i32 %46, i32 1)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 @_exit(i32 100)
unreachable
51: ; preds = %41
%52 = load i8*, i8** %3
%53 = load i8*, i8** %3
%54 = call i32 @execl(i8* %52, i8* %53, i8* null)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @_exit(i32 101)
unreachable
58: ; preds = %51
br label %63
59: ; preds = %25
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %59, %58
br label %64
64: ; preds = %63, %20
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%66 = load i32, i32* %65
%67 = load i32*, i32** %4
store i32 %66, i32* %67
%68 = load i64, i64* %7
ret i64 %68
}

LLVM IR (-O2):
@.str = [8 x i8] c"ABORTED\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"can't create pipe\00"
@.str.2 = [11 x i8] c"can't fork\00"
@.str.3 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define i64 @test_start(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = call i32 @sysdie(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %8, %2
%14 = call i64 (...) @fork()
switch i64 %14, label %45 [
i64 -1, label %15
i64 0, label %20
]
15: ; preds = %13
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @stdout
%18 = call i32 @fflush(i32 %17)
%19 = call i32 @sysdie(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %49
20: ; preds = %13
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @_exit(i32 102)
unreachable
26: ; preds = %20
%27 = call i32 @dup2(i32 %22, i32 2)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @_exit(i32 100)
unreachable
31: ; preds = %26
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @dup2(i32 %35, i32 1)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @_exit(i32 100)
unreachable
40: ; preds = %31
%41 = call i32 @execl(i8* %0, i8* %0, i8* null)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = call i32 @_exit(i32 101)
unreachable
45: ; preds = %13
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %45, %40, %15
%50 = load i32, i32* %5
store i32 %50, i32* %1
ret i64 %14
}