#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ EDESTADDRREQ;
scalar_t__ EINVAL;
scalar_t__ EISCONN;
scalar_t__ ENOTSOCK;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_REUSEPORT;
int /*<<< orphan*/ assert(int);
int bind(int const, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int listen(int const, int /*<<< orphan*/);
int sock_close(int const, int);
int /*<<< orphan*/ sock_enopt(int const, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_listen(const struct addrinfo *const ai) {
  const int fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (fd == -1)
    return (-1);
  sock_enopt(fd, SO_REUSEADDR);
  sock_enopt(fd, SO_REUSEPORT);
  if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) {
    assert(errno != EBADF);
    assert(errno != ENOTSOCK);
    assert(errno != EINVAL);
    assert(errno != EISCONN);
    return sock_close(fd, (-2));
  }
  if (listen(fd, 0) == -1) {
    assert(errno != EBADF);
    assert(errno != EDESTADDRREQ);
    assert(errno != EINVAL);
    assert(errno != ENOTSOCK);
    return sock_close(fd, (-3));
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119f <tcp_listen+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    132a <tcp_listen+0x1ca>
mov    -0x14(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1030 <sock_enopt@plt>
mov    -0x14(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1030 <sock_enopt@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1278 <tcp_listen+0x118>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x14(%rbp),%edi
mov    $0xfffffffe,%esi
call   1070 <sock_close@plt>
mov    %eax,-0x4(%rbp)
jmp    132a <tcp_listen+0x1ca>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1324 <tcp_listen+0x1c4>
mov    0x2d2e(%rip),%rax
mov    (%rax),%rax
mov    0x2d14(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%rax
mov    0x2cda(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%rax
mov    0x2cc9(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x14(%rbp),%edi
mov    $0xfffffffd,%esi
call   1070 <sock_close@plt>
mov    %eax,-0x4(%rbp)
jmp    132a <tcp_listen+0x1ca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
mov    0x10(%rdi),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d6 <tcp_listen+0x76>
mov    %eax,%ebx
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <sock_enopt@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <sock_enopt@plt>
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %ebx,%edi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11e6 <tcp_listen+0x86>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    11db <tcp_listen+0x7b>
mov    $0xfffffffd,%r14d
mov    0x2dfb(%rip),%r15
mov    0x2e04(%rip),%r12
jmp    11fa <tcp_listen+0x9a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffe,%r14d
mov    0x2ded(%rip),%r15
mov    0x2dce(%rip),%r12
mov    0x2dbf(%rip),%rbp
mov    0x0(%rbp),%rax
mov    0x2da4(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1040 <assert@plt>
mov    0x0(%rbp),%rax
xor    %edi,%edi
cmp    (%r12),%rax
setne  %dil
call   1040 <assert@plt>
mov    0x0(%rbp),%rax
mov    0x2d80(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%rax
setne  %dil
call   1040 <assert@plt>
mov    0x0(%rbp),%rax
xor    %edi,%edi
cmp    (%r15),%rax
setne  %dil
call   1040 <assert@plt>
mov    %ebx,%edi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <sock_close@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@ENOTSOCK = global i64 0
@EINVAL = global i64 0
@EISCONN = global i64 0
@EDESTADDRREQ = global i64 0
define i32 @tcp_listen(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%5 = load %struct.addrinfo*, %struct.addrinfo** %3
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
%7 = load i32, i32* %6
%8 = load %struct.addrinfo*, %struct.addrinfo** %3
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
%10 = load i32, i32* %9
%11 = load %struct.addrinfo*, %struct.addrinfo** %3
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 2
%13 = load i32, i32* %12
%14 = call i32 @socket(i32 %7, i32 %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %86
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @sock_enopt(i32 %19, i32 %20)
%22 = load i32, i32* %4
%23 = load i32, i32* @SO_REUSEPORT
%24 = call i32 @sock_enopt(i32 %22, i32 %23)
%25 = load i32, i32* %4
%26 = load %struct.addrinfo*, %struct.addrinfo** %3
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %3
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @bind(i32 %25, i32 %28, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %57
34: ; preds = %18
%35 = load i64, i64* @errno
%36 = load i64, i64* @EBADF
%37 = icmp ne i64 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i64, i64* @errno
%41 = load i64, i64* @ENOTSOCK
%42 = icmp ne i64 %40, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINVAL
%47 = icmp ne i64 %45, %46
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i64, i64* @errno
%51 = load i64, i64* @EISCONN
%52 = icmp ne i64 %50, %51
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = load i32, i32* %4
%56 = call i32 @sock_close(i32 %55, i32 -2)
store i32 %56, i32* %2
br label %86
57: ; preds = %18
%58 = load i32, i32* %4
%59 = call i32 @listen(i32 %58, i32 0)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %84
61: ; preds = %57
%62 = load i64, i64* @errno
%63 = load i64, i64* @EBADF
%64 = icmp ne i64 %62, %63
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = load i64, i64* @errno
%68 = load i64, i64* @EDESTADDRREQ
%69 = icmp ne i64 %67, %68
%70 = zext i1 %69 to i32
%71 = call i32 @assert(i32 %70)
%72 = load i64, i64* @errno
%73 = load i64, i64* @EINVAL
%74 = icmp ne i64 %72, %73
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = load i64, i64* @errno
%78 = load i64, i64* @ENOTSOCK
%79 = icmp ne i64 %77, %78
%80 = zext i1 %79 to i32
%81 = call i32 @assert(i32 %80)
%82 = load i32, i32* %4
%83 = call i32 @sock_close(i32 %82, i32 -3)
store i32 %83, i32* %2
br label %86
84: ; preds = %57
%85 = load i32, i32* %4
store i32 %85, i32* %2
br label %86
86: ; preds = %84, %61, %34, %17
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@errno = global i64 0
@EBADF = global i64 0
@ENOTSOCK = global i64 0
@EINVAL = global i64 0
@EISCONN = global i64 0
@EDESTADDRREQ = global i64 0
define i32 @tcp_listen(%struct.addrinfo* nocapture %0) {
%2 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 4
%3 = load i32, i32* %2
%4 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %3, i32 %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %49, label %10
10: ; preds = %1
%11 = load i32, i32* @SO_REUSEADDR
%12 = tail call i32 @sock_enopt(i32 %8, i32 %11)
%13 = load i32, i32* @SO_REUSEPORT
%14 = tail call i32 @sock_enopt(i32 %8, i32 %13)
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @bind(i32 %8, i32 %16, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %10
%22 = tail call i32 @listen(i32 %8, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %49
24: ; preds = %21, %10
%25 = phi i64* [ @ENOTSOCK, %10 ], [ @EDESTADDRREQ, %21 ]
%26 = phi i64* [ @EISCONN, %10 ], [ @ENOTSOCK, %21 ]
%27 = phi i32 [ -2, %10 ], [ -3, %21 ]
%28 = load i64, i64* @errno
%29 = load i64, i64* @EBADF
%30 = icmp ne i64 %28, %29
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = load i64, i64* @errno
%34 = load i64, i64* %25
%35 = icmp ne i64 %33, %34
%36 = zext i1 %35 to i32
%37 = tail call i32 @assert(i32 %36)
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINVAL
%40 = icmp ne i64 %38, %39
%41 = zext i1 %40 to i32
%42 = tail call i32 @assert(i32 %41)
%43 = load i64, i64* @errno
%44 = load i64, i64* %26
%45 = icmp ne i64 %43, %44
%46 = zext i1 %45 to i32
%47 = tail call i32 @assert(i32 %46)
%48 = tail call i32 @sock_close(i32 %8, i32 %27)
br label %49
49: ; preds = %24, %21, %1
%50 = phi i32 [ -1, %1 ], [ %8, %21 ], [ %48, %24 ]
ret i32 %50
}