#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_block(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_block>:
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_block+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
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    121e <setfd_block+0xde>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe0d(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setfd_block>:
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     1185 <setfd_block+0x45>
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %ecx,%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11a5 <setfd_block+0x65>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e44(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe5d(%rip),%rsi
jmp    11c3 <setfd_block+0x83>
mov    0x2e24(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe55(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    0x2e0a(%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 = [43 x i8] c"fcntl(%d, F_SETFL, ... & ~O_NONBLOCK): %s\0A\00"
define void @setfd_block(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 = xor i32 %21, -1
%23 = and i32 %20, %22
%24 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i32, i32* %2
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %28, i8* %30)
%32 = load i32, i32* @EX_IOERR
%33 = call i32 @exit(i32 %32)
unreachable
34: ; 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 = [43 x i8] c"fcntl(%d, F_SETFL, ... & ~O_NONBLOCK): %s\0A\00"
define void @setfd_block(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 = xor i32 %14, -1
%16 = and i32 %3, %15
%17 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %13, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %22)
%24 = load i32, i32* @EX_IOERR
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %12
ret void
}