#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 O_NONBLOCK;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ error(char *);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fd_is_blocking(int fd) {
  int val = 0;
  assert(fd >= 0);
  if ((val = fcntl(fd, F_GETFL, 0)) < 0)
    error("fnctl(F_GET_FL) failed: %m");
  return (val & O_NONBLOCK) ? 0 : 1;
}


x86 assembly (clang, -O0):
<fd_is_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    117e <fd_is_blocking+0x4e>
lea    0xe87(%rip),%rdi
call   1050 <error@plt>
mov    -0x8(%rbp),%edx
mov    0x2e60(%rip),%rax
and    (%rax),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_is_blocking>:
push   %rbx
mov    %edi,%ebx
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1161 <fd_is_blocking+0x31>
lea    0xea4(%rip),%rdi
call   1050 <error@plt>
mov    0x2e80(%rip),%rcx
xor    %eax,%eax
test   %ebx,(%rcx)
sete   %al
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [27 x i8] c"fnctl(F_GET_FL) failed: %m\00"
@O_NONBLOCK = global i32 0
define i32 @fd_is_blocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%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 ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = load i32, i32* %3
%16 = load i32, i32* @O_NONBLOCK
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 0, i32 1
ret i32 %20
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [27 x i8] c"fnctl(F_GET_FL) failed: %m\00"
@O_NONBLOCK = global i32 0
define i32 @fd_is_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 ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* @O_NONBLOCK
%12 = and i32 %11, %6
%13 = icmp eq i32 %12, 0
%14 = zext i1 %13 to i32
ret i32 %14
}