#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_storage {
  int dummy;
};
struct sigaction {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  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;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ 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 start_listening_on(char *port) {
  int sockfd, new_fd;
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_storage their_addr;
  socklen_t sin_size;
  struct sigaction sa;
  int yes = 1;
  char s[INET6_ADDRSTRLEN];
  int rv;
  char buf[100];
  int numbytes;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  getaddrinfo(NULL, port, &hints, &servinfo);
  sockfd =
      socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen);
  freeaddrinfo(servinfo);
  listen(sockfd, 10);
  return sockfd;
}


x86 assembly (clang, -O0):
<start_listening_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,-0x60(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
mov    -0x30(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
mov    -0xc(%rbp),%eax
mov    -0x58(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_listening_on>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
mov    %rsp,%r15
movl   $0x1,0x14(%rsp)
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1090 <socket@plt>
mov    %eax,%ebx
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1060 <bind@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
mov    %r15,%rsp
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sigaction = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@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 @start_listening_on(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca %struct.sigaction
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca [100 x i8]
%16 = alloca i32
store i8* %0, i8** %2
store i32 1, i32* %11
%17 = load i32, i32* @INET6_ADDRSTRLEN
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %12
%20 = alloca i8, i64 %18
store i64 %18, i64* %13
%21 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 24)
%22 = load i32, i32* @AF_UNSPEC
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %22, i32* %23
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i32, i32* @AI_PASSIVE
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 5
store i32 %26, i32* %27
%28 = load i8*, i8** %2
%29 = call i32 @getaddrinfo(i32* null, i8* %28, %struct.addrinfo* %5, %struct.addrinfo** %6)
%30 = load %struct.addrinfo*, %struct.addrinfo** %6
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 4
%32 = load i32, i32* %31
%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 %32, i32 %35, i32 %38)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %11, i32 4)
%44 = load i32, i32* %3
%45 = load %struct.addrinfo*, %struct.addrinfo** %6
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %6
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @bind(i32 %44, i32 %47, i32 %50)
%52 = load %struct.addrinfo*, %struct.addrinfo** %6
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
%54 = load i32, i32* %3
%55 = call i32 @listen(i32 %54, i32 10)
%56 = load i32, i32* %3
%57 = load i8*, i8** %12
ret i32 %56
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@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 @start_listening_on(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = tail call i8* @llvm.stacksave()
%9 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 2
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %19, i32 %21, i32 %23)
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %4, i32 4)
%28 = load %struct.addrinfo*, %struct.addrinfo** %3
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @bind(i32 %24, i32 %30, i32 %32)
%34 = load %struct.addrinfo*, %struct.addrinfo** %3
%35 = call i32 @freeaddrinfo(%struct.addrinfo* %34)
%36 = call i32 @listen(i32 %24, i32 10)
ret i32 %24
}