#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 /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, char *);
char *strerror(int /*<<< orphan*/);
int setnonblock(int fd) {
  int fdflags;
  if ((fdflags = fcntl(fd, F_GETFL, 0)) == -1) {
    printf("get fd[%d] flag error[%s].\n", fd, strerror(errno));
    return -1;
  }
  if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) == -1) {
    printf("set fd[%d] flag error[%s].\n", fd, strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setnonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    118c <setnonblock+0x5c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x10(%rbp),%esi
mov    %rax,%rdx
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <setnonblock+0xbb>
mov    -0x8(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11e4 <setnonblock+0xb4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <setnonblock+0xbb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setnonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1175 <setnonblock+0x45>
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e8c(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    119c <setnonblock+0x6c>
lea    0xea9(%rip),%rbp
jmp    117c <setnonblock+0x4c>
lea    0xe84(%rip),%rbp
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [28 x i8] c"get fd[%d] flag error[%s].\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [28 x i8] c"set fd[%d] flag error[%s].\0A\00"
define i32 @setnonblock(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 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* %3
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %12)
store i32 -1, i32* %2
br label %28
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 @fcntl(i32 %15, i32 %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %14
%23 = load i32, i32* %3
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %25)
store i32 -1, i32* %2
br label %28
27: ; preds = %14
store i32 0, i32* %2
br label %28
28: ; preds = %27, %22, %9
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [28 x i8] c"get fd[%d] flag error[%s].\0A\00"
@errno = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [28 x i8] c"set fd[%d] flag error[%s].\0A\00"
define i32 @setnonblock(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 %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 @fcntl(i32 %0, i32 %6, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %5, %1
%12 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* %12, i32 %0, i8* %14)
br label %16
16: ; preds = %11, %5
%17 = phi i32 [ 0, %5 ], [ -1, %11 ]
ret i32 %17
}