#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 */
struct linger {
  int member_0;
  int member_1;
};
typedef int /*<<< orphan*/ linger_val;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_LINGER;
int fcntl(int, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int sock_set_nonblock(int sock) {
  struct linger linger_val = {1, 1};
  int flags;
  if ((flags = fcntl(sock, F_GETFL, 0)) == -1 ||
      fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
    return -1;
  if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger_val,
                 sizeof(linger_val)) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<sock_set_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     117b <sock_set_nonblock+0x5b>
mov    -0x8(%rbp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    0x2e75(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1187 <sock_set_nonblock+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <sock_set_nonblock+0xa7>
mov    -0x8(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11c0 <sock_set_nonblock+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <sock_set_nonblock+0xa7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_set_nonblock>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movabs $0x100000001,%rax
mov    %rax,(%rsp)
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     119a <sock_set_nonblock+0x7a>
mov    0x2e74(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e83(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     119a <sock_set_nonblock+0x7a>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.linger = type { i32, i32 }
@__const.sock_set_nonblock.linger_val = %struct.linger { i32 1, i32 1 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_LINGER = global i32 0
define i32 @sock_set_nonblock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.linger
%5 = alloca i32
store i32 %0, i32* %3
%6 = bitcast %struct.linger* %4 to i8*
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFL
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %19, label %11
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* %5
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %14, %15
%17 = call i32 @fcntl(i32 %12, i32 %13, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %11, %1
store i32 -1, i32* %2
br label %29
20: ; preds = %11
%21 = load i32, i32* %3
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_LINGER
%24 = bitcast %struct.linger* %4 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 -1, i32* %2
br label %29
28: ; preds = %20
store i32 0, i32* %2
br label %29
29: ; preds = %28, %27, %19
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@SOL_SOCKET = global i32 0
@SO_LINGER = global i32 0
define i32 @sock_set_nonblock(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 4294967297, i64* %2
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %0, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %19, label %7
7: ; preds = %1
%8 = load i32, i32* @F_SETFL
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %5
%11 = tail call i32 @fcntl(i32 %0, i32 %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %7
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_LINGER
%16 = call i32 @setsockopt(i32 %0, i32 %14, i32 %15, i8* %3, i32 8)
%17 = icmp eq i32 %16, -1
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %13, %1, %7
%20 = phi i32 [ -1, %7 ], [ -1, %1 ], [ %18, %13 ]
ret i32 %20
}