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


x86 assembly (clang, -O0):
<set_cloexec_or_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1141 <set_cloexec_or_close+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <set_cloexec_or_close+0x92>
mov    -0x8(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cltq
mov    %rax,-0x10(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1169 <set_cloexec_or_close+0x49>
jmp    11a3 <set_cloexec_or_close+0x83>
mov    -0x8(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    0x2e58(%rip),%rax
or     (%rax),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1198 <set_cloexec_or_close+0x78>
jmp    11a3 <set_cloexec_or_close+0x83>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <set_cloexec_or_close+0x92>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cloexec_or_close>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%edi
je     1173 <set_cloexec_or_close+0x53>
mov    %edi,%ebp
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     116c <set_cloexec_or_close+0x4c>
movslq %eax,%rdx
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%rdx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     116c <set_cloexec_or_close+0x4c>
mov    %ebp,%ebx
jmp    1173 <set_cloexec_or_close+0x53>
mov    %ebp,%edi
call   1040 <close@plt>
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 @set_cloexec_or_close(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 %29
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 = icmp eq i64 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %8
br label %26
15: ; preds = %8
%16 = load i32, i32* %3
%17 = load i32, i32* @F_SETFD
%18 = load i64, i64* %4
%19 = load i64, i64* @FD_CLOEXEC
%20 = or i64 %18, %19
%21 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17, i64 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %15
br label %26
24: ; preds = %15
%25 = load i32, i32* %3
store i32 %25, i32* %2
br label %29
26: ; preds = %23, %14
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %29
29: ; preds = %26, %24, %7
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i64 0
define i32 @set_cloexec_or_close(i32 %0) {
%2 = icmp eq i32 %0, -1
br i1 %2, label %16, 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 %14, 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
br i1 %13, label %14, label %16
14: ; preds = %7, %3
%15 = tail call i32 @close(i32 %0)
br label %16
16: ; preds = %7, %1, %14
%17 = phi i32 [ -1, %14 ], [ -1, %1 ], [ %0, %7 ]
ret i32 %17
}