#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_blocking(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_blocking>:
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_blocking+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
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11c0 <set_fd_blocking+0x90>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <log_err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_fd_blocking>:
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_blocking+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),%eax
not    %eax
and    %eax,%ebp
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1196 <set_fd_blocking+0x66>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%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_blocking(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 = xor i32 %19, -1
%21 = and i32 %18, %20
%22 = call i32 @fcntl(i32 %16, i32 %17, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* @errno
%26 = call i32 @log_err(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %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_blocking(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 = xor i32 %13, -1
%15 = and i32 %6, %14
%16 = tail call i32 @fcntl(i32 %0, i32 %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @errno
%20 = tail call i32 @log_err(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %11
ret void
}