#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 errno;
int fcntl(int, int /*<<< orphan*/, ...);
int oop_fd_nonblock(int fd, int nonblock) {
  int flags;
  flags = fcntl(fd, F_GETFL);
  if (flags == -1)
    return errno;
  if (nonblock)
    flags |= O_NONBLOCK;
  else
    flags &= ~O_NONBLOCK;
  return fcntl(fd, F_SETFL, flags) ? errno : 0;
}


x86 assembly (clang, -O0):
<oop_fd_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    114f <oop_fd_nonblock+0x3f>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <oop_fd_nonblock+0xaf>
cmpl   $0x0,-0xc(%rbp)
je     116d <oop_fd_nonblock+0x5d>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    117f <oop_fd_nonblock+0x6f>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     11af <oop_fd_nonblock+0x9f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11b9 <oop_fd_nonblock+0xa9>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    11b9 <oop_fd_nonblock+0xa9>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<oop_fd_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1158 <oop_fd_nonblock+0x48>
mov    0x2eb5(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %ebp,%ebp
cmovne %ecx,%edx
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
je     1161 <oop_fd_nonblock+0x51>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@errno = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @oop_fd_nonblock(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
%7 = load i32, i32* %4
%8 = load i32, i32* @F_GETFL
%9 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @errno
store i32 %13, i32* %3
br label %37
14: ; preds = %2
%15 = load i32, i32* %5
%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* %6
%20 = or i32 %19, %18
store i32 %20, i32* %6
br label %26
21: ; preds = %14
%22 = load i32, i32* @O_NONBLOCK
%23 = xor i32 %22, -1
%24 = load i32, i32* %6
%25 = and i32 %24, %23
store i32 %25, i32* %6
br label %26
26: ; preds = %21, %17
%27 = load i32, i32* %4
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* %6
%30 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = load i32, i32* @errno
br label %35
34: ; preds = %26
br label %35
35: ; preds = %34, %32
%36 = phi i32 [ %33, %32 ], [ 0, %34 ]
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %12
%38 = load i32, i32* %3
ret i32 %38
}

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