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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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(AF_INET, type, 0)) < 0)
    return (-1);
  if (bind(fd, addr, alen) < 0) {
    err = errno;
    goto errout;
  }
  if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
    if (listen(fd, qlen) < 0) {
      err = errno;
      goto errout;
    }
  }
  return (fd);
errout:
  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    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1187 <initserver+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <initserver+0xe6>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11b1 <initserver+0x71>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    120b <initserver+0xcb>
mov    -0x8(%rbp),%eax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
je     11d5 <initserver+0x95>
mov    -0x8(%rbp),%eax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%eax
jne    1200 <initserver+0xc0>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    11fb <initserver+0xbb>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    120b <initserver+0xcb>
jmp    1200 <initserver+0xc0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1226 <initserver+0xe6>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2db3(%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,%r13
mov    %edi,%ebp
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11c4 <initserver+0x84>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11b0 <initserver+0x70>
mov    0x2e4b(%rip),%rax
cmp    %ebp,(%rax)
je     119c <initserver+0x5c>
mov    0x2e30(%rip),%rax
cmp    %ebp,(%rax)
jne    11ab <initserver+0x6b>
mov    %ebx,%edi
mov    %r14d,%esi
call   1040 <listen@plt>
test   %rax,%rax
js     11b0 <initserver+0x70>
mov    %ebx,%r15d
jmp    11c4 <initserver+0x84>
mov    0x2e19(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1030 <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 }
@AF_INET = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_SEQPACKET = 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 i32, i32* @AF_INET
%13 = load i32, i32* %6
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %47
17: ; preds = %4
%18 = load i32, i32* %10
%19 = load %struct.sockaddr*, %struct.sockaddr** %7
%20 = load i32, i32* %8
%21 = call i64 @bind(i32 %18, %struct.sockaddr* %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @errno
store i32 %24, i32* %11
br label %43
25: ; preds = %17
%26 = load i32, i32* %6
%27 = load i32, i32* @SOCK_STREAM
%28 = icmp eq i32 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i32, i32* %6
%31 = load i32, i32* @SOCK_SEQPACKET
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %29, %25
%34 = load i32, i32* %10
%35 = load i32, i32* %9
%36 = call i64 @listen(i32 %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i32, i32* @errno
store i32 %39, i32* %11
br label %43
40: ; preds = %33
br label %41
41: ; preds = %40, %29
%42 = load i32, i32* %10
store i32 %42, i32* %5
br label %47
43: ; preds = %38, %23
%44 = load i32, i32* %10
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %11
store i32 %46, i32* @errno
store i32 -1, i32* %5
br label %47
47: ; preds = %43, %41, %16
%48 = load i32, i32* %5
ret i32 %48
}

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