#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 set_fd_coe(int fd, int enable) {
  int rc;
  long flags = 0;
  rc = fcntl(fd, F_GETFD, &flags);
  if (rc < 0)
    return rc;
  if (enable)
    flags |= FD_CLOEXEC;
  else
    flags &= ~FD_CLOEXEC;
  rc = fcntl(fd, F_SETFD, flags);
  return rc;
}


x86 assembly (clang, -O0):
<set_fd_coe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1155 <set_fd_coe+0x45>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ac <set_fd_coe+0x9c>
cmpl   $0x0,-0xc(%rbp)
je     1176 <set_fd_coe+0x66>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
or     -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    118c <set_fd_coe+0x7c>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
xor    $0xffffffffffffffff,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fd_coe>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
movq   $0x0,(%rsp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1167 <set_fd_coe+0x57>
mov    0x2e9b(%rip),%rax
mov    (%rax),%rdx
test   %ebp,%ebp
je     114a <set_fd_coe+0x3a>
or     (%rsp),%rdx
jmp    1151 <set_fd_coe+0x41>
not    %rdx
and    (%rsp),%rdx
mov    %rdx,(%rsp)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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