#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 */
scalar_t__ close(int);
int dup2(int, int);
int _popen_noshell_close_and_dup(int pipefd[2], int closed_pipefd,
                                 int target_fd) {
  int dupped_pipefd;
  dupped_pipefd = (closed_pipefd == 0 ? 1 : 0);
  if (close(pipefd[closed_pipefd]) != 0) {
    return -1;
  }
  if (close(target_fd) != 0) {
    return -1;
  }
  if (dup2(pipefd[dupped_pipefd], target_fd) == -1) {
    return -1;
  }
  if (close(pipefd[dupped_pipefd]) != 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_popen_noshell_close_and_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     116b <_popen_noshell_close_and_dup+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <_popen_noshell_close_and_dup+0xbe>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1189 <_popen_noshell_close_and_dup+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <_popen_noshell_close_and_dup+0xbe>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x18(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11b1 <_popen_noshell_close_and_dup+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <_popen_noshell_close_and_dup+0xbe>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     11d7 <_popen_noshell_close_and_dup+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <_popen_noshell_close_and_dup+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_popen_noshell_close_and_dup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
movslq %esi,%rax
mov    (%rdi,%rax,4),%edi
call   1040 <close@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    117c <_popen_noshell_close_and_dup+0x5c>
mov    %r14d,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    117c <_popen_noshell_close_and_dup+0x5c>
xor    %r12d,%r12d
test   %ebp,%ebp
sete   %r12b
mov    (%rbx,%r12,4),%edi
mov    %r14d,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     117c <_popen_noshell_close_and_dup+0x5c>
mov    (%rbx,%r12,4),%edi
call   1040 <close@plt>
neg    %rax
sbb    %r15d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @_popen_noshell_close_and_dup(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i64
%12 = select i1 %10, i32 1, i32 0
store i32 %12, i32* %8
%13 = load i32*, i32** %5
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = load i32, i32* %16
%18 = call i64 @close(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %46
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i64 @close(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %4
br label %46
26: ; preds = %21
%27 = load i32*, i32** %5
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = load i32, i32* %7
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %26
store i32 -1, i32* %4
br label %46
36: ; preds = %26
%37 = load i32*, i32** %5
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = call i64 @close(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 -1, i32* %4
br label %46
45: ; preds = %36
store i32 0, i32* %4
br label %46
46: ; preds = %45, %44, %35, %25, %20
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
define i32 @_popen_noshell_close_and_dup(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
%5 = sext i32 %1 to i64
%6 = getelementptr inbounds i32, i32* %0, i64 %5
%7 = load i32, i32* %6
%8 = tail call i64 @close(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %3
%11 = tail call i64 @close(i32 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = zext i1 %4 to i64
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 @dup2(i32 %16, i32 %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %24, label %19
19: ; preds = %13
%20 = load i32, i32* %15
%21 = tail call i64 @close(i32 %20)
%22 = icmp ne i64 %21, 0
%23 = sext i1 %22 to i32
br label %24
24: ; preds = %19, %13, %10, %3
%25 = phi i32 [ -1, %3 ], [ -1, %10 ], [ -1, %13 ], [ %23, %19 ]
ret i32 %25
}