#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*/ EXIT_FAILURE;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int fcntl(int, int /*<<< orphan*/, int);
int set_cloexec(int fd) {
  int flags = fcntl(fd, F_GETFD, 0);
  do {
    if ((flags >= 0) < 0) {
      error(EXIT_FAILURE, errno, "fcntl error");
    }
  } while (0);
  return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
}


x86 assembly (clang, -O0):
<set_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%eax
cmp    $0x0,%eax
jge    1174 <set_cloexec+0x54>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdx
call   1040 <error@plt>
jmp    1179 <set_cloexec+0x59>
mov    -0x4(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e49(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
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    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2eb6(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ea5(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
pop    %rbx
jmp    1030 <fcntl@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str = [12 x i8] c"fcntl error\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @set_cloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @F_GETFD
%6 = call i32 @fcntl(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %3
br label %7
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp sge i32 %8, 0
%10 = zext i1 %9 to i32
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32, i32* @EXIT_FAILURE
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 %13, i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %12, %7
br label %17
17: ; preds = %16
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SETFD
%20 = load i32, i32* %3
%21 = load i32, i32* @FD_CLOEXEC
%22 = or i32 %20, %21
%23 = call i32 @fcntl(i32 %18, i32 %19, i32 %22)
ret i32 %23
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @set_cloexec(i32 %0) {
%2 = load i32, i32* @F_GETFD
%3 = tail call i32 @fcntl(i32 %0, i32 %2, i32 0)
%4 = load i32, i32* @F_SETFD
%5 = load i32, i32* @FD_CLOEXEC
%6 = or i32 %5, %3
%7 = tail call i32 @fcntl(i32 %0, i32 %4, i32 %6)
ret i32 %7
}