#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 fcntl(int, int /*<<< orphan*/, ...);
__attribute__((unused)) int ls_make_cloexec(int fd) {
  int flags = fcntl(fd, F_GETFD);
  if (flags < 0 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<ls_make_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1163 <ls_make_cloexec+0x53>
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e87(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    116f <ls_make_cloexec+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1175 <ls_make_cloexec+0x65>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_make_cloexec>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1150 <ls_make_cloexec+0x40>
mov    0x2eb2(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ea1(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @ls_make_cloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETFD
%7 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i32, i32* @F_SETFD
%13 = load i32, i32* %4
%14 = load i32, i32* @FD_CLOEXEC
%15 = or i32 %13, %14
%16 = call i32 (i32, i32, ...) @fcntl(i32 %11, i32 %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10, %1
store i32 -1, i32* %2
br label %21
19: ; preds = %10
%20 = load i32, i32* %3
store i32 %20, i32* %2
br label %21
21: ; preds = %19, %18
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @ls_make_cloexec(i32 %0) {
%2 = load i32, i32* @F_GETFD
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = load i32, i32* @F_SETFD
%7 = load i32, i32* @FD_CLOEXEC
%8 = or i32 %7, %3
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %6, i32 %8)
%10 = icmp sgt i32 %9, -1
%11 = select i1 %10, i32 %0, i32 -1
br label %12
12: ; preds = %5, %1
%13 = phi i32 [ -1, %1 ], [ %11, %5 ]
ret i32 %13
}