#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 */
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
void _set_socket_nonblocking(int socket) {
  int opts = fcntl(socket, F_GETFL);
  int opt = 1;
  if (0 > opts) {
    printf("failed to call fcntl with F_GETFL\n");
    exit(1);
  }
  opts |= O_NONBLOCK;
  if (0 > fcntl(socket, F_SETFL, opts)) {
    printf("failed to call fcntl with F_SETFL\n");
    exit(1);
  }
  setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
}


x86 assembly (clang, -O0):
<_set_socket_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jle    1189 <_set_socket_nonblocking+0x49>
lea    0xe86(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
or     -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11d2 <_set_socket_nonblocking+0x92>
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_socket_nonblocking>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <fcntl@plt>
movl   $0x1,0xc(%rsp)
test   %eax,%eax
js     11af <_set_socket_nonblocking+0x6f>
mov    0x2e76(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
test   %rax,%rax
js     11b8 <_set_socket_nonblocking+0x78>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6c(%rip),%rdi
jmp    11bf <_set_socket_nonblocking+0x7f>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [35 x i8] c"failed to call fcntl with F_GETFL\0A\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [35 x i8] c"failed to call fcntl with F_SETFL\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define void @_set_socket_nonblocking(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%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* %3
store i32 1, i32* %4
%9 = load i32, i32* %3
%10 = icmp sgt i32 0, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* @O_NONBLOCK
%16 = load i32, i32* %3
%17 = or i32 %16, %15
store i32 %17, i32* %3
%18 = load i32, i32* %2
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %3
%21 = call i64 (i32, i32, ...) @fcntl(i32 %18, i32 %19, i32 %20)
%22 = icmp sgt i64 0, %21
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %14
%27 = load i32, i32* %2
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %4, i32 4)
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@str = [34 x i8] c"failed to call fcntl with F_SETFL\00"
@str.2 = [34 x i8] c"failed to call fcntl with F_GETFL\00"
define void @_set_socket_nonblocking(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @F_GETFL
%4 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = trunc i64 %4 to i32
%6 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %5
%14 = load i32, i32* @F_SETFL
%15 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %14, i32 %13)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %0, i32 %21, i32 %22, i32* %2, i32 4)
ret void
}