#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 set_cloexec_flag(int desc, int value) {
  int oldflags = fcntl(desc, F_GETFD, 0);
  if (oldflags < 0)
    return oldflags;
  if (value != 0)
    oldflags |= FD_CLOEXEC;
  else
    oldflags &= ~FD_CLOEXEC;
  return fcntl(desc, F_SETFD, oldflags);
}


x86 assembly (clang, -O0):
<set_cloexec_flag>:
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)
cmpl   $0x0,-0x10(%rbp)
jge    1149 <set_cloexec_flag+0x39>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <set_cloexec_flag+0x80>
cmpl   $0x0,-0xc(%rbp)
je     1167 <set_cloexec_flag+0x57>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1179 <set_cloexec_flag+0x69>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -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):
<set_cloexec_flag>:
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>
test   %eax,%eax
js     1157 <set_cloexec_flag+0x47>
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %ebp,%ebp
cmovne %ecx,%edx
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fcntl@plt>
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 @set_cloexec_flag(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 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %6
store i32 %13, i32* %3
br label %31
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* @FD_CLOEXEC
%19 = load i32, i32* %6
%20 = or i32 %19, %18
store i32 %20, i32* %6
br label %26
21: ; preds = %14
%22 = load i32, i32* @FD_CLOEXEC
%23 = xor i32 %22, -1
%24 = load i32, i32* %6
%25 = and i32 %24, %23
store i32 %25, i32* %6
br label %26
26: ; preds = %21, %17
%27 = load i32, i32* %4
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* %6
%30 = call i32 @fcntl(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %3
br label %31
31: ; preds = %26, %12
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @set_cloexec_flag(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 %15, 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 = 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 ], [ %4, %2 ]
ret i32 %16
}