#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_GETFL;
int /*<<< orphan*/ F_SETFL;
int fcntl(int, int /*<<< orphan*/, ...);
int ksock_set_close_onexec(int fd) {
  int opts = fcntl(fd, F_GETFL);
  if (-1 == opts) {
    return 1;
  }
  opts |= FD_CLOEXEC;
  opts = fcntl(fd, F_SETFL, opts);
  return opts;
}


x86 assembly (clang, -O0):
<ksock_set_close_onexec>:
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)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    114b <ksock_set_close_onexec+0x3b>
movl   $0x1,-0x4(%rbp)
jmp    1179 <ksock_set_close_onexec+0x69>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ksock_set_close_onexec>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1146 <ksock_set_close_onexec+0x36>
mov    0x2ea9(%rip),%rcx
or     (%rcx),%eax
mov    0x2ea8(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
pop    %rbx
jmp    1030 <fcntl@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
define i32 @ksock_set_close_onexec(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_GETFL
%7 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1, i32* %2
br label %20
11: ; preds = %1
%12 = load i32, i32* @FD_CLOEXEC
%13 = load i32, i32* %4
%14 = or i32 %13, %12
store i32 %14, i32* %4
%15 = load i32, i32* %3
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* %4
%18 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %20
20: ; preds = %11, %10
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
define i32 @ksock_set_close_onexec(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = load i32, i32* @FD_CLOEXEC
%7 = or i32 %6, %3
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %8, i32 %7)
br label %10
10: ; preds = %1, %5
%11 = phi i32 [ %9, %5 ], [ 1, %1 ]
ret i32 %11
}