#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execl(char const *, char const *, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int 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], infd, errfd;
  pid_t child;
  if (pipe(fds) == -1) {
    puts("ABORTED");
    fflush(stdout);
    sysdie("can't create pipe");
  }
  child = fork();
  switch (child) {
  case -1:
    puts("ABORTED");
    fflush(stdout);
    sysdie("can't fork");
  case 0:
    close(fds[0]);
    close(1);
    if (dup2(fds[1], 1) < 0)
      _exit(100);
    close(fds[1]);
    close(0);
    infd = open("/dev/null", O_RDONLY);
    if (infd < 0)
      _exit(102);
    if (infd != 0) {
      if (dup2(infd, 0) < 0)
        _exit(100);
      close(infd);
    }
    close(2);
    errfd = open("/dev/null", O_WRONLY);
    if (errfd < 0)
      _exit(103);
    if (errfd != 2) {
      if (dup2(errfd, 2) < 0)
        _exit(100);
      close(errfd);
    }
    if (execl(path, path, (char *)0) == -1)
      _exit(101);
  default:
    close(fds[1]);
    break;
  }
  *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    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xe25(%rip),%rdi
call   1080 <sysdie@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1216 <test_start+0x76>
jmp    1206 <test_start+0x66>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     123c <test_start+0x9c>
jmp    1369 <test_start+0x1c9>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xde3(%rip),%rdi
call   1080 <sysdie@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%eax
jge    126e <test_start+0xce>
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
lea    0xd98(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a9 <test_start+0x109>
mov    $0x66,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12d8 <test_start+0x138>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
cmp    $0x0,%eax
jge    12d0 <test_start+0x130>
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%esi
lea    0xd33(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    130e <test_start+0x16e>
mov    $0x67,%edi
call   1030 <_exit@plt>
cmpl   $0x2,-0x20(%rbp)
je     1340 <test_start+0x1a0>
mov    -0x20(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x0,%eax
jge    1338 <test_start+0x198>
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1364 <test_start+0x1c4>
mov    $0x65,%edi
call   1030 <_exit@plt>
jmp    1369 <test_start+0x1c9>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_start>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e0 <test_start+0x40>
lea    0xe3f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xe2d(%rip),%rdi
call   1080 <sysdie@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     121c <test_start+0x7c>
cmp    $0xffffffff,%ebp
jne    12dc <test_start+0x13c>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xe03(%rip),%rdi
call   1080 <sysdie@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     12f8 <test_start+0x158>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    0xdc1(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1302 <test_start+0x162>
je     1289 <test_start+0xe9>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     12f8 <test_start+0x158>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
lea    0xd82(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     130c <test_start+0x16c>
mov    %eax,%ebx
cmp    $0x2,%eax
je     12ca <test_start+0x12a>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     12f8 <test_start+0x158>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   10b0 <execl@plt>
cmp    $0xffffffff,%eax
je     1316 <test_start+0x176>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%eax
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x64,%edi
call   1030 <_exit@plt>
mov    $0x66,%edi
call   1030 <_exit@plt>
mov    $0x67,%edi
call   1030 <_exit@plt>
mov    $0x65,%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_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @test_start(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stdout
%15 = call i32 @fflush(i32 %14)
%16 = call i32 @sysdie(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %12, %2
%18 = call i32 (...) @fork()
store i32 %18, i32* %8
%19 = load i32, i32* %8
switch i32 %19, label %87 [
i32 -1, label %20
i32 0, label %25
]
20: ; preds = %17
%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 %25
25: ; preds = %17, %20
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 @close(i32 1)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @_exit(i32 100)
unreachable
36: ; preds = %25
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = call i32 @close(i32 0)
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = call i32 @_exit(i32 102)
unreachable
47: ; preds = %36
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %47
%51 = load i32, i32* %6
%52 = call i32 @dup2(i32 %51, i32 0)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @_exit(i32 100)
unreachable
56: ; preds = %50
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %47
%60 = call i32 @close(i32 2)
%61 = load i32, i32* @O_WRONLY
%62 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %61)
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 @_exit(i32 103)
unreachable
67: ; preds = %59
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, 2
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i32, i32* %7
%72 = call i32 @dup2(i32 %71, i32 2)
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @_exit(i32 100)
unreachable
76: ; preds = %70
%77 = load i32, i32* %7
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %76, %67
%80 = load i8*, i8** %3
%81 = load i8*, i8** %3
%82 = call i32 @execl(i8* %80, i8* %81, i8* null)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = call i32 @_exit(i32 101)
unreachable
86: ; preds = %79
br label %87
87: ; preds = %17, %86
%88 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %87
%92 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%93 = load i32, i32* %92
%94 = load i32*, i32** %4
store i32 %93, i32* %94
%95 = load i32, i32* %8
ret i32 %95
}

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_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @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 i32 (...) @fork()
switch i32 %14, label %69 [
i32 -1, label %15
i32 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 %20
20: ; preds = %13, %15
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
%23 = call i32 @close(i32 1)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @dup2(i32 %25, i32 1)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @_exit(i32 100)
unreachable
30: ; preds = %20
%31 = load i32, i32* %24
%32 = call i32 @close(i32 %31)
%33 = call i32 @close(i32 0)
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @_exit(i32 102)
unreachable
39: ; preds = %30
%40 = icmp eq i32 %35, 0
br i1 %40, label %48, label %41
41: ; preds = %39
%42 = call i32 @dup2(i32 %35, i32 0)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @_exit(i32 100)
unreachable
46: ; preds = %41
%47 = call i32 @close(i32 %35)
br label %48
48: ; preds = %46, %39
%49 = call i32 @close(i32 2)
%50 = load i32, i32* @O_WRONLY
%51 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 @_exit(i32 103)
unreachable
55: ; preds = %48
%56 = icmp eq i32 %51, 2
br i1 %56, label %64, label %57
57: ; preds = %55
%58 = call i32 @dup2(i32 %51, i32 2)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @_exit(i32 100)
unreachable
62: ; preds = %57
%63 = call i32 @close(i32 %51)
br label %64
64: ; preds = %62, %55
%65 = call i32 @execl(i8* %0, i8* %0, i8* null)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 @_exit(i32 101)
unreachable
69: ; preds = %64, %13
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %5
store i32 %73, i32* %1
ret i32 %14
}