#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 fcntl(int, int /*<<< orphan*/, int);
int fd_closeonexec(int fd, int on) {
  int x;
  if ((x = fcntl(fd, F_GETFD, 0)) == -1)
    return -1;
  if (on)
    x &= ~FD_CLOEXEC;
  else
    x |= FD_CLOEXEC;
  return fcntl(fd, F_SETFD, x);
}


x86 assembly (clang, -O0):
<fd_closeonexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1149 <fd_closeonexec+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1190 <fd_closeonexec+0x80>
cmpl   $0x0,-0xc(%rbp)
je     116a <fd_closeonexec+0x5a>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1179 <fd_closeonexec+0x69>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_closeonexec>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1158 <fd_closeonexec+0x48>
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
not    %ecx
and    %eax,%ecx
or     %eax,%edx
test   %ebp,%ebp
cmovne %ecx,%edx
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fcntl@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @fd_closeonexec(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @F_GETFD
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %29
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @FD_CLOEXEC
%17 = xor i32 %16, -1
%18 = load i32, i32* %6
%19 = and i32 %18, %17
store i32 %19, i32* %6
br label %24
20: ; preds = %12
%21 = load i32, i32* @FD_CLOEXEC
%22 = load i32, i32* %6
%23 = or i32 %22, %21
store i32 %23, i32* %6
br label %24
24: ; preds = %20, %15
%25 = load i32, i32* %4
%26 = load i32, i32* @F_SETFD
%27 = load i32, i32* %6
%28 = call i32 @fcntl(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %24, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @fd_closeonexec(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 @fcntl(i32 %0, i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = icmp eq i32 %1, 0
%8 = load i32, i32* @FD_CLOEXEC
%9 = xor i32 %8, -1
%10 = and i32 %4, %9
%11 = or i32 %8, %4
%12 = select i1 %7, i32 %11, i32 %10
%13 = load i32, i32* @F_SETFD
%14 = tail call i32 @fcntl(i32 %0, i32 %13, i32 %12)
br label %15
15: ; preds = %2, %6
%16 = phi i32 [ %14, %6 ], [ -1, %2 ]
ret i32 %16
}