#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*/ error(char *);
int fcntl(int, int /*<<< orphan*/, int);
void fd_set_blocking(int fd) {
  int fval;
  assert(fd >= 0);
  if ((fval = fcntl(fd, F_GETFL, 0)) < 0)
    error("fcntl(F_GETFL) failed: %m");
  if (fcntl(fd, F_SETFL, fval & ~O_NONBLOCK) < 0)
    error("fcntl(F_SETFL) failed: %m");
  return;
}


x86 assembly (clang, -O0):
<fd_set_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    0x2e7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1177 <fd_set_blocking+0x47>
lea    0xe8e(%rip),%rdi
call   1050 <error@plt>
mov    -0x4(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11ae <fd_set_blocking+0x7e>
lea    0xe71(%rip),%rdi
call   1050 <error@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_set_blocking>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1163 <fd_set_blocking+0x33>
lea    0xea2(%rip),%rdi
call   1050 <error@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,%ebp
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     118d <fd_set_blocking+0x5d>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe86(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <error@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [26 x i8] c"fcntl(F_GETFL) failed: %m\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"fcntl(F_SETFL) failed: %m\00"
define void @fd_set_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 %14
12: ; preds = %1
%13 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* %2
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* %3
%18 = load i32, i32* @O_NONBLOCK
%19 = xor i32 %18, -1
%20 = and i32 %17, %19
%21 = call i32 @fcntl(i32 %15, i32 %16, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [26 x i8] c"fcntl(F_GETFL) failed: %m\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"fcntl(F_SETFL) failed: %m\00"
define void @fd_set_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 %10
8: ; preds = %1
%9 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* @F_SETFL
%12 = load i32, i32* @O_NONBLOCK
%13 = xor i32 %12, -1
%14 = and i32 %6, %13
%15 = tail call i32 @fcntl(i32 %0, i32 %11, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
ret void
}