#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 */
typedef int uint16_t;
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
char *alloca(int);
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int listen_to(const char *host, uint16_t port) {
  struct addrinfo *addrinfos, hints = {
                                  0,
                              };
  char *port_s = NULL;
  int addrinfo_ret = 0;
  int on = 1;
  int fd = -1;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  port_s = alloca((sizeof("65535") / sizeof("65535"[0])));
  snprintf(port_s, (sizeof("65535") / sizeof("65535"[0])), "%d", port);
  if ((addrinfo_ret = getaddrinfo(host, port_s, &hints, &addrinfos)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(addrinfo_ret));
    return -1;
  }
  if ((fd = socket(addrinfos->ai_family, addrinfos->ai_socktype, 0)) < 0) {
    perror("socket");
    freeaddrinfo(addrinfos);
    return -1;
  }
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) {
    perror("setsockopt");
    freeaddrinfo(addrinfos);
    return -1;
  }
  if (bind(fd, addrinfos->ai_addr, addrinfos->ai_addrlen) < 0) {
    perror("bind");
    freeaddrinfo(addrinfos);
    return -1;
  }
  freeaddrinfo(addrinfos);
  if (listen(fd, 10) < 0) {
    perror("listen");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<listen_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x1,-0x48(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x6,%edi
call   1080 <alloca@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xdbf(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1299 <listen_to+0xd9>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xd7b(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <listen_to+0x1e8>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    12db <listen_to+0x11b>
lea    0xd53(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <listen_to+0x1e8>
mov    -0x4c(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
mov    0x2cca(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    132a <listen_to+0x16a>
lea    0xd0b(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <listen_to+0x1e8>
mov    -0x4c(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    136a <listen_to+0x1aa>
lea    0xcd6(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <listen_to+0x1e8>
mov    -0x20(%rbp),%rdi
call   10d0 <freeaddrinfo@plt>
mov    -0x4c(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    13a2 <listen_to+0x1e2>
lea    0xc9a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a8 <listen_to+0x1e8>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_to>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    $0x6,%edi
call   1070 <alloca@plt>
mov    %rax,%rbx
lea    0xdef(%rip),%rdx
mov    %rax,%rdi
mov    $0x6,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     125f <listen_to+0xaf>
mov    0x2d8a(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdb5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12ce <listen_to+0x11e>
mov    (%rsp),%rax
mov    0x8(%rax),%esi
mov    0xc(%rax),%edi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12d9 <listen_to+0x129>
mov    %eax,%ebx
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12e2 <listen_to+0x132>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
test   %rax,%rax
js     12eb <listen_to+0x13b>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     1305 <listen_to+0x155>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd34(%rip),%rdi
jmp    12f2 <listen_to+0x142>
lea    0xd32(%rip),%rdi
jmp    12f2 <listen_to+0x142>
lea    0xd34(%rip),%rdi
call   10a0 <perror@plt>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
jmp    1258 <listen_to+0xa8>
lea    0xd1f(%rip),%rdi
call   10a0 <perror@plt>
jmp    1258 <listen_to+0xa8>

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

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