#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;
int /*<<< orphan*/ abort();
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
void fcntl_set_cloexec(int fd, int on) {
  int rv;
  int fl;
  rv = fcntl(fd, F_GETFD);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
  fl = rv;
  if (on) {
    fl |= FD_CLOEXEC;
  } else {
    fl &= ~FD_CLOEXEC;
  }
  rv = fcntl(fd, F_SETFD, fl);
  if (rv == -1) {
    perror("fcntl");
    abort();
  }
}


x86 assembly (clang, -O0):
<fcntl_set_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1171 <fcntl_set_cloexec+0x41>
lea    0xe9b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1195 <fcntl_set_cloexec+0x65>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11a7 <fcntl_set_cloexec+0x77>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11dd <fcntl_set_cloexec+0xad>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcntl_set_cloexec>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1180 <fcntl_set_cloexec+0x50>
mov    0x2e85(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %ebp,%ebp
cmovne %ecx,%edx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1180 <fcntl_set_cloexec+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@.str = [6 x i8] c"fcntl\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define void @fcntl_set_cloexec(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFD
%9 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (...) @abort()
unreachable
15: ; preds = %2
%16 = load i32, i32* %5
store i32 %16, i32* %6
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @FD_CLOEXEC
%21 = load i32, i32* %6
%22 = or i32 %21, %20
store i32 %22, i32* %6
br label %28
23: ; preds = %15
%24 = load i32, i32* @FD_CLOEXEC
%25 = xor i32 %24, -1
%26 = load i32, i32* %6
%27 = and i32 %26, %25
store i32 %27, i32* %6
br label %28
28: ; preds = %23, %19
%29 = load i32, i32* %3
%30 = load i32, i32* @F_SETFD
%31 = load i32, i32* %6
%32 = call i32 (i32, i32, ...) @fcntl(i32 %29, i32 %30, i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %28
ret void
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@.str = [6 x i8] c"fcntl\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define void @fcntl_set_cloexec(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %2
%10 = icmp eq i32 %1, 0
%11 = load i32, i32* @FD_CLOEXEC
%12 = or i32 %11, %4
%13 = xor i32 %11, -1
%14 = and i32 %4, %13
%15 = select i1 %10, i32 %14, i32 %12
%16 = load i32, i32* @F_SETFD
%17 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %16, i32 %15)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %9
ret void
}