#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*/ AF_INET;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_ka(int);
int /*<<< orphan*/ set_no_delay(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_nonblocking_socket(void) {
  int handle = socket(AF_INET, SOCK_STREAM, 0);
  if (handle == -1)
    return -1;
  if (fcntl(handle, F_SETFL, O_NONBLOCK) == -1) {
    int e = errno;
    close(handle);
    errno = e;
    return -1;
  }
  set_ka(handle);
  set_no_delay(handle);
  return handle;
}


x86 assembly (clang, -O0):
<setup_nonblocking_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118a <setup_nonblocking_socket+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <setup_nonblocking_socket+0x9f>
mov    -0x8(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11d9 <setup_nonblocking_socket+0x89>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%ecx
mov    0x2df5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <setup_nonblocking_socket+0x9f>
mov    -0x8(%rbp),%edi
call   1050 <set_ka@plt>
mov    -0x8(%rbp),%edi
call   1060 <set_no_delay@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_nonblocking_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11c0 <setup_nonblocking_socket+0x70>
mov    %eax,%ebp
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11ac <setup_nonblocking_socket+0x5c>
mov    %ebp,%edi
call   1050 <set_ka@plt>
mov    %ebp,%edi
call   1060 <set_no_delay@plt>
mov    %ebp,%ebx
jmp    11c0 <setup_nonblocking_socket+0x70>
mov    0x2e0d(%rip),%r14
mov    (%r14),%r15d
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15d,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @setup_nonblocking_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %27
10: ; preds = %0
%11 = load i32, i32* %2
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = call i32 @fcntl(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i32, i32* @errno
store i32 %17, i32* %3
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %3
store i32 %20, i32* @errno
store i32 -1, i32* %1
br label %27
21: ; preds = %10
%22 = load i32, i32* %2
%23 = call i32 @set_ka(i32 %22)
%24 = load i32, i32* %2
%25 = call i32 @set_no_delay(i32 %24)
%26 = load i32, i32* %2
store i32 %26, i32* %1
br label %27
27: ; preds = %21, %16, %9
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @setup_nonblocking_socket() {
%1 = load i32, i32* @AF_INET
%2 = load i32, i32* @SOCK_STREAM
%3 = tail call i32 @socket(i32 %1, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %16, label %5
5: ; preds = %0
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = tail call i32 @fcntl(i32 %3, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = tail call i32 @close(i32 %3)
store i32 %11, i32* @errno
br label %16
13: ; preds = %5
%14 = tail call i32 @set_ka(i32 %3)
%15 = tail call i32 @set_no_delay(i32 %3)
br label %16
16: ; preds = %0, %13, %10
%17 = phi i32 [ -1, %10 ], [ %3, %13 ], [ -1, %0 ]
ret i32 %17
}