#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*/, ...);
bool fd_set_cloexec(int fd, bool cloexec) {
  int flags = fcntl(fd, F_GETFD);
  if ((flags < 0)) {
    return false;
  }
  int new_flags = cloexec ? (flags | FD_CLOEXEC) : (flags & ~FD_CLOEXEC);
  return new_flags == flags || fcntl(fd, F_SETFD, new_flags) != -1;
}


x86 assembly (clang, -O0):
<fd_set_cloexec>:
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
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    114a <fd_set_cloexec+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11bd <fd_set_cloexec+0xad>
cmpl   $0x0,-0xc(%rbp)
je     1168 <fd_set_cloexec+0x58>
mov    -0x10(%rbp),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
jmp    117c <fd_set_cloexec+0x6c>
mov    -0x10(%rbp),%eax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    $0x1,%al
cmp    -0x10(%rbp),%ecx
mov    %al,-0x19(%rbp)
je     11b2 <fd_set_cloexec+0xa2>
mov    -0x8(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_set_cloexec>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     116a <fd_set_cloexec+0x5a>
mov    0x2ea2(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %r14d,%r14d
cmovne %ecx,%edx
mov    $0x1,%ebp
cmp    %eax,%edx
je     116a <fd_set_cloexec+0x5a>
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
setne  %bpl
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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