#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*/ EX_IOERR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void setfd_nonblock(int fd) {
  int fsflags = fcntl(fd, F_GETFL);
  if (fsflags < 0) {
    fprintf(stderr, "fcntl(%d, F_GETFL): %s\n", fd, strerror(errno));
    exit(EX_IOERR);
  }
  if (fcntl(fd, F_SETFL, fsflags | O_NONBLOCK) < 0) {
    fprintf(stderr, "fcntl(%d, F_SETFL, ... | O_NONBLOCK): %s\n", fd,
            strerror(errno));
    exit(EX_IOERR);
  }
}


x86 assembly (clang, -O0):
<setfd_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ae <setfd_nonblock+0x6e>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%edx
mov    %rax,%rcx
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e24(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1219 <setfd_nonblock+0xd9>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe12(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setfd_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1181 <setfd_nonblock+0x41>
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e7f(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11a1 <setfd_nonblock+0x61>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e48(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe61(%rip),%rsi
jmp    11bf <setfd_nonblock+0x7f>
mov    0x2e28(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"fcntl(%d, F_GETFL): %s\0A\00"
@errno = global i32 0
@EX_IOERR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [42 x i8] c"fcntl(%d, F_SETFL, ... | O_NONBLOCK): %s\0A\00"
define void @setfd_nonblock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @F_GETFL
%6 = call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* %2
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %13)
%15 = load i32, i32* @EX_IOERR
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %3
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %20, %21
%23 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i32, i32* %2
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %29)
%31 = load i32, i32* @EX_IOERR
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %17
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"fcntl(%d, F_GETFL): %s\0A\00"
@errno = global i32 0
@EX_IOERR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [42 x i8] c"fcntl(%d, F_SETFL, ... | O_NONBLOCK): %s\0A\00"
define void @setfd_nonblock(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 %5, label %12
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %8)
%10 = load i32, i32* @EX_IOERR
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %14, %3
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %13, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %21)
%23 = load i32, i32* @EX_IOERR
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %12
ret void
}