#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*/ EINVAL;
int /*<<< orphan*/ F_GETFL;
int O_NONBLOCK;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int fd_is_nonblocking(int fd) {
  int fval;
  if (fd < 0) {
    errno = EINVAL;
    return (-1);
  }
  if ((fval = fcntl(fd, F_GETFL, 0)) < 0) {
    return (-1);
  }
  return ((fval & O_NONBLOCK) ? 1 : 0);
}


x86 assembly (clang, -O0):
<fd_is_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <fd_is_nonblocking+0x33>
mov    0x2e9c(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <fd_is_nonblocking+0x7a>
mov    -0x8(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    116e <fd_is_nonblocking+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <fd_is_nonblocking+0x7a>
mov    -0xc(%rbp),%edx
mov    0x2e70(%rip),%rax
and    (%rax),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_is_nonblocking>:
push   %rax
test   %edi,%edi
js     113b <fd_is_nonblocking+0x2b>
mov    0x2eb4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     114d <fd_is_nonblocking+0x3d>
mov    %eax,%ecx
mov    0x2eb6(%rip),%rdx
xor    %eax,%eax
test   %ecx,(%rdx)
setne  %al
pop    %rcx
ret
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    0x2e8d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @fd_is_nonblocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
store i32 -1, i32* %2
br label %22
9: ; preds = %1
%10 = load i32, i32* %3
%11 = load i32, i32* @F_GETFL
%12 = call i32 @fcntl(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %2
br label %22
15: ; preds = %9
%16 = load i32, i32* %4
%17 = load i32, i32* @O_NONBLOCK
%18 = and i32 %16, %17
%19 = icmp ne i32 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 1, i32 0
store i32 %21, i32* %2
br label %22
22: ; preds = %15, %14, %7
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @fd_is_nonblocking(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32, i32* @EINVAL
store i32 %4, i32* @errno
br label %14
5: ; preds = %1
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 @fcntl(i32 %0, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = load i32, i32* @O_NONBLOCK
%11 = and i32 %10, %7
%12 = icmp ne i32 %11, 0
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %5, %9, %3
%15 = phi i32 [ -1, %3 ], [ %13, %9 ], [ -1, %5 ]
ret i32 %15
}