#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;
  flags = fcntl(fd, F_GETFD);
  if (flags == -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    11b3 <set_cloexec_or_close+0x93>
mov    -0x8(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    116a <set_cloexec_or_close+0x4a>
jmp    11a4 <set_cloexec_or_close+0x84>
mov    -0x8(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    0x2e57(%rip),%rax
or     (%rax),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1199 <set_cloexec_or_close+0x79>
jmp    11a4 <set_cloexec_or_close+0x84>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <set_cloexec_or_close+0x93>
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 %30
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 = load i64, i64* %4
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %8
br label %27
16: ; preds = %8
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFD
%19 = load i64, i64* %4
%20 = load i64, i64* @FD_CLOEXEC
%21 = or i64 %19, %20
%22 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %18, i64 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %16
br label %27
25: ; preds = %16
%26 = load i32, i32* %3
store i32 %26, i32* %2
br label %30
27: ; preds = %24, %15
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %2
br label %30
30: ; preds = %27, %25, %7
%31 = load i32, i32* %2
ret i32 %31
}

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
}