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


x86 assembly (clang, -O0):
<zed_file_close_on_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <zed_file_close_on_exec+0x33>
mov    0x2e94(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <zed_file_close_on_exec+0xa0>
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    116f <zed_file_close_on_exec+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <zed_file_close_on_exec+0xa0>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11a9 <zed_file_close_on_exec+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <zed_file_close_on_exec+0xa0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zed_file_close_on_exec>:
push   %rbx
test   %edi,%edi
js     1157 <zed_file_close_on_exec+0x47>
mov    %edi,%ebx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1169 <zed_file_close_on_exec+0x59>
mov    0x2ea5(%rip),%rcx
or     (%rcx),%eax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
pop    %rbx
ret
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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