#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;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, char const *, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_listen(const char *host, const char *serv, socklen_t *p_addrlen) {
  int listenfd, n, len;
  const int on = 1;
  struct addrinfo hints, *res, *p_res;
  bzero(&hints, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (0 != (n = getaddrinfo(host, serv, &hints, &res))) {
    printf("%s error for %s, %s : %s.\n", host, serv, gai_strerror(n));
    exit(EXIT_FAILURE);
  }
  p_res = res;
  while (NULL != res) {
    listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (listenfd < 0) {
      continue;
    }
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
                   sizeof(on)) < 0) {
      perror("setsockopt");
      exit(EXIT_FAILURE);
    }
    if (0 == bind(listenfd, res->ai_addr, res->ai_addrlen)) {
      break;
    }
    close(listenfd);
    res = res->ai_next;
  }
  if (listen(listenfd, 10) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  if (NULL != p_addrlen) {
    *p_addrlen = res->ai_addrlen;
  }
  freeaddrinfo(p_res);
  return listenfd;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x28,%esi
call   10a0 <bzero@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     126e <tcp_listen+0xae>
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xda5(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
xor    %eax,%eax
cmp    -0x58(%rbp),%rax
je     1337 <tcp_listen+0x177>
mov    -0x58(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x14(%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ae <tcp_listen+0xee>
jmp    1276 <tcp_listen+0xb6>
mov    -0x1c(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12f6 <tcp_listen+0x136>
lea    0xd38(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    131e <tcp_listen+0x15e>
jmp    1337 <tcp_listen+0x177>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1276 <tcp_listen+0xb6>
mov    -0x1c(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1368 <tcp_listen+0x1a8>
lea    0xcd1(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     1380 <tcp_listen+0x1c0>
mov    -0x58(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x60(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x1c(%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    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   10a0 <bzero@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
jne    131b <tcp_listen+0x15b>
mov    (%rsp),%r15
test   %r15,%r15
je     12c9 <tcp_listen+0x109>
mov    0x2da3(%rip),%r12
mov    0x2d6c(%rip),%r13
lea    0xc(%rsp),%rbx
mov    %r15,%rax
jmp    1269 <tcp_listen+0xa9>
cs nopw 0x0(%rax,%rax,1)
mov    (%rsp),%rax
test   %rax,%rax
je     12c9 <tcp_listen+0x109>
mov    0x1c(%rax),%edi
mov    0x14(%rax),%edx
mov    0x18(%rax),%esi
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1260 <tcp_listen+0xa0>
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1301 <tcp_listen+0x141>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x10(%rax),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %rax,%rax
je     12c9 <tcp_listen+0x109>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%rax
mov    0x8(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
jne    1269 <tcp_listen+0xa9>
jmp    12c9 <tcp_listen+0x109>
mov    %ebp,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     1339 <tcp_listen+0x179>
test   %r14,%r14
je     12e8 <tcp_listen+0x128>
mov    (%rsp),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    %r15,%rdi
call   10d0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd13(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xcd7(%rip),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
jmp    130d <tcp_listen+0x14d>
lea    0xce6(%rip),%rdi
jmp    1308 <tcp_listen+0x148>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo*, i32, i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"%s error for %s, %s : %s.\0A\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [7 x i8] c"listen\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 @bzero(%struct.addrinfo* %5, 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 5
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
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 %24, label %19
19: ; preds = %3
%20 = call i8* @gai_strerror(i32 %17)
%21 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %64, label %29
27: ; preds = %29
%28 = load %struct.addrinfo*, %struct.addrinfo** %6
br label %61
29: ; preds = %24, %61
%30 = phi %struct.addrinfo* [ %62, %61 ], [ %25, %24 ]
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 5
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 4
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 3
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %32, i32 %34, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %27, label %39
39: ; preds = %29
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_REUSEADDR
%42 = call i64 @setsockopt(i32 %37, i32 %40, i32 %41, i8* %7, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %39
%49 = load %struct.addrinfo*, %struct.addrinfo** %6
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 2
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @bind(i32 %37, i32 %51, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %64, label %56
56: ; preds = %48
%57 = call i32 @close(i32 %37)
%58 = load %struct.addrinfo*, %struct.addrinfo** %6
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i64 0, i32 1
%60 = load %struct.addrinfo*, %struct.addrinfo** %59
store %struct.addrinfo* %60, %struct.addrinfo** %6
br label %61
61: ; preds = %56, %27
%62 = phi %struct.addrinfo* [ %28, %27 ], [ %60, %56 ]
%63 = icmp eq %struct.addrinfo* %62, null
br i1 %63, label %64, label %29
64: ; preds = %61, %48, %24
%65 = phi i32 [ undef, %24 ], [ %37, %48 ], [ %37, %61 ]
%66 = call i64 @listen(i32 %65, i32 10)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %64
%69 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %64
%73 = icmp eq i32* %2, null
br i1 %73, label %78, label %74
74: ; preds = %72
%75 = load %struct.addrinfo*, %struct.addrinfo** %6
%76 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %75, i64 0, i32 0
%77 = load i32, i32* %76
store i32 %77, i32* %2
br label %78
78: ; preds = %74, %72
%79 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
ret i32 %65
}