#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*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int O_CLOEXEC;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
void unset_cloexec(int fd) {
  int flags = fcntl(fd, F_GETFD);
  assert(flags != -1 && "couldn't get flags to unset cloexec");
  int result = fcntl(fd, F_SETFD, flags & ~O_CLOEXEC);
  assert(result == 0 && "couldn't set flags to unset cloexec");
}


x86 assembly (clang, -O0):
<unset_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x8(%rbp)
mov    %al,-0xd(%rbp)
je     115a <unset_cloexec+0x3a>
mov    $0x1,%al
mov    %al,-0xd(%rbp)
jmp    115a <unset_cloexec+0x3a>
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xe(%rbp)
jne    11a7 <unset_cloexec+0x87>
mov    $0x1,%al
mov    %al,-0xe(%rbp)
jmp    11a7 <unset_cloexec+0x87>
mov    -0xe(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unset_cloexec>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1040 <assert@plt>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
not    %edx
and    %ebp,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <assert@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@.str = [36 x i8] c"couldn't get flags to unset cloexec\00"
@F_SETFD = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [36 x i8] c"couldn't set flags to unset cloexec\00"
define void @unset_cloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @F_GETFD
%7 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
br label %11
11: ; preds = %10, %1
%12 = phi i1 [ false, %1 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %2
%16 = load i32, i32* @F_SETFD
%17 = load i32, i32* %3
%18 = load i32, i32* @O_CLOEXEC
%19 = xor i32 %18, -1
%20 = and i32 %17, %19
%21 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %11
br label %25
25: ; preds = %24, %11
%26 = phi i1 [ false, %11 ], [ true, %24 ]
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
ret void
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@O_CLOEXEC = global i32 0
define void @unset_cloexec(i32 %0) {
%2 = load i32, i32* @F_GETFD
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp ne i32 %3, -1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i32, i32* @F_SETFD
%8 = load i32, i32* @O_CLOEXEC
%9 = xor i32 %8, -1
%10 = and i32 %3, %9
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %7, i32 %10)
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
ret void
}