#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 fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ report_errno(char *, int);
int fd_set_non_blocking(int fd) {
  int flags = fcntl(fd, F_GETFL);
  if (flags < 0) {
    report_errno("fcntl getfl", flags);
    return -1;
  }
  int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  if (ret < 0) {
    report_errno("fcntl setfl", flags);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fd_set_non_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1166 <fd_set_non_blocking+0x46>
mov    -0xc(%rbp),%esi
lea    0xeab(%rip),%rdi
call   1040 <report_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <fd_set_non_blocking+0x94>
mov    -0x8(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e6c(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ad <fd_set_non_blocking+0x8d>
mov    -0xc(%rbp),%esi
lea    0xe70(%rip),%rdi
call   1040 <report_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <fd_set_non_blocking+0x94>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_set_non_blocking>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1169 <fd_set_non_blocking+0x49>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    117d <fd_set_non_blocking+0x5d>
lea    0xea5(%rip),%rdi
jmp    1170 <fd_set_non_blocking+0x50>
lea    0xe90(%rip),%rdi
mov    %ebx,%esi
call   1040 <report_errno@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [12 x i8] c"fcntl getfl\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [12 x i8] c"fcntl setfl\00"
define i32 @fd_set_non_blocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = load i32, i32* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i32 @report_errno(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 -1, i32* %2
br label %27
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* %4
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = load i32, i32* %4
%25 = call i32 @report_errno(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 -1, i32* %2
br label %27
26: ; preds = %14
store i32 0, i32* %2
br label %27
27: ; preds = %26, %23, %11
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [12 x i8] c"fcntl getfl\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [12 x i8] c"fcntl setfl\00"
define i32 @fd_set_non_blocking(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %3
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5, %1
%12 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = tail call i32 @report_errno(i8* %12, i32 %3)
br label %14
14: ; preds = %11, %5
%15 = phi i32 [ 0, %5 ], [ -1, %11 ]
ret i32 %15
}