#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 */
scalar_t__ EINTR;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int anetCloexec(int fd) {
  int r;
  int flags;
  do {
    r = fcntl(fd, F_GETFD);
  } while (r == -1 && errno == EINTR);
  if (r == -1 || (r & FD_CLOEXEC))
    return r;
  flags = r | FD_CLOEXEC;
  do {
    r = fcntl(fd, F_SETFD, flags);
  } while (r == -1 && errno == EINTR);
  return r;
}


x86 assembly (clang, -O0):
<anetCloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x11(%rbp)
jne    115a <anetCloexec+0x4a>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    111b <anetCloexec+0xb>
cmpl   $0xffffffff,-0xc(%rbp)
je     1184 <anetCloexec+0x74>
mov    -0xc(%rbp),%eax
mov    0x2e5f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     118f <anetCloexec+0x7f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <anetCloexec+0xe1>
mov    -0xc(%rbp),%eax
mov    0x2e3f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x12(%rbp)
jne    11e0 <anetCloexec+0xd0>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    119e <anetCloexec+0x8e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetCloexec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebx
mov    0x2ea7(%rip),%rbp
mov    0x2ea8(%rip),%r15
mov    0x2e91(%rip),%r12
nop
mov    0x0(%rbp),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    114a <anetCloexec+0x3a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1130 <anetCloexec+0x20>
cmp    $0xffffffff,%eax
je     118f <anetCloexec+0x7f>
mov    0x2e82(%rip),%rcx
mov    (%rcx),%r14d
test   %eax,%r14d
jne    1194 <anetCloexec+0x84>
or     %eax,%r14d
mov    0x2e78(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1194 <anetCloexec+0x84>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1170 <anetCloexec+0x60>
jmp    1194 <anetCloexec+0x84>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @anetCloexec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %17, %1
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFD
%9 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8)
store i32 %9, i32* %4
br label %10
10: ; preds = %6
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br label %17
17: ; preds = %13, %10
%18 = phi i1 [ false, %10 ], [ %16, %13 ]
br i1 %18, label %6, label %19
19: ; preds = %17
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i32, i32* @FD_CLOEXEC
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22, %19
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %49
29: ; preds = %22
%30 = load i32, i32* %4
%31 = load i32, i32* @FD_CLOEXEC
%32 = or i32 %30, %31
store i32 %32, i32* %5
br label %33
33: ; preds = %45, %29
%34 = load i32, i32* %3
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* %5
%37 = call i32 (i32, i32, ...) @fcntl(i32 %34, i32 %35, i32 %36)
store i32 %37, i32* %4
br label %38
38: ; preds = %33
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %33, label %47
47: ; preds = %45
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %27
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @anetCloexec(i32 %0) {
br label %2
2: ; preds = %2, %1
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
%6 = load i64, i64* @errno
%7 = load i64, i64* @EINTR
%8 = icmp eq i64 %6, %7
%9 = select i1 %5, i1 %8, i1 false
br i1 %9, label %2, label %10
10: ; preds = %2
br i1 %5, label %25, label %11
11: ; preds = %10
%12 = load i32, i32* @FD_CLOEXEC
%13 = and i32 %12, %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = or i32 %12, %4
br label %17
17: ; preds = %17, %15
%18 = load i32, i32* @F_SETFD
%19 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %18, i32 %16)
%20 = icmp eq i32 %19, -1
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %17, label %25
25: ; preds = %17, %10, %11
%26 = phi i32 [ %4, %11 ], [ -1, %10 ], [ %19, %17 ]
ret i32 %26
}