#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;
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *);
int fd_set_nonblock(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL);
  flags |= O_NONBLOCK;
  if (fcntl(fd, F_SETFL, flags) < 0) {
    printf("fnctl: %m\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fd_set_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1188 <fd_set_nonblock+0x68>
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118f <fd_set_nonblock+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_set_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2eaa(%rip),%rcx
or     (%rcx),%eax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    116a <fd_set_nonblock+0x4a>
lea    0xea0(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [11 x i8] c"fnctl: %m\0A\00"
define i32 @fd_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 i64 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = load i32, i32* @O_NONBLOCK
%10 = load i32, i32* %4
%11 = or i32 %10, %9
store i32 %11, i32* %4
%12 = load i32, i32* %3
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* %4
%15 = call i64 (i32, i32, ...) @fcntl(i32 %12, i32 %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %20
19: ; preds = %1
store i32 0, i32* %2
br label %20
20: ; preds = %19, %17
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [11 x i8] c"fnctl: %m\0A\00"
define i32 @fd_set_nonblock(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = trunc i64 %3 to i32
%5 = load i32, i32* @O_NONBLOCK
%6 = or i32 %5, %4
%7 = load i32, i32* @F_SETFL
%8 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %7, i32 %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %1, %10
%13 = phi i32 [ -1, %10 ], [ 0, %1 ]
ret i32 %13
}