#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*/ 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*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
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*/ perror(char *);
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) {
  int listenfd, n;
  const int on = 1;
  struct addrinfo hints, *res, *ressave;
  bzero(&hints, 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, "getaddrinfo: %s\n", gai_strerror(n));
    exit(3);
  }
  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 == 0) {
    perror("socket or bind");
    exit(4);
  }
  listen(listenfd, 1024);
  freeaddrinfo(ressave);
  return (listenfd);
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0x28,%esi
call   10a0 <bzero@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     125a <tcp_listen+0x9a>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xdb5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-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   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    128e <tcp_listen+0xce>
jmp    12de <tcp_listen+0x11e>
mov    -0x14(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    12d6 <tcp_listen+0x116>
jmp    12f4 <tcp_listen+0x134>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
jne    1262 <tcp_listen+0xa2>
cmpq   $0x0,-0x50(%rbp)
jne    1315 <tcp_listen+0x155>
lea    0xd0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   1070 <listen@plt>
mov    -0x58(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
movl   $0x1,0x14(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
mov    $0x28,%esi
call   10a0 <bzero@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
jne    12ec <tcp_listen+0x12c>
mov    0x8(%rsp),%r14
mov    0x2dae(%rip),%r15
mov    0x2d77(%rip),%r12
lea    0x14(%rsp),%rbx
mov    %r14,%rax
jmp    126a <tcp_listen+0xaa>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rax
mov    0x18(%rax),%rax
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     12d6 <tcp_listen+0x116>
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10e0 <socket@plt>
test   %eax,%eax
js     1257 <tcp_listen+0x97>
mov    %eax,%ebp
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %rax,%rax
jne    1250 <tcp_listen+0x90>
cmpq   $0x0,0x8(%rsp)
je     12d6 <tcp_listen+0x116>
mov    %ebp,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
mov    %r14,%rdi
call   10d0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd34(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
mov    0x2cd5(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xcfd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>

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

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 = [17 x i8] c"getaddrinfo: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [15 x i8] c"socket or bind\00"
define i32 @tcp_listen(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = call i32 @bzero(%struct.addrinfo* %4, i32 40)
%10 = load i32, i32* @AI_PASSIVE
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %10, i32* %11
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %16)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 3)
unreachable
23: ; preds = %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %5
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* %3, i32 4)
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%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** %5
%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** %5
%52 = icmp eq %struct.addrinfo* %51, null
br i1 %52, label %56, label %25
53: ; preds = %35
%54 = load %struct.addrinfo*, %struct.addrinfo** %5
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %59
56: ; preds = %48, %53
%57 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%58 = call i32 @exit(i32 4)
unreachable
59: ; preds = %53
%60 = call i32 @listen(i32 %33, i32 1024)
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %24)
ret i32 %33
}