#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 sockaddr {
  int /*<<< orphan*/ sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_server(struct sockaddr *addr) {
  int fd;
  int err = 0;
  int reuse = 1;
  if ((fd = socket(addr->sa_family, SOCK_STREAM, 0)) < 0)
    return (-1);
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) {
    err = errno;
    goto errout;
  }
  if (bind(fd, addr, sizeof(struct sockaddr)) < 0) {
    err = errno;
    goto errout;
  }
  if (listen(fd, 10) < 0) {
    err = errno;
    goto errout;
  }
  return (fd);
errout:
  close(fd);
  errno = err;
  return (-1);
}


x86 assembly (clang, -O0):
<init_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1198 <init_server+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1251 <init_server+0x101>
mov    -0x14(%rbp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11d7 <init_server+0x87>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1236 <init_server+0xe6>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1203 <init_server+0xb3>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1236 <init_server+0xe6>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    122b <init_server+0xdb>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1236 <init_server+0xe6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <init_server+0x101>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2d88(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
mov    (%rdi),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11e7 <init_server+0x97>
mov    %eax,%ebp
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11d3 <init_server+0x83>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11d3 <init_server+0x83>
mov    %ebp,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     11d3 <init_server+0x83>
mov    %ebp,%r14d
jmp    11e7 <init_server+0x97>
mov    0x2df6(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r15)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @init_server(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i32 0, i32* %5
store i32 1, i32* %6
%7 = load %struct.sockaddr*, %struct.sockaddr** %3
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %41
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i64 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %6, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* @errno
store i32 %21, i32* %5
br label %37
22: ; preds = %14
%23 = load i32, i32* %4
%24 = load %struct.sockaddr*, %struct.sockaddr** %3
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i32, i32* @errno
store i32 %28, i32* %5
br label %37
29: ; preds = %22
%30 = load i32, i32* %4
%31 = call i64 @listen(i32 %30, i32 10)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* @errno
store i32 %34, i32* %5
br label %37
35: ; preds = %29
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %41
37: ; preds = %33, %27, %20
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %5
store i32 %40, i32* @errno
store i32 -1, i32* %2
br label %41
41: ; preds = %37, %35, %13
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @init_server(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_REUSEADDR
%12 = call i64 @setsockopt(i32 %7, i32 %10, i32 %11, i32* %2, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %9
%15 = call i64 @bind(i32 %7, %struct.sockaddr* %0, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i64 @listen(i32 %7, i32 10)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17, %14, %9
%21 = load i32, i32* @errno
%22 = call i32 @close(i32 %7)
store i32 %21, i32* @errno
br label %23
23: ; preds = %17, %1, %20
%24 = phi i32 [ -1, %20 ], [ -1, %1 ], [ %7, %17 ]
ret i32 %24
}