#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*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int set_nonblock(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL);
  if (flags == -1) {
    fprintf(stderr, "Error getting flags on fd %d", fd);
    return -1;
  }
  flags |= O_NONBLOCK;
  if (fcntl(fd, F_SETFL, flags)) {
    fprintf(stderr, "Error setting non-blocking I/O on fd %d", fd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    116f <set_nonblock+0x4f>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xea2(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <set_nonblock+0xa8>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
je     11c1 <set_nonblock+0xa1>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe6d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <set_nonblock+0xa8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1166 <set_nonblock+0x46>
mov    0x2ea5(%rip),%rcx
or     (%rcx),%eax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
je     1182 <set_nonblock+0x62>
lea    0xeb9(%rip),%rsi
jmp    116d <set_nonblock+0x4d>
lea    0xe93(%rip),%rsi
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Error getting flags on fd %d\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [40 x i8] c"Error setting non-blocking I/O on fd %d\00"
define i32 @set_nonblock(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 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 -1, i32* %2
br label %28
14: ; preds = %1
%15 = load i32, i32* @O_NONBLOCK
%16 = load i32, i32* %4
%17 = or i32 %16, %15
store i32 %17, i32* %4
%18 = load i32, i32* %3
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %4
%21 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = load i32, i32* %3
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 -1, i32* %2
br label %28
27: ; preds = %14
store i32 0, i32* %2
br label %28
28: ; preds = %27, %23, %10
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Error getting flags on fd %d\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [40 x i8] c"Error setting non-blocking I/O on fd %d\00"
define i32 @set_nonblock(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %11, 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 (i32, i32, ...) @fcntl(i32 %0, i32 %8, i32 %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %5, %1
%12 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* %12, i32 %0)
br label %15
15: ; preds = %11, %5
%16 = phi i32 [ 0, %5 ], [ -1, %11 ]
ret i32 %16
}