#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;
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int set_cloexec(int fd) {
  int flags;
  if ((flags = fcntl(fd, F_GETFD)) < 0)
    return -1;
  flags |= FD_CLOEXEC;
  if (fcntl(fd, F_SETFD, flags) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<set_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1146 <set_cloexec+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <set_cloexec+0x78>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1181 <set_cloexec+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <set_cloexec+0x78>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cloexec>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     114a <set_cloexec+0x3a>
mov    0x2eaa(%rip),%rcx
or     (%rcx),%eax
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
sar    $0x3f,%rax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @set_cloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETFD
%7 = call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %22
11: ; preds = %1
%12 = load i32, i32* @FD_CLOEXEC
%13 = load i32, i32* %4
%14 = or i32 %13, %12
store i32 %14, i32* %4
%15 = load i32, i32* %3
%16 = load i32, i32* @F_SETFD
%17 = load i32, i32* %4
%18 = call i64 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %11
store i32 -1, i32* %2
br label %22
21: ; preds = %11
store i32 0, i32* %2
br label %22
22: ; preds = %21, %20, %10
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @set_cloexec(i32 %0) {
%2 = load i32, i32* @F_GETFD
%3 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = trunc i64 %3 to i32
%5 = icmp slt i32 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = load i32, i32* @FD_CLOEXEC
%8 = or i32 %7, %4
%9 = load i32, i32* @F_SETFD
%10 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %9, i32 %8)
%11 = ashr i64 %10, 63
%12 = trunc i64 %11 to i32
br label %13
13: ; preds = %6, %1
%14 = phi i32 [ -1, %1 ], [ %12, %6 ]
ret i32 %14
}