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

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ setup_addrinfo(struct addrinfo **, int /*<<< orphan*/ *,
                                  char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_listener_socket_file_descriptor(char *port) {
  int sockfd;
  struct addrinfo *servinfo, *p;
  char s[INET_ADDRSTRLEN];
  int yes = 1;
  setup_addrinfo(&servinfo, NULL, port, AI_PASSIVE);
  for (p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      perror("socket");
      continue;
    }
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
      perror("setsockopt");
      exit(1);
    }
    if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
      close(sockfd);
      perror("connection");
      continue;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "%s\n", "server: failed to bind");
    exit(1);
  }
  inet_ntop(p->ai_family, &(((struct sockaddr_in *)p->ai_addr)->sin_addr), s,
            sizeof(s));
  printf("listening on %s:%s\n", s, port);
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<get_listener_socket_file_descriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x8(%rbp),%rdx
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <setup_addrinfo@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12e8 <get_listener_socket_file_descriptor+0x138>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1257 <get_listener_socket_file_descriptor+0xa7>
lea    0xdb3(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d7 <get_listener_socket_file_descriptor+0x127>
mov    -0xc(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
lea    -0x34(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    129a <get_listener_socket_file_descriptor+0xea>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12d2 <get_listener_socket_file_descriptor+0x122>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    0xd4a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d7 <get_listener_socket_file_descriptor+0x127>
jmp    12e8 <get_listener_socket_file_descriptor+0x138>
mov    -0x20(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1215 <get_listener_socket_file_descriptor+0x65>
cmpq   $0x0,-0x20(%rbp)
jne    1319 <get_listener_socket_file_descriptor+0x169>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
lea    0xd13(%rip),%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%edi
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xcef(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0xc(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_listener_socket_file_descriptor>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rdx
mov    %rsp,-0x48(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r13
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
movl   $0x1,-0x2c(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    %rdx,-0x40(%rbp)
call   1080 <setup_addrinfo@plt>
mov    -0x38(%rbp),%r14
test   %r14,%r14
je     1288 <get_listener_socket_file_descriptor+0xd8>
lea    0xdee(%rip),%r12
mov    0x14(%r14),%esi
mov    0x8(%r14),%edi
mov    0x10(%r14),%edx
call   10d0 <socket@plt>
mov    %r12,%rdi
cmp    $0xffffffff,%eax
je     127a <get_listener_socket_file_descriptor+0xca>
mov    %eax,%ebx
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12f1 <get_listener_socket_file_descriptor+0x141>
mov    (%r14),%rsi
mov    0xc(%r14),%edx
mov    %ebx,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ae <get_listener_socket_file_descriptor+0xfe>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
mov    0x18(%r14),%r14
test   %r14,%r14
jne    1212 <get_listener_socket_file_descriptor+0x62>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
lea    0xd7e(%rip),%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%edi
mov    (%r14),%rsi
mov    %r13,%rdx
mov    %r15d,%ecx
call   1030 <inet_ntop@plt>
lea    0xd6d(%rip),%rdi
mov    %r13,%rsi
mov    -0x40(%rbp),%rdx
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x48(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AI_PASSIVE = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [11 x i8] c"connection\00"
@stderr = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [23 x i8] c"server: failed to bind\00"
@.str.5 = [20 x i8] c"listening on %s:%s\0A\00"
define i32 @get_listener_socket_file_descriptor(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = load i32, i32* @INET_ADDRSTRLEN
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%10 = load i32, i32* @AI_PASSIVE
%11 = call i32 @setup_addrinfo(%struct.addrinfo** %2, i32* null, i8* %0, i32 %10)
%12 = load %struct.addrinfo*, %struct.addrinfo** %2
%13 = icmp eq %struct.addrinfo* %12, null
br i1 %13, label %47, label %14
14: ; preds = %1, %41
%15 = phi %struct.addrinfo* [ %45, %41 ], [ %12, %1 ]
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 3
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %41, label %24
24: ; preds = %14
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %22, i32 %25, i32 %26, i32* %3, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%34 = load i64, i64* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @bind(i32 %22, i64 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %51
39: ; preds = %32
%40 = call i32 @close(i32 %22)
br label %41
41: ; preds = %14, %39
%42 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %39 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %14 ]
%43 = call i32 @perror(i8* %42)
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %47, label %14
47: ; preds = %41, %1
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %32
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%54 = load i32, i32* %52
%55 = load i64, i64* %53
%56 = inttoptr i64 %55 to %struct.sockaddr_in*
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %56, i64 0, i32 0
%58 = call i32 @inet_ntop(i32 %54, i32* %57, i8* %8, i32 %5)
%59 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %8, i8* %0)
%60 = load %struct.addrinfo*, %struct.addrinfo** %2
%61 = call i32 @freeaddrinfo(%struct.addrinfo* %60)
ret i32 %22
}