#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 _co_close_on_exec(int fd, int on) {
  int flags;
  if ((flags = fcntl(fd, F_GETFD, 0)) < 0) {
    return -1;
  }
  if (fcntl(fd, F_SETFD, on ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC) < 0) {
    return -1;
  }
  return ((flags & FD_CLOEXEC) ? 1 : 0);
}


x86 assembly (clang, -O0):
<_co_close_on_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    $0x0,%eax
jge    1149 <_co_close_on_exec+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <_co_close_on_exec+0xbc>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1179 <_co_close_on_exec+0x69>
mov    -0x10(%rbp),%eax
mov    0x2e69(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    118d <_co_close_on_exec+0x7d>
mov    -0x10(%rbp),%eax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11b0 <_co_close_on_exec+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <_co_close_on_exec+0xbc>
mov    -0x10(%rbp),%edx
mov    0x2e1e(%rip),%rax
and    (%rax),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_co_close_on_exec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %edi,%r14d
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1170 <_co_close_on_exec+0x60>
mov    %eax,%ebp
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    0x2e8f(%rip),%r12
mov    (%r12),%edx
mov    %edx,%eax
or     %ebp,%eax
not    %edx
and    %ebp,%edx
test   %r15d,%r15d
cmovne %eax,%edx
mov    %r14d,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     1170 <_co_close_on_exec+0x60>
xor    %ebx,%ebx
test   %ebp,(%r12)
setne  %bl
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @_co_close_on_exec(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 slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %38
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* @F_SETFD
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* %6
%19 = load i32, i32* @FD_CLOEXEC
%20 = or i32 %18, %19
br label %26
21: ; preds = %12
%22 = load i32, i32* %6
%23 = load i32, i32* @FD_CLOEXEC
%24 = xor i32 %23, -1
%25 = and i32 %22, %24
br label %26
26: ; preds = %21, %17
%27 = phi i32 [ %20, %17 ], [ %25, %21 ]
%28 = call i32 @fcntl(i32 %13, i32 %14, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %3
br label %38
31: ; preds = %26
%32 = load i32, i32* %6
%33 = load i32, i32* @FD_CLOEXEC
%34 = and i32 %32, %33
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 1, i32 0
store i32 %37, i32* %3
br label %38
38: ; preds = %31, %30, %11
%39 = load i32, i32* %3
ret i32 %39
}

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