#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
void set_fd_nonblocking(int fd) {
  int fval;
  assert(fd >= 0);
  if ((fval = fcntl(fd, F_GETFL, 0)) < 0)
    log_err(errno, "fcntl(F_GETFL) failed");
  if (fcntl(fd, F_SETFL, fval | O_NONBLOCK) < 0)
    log_err(errno, "fcntl(F_SETFL) failed");
  return;
}


x86 assembly (clang, -O0):
<set_fd_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1180 <set_fd_nonblocking+0x50>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1050 <log_err@plt>
mov    -0x4(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11bb <set_fd_nonblocking+0x8b>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <log_err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fd_nonblocking>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    116c <set_fd_nonblocking+0x3c>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe99(%rip),%rsi
call   1050 <log_err@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
js     1192 <set_fd_nonblocking+0x62>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <log_err@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@errno = global i32 0
@.str = [22 x i8] c"fcntl(F_GETFL) failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"fcntl(F_SETFL) failed\00"
define void @set_fd_nonblocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sge i32 %4, 0
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
%8 = load i32, i32* %2
%9 = load i32, i32* @F_GETFL
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i32 @log_err(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* %2
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %3
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @fcntl(i32 %16, i32 %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* @errno
%25 = call i32 @log_err(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %15
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i32 0
@.str = [22 x i8] c"fcntl(F_GETFL) failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"fcntl(F_SETFL) failed\00"
define void @set_fd_nonblocking(i32 %0) {
%2 = xor i32 %0, -1
%3 = lshr i32 %2, 31
%4 = tail call i32 @assert(i32 %3)
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 @fcntl(i32 %0, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @log_err(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %1
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %6
%15 = tail call i32 @fcntl(i32 %0, i32 %12, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = tail call i32 @log_err(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %11
ret void
}