#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*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
scalar_t__ execv(char const *, char *const *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int socket_launch(const char *path, char *const argv[], pid_t *return_pid) {
  int sockfd[2];
  pid_t pid;
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0)
    return -1;
  pid = fork();
  if (pid < 0)
    return -1;
  if (pid == 0) {
    close(sockfd[0]);
    if (dup2(sockfd[1], STDIN_FILENO) != STDIN_FILENO)
      perror("Could not duplicate socket onto child STDIN");
    if (dup2(sockfd[1], STDOUT_FILENO) != STDOUT_FILENO)
      perror("Could not duplicate socket onto child STDOUT");
    if (execv(path, argv) < 0)
      perror("Could not launch child executable");
    exit(-1);
  }
  if (return_pid)
    *return_pid = pid;
  close(sockfd[1]);
  return sockfd[0];
}


x86 assembly (clang, -O0):
<socket_launch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
cmp    $0x0,%rax
jge    11b7 <socket_launch+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <socket_launch+0x129>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11d9 <socket_launch+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <socket_launch+0x129>
cmpq   $0x0,-0x30(%rbp)
jne    1275 <socket_launch+0x105>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
je     121a <socket_launch+0xaa>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%rsi
call   1030 <dup2@plt>
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
je     1248 <socket_launch+0xd8>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execv@plt>
cmp    $0x0,%rax
jge    126b <socket_launch+0xfb>
lea    0xdf3(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     128b <socket_launch+0x11b>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_launch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
xor    %edx,%edx
call   1050 <socketpair@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     11c8 <socket_launch+0x58>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11c8 <socket_launch+0x58>
je     11d5 <socket_launch+0x65>
test   %rbx,%rbx
je     11bc <socket_launch+0x4c>
mov    %rax,(%rbx)
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2de0(%rip),%rbx
mov    (%rbx),%rsi
call   1030 <dup2@plt>
cmp    (%rbx),%rax
je     1201 <socket_launch+0x91>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
mov    0x4(%rsp),%edi
mov    0x2de4(%rip),%rbx
mov    (%rbx),%rsi
call   1030 <dup2@plt>
cmp    (%rbx),%rax
je     1225 <socket_launch+0xb5>
lea    0xe0c(%rip),%rdi
call   1060 <perror@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <execv@plt>
test   %rax,%rax
jns    1241 <socket_launch+0xd1>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@STDIN_FILENO = global i64 0
@.str = [44 x i8] c"Could not duplicate socket onto child STDIN\00"
@STDOUT_FILENO = global i64 0
@.str.1 = [45 x i8] c"Could not duplicate socket onto child STDOUT\00"
@.str.2 = [34 x i8] c"Could not launch child executable\00"
define i32 @socket_launch(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca [2 x i32]
%9 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%13 = call i64 @socketpair(i32 %10, i32 %11, i32 0, i32* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %66
16: ; preds = %3
%17 = call i64 (...) @fork()
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %4
br label %66
21: ; preds = %16
%22 = load i64, i64* %9
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %54
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%29 = load i32, i32* %28
%30 = load i64, i64* @STDIN_FILENO
%31 = call i64 @dup2(i32 %29, i64 %30)
%32 = load i64, i64* @STDIN_FILENO
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%38 = load i32, i32* %37
%39 = load i64, i64* @STDOUT_FILENO
%40 = call i64 @dup2(i32 %38, i64 %39)
%41 = load i64, i64* @STDOUT_FILENO
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = load i8*, i8** %5
%47 = load i8**, i8*** %6
%48 = call i64 @execv(i8* %46, i8** %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %21
%55 = load i64*, i64** %7
%56 = icmp ne i64* %55, null
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = load i64, i64* %9
%59 = load i64*, i64** %7
store i64 %58, i64* %59
br label %60
60: ; preds = %57, %54
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%65 = load i32, i32* %64
store i32 %65, i32* %4
br label %66
66: ; preds = %60, %20, %15
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@STDIN_FILENO = global i64 0
@.str = [44 x i8] c"Could not duplicate socket onto child STDIN\00"
@STDOUT_FILENO = global i64 0
@.str.1 = [45 x i8] c"Could not duplicate socket onto child STDOUT\00"
@.str.2 = [34 x i8] c"Could not launch child executable\00"
define i32 @socket_launch(i8* %0, i8** %1, i64* writeonly %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i64 @socketpair(i32 %6, i32 %7, i32 0, i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %50, label %11
11: ; preds = %3
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %50, label %14
14: ; preds = %11
%15 = icmp eq i64 %12, 0
br i1 %15, label %16, label %42
16: ; preds = %14
%17 = load i32, i32* %8
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i64, i64* @STDIN_FILENO
%22 = call i64 @dup2(i32 %20, i64 %21)
%23 = load i64, i64* @STDIN_FILENO
%24 = icmp eq i64 %22, %23
br i1 %24, label %27, label %25
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %16
%28 = load i32, i32* %19
%29 = load i64, i64* @STDOUT_FILENO
%30 = call i64 @dup2(i32 %28, i64 %29)
%31 = load i64, i64* @STDOUT_FILENO
%32 = icmp eq i64 %30, %31
br i1 %32, label %35, label %33
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = call i64 @execv(i8* %0, i8** %1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %14
%43 = icmp eq i64* %2, null
br i1 %43, label %45, label %44
44: ; preds = %42
store i64 %12, i64* %2
br label %45
45: ; preds = %44, %42
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
br label %50
50: ; preds = %11, %3, %45
%51 = phi i32 [ %49, %45 ], [ -1, %3 ], [ -1, %11 ]
ret i32 %51
}