#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 */

/* Variables and functions */
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ WEXITSTATUS(int);
int childProcess(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ parentProcess(int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int socks[2];
  int status;
  if (argc != 2) {
    fprintf(stderr, "only a single argument is supported\n");
    return 1;
  }
  if (socketpair(PF_UNIX, SOCK_STREAM, 0, socks))
    die("socketpair");
  if (!fork()) {
    close(socks[0]);
    return childProcess(argv[1], socks[1]);
  }
  close(socks[1]);
  parentProcess(socks[0]);
  wait(&status);
  if (WEXITSTATUS(status))
    fprintf(stderr, "child failed\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x41>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127b <main+0xeb>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
je     1204 <main+0x74>
lea    0xe26(%rip),%rdi
call   1090 <die@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%eax
jne    1234 <main+0xa4>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1030 <childProcess@plt>
mov    %eax,-0x4(%rbp)
jmp    127b <main+0xeb>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <parentProcess@plt>
lea    -0x1c(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     1274 <main+0xe4>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1220 <main+0x90>
mov    %rsi,%r14
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
je     11d2 <main+0x42>
lea    0xe58(%rip),%rdi
call   1090 <die@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     123c <main+0xac>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <parentProcess@plt>
lea    0x14(%rsp),%rdi
call   10a0 <wait@plt>
mov    0x14(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %rax,%rax
je     1254 <main+0xc4>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1254 <main+0xc4>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebx
jmp    1254 <main+0xc4>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%r14),%rdi
mov    0x10(%rsp),%esi
call   1030 <childProcess@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"socketpair\00"
@.str.2 = [14 x i8] c"child failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %49
13: ; preds = %2
%14 = load i32, i32* @PF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i64 @socketpair(i32 %14, i32 %15, i32 0, i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = call i32 (...) @fork()
%23 = icmp ne i32 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @childProcess(i8* %30, i32 %32)
store i32 %33, i32* %3
br label %49
34: ; preds = %21
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @parentProcess(i32 %39)
%41 = call i32 @wait(i32* %7)
%42 = load i32, i32* %7
%43 = call i64 @WEXITSTATUS(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %45, %34
store i32 0, i32* %3
br label %49
49: ; preds = %48, %24, %10
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"socketpair\00"
@.str.2 = [14 x i8] c"child failed\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %43
11: ; preds = %2
%12 = load i32, i32* @PF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = call i64 @socketpair(i32 %12, i32 %13, i32 0, i32* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = call i32 (...) @fork()
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i32, i32* %14
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @childProcess(i8* %26, i32 %28)
br label %43
30: ; preds = %19
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %14
%35 = call i32 @parentProcess(i32 %34)
%36 = call i32 @wait(i32* %4)
%37 = load i32, i32* %4
%38 = call i64 @WEXITSTATUS(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %30, %40, %22, %8
%44 = phi i32 [ 1, %8 ], [ %29, %22 ], [ 0, %40 ], [ 0, %30 ]
ret i32 %44
}