#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;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_listen(char *address, char *port) {
  struct addrinfo hints, *ai, *p;
  int rv;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rv = getaddrinfo(address, port, &hints, &ai)) != 0) {
    return 0;
  }
  for (p = ai; p != NULL; p = p->ai_next) {
    int yes = 1;
    rv = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (rv < 0)
      continue;
    setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    if (bind(rv, p->ai_addr, p->ai_addrlen) < 0) {
      close(rv);
      continue;
    }
    break;
  }
  freeaddrinfo(ai);
  if (p == NULL) {
    return 0;
  }
  if (listen(rv, 10) == -1) {
    return 0;
  }
  return rv;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     11f1 <tcp_listen+0x71>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <tcp_listen+0x165>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     129d <tcp_listen+0x11d>
movl   $0x1,-0x58(%rbp)
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1237 <tcp_listen+0xb7>
jmp    128c <tcp_listen+0x10c>
mov    -0x54(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
lea    -0x58(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1287 <tcp_listen+0x107>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
jmp    128c <tcp_listen+0x10c>
jmp    129d <tcp_listen+0x11d>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11f9 <tcp_listen+0x79>
mov    -0x48(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12bd <tcp_listen+0x13d>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <tcp_listen+0x165>
mov    -0x54(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12df <tcp_listen+0x15f>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <tcp_listen+0x165>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11f9 <tcp_listen+0x79>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbx
xor    %r14d,%r14d
test   %rbx,%rbx
je     1277 <tcp_listen+0xf7>
mov    0x2ddc(%rip),%r12
mov    0x2dad(%rip),%r13
lea    0xc(%rsp),%r15
jmp    1229 <tcp_listen+0xa9>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1283 <tcp_listen+0x103>
movl   $0x1,0xc(%rsp)
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
test   %eax,%eax
js     1220 <tcp_listen+0xa0>
mov    %eax,%ebp
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
test   %rax,%rax
jns    1291 <tcp_listen+0x111>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1220 <tcp_listen+0xa0>
xor    %edi,%edi
call   1090 <freeaddrinfo@plt>
jmp    11e7 <tcp_listen+0x67>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    11e7 <tcp_listen+0x67>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
cmove  %ecx,%ebp
mov    %ebp,%r14d
jmp    11e7 <tcp_listen+0x67>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @tcp_listen(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @AI_PASSIVE
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %16, i32* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %20, i32* %9
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 0, i32* %3
br label %77
23: ; preds = %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %24, %struct.addrinfo** %8
br label %25
25: ; preds = %60, %23
%26 = load %struct.addrinfo*, %struct.addrinfo** %8
%27 = icmp ne %struct.addrinfo* %26, null
br i1 %27, label %28, label %64
28: ; preds = %25
store i32 1, i32* %10
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %28
br label %60
42: ; preds = %28
%43 = load i32, i32* %9
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i32* %10, i32 4)
%47 = load i32, i32* %9
%48 = load %struct.addrinfo*, %struct.addrinfo** %8
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @bind(i32 %47, i32 %50, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %42
%57 = load i32, i32* %9
%58 = call i32 @close(i32 %57)
br label %60
59: ; preds = %42
br label %64
60: ; preds = %56, %41
%61 = load %struct.addrinfo*, %struct.addrinfo** %8
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 5
%63 = load %struct.addrinfo*, %struct.addrinfo** %62
store %struct.addrinfo* %63, %struct.addrinfo** %8
br label %25
64: ; preds = %59, %25
%65 = load %struct.addrinfo*, %struct.addrinfo** %7
%66 = call i32 @freeaddrinfo(%struct.addrinfo* %65)
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = icmp eq %struct.addrinfo* %67, null
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 0, i32* %3
br label %77
70: ; preds = %64
%71 = load i32, i32* %9
%72 = call i32 @listen(i32 %71, i32 10)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %75
74: ; preds = %70
store i32 0, i32* %3
br label %77
75: ; preds = %70
%76 = load i32, i32* %9
store i32 %76, i32* %3
br label %77
77: ; preds = %75, %74, %69, %22
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @tcp_listen(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %59
17: ; preds = %2
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = bitcast i32* %5 to i8*
br label %27
22: ; preds = %49
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %24
24: ; preds = %22, %17
%25 = phi %struct.addrinfo* [ %23, %22 ], [ null, %17 ]
%26 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
br label %59
27: ; preds = %20, %49
%28 = phi %struct.addrinfo* [ %18, %20 ], [ %51, %49 ]
store i32 1, i32* %5
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %49, label %37
37: ; preds = %27
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_REUSEADDR
%40 = call i32 @setsockopt(i32 %35, i32 %38, i32 %39, i32* %5, i32 4)
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @bind(i32 %35, i32 %42, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %37
%48 = call i32 @close(i32 %35)
br label %49
49: ; preds = %47, %27
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%51 = load %struct.addrinfo*, %struct.addrinfo** %50
%52 = icmp eq %struct.addrinfo* %51, null
br i1 %52, label %22, label %27
53: ; preds = %37
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
%56 = call i32 @listen(i32 %35, i32 10)
%57 = icmp eq i32 %56, -1
%58 = select i1 %57, i32 0, i32 %35
br label %59
59: ; preds = %24, %53, %2
%60 = phi i32 [ 0, %2 ], [ %58, %53 ], [ 0, %24 ]
ret i32 %60
}