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

/* 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*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_listen(const char *host, const char *serv, socklen_t *addrlenp) {
  int listenfd;
  int n;
  const int on = 1;
  struct addrinfo hints;
  struct addrinfo *res;
  struct addrinfo *ressave;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((n = getaddrinfo(host, serv, &hints, &res)) != 0) {
    fprintf(stderr, "tcp_listen error for %s , %s:%s \n", gai_strerror(n), host,
            serv);
    return 0;
  }
  ressave = res;
  do {
    listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (listenfd < 0)
      continue;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
      break;
    close(listenfd);
  } while ((res = res->ai_next) != NULL);
  if (res == NULL)
    fprintf(stderr, "tcp_listen error for %s:%s \n", host, serv);
  listen(listenfd, 1024);
  if (addrlenp)
    *addrlenp = res->ai_addrlen;
  freeaddrinfo(ressave);
  return listenfd;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x2c(%rbp)
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     124c <tcp_listen+0xac>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1343 <tcp_listen+0x1a3>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1280 <tcp_listen+0xe0>
jmp    12d0 <tcp_listen+0x130>
mov    -0x24(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x24(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jne    12c8 <tcp_listen+0x128>
jmp    12e6 <tcp_listen+0x146>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
cmp    $0x0,%rax
jne    1254 <tcp_listen+0xb4>
cmpq   $0x0,-0x60(%rbp)
jne    1310 <tcp_listen+0x170>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
cmpq   $0x0,-0x20(%rbp)
je     1334 <tcp_listen+0x194>
mov    -0x60(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x24(%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   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
movl   $0x1,0xc(%rsp)
lea    0x20(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     123f <tcp_listen+0x9f>
mov    0x2db0(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdd8(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %r12,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1316 <tcp_listen+0x176>
mov    %r14,0x18(%rsp)
mov    (%rsp),%rax
mov    0x2d99(%rip),%r13
mov    0x2d62(%rip),%r14
lea    0xc(%rsp),%rbp
mov    %rax,0x10(%rsp)
jmp    1281 <tcp_listen+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp),%rax
mov    0x18(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     12d1 <tcp_listen+0x131>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1270 <tcp_listen+0xd0>
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %ebx,%edi
mov    %rbp,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     12ca <tcp_listen+0x12a>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1270 <tcp_listen+0xd0>
cmpq   $0x0,(%rsp)
jne    12ee <tcp_listen+0x14e>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
mov    $0x400,%esi
call   1080 <listen@plt>
mov    0x18(%rsp),%rcx
test   %rcx,%rcx
je     130c <tcp_listen+0x16c>
mov    (%rsp),%rax
mov    (%rax),%eax
mov    %eax,(%rcx)
mov    0x10(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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