#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;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ nq_errmsg(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int nq_set_fd_nonblocking(int fd) {
  int fd_flag;
  if ((fd_flag = fcntl(fd, F_GETFL, 0)) < 0) {
    nq_errmsg("fcntl: %s", strerror(errno));
    return -1;
  }
  fd_flag |= O_NONBLOCK;
  if (fcntl(fd, F_SETFL, fd_flag) < 0) {
    nq_errmsg("fcntl: %s", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<nq_set_fd_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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    $0x0,%eax
jge    1182 <nq_set_fd_nonblocking+0x52>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe8f(%rip),%rdi
call   1040 <nq_errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <nq_set_fd_nonblocking+0xae>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11d7 <nq_set_fd_nonblocking+0xa7>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe3a(%rip),%rdi
call   1040 <nq_errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <nq_set_fd_nonblocking+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nq_set_fd_nonblocking>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     116b <nq_set_fd_nonblocking+0x3b>
mov    0x2e96(%rip),%rcx
or     (%rcx),%eax
mov    0x2e7d(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
jns    118c <nq_set_fd_nonblocking+0x5c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe80(%rip),%rdi
mov    %eax,%esi
call   1040 <nq_errmsg@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [10 x i8] c"fcntl: %s\00"
@errno = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @nq_set_fd_nonblocking(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 i64 @fcntl(i32 %5, i32 %6, i32 0)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @nq_errmsg(i8* getelementptr inbounds ([10 x i8], [10 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 i64 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %14
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @nq_errmsg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, 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
@.str = [10 x i8] c"fcntl: %s\00"
@errno = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @nq_set_fd_nonblocking(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i64 @fcntl(i32 %0, i32 %2, i32 0)
%4 = trunc i64 %3 to i32
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %4
%9 = load i32, i32* @F_SETFL
%10 = tail call i64 @fcntl(i32 %0, i32 %9, i32 %8)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %6, %1
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @nq_errmsg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %6
%17 = phi i32 [ 0, %6 ], [ -1, %12 ]
ret i32 %17
}