#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;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ puts(char *);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int execute_helper() {
  int sv[2];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
    puts("internal error");
    return -1;
  }
  pid_t pid = fork();
  if (pid < 0) {
    puts("internal error");
    return -1;
  }
  if (pid == 0) {
    close(sv[1]);
    dup2(sv[0], STDIN_FILENO);
    dup2(sv[0], STDOUT_FILENO);
    close(sv[0]);
    execl("/root/helper", "/root/helper", NULL);
    abort();
  }
  close(sv[0]);
  return sv[1];
}


x86 assembly (clang, -O0):
<execute_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
je     11b7 <execute_helper+0x47>
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <execute_helper+0xda>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11e5 <execute_helper+0x75>
lea    0xe2c(%rip),%rdi
call   1040 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <execute_helper+0xda>
cmpq   $0x0,-0x18(%rbp)
jne    123c <execute_helper+0xcc>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xde6(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execl@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_helper>:
push   %rax
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
je     11a5 <execute_helper+0x35>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1192 <execute_helper+0x22>
je     11c1 <execute_helper+0x51>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%eax
pop    %rcx
ret
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    (%rsp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
lea    0xe14(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execl@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"internal error\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [13 x i8] c"/root/helper\00"
define i32 @execute_helper() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %43
11: ; preds = %0
%12 = call i64 (...) @fork()
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %43
17: ; preds = %11
%18 = load i64, i64* %3
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%25 = load i32, i32* %24
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @dup2(i32 %25, i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @dup2(i32 %29, i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* null)
%36 = call i32 (...) @abort()
unreachable
37: ; preds = %17
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
store i32 %42, i32* %1
br label %43
43: ; preds = %37, %15, %9
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"internal error\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [13 x i8] c"/root/helper\00"
define i32 @execute_helper() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @socketpair(i32 %3, i32 %4, i32 0, i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %0
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %36
15: ; preds = %10
%16 = icmp eq i64 %11, 0
br i1 %16, label %17, label %31
17: ; preds = %15
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = load i32, i32* %5
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* null)
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %15
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%35 = load i32, i32* %34
br label %36
36: ; preds = %13, %31, %8
%37 = phi i32 [ -1, %8 ], [ -1, %13 ], [ %35, %31 ]
ret i32 %37
}