#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;
scalar_t__ bind(int, struct sockaddr const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ listen(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;
  if ((fd = socket(addr->sa_family, type, 0)) < 0)
    return (-1);
  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    $0x20,%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   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1184 <initserver+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <initserver+0xd7>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11a2 <initserver+0x62>
jmp    11f0 <initserver+0xb0>
mov    -0x8(%rbp),%eax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%eax
je     11c6 <initserver+0x86>
mov    -0x8(%rbp),%eax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    11e5 <initserver+0xa5>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    11e0 <initserver+0xa0>
jmp    11f0 <initserver+0xb0>
jmp    11e5 <initserver+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <initserver+0xd7>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dca(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%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,%rbx
mov    %edi,%r13d
mov    (%rsi),%edi
mov    %r13d,%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11c1 <initserver+0x81>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11ad <initserver+0x6d>
mov    0x2e58(%rip),%rax
cmp    %r13d,(%rax)
je     1199 <initserver+0x59>
mov    0x2e3c(%rip),%rax
cmp    %r13d,(%rax)
jne    11a8 <initserver+0x68>
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <listen@plt>
test   %rax,%rax
js     11ad <initserver+0x6d>
mov    %ebp,%r15d
jmp    11c1 <initserver+0x81>
mov    0x2e24(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,(%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 }
@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
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
%12 = load %struct.sockaddr*, %struct.sockaddr** %7
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* %6
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %48
19: ; preds = %4
%20 = load i32, i32* %10
%21 = load %struct.sockaddr*, %struct.sockaddr** %7
%22 = load i32, i32* %8
%23 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
br label %43
26: ; preds = %19
%27 = load i32, i32* %6
%28 = load i32, i32* @SOCK_STREAM
%29 = icmp eq i32 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* %6
%32 = load i32, i32* @SOCK_SEQPACKET
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %41
34: ; preds = %30, %26
%35 = load i32, i32* %10
%36 = load i32, i32* %9
%37 = call i64 @listen(i32 %35, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %43
40: ; preds = %34
br label %41
41: ; preds = %40, %30
%42 = load i32, i32* %10
store i32 %42, i32* %5
br label %48
43: ; preds = %39, %25
%44 = load i32, i32* @errno
store i32 %44, i32* %11
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %11
store i32 %47, i32* @errno
store i32 -1, i32* %5
br label %48
48: ; preds = %43, %41, %18
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@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 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @socket(i32 %6, i32 %0, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %24, label %9
9: ; preds = %4
%10 = tail call i64 @bind(i32 %7, %struct.sockaddr* %1, i32 %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %9
%13 = load i32, i32* @SOCK_STREAM
%14 = icmp eq i32 %13, %0
%15 = load i32, i32* @SOCK_SEQPACKET
%16 = icmp eq i32 %15, %0
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = tail call i64 @listen(i32 %7, i32 %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18, %9
%22 = load i32, i32* @errno
%23 = tail call i32 @close(i32 %7)
store i32 %22, i32* @errno
br label %24
24: ; preds = %18, %12, %4, %21
%25 = phi i32 [ -1, %21 ], [ -1, %4 ], [ %7, %12 ], [ %7, %18 ]
ret i32 %25
}