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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_to(const char *node, const char *service) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *result;
  if (getaddrinfo(node, service, &hints, &result))
    return -1;
  int sock;
  struct addrinfo *cur = result;
  while (cur) {
    sock = socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
    if (sock == -1)
      continue;
    int v = 1;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(int));
    if (bind(sock, cur->ai_addr, cur->ai_addrlen) != -1 &&
        listen(sock, 10) != -1)
      break;
    close(sock);
    cur = cur->ai_next;
  }
  if (!cur)
    sock = -1;
  freeaddrinfo(result);
  return sock;
}


x86 assembly (clang, -O0):
<listen_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11ef <listen_to+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <listen_to+0x14c>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12ab <listen_to+0x12b>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    122e <listen_to+0xae>
jmp    11f7 <listen_to+0x77>
movl   $0x1,-0x54(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1293 <listen_to+0x113>
mov    -0x44(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     1293 <listen_to+0x113>
jmp    12ab <listen_to+0x12b>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11f7 <listen_to+0x77>
cmpq   $0x0,-0x50(%rbp)
jne    12bd <listen_to+0x13d>
movl   $0xffffffff,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_to>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1291 <listen_to+0x111>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1279 <listen_to+0xf9>
mov    0x2de5(%rip),%r15
mov    0x2db6(%rip),%r12
lea    0x4(%rsp),%r14
jmp    1220 <listen_to+0xa0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rbx),%rbx
test   %rbx,%rbx
je     127d <listen_to+0xfd>
mov    0x18(%rbx),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <listen_to+0xa0>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1211 <listen_to+0x91>
mov    %ebp,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     1211 <listen_to+0x91>
jmp    127f <listen_to+0xff>
xor    %ebx,%ebx
jmp    127f <listen_to+0xff>
xor    %ebx,%ebx
cmp    $0x1,%rbx
sbb    %ebx,%ebx
or     %ebp,%ebx
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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