#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;
int getpid();
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;
  }
  printf("setown to %d\n", getpid());
  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   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ca <initserver+0x5a>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <initserver+0x1d6>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    121f <initserver+0xaf>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
jmp    132b <initserver+0x1bb>
movl   $0x1,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1274 <initserver+0x104>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xda8(%rip),%rdi
call   1050 <printf@plt>
jmp    132b <initserver+0x1bb>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xda3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12c2 <initserver+0x152>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd7d(%rip),%rdi
call   1050 <printf@plt>
jmp    132b <initserver+0x1bb>
mov    -0x8(%rbp),%eax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%eax
je     12e6 <initserver+0x176>
mov    -0x8(%rbp),%eax
mov    0x2cda(%rip),%rcx
cmp    (%rcx),%eax
jne    1320 <initserver+0x1b0>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    131b <initserver+0x1ab>
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd33(%rip),%rdi
call   1050 <printf@plt>
jmp    132b <initserver+0x1bb>
jmp    1320 <initserver+0x1b0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1346 <initserver+0x1d6>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2c93(%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   1090 <socket@plt>
test   %eax,%eax
js     128a <initserver+0x11a>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e3c(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1261 <initserver+0xf1>
movl   $0x1,(%rsp)
mov    0x0(%r13),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1261 <initserver+0xf1>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     12b5 <initserver+0x145>
mov    0x2da9(%rip),%rax
cmp    %r12d,(%rax)
je     1240 <initserver+0xd0>
mov    0x2d7d(%rip),%rax
cmp    %r12d,(%rax)
jne    12a4 <initserver+0x134>
mov    %ebx,%edi
mov    %r14d,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    12a4 <initserver+0x134>
mov    0x2d7a(%rip),%rax
mov    (%rax),%ebp
lea    0xde5(%rip),%rdi
jmp    1271 <initserver+0x101>
mov    0x2d68(%rip),%rax
mov    (%rax),%ebp
lea    0xda1(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d4a(%rip),%rax
mov    %ebp,(%rax)
jmp    129f <initserver+0x12f>
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
lea    0xd66(%rip),%rdi
call   1050 <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    0x2d14(%rip),%rax
mov    (%rax),%ebp
lea    0xd70(%rip),%rdi
jmp    1271 <initserver+0x101>

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 = [14 x i8] c"setown to %d\0A\00"
@.str.3 = [15 x i8] c"bind error %d\0A\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.4 = [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 %80
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 %76
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 %76
44: ; preds = %34
%45 = call i32 (...) @getpid()
%46 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %10
%48 = load %struct.sockaddr*, %struct.sockaddr** %7
%49 = load i32, i32* %8
%50 = call i64 @bind(i32 %47, %struct.sockaddr* %48, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %44
%53 = load i32, i32* @errno
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %76
56: ; preds = %44
%57 = load i32, i32* %6
%58 = load i32, i32* @SOCK_STREAM
%59 = icmp eq i32 %57, %58
br i1 %59, label %64, label %60
60: ; preds = %56
%61 = load i32, i32* %6
%62 = load i32, i32* @SOCK_SEQPACKET
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %74
64: ; preds = %60, %56
%65 = load i32, i32* %10
%66 = load i32, i32* %9
%67 = call i64 @listen(i32 %65, i32 %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i32, i32* @errno
store i32 %70, i32* %11
%71 = load i32, i32* %11
%72 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %71)
br label %76
73: ; preds = %64
br label %74
74: ; preds = %73, %60
%75 = load i32, i32* %10
store i32 %75, i32* %5
br label %80
76: ; preds = %69, %52, %40, %30
%77 = load i32, i32* %10
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %11
store i32 %79, i32* @errno
store i32 -1, i32* %5
br label %80
80: ; preds = %76, %74, %21
%81 = load i32, i32* %5
ret i32 %81
}

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 = [14 x i8] c"setown to %d\0A\00"
@.str.3 = [15 x i8] c"bind error %d\0A\00"
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.4 = [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 %56
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 %51
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 %51
31: ; preds = %23
%32 = call i32 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i64 @bind(i32 %9, %struct.sockaddr* %1, i32 %2)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @errno
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %51
39: ; preds = %31
%40 = load i32, i32* @SOCK_STREAM
%41 = icmp eq i32 %40, %0
%42 = load i32, i32* @SOCK_SEQPACKET
%43 = icmp eq i32 %42, %0
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %54
45: ; preds = %39
%46 = call i64 @listen(i32 %9, i32 %3)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* @errno
%50 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %36, %28, %20
%52 = phi i32 [ %21, %20 ], [ %29, %28 ], [ %37, %36 ], [ %49, %48 ]
%53 = call i32 @close(i32 %9)
store i32 %52, i32* @errno
br label %54
54: ; preds = %45, %39, %51
%55 = phi i32 [ -1, %51 ], [ %9, %39 ], [ %9, %45 ]
br label %56
56: ; preds = %54, %11
%57 = phi i32 [ -1, %11 ], [ %55, %54 ]
ret i32 %57
}