#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int STDIN_FILENO;
int STDOUT_FILENO;
int dup(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int remote_setup(const char *pProgram, int eExecute, int *cClientIn,
                 int *cClientOut, int *fFarIn, int *fFarOut) {
  if (!cClientIn || !cClientOut || !fFarIn || !fFarOut) {
    fprintf(stderr, "%s: internal error\n", pProgram);
    return -1;
  }
  if (eExecute) {
    int new_sockets[2] = {-1, -1};
    if (socketpair(PF_LOCAL, SOCK_STREAM, 0, new_sockets) < 0) {
      fprintf(stderr, "%s: could not create new socket: %s\n", pProgram,
              strerror(errno));
      return -1;
    }
    *cClientIn = *cClientOut = dup(STDIN_FILENO);
    *fFarIn = *fFarOut = new_sockets[0];
    if (*cClientIn < 0 || *cClientOut < 0 ||
        dup2(new_sockets[1], STDIN_FILENO) < 0) {
      fprintf(stderr, "%s: could not duplicate socket: %s\n", pProgram,
              strerror(errno));
      return -1;
    }
    fcntl(*cClientIn, F_SETFD, fcntl(*cClientIn, F_GETFD) | FD_CLOEXEC);
    fcntl(*fFarIn, F_SETFD, fcntl(*fFarIn, F_GETFD) | FD_CLOEXEC);
  } else {
    *fFarIn = STDOUT_FILENO;
    *fFarOut = STDOUT_FILENO;
    *cClientIn = STDIN_FILENO;
    *cClientOut = STDIN_FILENO;
  }
  return 0;
}


x86 assembly (clang, -O0):
<remote_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ab <remote_setup+0x4b>
cmpq   $0x0,-0x28(%rbp)
je     11ab <remote_setup+0x4b>
cmpq   $0x0,-0x30(%rbp)
je     11ab <remote_setup+0x4b>
cmpq   $0x0,-0x38(%rbp)
jne    11d2 <remote_setup+0x72>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <remote_setup+0x268>
cmpl   $0x0,-0x14(%rbp)
je     1385 <remote_setup+0x225>
mov    0xe7d(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
jge    1254 <remote_setup+0xf4>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <remote_setup+0x268>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jl     12b4 <remote_setup+0x154>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jl     12b4 <remote_setup+0x154>
mov    -0x3c(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    12fa <remote_setup+0x19a>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c8 <remote_setup+0x268>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%esi
mov    %eax,%edx
mov    0x2c8c(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
mov    0x2c51(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%esi
mov    %eax,%edx
mov    0x2c49(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
jmp    13c1 <remote_setup+0x261>
mov    0x2c64(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2c55(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2bf6(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2be7(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remote_setup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rdx,%rdx
je     128d <remote_setup+0x12d>
mov    %rcx,%rbx
test   %rcx,%rcx
je     128d <remote_setup+0x12d>
mov    %r8,%r15
test   %r8,%r8
je     128d <remote_setup+0x12d>
mov    %r9,%r13
test   %r9,%r9
je     128d <remote_setup+0x12d>
mov    %rdx,%r12
test   %esi,%esi
je     12a9 <remote_setup+0x149>
movq   $0xffffffffffffffff,(%rsp)
ff
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
js     12d0 <remote_setup+0x170>
mov    0x2dc6(%rip),%rbp
mov    0x0(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,(%rbx)
mov    %eax,(%r12)
mov    (%rsp),%eax
mov    %eax,0x0(%r13)
mov    %eax,(%r15)
lea    0xe40(%rip),%r13
cmpl   $0x0,(%r12)
js     12d7 <remote_setup+0x177>
cmpl   $0x0,(%rbx)
js     12d7 <remote_setup+0x177>
mov    0x4(%rsp),%edi
mov    0x0(%rbp),%esi
call   1040 <dup2@plt>
test   %rax,%rax
js     12d7 <remote_setup+0x177>
mov    (%r12),%ebp
mov    0x2da3(%rip),%rax
mov    (%rax),%r14d
mov    0x2d79(%rip),%r13
mov    0x0(%r13),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2d73(%rip),%r12
or     (%r12),%eax
mov    %ebp,%edi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r15),%ebp
mov    0x2d67(%rip),%rax
mov    (%rax),%r14d
mov    0x0(%r13),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
or     (%r12),%eax
mov    %ebp,%edi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
jmp    1305 <remote_setup+0x1a5>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1300 <remote_setup+0x1a0>
mov    0x2d40(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,(%r15)
mov    (%rax),%eax
mov    %eax,0x0(%r13)
mov    0x2cde(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,(%r12)
mov    (%rax),%eax
mov    %eax,(%rbx)
xor    %ebx,%ebx
jmp    1305 <remote_setup+0x1a5>
lea    0xd3d(%rip),%r13
mov    0x2cfa(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"%s: internal error\0A\00"
@__const.remote_setup.new_sockets = [2 x i32] [i32 -1, i32 -1]
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"%s: could not create new socket: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [36 x i8] c"%s: could not duplicate socket: %s\0A\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @remote_setup(i8* %0, i32 %1, i32* %2, i32* %3, i32* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca [2 x i32]
store i8* %0, i8** %8
store i32 %1, i32* %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %26
17: ; preds = %6
%18 = load i32*, i32** %11
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32*, i32** %12
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32*, i32** %13
%25 = icmp ne i32* %24, null
br i1 %25, label %30, label %26
26: ; preds = %23, %20, %17, %6
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %8
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 -1, i32* %7
br label %105
30: ; preds = %23
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %95
33: ; preds = %30
%34 = bitcast [2 x i32]* %14 to i8*
%35 = load i32, i32* @PF_LOCAL
%36 = load i32, i32* @SOCK_STREAM
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%38 = call i64 @socketpair(i32 %35, i32 %36, i32 0, i32* %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %8
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44)
store i32 -1, i32* %7
br label %105
46: ; preds = %33
%47 = load i32, i32* @STDIN_FILENO
%48 = call i32 @dup(i32 %47)
%49 = load i32*, i32** %11
store i32 %48, i32* %49
%50 = load i32*, i32** %10
store i32 %48, i32* %50
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 0
%52 = load i32, i32* %51
%53 = load i32*, i32** %13
store i32 %52, i32* %53
%54 = load i32*, i32** %12
store i32 %52, i32* %54
%55 = load i32*, i32** %10
%56 = load i32, i32* %55
%57 = icmp slt i32 %56, 0
br i1 %57, label %68, label %58
58: ; preds = %46
%59 = load i32*, i32** %11
%60 = load i32, i32* %59
%61 = icmp slt i32 %60, 0
br i1 %61, label %68, label %62
62: ; preds = %58
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %14, i64 0, i64 1
%64 = load i32, i32* %63
%65 = load i32, i32* @STDIN_FILENO
%66 = call i64 @dup2(i32 %64, i32 %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %62, %58, %46
%69 = load i32, i32* @stderr
%70 = load i8*, i8** %8
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %70, i8* %72)
store i32 -1, i32* %7
br label %105
74: ; preds = %62
%75 = load i32*, i32** %10
%76 = load i32, i32* %75
%77 = load i32, i32* @F_SETFD
%78 = load i32*, i32** %10
%79 = load i32, i32* %78
%80 = load i32, i32* @F_GETFD
%81 = call i32 (i32, i32, ...) @fcntl(i32 %79, i32 %80)
%82 = load i32, i32* @FD_CLOEXEC
%83 = or i32 %81, %82
%84 = call i32 (i32, i32, ...) @fcntl(i32 %76, i32 %77, i32 %83)
%85 = load i32*, i32** %12
%86 = load i32, i32* %85
%87 = load i32, i32* @F_SETFD
%88 = load i32*, i32** %12
%89 = load i32, i32* %88
%90 = load i32, i32* @F_GETFD
%91 = call i32 (i32, i32, ...) @fcntl(i32 %89, i32 %90)
%92 = load i32, i32* @FD_CLOEXEC
%93 = or i32 %91, %92
%94 = call i32 (i32, i32, ...) @fcntl(i32 %86, i32 %87, i32 %93)
br label %104
95: ; preds = %30
%96 = load i32, i32* @STDOUT_FILENO
%97 = load i32*, i32** %12
store i32 %96, i32* %97
%98 = load i32, i32* @STDOUT_FILENO
%99 = load i32*, i32** %13
store i32 %98, i32* %99
%100 = load i32, i32* @STDIN_FILENO
%101 = load i32*, i32** %10
store i32 %100, i32* %101
%102 = load i32, i32* @STDIN_FILENO
%103 = load i32*, i32** %11
store i32 %102, i32* %103
br label %104
104: ; preds = %95, %74
store i32 0, i32* %7
br label %105
105: ; preds = %104, %68, %40, %26
%106 = load i32, i32* %7
ret i32 %106
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"%s: internal error\0A\00"
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"%s: could not create new socket: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [36 x i8] c"%s: could not duplicate socket: %s\0A\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @remote_setup(i8* %0, i32 %1, i32* %2, i32* %3, i32* %4, i32* writeonly %5) {
%7 = alloca i64
%8 = bitcast i64* %7 to [2 x i32]*
%9 = icmp ne i32* %2, null
%10 = icmp ne i32* %3, null
%11 = and i1 %9, %10
%12 = icmp ne i32* %4, null
%13 = and i1 %11, %12
%14 = icmp ne i32* %5, null
%15 = and i1 %13, %14
br i1 %15, label %19, label %16
16: ; preds = %6
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %69
19: ; preds = %6
%20 = icmp eq i32 %1, 0
br i1 %20, label %64, label %21
21: ; preds = %19
%22 = bitcast i64* %7 to i8*
store i64 -1, i64* %7
%23 = load i32, i32* @PF_LOCAL
%24 = load i32, i32* @SOCK_STREAM
%25 = bitcast i64* %7 to i32*
%26 = call i64 @socketpair(i32 %23, i32 %24, i32 0, i32* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %43, label %28
28: ; preds = %21
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @dup(i32 %29)
store i32 %30, i32* %3
store i32 %30, i32* %2
%31 = load i32, i32* %25
store i32 %31, i32* %5
store i32 %31, i32* %4
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %28
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%39 = load i32, i32* %38
%40 = load i32, i32* @STDIN_FILENO
%41 = call i64 @dup2(i32 %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %28, %34, %37, %21
%44 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %21 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %37 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %34 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), %28 ]
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* %44, i8* %0, i8* %47)
br label %69
49: ; preds = %37
%50 = load i32, i32* %2
%51 = load i32, i32* @F_SETFD
%52 = load i32, i32* @F_GETFD
%53 = call i32 (i32, i32, ...) @fcntl(i32 %50, i32 %52)
%54 = load i32, i32* @FD_CLOEXEC
%55 = or i32 %54, %53
%56 = call i32 (i32, i32, ...) @fcntl(i32 %50, i32 %51, i32 %55)
%57 = load i32, i32* %4
%58 = load i32, i32* @F_SETFD
%59 = load i32, i32* @F_GETFD
%60 = call i32 (i32, i32, ...) @fcntl(i32 %57, i32 %59)
%61 = load i32, i32* @FD_CLOEXEC
%62 = or i32 %61, %60
%63 = call i32 (i32, i32, ...) @fcntl(i32 %57, i32 %58, i32 %62)
br label %69
64: ; preds = %19
%65 = load i32, i32* @STDOUT_FILENO
store i32 %65, i32* %4
%66 = load i32, i32* @STDOUT_FILENO
store i32 %66, i32* %5
%67 = load i32, i32* @STDIN_FILENO
store i32 %67, i32* %2
%68 = load i32, i32* @STDIN_FILENO
store i32 %68, i32* %3
br label %69
69: ; preds = %64, %49, %43, %16
%70 = phi i32 [ -1, %16 ], [ -1, %43 ], [ 0, %49 ], [ 0, %64 ]
ret i32 %70
}