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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int tcp_listen(const char *host, int port, int backlog) {
  char portstr[16];
  sprintf(portstr, "%d", port);
  struct addrinfo ai_hints;
  memset(&ai_hints, 0, sizeof(ai_hints));
  ai_hints.ai_family = AF_UNSPEC;
  ai_hints.ai_socktype = SOCK_STREAM;
  ai_hints.ai_protocol = IPPROTO_TCP;
  struct addrinfo *ai_list = NULL;
  int status = getaddrinfo(host, portstr, &ai_hints, &ai_list);
  if (status != 0) {
    return -1;
  }
  int fd = socket(ai_list->ai_family, ai_list->ai_socktype, 0);
  if (fd < 0) {
    freeaddrinfo(ai_list);
    return -2;
  }
  int on = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
    close(fd);
    freeaddrinfo(ai_list);
    return -3;
  }
  if (bind(fd, (struct sockaddr *)ai_list->ai_addr, ai_list->ai_addrlen) != 0) {
    close(fd);
    freeaddrinfo(ai_list);
    return -4;
  }
  freeaddrinfo(ai_list);
  if (listen(fd, backlog) == -1) {
    close(fd);
    return -5;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     121f <tcp_listen+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    131b <tcp_listen+0x18b>
mov    -0x58(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jge    1256 <tcp_listen+0xc6>
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    131b <tcp_listen+0x18b>
movl   $0x1,-0x64(%rbp)
mov    -0x60(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12a7 <tcp_listen+0x117>
mov    -0x60(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    131b <tcp_listen+0x18b>
mov    -0x60(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     12e4 <tcp_listen+0x154>
mov    -0x60(%rbp),%edi
call   1050 <close@plt>
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    131b <tcp_listen+0x18b>
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x60(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1315 <tcp_listen+0x185>
mov    -0x60(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    131b <tcp_listen+0x18b>
mov    -0x60(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r14d
mov    %esi,%edx
mov    %rdi,%rbp
lea    0xe57(%rip),%rsi
lea    0x30(%rsp),%r15
mov    %r15,%rdi
call   1080 <sprintf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
je     1220 <tcp_listen+0x90>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
mov    0x10(%rax),%esi
mov    0x14(%rax),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1298 <tcp_listen+0x108>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12ab <tcp_listen+0x11b>
mov    (%rsp),%rax
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %ebp,%edi
call   1070 <bind@plt>
test   %rax,%rax
je     12c5 <tcp_listen+0x135>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xfffffffc,%ebp
jmp    1213 <tcp_listen+0x83>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xfffffffe,%ebp
jmp    1213 <tcp_listen+0x83>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xfffffffd,%ebp
jmp    1213 <tcp_listen+0x83>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1213 <tcp_listen+0x83>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xfffffffb,%ebp
jmp    1213 <tcp_listen+0x83>

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

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