#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 /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int _set_nonblock(int fd, int nonblockflag) {
  int flags;
  int res = 0;
  flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0) {
    res = 15;
    goto done;
  }
  if (nonblockflag)
    flags |= O_NONBLOCK;
  else
    flags &= ~O_NONBLOCK;
  if (fcntl(fd, F_SETFL, flags) < 0) {
    res = 15;
    goto done;
  }
done:
  return res;
}


x86 assembly (clang, -O0):
<_set_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1151 <_set_nonblock+0x41>
movl   $0xf,-0x10(%rbp)
jmp    11b0 <_set_nonblock+0xa0>
cmpl   $0x0,-0x8(%rbp)
je     116f <_set_nonblock+0x5f>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1181 <_set_nonblock+0x71>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11ab <_set_nonblock+0x9b>
movl   $0xf,-0x10(%rbp)
jmp    11b0 <_set_nonblock+0xa0>
jmp    11b0 <_set_nonblock+0xa0>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     115a <_set_nonblock+0x4a>
mov    0x2eb6(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %ebp,%ebp
cmovne %ecx,%edx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
sar    $0x3f,%rax
and    $0xf,%eax
jmp    115f <_set_nonblock+0x4f>
mov    $0xf,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @_set_nonblock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %6
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFL
%9 = call i64 @fcntl(i32 %7, i32 %8, i32 0)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 15, i32* %6
br label %34
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @O_NONBLOCK
%19 = load i32, i32* %5
%20 = or i32 %19, %18
store i32 %20, i32* %5
br label %26
21: ; preds = %14
%22 = load i32, i32* @O_NONBLOCK
%23 = xor i32 %22, -1
%24 = load i32, i32* %5
%25 = and i32 %24, %23
store i32 %25, i32* %5
br label %26
26: ; preds = %21, %17
%27 = load i32, i32* %3
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* %5
%30 = call i64 @fcntl(i32 %27, i32 %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 15, i32* %6
br label %34
33: ; preds = %26
br label %34
34: ; preds = %33, %32, %13
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @_set_nonblock(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i64 @fcntl(i32 %0, i32 %3, i32 0)
%5 = trunc i64 %4 to i32
%6 = icmp slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = icmp eq i32 %1, 0
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %5
%11 = xor i32 %9, -1
%12 = and i32 %11, %5
%13 = select i1 %8, i32 %12, i32 %10
%14 = load i32, i32* @F_SETFL
%15 = tail call i64 @fcntl(i32 %0, i32 %14, i32 %13)
%16 = icmp slt i64 %15, 0
%17 = select i1 %16, i32 15, i32 0
br label %18
18: ; preds = %7, %2
%19 = phi i32 [ 15, %2 ], [ %17, %7 ]
ret i32 %19
}