#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;
int fcntl(int const, int /*<<< orphan*/, ...);
int nonblocking_fd(const int fd) {
  int x, r;
  x = fcntl(fd, F_GETFL);
  if (x < 0)
    return x;
  x |= O_NONBLOCK;
  if ((r = fcntl(fd, F_SETFL, x)) < 0)
    return r;
  return 0;
}


x86 assembly (clang, -O0):
<nonblocking_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1146 <nonblocking_fd+0x36>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <nonblocking_fd+0x79>
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1182 <nonblocking_fd+0x72>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <nonblocking_fd+0x79>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nonblocking_fd>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     114b <nonblocking_fd+0x3b>
mov    0x2eba(%rip),%rcx
mov    (%rcx),%edx
or     %eax,%edx
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ecx
sar    $0x1f,%eax
and    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @nonblocking_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* %4
store i32 %12, i32* %2
br label %25
13: ; preds = %1
%14 = load i32, i32* @O_NONBLOCK
%15 = load i32, i32* %4
%16 = or i32 %15, %14
store i32 %16, i32* %4
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* %4
%20 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = load i32, i32* %5
store i32 %23, i32* %2
br label %25
24: ; preds = %13
store i32 0, i32* %2
br label %25
25: ; preds = %24, %22, %11
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @nonblocking_fd(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %3
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %8, i32 %7)
%10 = icmp slt i32 %9, 0
%11 = select i1 %10, i32 %9, i32 0
br label %12
12: ; preds = %5, %1
%13 = phi i32 [ %3, %1 ], [ %11, %5 ]
ret i32 %13
}