#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*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ mem_set(char *, int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int net_open() {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  char on[sizeof(int)];
  mem_set(on, 1, sizeof(on));
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, on, sizeof(on)) < 0)
    return -1;
  mem_set(on, 1, sizeof(on));
  if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, on, sizeof(on)) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<net_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116a <net_open+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <net_open+0xda>
lea    -0xc(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <mem_set@plt>
mov    -0x8(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11b7 <net_open+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <net_open+0xda>
lea    -0xc(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <mem_set@plt>
mov    -0x8(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1204 <net_open+0xd4>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <net_open+0xda>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
js     11db <net_open+0xab>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <mem_set@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11db <net_open+0xab>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <mem_set@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebx
jmp    11e0 <net_open+0xb0>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @net_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [4 x i8]
%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 slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %32
10: ; preds = %0
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%12 = call i32 @mem_set(i8* %11, i32 1, i32 4)
%13 = load i32, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%17 = call i64 @setsockopt(i32 %13, i32 %14, i32 %15, i8* %16, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %10
store i32 -1, i32* %1
br label %32
20: ; preds = %10
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%22 = call i32 @mem_set(i8* %21, i32 1, i32 4)
%23 = load i32, i32* %2
%24 = load i32, i32* @IPPROTO_TCP
%25 = load i32, i32* @TCP_NODELAY
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%27 = call i64 @setsockopt(i32 %23, i32 %24, i32 %25, i8* %26, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %20
store i32 -1, i32* %1
br label %32
30: ; preds = %20
%31 = load i32, i32* %2
store i32 %31, i32* %1
br label %32
32: ; preds = %30, %29, %19, %9
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @net_open() {
%1 = alloca [4 x i8]
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %22, label %6
6: ; preds = %0
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%8 = call i32 @mem_set(i8* %7, i32 1, i32 4)
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_REUSEADDR
%11 = call i64 @setsockopt(i32 %4, i32 %9, i32 %10, i8* %7, i32 4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %6
%14 = call i32 @mem_set(i8* %7, i32 1, i32 4)
%15 = load i32, i32* @IPPROTO_TCP
%16 = load i32, i32* @TCP_NODELAY
%17 = call i64 @setsockopt(i32 %4, i32 %15, i32 %16, i8* %7, i32 4)
%18 = icmp slt i64 %17, 0
%19 = select i1 %18, i32 -1, i32 %4
br label %20
20: ; preds = %13, %6
%21 = phi i32 [ -1, %6 ], [ %19, %13 ]
br label %22
22: ; preds = %0, %20
%23 = phi i32 [ %21, %20 ], [ -1, %0 ]
ret i32 %23
}