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

/* Variables and functions */
int SOCK_SEQPACKET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int /*<<< orphan*/);
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, int /*<<< orphan*/);
int initserver(int type, const struct sockaddr *addr, socklen_t alen,
               int qlen) {
  int fd, err;
  int reuse = 1;
  if ((fd = socket(addr->sa_family, type, 0)) < 0)
    return (-1);
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0)
    goto errout;
  if (bind(fd, addr, alen) < 0)
    goto errout;
  if (type == SOCK_STREAM || type == SOCK_SEQPACKET)
    if (listen(fd, qlen) < 0)
      goto errout;
  return (fd);
errout:
  err = errno;
  close(fd);
  errno = err;
  return (-1);
}


x86 assembly (clang, -O0):
<initserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1194 <initserver+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    125a <initserver+0x10a>
mov    -0x1c(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11c7 <initserver+0x77>
jmp    1233 <initserver+0xe3>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11e5 <initserver+0x95>
jmp    1233 <initserver+0xe3>
mov    -0x8(%rbp),%eax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%eax
je     1209 <initserver+0xb9>
mov    -0x8(%rbp),%eax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%eax
jne    1228 <initserver+0xd8>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    1223 <initserver+0xd3>
jmp    1233 <initserver+0xe3>
jmp    1228 <initserver+0xd8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <initserver+0x10a>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d7f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %edi,%r13d
movl   $0x1,0x4(%rsp)
mov    (%rsi),%edi
mov    %r13d,%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1202 <initserver+0xb2>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11ee <initserver+0x9e>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11ee <initserver+0x9e>
mov    0x2e0f(%rip),%rax
cmp    %r13d,(%rax)
je     11da <initserver+0x8a>
mov    0x2deb(%rip),%rax
cmp    %r13d,(%rax)
jne    11e9 <initserver+0x99>
mov    %ebx,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
test   %rax,%rax
js     11ee <initserver+0x9e>
mov    %ebx,%r15d
jmp    1202 <initserver+0xb2>
mov    0x2ddb(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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