#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 /*<<< orphan*/ perror(char *);
bool fd_nonblock(int fd, bool nonblock) {
  bool ret = false;
  int flags, mask = O_NONBLOCK, bit = nonblock ? mask : 0;
  flags = fcntl(fd, F_GETFL);
  if (flags < 0) {
    perror("fcntl(F_GETFL)");
    goto fail;
  }
  flags ^= mask;
  if ((flags & mask) == bit && fcntl(fd, F_SETFL, flags) != 0) {
    perror("fcntl(F_SETFL)");
    goto fail;
  }
  ret = true;
fail:
  return ret;
}


x86 assembly (clang, -O0):
<fd_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1156 <fd_nonblock+0x36>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1160 <fd_nonblock+0x40>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1160 <fd_nonblock+0x40>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1197 <fd_nonblock+0x77>
lea    0xe73(%rip),%rdi
call   1040 <perror@plt>
jmp    11e7 <fd_nonblock+0xc7>
mov    -0x14(%rbp),%eax
xor    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    11e0 <fd_nonblock+0xc0>
mov    -0x4(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     11e0 <fd_nonblock+0xc0>
lea    0xe39(%rip),%rdi
call   1040 <perror@plt>
jmp    11e7 <fd_nonblock+0xc7>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_nonblock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r15d
mov    0x2eb5(%rip),%rax
mov    (%rax),%ebx
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1184 <fd_nonblock+0x64>
test   %ebp,%ebp
cmovne %ebx,%ebp
xor    %ebx,%eax
and    %eax,%ebx
mov    $0x1,%r14d
cmp    %ebp,%ebx
jne    1176 <fd_nonblock+0x56>
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jne    118d <fd_nonblock+0x6d>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe75(%rip),%rdi
jmp    1194 <fd_nonblock+0x74>
lea    0xe7b(%rip),%rdi
call   1040 <perror@plt>
xor    %r14d,%r14d
jmp    1176 <fd_nonblock+0x56>

LLVM IR (-O0):
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
@.str = [15 x i8] c"fcntl(F_GETFL)\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @fd_nonblock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* @O_NONBLOCK
store i32 %9, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %7
br label %15
14: ; preds = %2
br label %15
15: ; preds = %14, %12
%16 = phi i32 [ %13, %12 ], [ 0, %14 ]
store i32 %16, i32* %8
%17 = load i32, i32* %3
%18 = load i32, i32* @F_GETFL
%19 = call i64 (i32, i32, ...) @fcntl(i32 %17, i32 %18)
%20 = trunc i64 %19 to i32
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %43
25: ; preds = %15
%26 = load i32, i32* %7
%27 = load i32, i32* %6
%28 = xor i32 %27, %26
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = and i32 %29, %30
%32 = load i32, i32* %8
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %25
%35 = load i32, i32* %3
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* %6
%38 = call i64 (i32, i32, ...) @fcntl(i32 %35, i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %43
42: ; preds = %34, %25
store i32 1, i32* %5
br label %43
43: ; preds = %42, %40, %23
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
@.str = [15 x i8] c"fcntl(F_GETFL)\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @fd_nonblock(i32 %0, i32 %1) {
%3 = load i32, i32* @O_NONBLOCK
%4 = load i32, i32* @F_GETFL
%5 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %4)
%6 = trunc i64 %5 to i32
%7 = icmp slt i32 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %2
%9 = icmp eq i32 %1, 0
%10 = select i1 %9, i32 0, i32 %3
%11 = xor i32 %3, %6
%12 = and i32 %11, %3
%13 = icmp eq i32 %12, %10
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = load i32, i32* @F_SETFL
%16 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %15, i32 %11)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14, %2
%19 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %14 ]
%20 = tail call i32 @perror(i8* %19)
br label %21
21: ; preds = %18, %8, %14
%22 = phi i32 [ 1, %14 ], [ 1, %8 ], [ 0, %18 ]
ret i32 %22
}