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


x86 assembly (clang, -O0):
<fd_set_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1143 <fd_set_nonblocking+0x33>
mov    0x2e94(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <fd_set_nonblocking+0x97>
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    116e <fd_set_nonblocking+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <fd_set_nonblocking+0x97>
mov    -0x8(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e64(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11a0 <fd_set_nonblocking+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <fd_set_nonblocking+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_set_nonblocking>:
push   %rbx
test   %edi,%edi
js     114b <fd_set_nonblocking+0x3b>
mov    %edi,%ebx
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     115d <fd_set_nonblocking+0x4d>
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ead(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
sar    $0x1f,%eax
pop    %rbx
ret
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    0x2e75(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @fd_set_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 %25
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 %25
15: ; preds = %9
%16 = load i32, i32* %3
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %4
%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 %24
23: ; preds = %15
store i32 -1, i32* %2
br label %25
24: ; preds = %15
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23, %14, %7
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @fd_set_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 %15
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 %15, label %9
9: ; preds = %5
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %7
%13 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %12)
%14 = ashr i32 %13, 31
br label %15
15: ; preds = %9, %5, %3
%16 = phi i32 [ -1, %3 ], [ -1, %5 ], [ %14, %9 ]
ret i32 %16
}