#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;
long errno;
int fcntl(int, int /*<<< orphan*/, int);
long fd_nonblock(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0)
    return -errno;
  if (flags & O_NONBLOCK)
    return 0;
  if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<fd_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1150 <fd_nonblock+0x40>
xor    %eax,%eax
mov    0x2e8c(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <fd_nonblock+0xa5>
mov    -0x10(%rbp),%eax
mov    0x2e8e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1172 <fd_nonblock+0x62>
movq   $0x0,-0x8(%rbp)
jmp    11b5 <fd_nonblock+0xa5>
mov    -0xc(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2e60(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11ad <fd_nonblock+0x9d>
xor    %eax,%eax
mov    0x2e2f(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <fd_nonblock+0xa5>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_nonblock>:
push   %rbx
mov    %edi,%ebx
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     114e <fd_nonblock+0x3e>
mov    0x2eba(%rip),%rcx
mov    (%rcx),%edx
test   %eax,%edx
jne    114a <fd_nonblock+0x3a>
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     %eax,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     114e <fd_nonblock+0x3e>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e7b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
pop    %rbx
ret

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

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