#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*/ close(int);
int dup2(int, int);
int execl(char const *, char const *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int pipe(int *);
int /*<<< orphan*/ stderr;
pid_t test_start(const char *path, int *fd) {
  int fds[2], errfd;
  pid_t child;
  if (pipe(fds) == -1) {
    fprintf(stderr, "ABORTED can't create pipe\n");
    exit(1);
  }
  child = fork();
  if (child == (pid_t)-1) {
    fprintf(stderr, "ABORTED can't fork\n");
    exit(2);
  } else if (child == 0) {
    errfd = open("/dev/null", O_WRONLY);
    if (errfd < 0)
      exit(3);
    if (dup2(errfd, 2) == -1)
      exit(4);
    close(fds[0]);
    if (dup2(fds[1], 1) == -1)
      exit(5);
    if (execl(path, path, (char *)0) == -1)
      exit(6);
  } 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   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c1 <test_start+0x41>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11f6 <test_start+0x76>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    129e <test_start+0x11e>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122d <test_start+0xad>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    124d <test_start+0xcd>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1275 <test_start+0xf5>
mov    $0x5,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
cmp    $0xffffffff,%eax
jne    1299 <test_start+0x119>
mov    $0x6,%edi
call   1080 <exit@plt>
jmp    12a6 <test_start+0x126>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
jmp    12ab <test_start+0x12b>
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   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     123a <test_start+0xba>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c6 <test_start+0x46>
cmp    $0xffffffffffffffff,%rbx
je     1259 <test_start+0xd9>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
jmp    1226 <test_start+0xa6>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1278 <test_start+0xf8>
mov    %eax,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1282 <test_start+0x102>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     128c <test_start+0x10c>
mov    %r15,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1090 <execl@plt>
cmp    $0xffffffff,%eax
je     1296 <test_start+0x116>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    $0x5,%edi
call   1080 <exit@plt>
mov    $0x6,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"ABORTED can't create pipe\0A\00"
@.str.1 = [20 x i8] c"ABORTED can't fork\0A\00"
@.str.2 = [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 %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i64 (...) @fork()
switch i64 %13, label %43 [
i64 -1, label %14
i64 0, label %18
]
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 2)
unreachable
18: ; preds = %12
%19 = load i32, i32* @O_WRONLY
%20 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @exit(i32 3)
unreachable
24: ; preds = %18
%25 = call i32 @dup2(i32 %20, i32 2)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit(i32 4)
unreachable
29: ; preds = %24
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 1)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @exit(i32 5)
unreachable
38: ; preds = %29
%39 = call i32 @execl(i8* %0, i8* %0, i8* null)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = call i32 @exit(i32 6)
unreachable
43: ; preds = %12
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %43, %38
%48 = load i32, i32* %5
store i32 %48, i32* %1
ret i64 %13
}