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

/* Variables and functions */
int SOCK_SEQPACKET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_OOBINLINE;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, 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;
  int err = 0;
  fd = socket(addr->sa_family, type, 0);
  if (fd < 0) {
    printf("socket failed %d\n", errno);
    return -1;
  }
  int reuse = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
    err = errno;
    printf("setsockopt error %d\n", err);
    goto errout;
  }
  int oil = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &oil, sizeof(oil)) < 0) {
    err = errno;
    printf("setsockopt error %d\n", err);
    goto errout;
  }
  if (bind(fd, addr, alen) < 0) {
    err = errno;
    printf("bind error %d\n", err);
    goto errout;
  }
  if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
    if (listen(fd, qlen) < 0) {
      err = errno;
      printf("listen error %d\n", err);
      goto errout;
    }
  }
  return fd;
errout:
  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   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ba <initserver+0x5a>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <initserver+0x1c1>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    120f <initserver+0xaf>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
jmp    1306 <initserver+0x1a6>
movl   $0x1,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1264 <initserver+0x104>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdb8(%rip),%rdi
call   1040 <printf@plt>
jmp    1306 <initserver+0x1a6>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    129d <initserver+0x13d>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
jmp    1306 <initserver+0x1a6>
mov    -0x8(%rbp),%eax
mov    0x2d31(%rip),%rcx
cmp    (%rcx),%eax
je     12c1 <initserver+0x161>
mov    -0x8(%rbp),%eax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%eax
jne    12fb <initserver+0x19b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    12f6 <initserver+0x196>
mov    0x2cf3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd4a(%rip),%rdi
call   1040 <printf@plt>
jmp    1306 <initserver+0x1a6>
jmp    12fb <initserver+0x19b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1321 <initserver+0x1c1>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2cb8(%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,%r15d
mov    %rsi,%rbp
mov    %edi,%r12d
mov    (%rsi),%edi
mov    %r12d,%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1261 <initserver+0x101>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e4c(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1238 <initserver+0xd8>
movl   $0x1,(%rsp)
mov    0x0(%r13),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1238 <initserver+0xd8>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     128c <initserver+0x12c>
mov    0x2dd2(%rip),%rax
cmp    %r12d,(%rax)
je     1217 <initserver+0xb7>
mov    0x2da6(%rip),%rax
cmp    %r12d,(%rax)
jne    127b <initserver+0x11b>
mov    %ebx,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    127b <initserver+0x11b>
mov    0x2da3(%rip),%rax
mov    (%rax),%ebp
lea    0xe00(%rip),%rdi
jmp    1248 <initserver+0xe8>
mov    0x2d91(%rip),%rax
mov    (%rax),%ebp
lea    0xdca(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d73(%rip),%rax
mov    %ebp,(%rax)
jmp    1276 <initserver+0x116>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
lea    0xd8f(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d3d(%rip),%rax
mov    (%rax),%ebp
lea    0xd8b(%rip),%rdi
jmp    1248 <initserver+0xe8>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"socket failed %d\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [21 x i8] c"setsockopt error %d\0A\00"
@SO_OOBINLINE = global i32 0
@.str.2 = [15 x i8] c"bind error %d\0A\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.3 = [17 x i8] c"listen error %d\0A\00"
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
%13 = 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 0, i32* %11
%14 = load %struct.sockaddr*, %struct.sockaddr** %7
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* %6
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %4
%22 = load i32, i32* @errno
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 -1, i32* %5
br label %78
24: ; preds = %4
store i32 1, i32* %12
%25 = load i32, i32* %10
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i64 @setsockopt(i32 %25, i32 %26, i32 %27, i32* %12, i32 4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @errno
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %74
34: ; preds = %24
store i32 1, i32* %13
%35 = load i32, i32* %10
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_OOBINLINE
%38 = call i64 @setsockopt(i32 %35, i32 %36, i32 %37, i32* %13, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* @errno
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %74
44: ; preds = %34
%45 = load i32, i32* %10
%46 = load %struct.sockaddr*, %struct.sockaddr** %7
%47 = load i32, i32* %8
%48 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = load i32, i32* @errno
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %74
54: ; preds = %44
%55 = load i32, i32* %6
%56 = load i32, i32* @SOCK_STREAM
%57 = icmp eq i32 %55, %56
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = load i32, i32* %6
%60 = load i32, i32* @SOCK_SEQPACKET
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %72
62: ; preds = %58, %54
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = call i64 @listen(i32 %63, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* @errno
store i32 %68, i32* %11
%69 = load i32, i32* %11
%70 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %69)
br label %74
71: ; preds = %62
br label %72
72: ; preds = %71, %58
%73 = load i32, i32* %10
store i32 %73, i32* %5
br label %78
74: ; preds = %67, %50, %40, %30
%75 = load i32, i32* %10
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %11
store i32 %77, i32* @errno
store i32 -1, i32* %5
br label %78
78: ; preds = %74, %72, %21
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"socket failed %d\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [21 x i8] c"setsockopt error %d\0A\00"
@SO_OOBINLINE = global i32 0
@.str.2 = [15 x i8] c"bind error %d\0A\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.3 = [17 x i8] c"listen error %d\0A\00"
define i32 @initserver(i32 %0, %struct.sockaddr* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%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 %11, label %14
11: ; preds = %4
%12 = load i32, i32* @errno
%13 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %54
14: ; preds = %4
%15 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i64 @setsockopt(i32 %9, i32 %16, i32 %17, i32* %5, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %49
23: ; preds = %14
store i32 1, i32* %6
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_OOBINLINE
%26 = call i64 @setsockopt(i32 %9, i32 %24, i32 %25, i32* %6, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %49
31: ; preds = %23
%32 = call i64 @bind(i32 %9, %struct.sockaddr* %1, i32 %2)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @errno
%36 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %49
37: ; preds = %31
%38 = load i32, i32* @SOCK_STREAM
%39 = icmp eq i32 %38, %0
%40 = load i32, i32* @SOCK_SEQPACKET
%41 = icmp eq i32 %40, %0
%42 = select i1 %39, i1 true, i1 %41
br i1 %42, label %43, label %52
43: ; preds = %37
%44 = call i64 @listen(i32 %9, i32 %3)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %46, %34, %28, %20
%50 = phi i32 [ %21, %20 ], [ %29, %28 ], [ %35, %34 ], [ %47, %46 ]
%51 = call i32 @close(i32 %9)
store i32 %50, i32* @errno
br label %52
52: ; preds = %43, %37, %49
%53 = phi i32 [ -1, %49 ], [ %9, %37 ], [ %9, %43 ]
br label %54
54: ; preds = %52, %11
%55 = phi i32 [ -1, %11 ], [ %53, %52 ]
ret i32 %55
}