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

/* Variables and functions */
int SOCK_SEQPACKET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int initserv(int type, struct sockaddr *paddr, socklen_t len) {
  int fd;
  int err;
  int reuse = 1;
  if ((fd = socket(paddr->sa_family, type, 0)) < 0)
    return -1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    goto errout;
  if (bind(fd, paddr, len) < 0) {
    err = errno;
    goto errout;
  }
  if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
    if (listen(fd, SOMAXCONN) < 0) {
      err = errno;
      goto errout;
    }
  }
  return fd;
errout:
  err = errno;
  close(fd);
  errno = err;
  return -1;
}


x86 assembly (clang, -O0):
<initserv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1191 <initserv+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <initserv+0x125>
mov    -0x18(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11c4 <initserv+0x74>
jmp    124e <initserv+0xfe>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11ee <initserv+0x9e>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    124e <initserv+0xfe>
mov    -0x8(%rbp),%eax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
je     1212 <initserv+0xc2>
mov    -0x8(%rbp),%eax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%eax
jne    1243 <initserv+0xf3>
mov    -0x18(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    123e <initserv+0xee>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    124e <initserv+0xfe>
jmp    1243 <initserv+0xf3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1275 <initserv+0x125>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d5c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initserv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
movl   $0x1,0xc(%rsp)
mov    (%rsi),%edi
mov    %r12d,%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     120a <initserv+0xba>
mov    %eax,%ebp
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11f6 <initserv+0xa6>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11f6 <initserv+0xa6>
mov    0x2e05(%rip),%rax
cmp    %r12d,(%rax)
je     11dc <initserv+0x8c>
mov    0x2de1(%rip),%rax
cmp    %r12d,(%rax)
jne    11f1 <initserv+0xa1>
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <listen@plt>
test   %rax,%rax
js     11f6 <initserv+0xa6>
mov    %ebp,%r14d
jmp    120a <initserv+0xba>
mov    0x2dcb(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,(%r15)
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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