#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;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ log_fatal_err(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int create_listener(const char *hostname, const char *servname) {
  struct addrinfo hints = {0};
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = PF_INET;
  hints.ai_socktype = SOCK_STREAM;
  struct addrinfo *res;
  int status = getaddrinfo(hostname, servname, &hints, &res);
  if (status)
    log_fatal_err("getaddrinfo error: %s\n", gai_strerror(status));
  int listen_fd = -1;
  struct addrinfo *addrinfo;
  for (addrinfo = res; addrinfo != NULL; addrinfo = addrinfo->ai_next) {
    listen_fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
                       addrinfo->ai_protocol);
    if (listen_fd == -1)
      continue;
    int socket_option_value = 1;
    setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option_value,
               sizeof(int));
    if (bind(listen_fd, addrinfo->ai_addr, addrinfo->ai_addrlen) == -1)
      log_fatal_err("bind error: %s\n", strerror(errno));
    break;
  }
  if (addrinfo == NULL) {
    fprintf(
        stderr,
        "Check that the host and port are correct, that the port is not used "
        "by another process and that the process has the right permission\n");
    return -1;
  }
  freeaddrinfo(res);
  int backlog = 20;
  if (listen(listen_fd, backlog) == -1)
    log_fatal_err("listen error: %s\n", strerror(errno));
  return listen_fd;
}


x86 assembly (clang, -O0):
<create_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     122c <create_listener+0x7c>
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%esi
lea    0xdd9(%rip),%rdi
call   1060 <log_fatal_err@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12ed <create_listener+0x13d>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1272 <create_listener+0xc2>
jmp    12dc <create_listener+0x12c>
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    -0x5c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x50(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12d7 <create_listener+0x127>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xd45(%rip),%rdi
call   1060 <log_fatal_err@plt>
jmp    12ed <create_listener+0x13d>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    123b <create_listener+0x8b>
cmpq   $0x0,-0x58(%rbp)
jne    1319 <create_listener+0x169>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
lea    0xd1f(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <create_listener+0x1af>
mov    -0x48(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
movl   $0x14,-0x60(%rbp)
mov    -0x50(%rbp),%edi
mov    -0x60(%rbp),%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    1359 <create_listener+0x1a9>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xd59(%rip),%rdi
call   1060 <log_fatal_err@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_listener>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     120b <create_listener+0x6b>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdfc(%rip),%rdi
mov    %eax,%esi
call   1050 <log_fatal_err@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     123c <create_listener+0x9c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
jne    125b <create_listener+0xbb>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1220 <create_listener+0x80>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12ed <create_listener+0x14d>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12b6 <create_listener+0x116>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd68(%rip),%rdi
mov    %eax,%esi
call   1050 <log_fatal_err@plt>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ed <create_listener+0x14d>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc7(%rip),%rdi
mov    %eax,%esi
call   1050 <log_fatal_err@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AI_PASSIVE = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [16 x i8] c"bind error: %s\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [134 x i8] c"Check that the host and port are correct, that the port is not used by another process and that the process has the right permission\0A\00"
@.str.3 = [18 x i8] c"listen error: %s\0A\00"
define i32 @create_listener(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 i32
%10 = alloca %struct.addrinfo*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = bitcast %struct.addrinfo* %6 to i8*
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32 %14, i32* %15
%16 = load i32, i32* @PF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i8*, i8** %4
%21 = load i8*, i8** %5
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = load i32, i32* %8
%27 = call i32 @gai_strerror(i32 %26)
%28 = call i32 @log_fatal_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %2
store i32 -1, i32* %9
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %30, %struct.addrinfo** %10
br label %31
31: ; preds = %67, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %10
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %71
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 4
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 3
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %10
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 2
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %34
br label %67
48: ; preds = %34
store i32 1, i32* %11
%49 = load i32, i32* %9
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_REUSEADDR
%52 = call i32 @setsockopt(i32 %49, i32 %50, i32 %51, i32* %11, i32 4)
%53 = load i32, i32* %9
%54 = load %struct.addrinfo*, %struct.addrinfo** %10
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %10
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @bind(i32 %53, i32 %56, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %48
%63 = load i32, i32* @errno
%64 = call i32 @strerror(i32 %63)
%65 = call i32 @log_fatal_err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %62, %48
br label %71
67: ; preds = %47
%68 = load %struct.addrinfo*, %struct.addrinfo** %10
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 5
%70 = load %struct.addrinfo*, %struct.addrinfo** %69
store %struct.addrinfo* %70, %struct.addrinfo** %10
br label %31
71: ; preds = %66, %31
%72 = load %struct.addrinfo*, %struct.addrinfo** %10
%73 = icmp eq %struct.addrinfo* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32, i32* @stderr
%76 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
77: ; preds = %71
%78 = load %struct.addrinfo*, %struct.addrinfo** %7
%79 = call i32 @freeaddrinfo(%struct.addrinfo* %78)
store i32 20, i32* %12
%80 = load i32, i32* %9
%81 = load i32, i32* %12
%82 = call i32 @listen(i32 %80, i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %88
84: ; preds = %77
%85 = load i32, i32* @errno
%86 = call i32 @strerror(i32 %85)
%87 = call i32 @log_fatal_err(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %84, %77
%89 = load i32, i32* %9
store i32 %89, i32* %3
br label %90
90: ; preds = %88, %74
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AI_PASSIVE = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [16 x i8] c"bind error: %s\0A\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [134 x i8] c"Check that the host and port are correct, that the port is not used by another process and that the process has the right permission\0A\00"
@.str.3 = [18 x i8] c"listen error: %s\0A\00"
define i32 @create_listener(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = load i32, i32* @AI_PASSIVE
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %7, i32* %8
%9 = load i32, i32* @PF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = bitcast %struct.addrinfo** %4 to i8*
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = call i32 @gai_strerror(i32 %14)
%18 = call i32 @log_fatal_err(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %51, label %22
22: ; preds = %19, %47
%23 = phi %struct.addrinfo* [ %49, %47 ], [ %20, %19 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @socket(i32 %25, i32 %27, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %47, label %32
32: ; preds = %22
%33 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %30, i32 %34, i32 %35, i32* %5, i32 4)
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @bind(i32 %30, i32 %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %54
43: ; preds = %32
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 @log_fatal_err(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %45)
br label %54
47: ; preds = %22
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %22
51: ; preds = %47, %19
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.2, i64 0, i64 0))
br label %63
54: ; preds = %43, %32
%55 = load %struct.addrinfo*, %struct.addrinfo** %4
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
%57 = call i32 @listen(i32 %30, i32 20)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i32, i32* @errno
%61 = call i32 @strerror(i32 %60)
%62 = call i32 @log_fatal_err(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %54, %59, %51
%64 = phi i32 [ -1, %51 ], [ %30, %59 ], [ %30, %54 ]
ret i32 %64
}