#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*/ lace_compat_fd_close(int);
int lace_compat_fd_copy_to(int, int);
int pipe(int *);
int lace_compat_fd_inherit_empty_stdio(int fd) {
  int fds[2] = {-1, -1};
  int istat;
  istat = pipe(fds);
  if (istat != 0) {
    return -1;
  }
  if (fd == 0) {
    istat = lace_compat_fd_copy_to(0, fds[0]);
  } else {
    istat = lace_compat_fd_copy_to(fd, fds[1]);
  }
  if (istat != 0 || fds[0] != fd) {
    lace_compat_fd_close(fds[0]);
  }
  if (istat != 0 || fds[1] != fd) {
    lace_compat_fd_close(fds[1]);
  }
  return istat;
}


x86 assembly (clang, -O0):
<lace_compat_fd_inherit_empty_stdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0xebe(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1168 <lace_compat_fd_inherit_empty_stdio+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <lace_compat_fd_inherit_empty_stdio+0xa4>
cmpl   $0x0,-0x8(%rbp)
jne    1184 <lace_compat_fd_inherit_empty_stdio+0x54>
mov    -0x10(%rbp),%esi
xor    %edi,%edi
call   1050 <lace_compat_fd_copy_to@plt>
mov    %eax,-0x14(%rbp)
jmp    1192 <lace_compat_fd_inherit_empty_stdio+0x62>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <lace_compat_fd_copy_to@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a8 <lace_compat_fd_inherit_empty_stdio+0x78>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11b0 <lace_compat_fd_inherit_empty_stdio+0x80>
mov    -0x10(%rbp),%edi
call   1030 <lace_compat_fd_close@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11c6 <lace_compat_fd_inherit_empty_stdio+0x96>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11ce <lace_compat_fd_inherit_empty_stdio+0x9e>
mov    -0xc(%rbp),%edi
call   1030 <lace_compat_fd_close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lace_compat_fd_inherit_empty_stdio>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movq   $0xffffffffffffffff,(%rsp)
ff
mov    %rsp,%rdi
call   1040 <pipe@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    118d <lace_compat_fd_inherit_empty_stdio+0x5d>
test   %ebx,%ebx
je     115c <lace_compat_fd_inherit_empty_stdio+0x2c>
mov    0x4(%rsp),%esi
mov    %ebx,%edi
jmp    1161 <lace_compat_fd_inherit_empty_stdio+0x31>
mov    (%rsp),%esi
xor    %edi,%edi
call   1050 <lace_compat_fd_copy_to@plt>
mov    %eax,%ebp
mov    (%rsp),%edi
test   %eax,%eax
jne    1173 <lace_compat_fd_inherit_empty_stdio+0x43>
cmp    %ebx,%edi
je     1178 <lace_compat_fd_inherit_empty_stdio+0x48>
call   1030 <lace_compat_fd_close@plt>
mov    0x4(%rsp),%edi
test   %ebp,%ebp
jne    1186 <lace_compat_fd_inherit_empty_stdio+0x56>
xor    %eax,%eax
cmp    %ebx,%edi
je     118d <lace_compat_fd_inherit_empty_stdio+0x5d>
call   1030 <lace_compat_fd_close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.lace_compat_fd_inherit_empty_stdio.fds = [2 x i32] [i32 -1, i32 -1]
define i32 @lace_compat_fd_inherit_empty_stdio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca i32
store i32 %0, i32* %3
%6 = bitcast [2 x i32]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %50
12: ; preds = %1
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%17 = load i32, i32* %16
%18 = call i32 @lace_compat_fd_copy_to(i32 0, i32 %17)
store i32 %18, i32* %5
br label %24
19: ; preds = %12
%20 = load i32, i32* %3
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @lace_compat_fd_copy_to(i32 %20, i32 %22)
store i32 %23, i32* %5
br label %24
24: ; preds = %19, %15
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* %3
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27, %24
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @lace_compat_fd_close(i32 %34)
br label %36
36: ; preds = %32, %27
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%41 = load i32, i32* %40
%42 = load i32, i32* %3
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %39, %36
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @lace_compat_fd_close(i32 %46)
br label %48
48: ; preds = %44, %39
%49 = load i32, i32* %5
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %11
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
define i32 @lace_compat_fd_inherit_empty_stdio(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to [2 x i32]*
%4 = bitcast i64* %2 to i8*
store i64 -1, i64* %2
%5 = bitcast i64* %2 to i32*
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %32
8: ; preds = %1
%9 = icmp eq i32 %0, 0
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = load i32, i32* %5
%12 = call i32 @lace_compat_fd_copy_to(i32 0, i32 %11)
br label %17
13: ; preds = %8
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @lace_compat_fd_copy_to(i32 %0, i32 %15)
br label %17
17: ; preds = %13, %10
%18 = phi i32 [ %12, %10 ], [ %16, %13 ]
%19 = icmp eq i32 %18, 0
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, %0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 @lace_compat_fd_close(i32 %20)
br label %25
25: ; preds = %17, %23
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, %0
%29 = select i1 %19, i1 %28, i1 false
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = call i32 @lace_compat_fd_close(i32 %27)
br label %32
32: ; preds = %30, %25, %1
%33 = phi i32 [ -1, %1 ], [ 0, %25 ], [ %18, %30 ]
ret i32 %33
}