#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 EINTR;
int /*<<< orphan*/ FIOCLEX;
int /*<<< orphan*/ FIONCLEX;
int errno;
int ioctl(int, int /*<<< orphan*/);
int sjs__cloexec(int fd, int set) {
  int r;
  do
    r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
  while (r == -1 && errno == EINTR);
  if (r)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<sjs__cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     113f <sjs__cloexec+0x2f>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    114b <sjs__cloexec+0x3b>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <ioctl@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x10(%rbp)
mov    %al,-0x19(%rbp)
jne    1180 <sjs__cloexec+0x70>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    111e <sjs__cloexec+0xe>
cmpl   $0x0,-0x10(%rbp)
je     11a8 <sjs__cloexec+0x98>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <sjs__cloexec+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sjs__cloexec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2eaa(%rip),%r14
mov    0x2eb3(%rip),%r15
mov    0x2ea4(%rip),%r12
mov    0x2e8d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rax
test   %ebx,%ebx
jne    114a <sjs__cloexec+0x3a>
mov    %r15,%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <ioctl@plt>
mov    (%r12),%ecx
cmp    $0xffffffff,%eax
jne    1162 <sjs__cloexec+0x52>
cmp    0x0(%r13),%ecx
je     1140 <sjs__cloexec+0x30>
neg    %ecx
test   %eax,%eax
cmove  %eax,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FIOCLEX = global i32 0
@FIONCLEX = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @sjs__cloexec(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %7
7: ; preds = %25, %2
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = load i32, i32* @FIOCLEX
br label %15
13: ; preds = %7
%14 = load i32, i32* @FIONCLEX
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ %12, %11 ], [ %14, %13 ]
%17 = call i32 @ioctl(i32 %8, i32 %16)
store i32 %17, i32* %6
br label %18
18: ; preds = %15
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %22, %23
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %7, label %27
27: ; preds = %25
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %34
33: ; preds = %27
store i32 0, i32* %3
br label %34
34: ; preds = %33, %30
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@FIOCLEX = global i32 0
@FIONCLEX = global i32 0
@errno = global i32 0
@EINTR = global i32 0
define i32 @sjs__cloexec(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br label %4
4: ; preds = %4, %2
%5 = load i32, i32* @FIOCLEX
%6 = load i32, i32* @FIONCLEX
%7 = select i1 %3, i32 %6, i32 %5
%8 = tail call i32 @ioctl(i32 %0, i32 %7)
%9 = icmp eq i32 %8, -1
%10 = load i32, i32* @errno
%11 = load i32, i32* @EINTR
%12 = icmp eq i32 %10, %11
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %4, label %14
14: ; preds = %4
%15 = icmp eq i32 %8, 0
%16 = sub nsw i32 0, %10
%17 = select i1 %15, i32 0, i32 %16
ret i32 %17
}