#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);
int set_non_block_fd(int fd) {
  int flags = fcntl(fd, F_GETFL, 0);
  if (-1 == flags) {
    return flags;
  }
  flags |= O_NONBLOCK;
  if (-1 == fcntl(fd, F_SETFL, flags)) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_non_block_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    114a <set_non_block_fd+0x3a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118f <set_non_block_fd+0x7f>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1188 <set_non_block_fd+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    118f <set_non_block_fd+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_non_block_fd>:
push   %rbx
mov    %edi,%ebx
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1151 <set_non_block_fd+0x41>
mov    0x2eb9(%rip),%rcx
or     (%rcx),%eax
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_non_block_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETFL
%7 = call i32 @fcntl(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 -1, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* %4
store i32 %11, i32* %2
br label %23
12: ; preds = %1
%13 = load i32, i32* @O_NONBLOCK
%14 = load i32, i32* %4
%15 = or i32 %14, %13
store i32 %15, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %4
%19 = call i32 @fcntl(i32 %16, i32 %17, i32 %18)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %22
21: ; preds = %12
store i32 -1, i32* %2
br label %23
22: ; preds = %12
store i32 0, i32* %2
br label %23
23: ; preds = %22, %21, %10
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_non_block_fd(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 @fcntl(i32 %0, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %3
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 @fcntl(i32 %0, i32 %8, i32 %7)
%10 = icmp eq i32 %9, -1
%11 = sext i1 %10 to i32
br label %12
12: ; preds = %5, %1
%13 = phi i32 [ -1, %1 ], [ %11, %5 ]
ret i32 %13
}