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


x86 assembly (clang, -O0):
<os_fd_set_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1131 <os_fd_set_cloexec+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <os_fd_set_cloexec+0x8e>
mov    -0x8(%rbp),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    1161 <os_fd_set_cloexec+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <os_fd_set_cloexec+0x8e>
mov    -0x8(%rbp),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    0x2e60(%rip),%rax
or     (%rax),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1197 <os_fd_set_cloexec+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <os_fd_set_cloexec+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_fd_set_cloexec>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%edi
je     115d <os_fd_set_cloexec+0x4d>
mov    %edi,%ebp
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     115d <os_fd_set_cloexec+0x4d>
movslq %eax,%rdx
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%rdx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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