#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;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_ADDRCONFIG;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
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 **);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int const *,
                      int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_listening_socket(const char *host, const char *port) {
  struct addrinfo addr_hints = {.ai_family = AF_UNSPEC,
                                .ai_socktype = SOCK_STREAM,
                                .ai_flags = AI_ADDRCONFIG};
  struct addrinfo *addr;
  int error;
  if ((error = getaddrinfo(host, port, &addr_hints, &addr))) {
    fprintf(stderr, "ERROR: getaddrinfo: %s\n", gai_strerror(error));
    exit(1);
  }
  int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  if (sock < 0) {
    perror("ERROR: socket");
    exit(1);
  }
  static const int one = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
    perror("ERROR: setsockopt");
    exit(1);
  }
  if (bind(sock, addr->ai_addr, addr->ai_addrlen) != 0) {
    perror("ERROR: bind");
    exit(1);
  }
  if (listen(sock, 10) != 0) {
    perror("ERROR: listen");
    exit(1);
  }
  freeaddrinfo(addr);
  return sock;
}


x86 assembly (clang, -O0):
<create_listening_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     123a <create_listening_socket+0x9a>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@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   10c0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1277 <create_listening_socket+0xd7>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
lea    0xdc1(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12be <create_listening_socket+0x11e>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     12f3 <create_listening_socket+0x153>
lea    0xd54(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     1320 <create_listening_socket+0x180>
lea    0xd33(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_listening_socket>:
push   %rbx
sub    $0x20,%rsp
movq   $0x0,0x8(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    126c <create_listening_socket+0xcc>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     1297 <create_listening_socket+0xf7>
mov    %eax,%ebx
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    12a0 <create_listening_socket+0x100>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1070 <bind@plt>
test   %rax,%rax
jne    12a9 <create_listening_socket+0x109>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
test   %rax,%rax
jne    12b2 <create_listening_socket+0x112>
mov    (%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2d5d(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd7d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd7a(%rip),%rdi
jmp    12b9 <create_listening_socket+0x119>
lea    0xd7f(%rip),%rdi
jmp    12b9 <create_listening_socket+0x119>
lea    0xd88(%rip),%rdi
jmp    12b9 <create_listening_socket+0x119>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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