#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 */
typedef void *uint16_t;
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin_addr;
  void *sin6_family;
  void *sin6_port;
  void *sin_family;
  void *sin_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin_addr;
  void *sin6_family;
  void *sin6_port;
  void *sin_family;
  void *sin_port;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
void *AF_INET;
void *AF_INET6;
#define PF_INET 129
#define PF_INET6 128
int const SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *get_ip_from_ifname(void *, char const *const);
void *htons(void *const);
int inet_pton(void *, char const *const, int /*<<< orphan*/ *);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_socket_options(int);
int /*<<< orphan*/ set_socket_options_low_volume(int);
int socket(int const, int const, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char *, char const *const);
int create_listening_socket(const char *const interface, const uint16_t port,
                            const int pf_family, const int socket_type,
                            const int keep_alive) {
  int tmp;
  int sock;
  char *ip = NULL;
  struct sockaddr_in s4;
  struct sockaddr_in6 s6;
  struct sockaddr *listen_addr;
  uint16_t nport;
  size_t addr_size;
  sock = -1;
  if (SOCK_STREAM != socket_type)
    return sock;
  nport = htons(port);
  memset(&s4, 0, sizeof(struct sockaddr_in));
  memset(&s6, 0, sizeof(struct sockaddr_in6));
  switch (pf_family) {
  case PF_INET:
    s4.sin_port = nport;
    s4.sin_family = AF_INET;
    listen_addr = (struct sockaddr *)&s4;
    addr_size = sizeof(struct sockaddr_in);
    break;
  case PF_INET6:
    s6.sin6_port = nport;
    s6.sin6_family = AF_INET6;
    listen_addr = (struct sockaddr *)&s6;
    addr_size = sizeof(struct sockaddr_in6);
    break;
  default:
    return sock;
  }
  sock = socket(pf_family, socket_type, 0);
  if (-1 == sock) {
    return sock;
  }
  if (keep_alive) {
    if (!set_socket_options_low_volume(sock)) {
      goto err;
    }
  } else {
    if (!set_socket_options(sock)) {
      goto err;
    }
  }
  if (!strcmp("localhost", interface)) {
    if (PF_INET == pf_family) {
      if (1 != inet_pton(AF_INET, "127.0.0.1", &s4.sin_addr)) {
        goto err;
      }
    } else {
      if (1 != inet_pton(AF_INET6, "::1", &s6.sin6_addr)) {
        goto err;
      }
    }
  } else {
    if (PF_INET == pf_family) {
      tmp = inet_pton(AF_INET, interface, &s4.sin_addr);
      if (!tmp) {
        ip = get_ip_from_ifname(AF_INET, interface);
        if (!ip) {
          goto err;
        }
        tmp = inet_pton(AF_INET, ip, &s4.sin_addr);
        free(ip);
      }
    } else {
      tmp = inet_pton(AF_INET6, interface, &s6.sin6_addr);
      if (!tmp) {
        ip = get_ip_from_ifname(AF_INET6, interface);
        if (!ip) {
          goto err;
        }
        tmp = inet_pton(AF_INET6, ip, &s6.sin6_addr);
        free(ip);
      }
    }
    switch (tmp) {
    case 1:
      break;
    case 0:
      goto err;
    case -1:
    default:
      goto err;
    }
  }
  if (bind(sock, listen_addr, addr_size)) {
    goto err;
  }
  if (listen(sock, SOMAXCONN)) {
    goto err;
  }
  goto out;
err:
  if (-1 != sock) {
    close(sock);
    sock = -1;
  }
out:
  return sock;
}


x86 assembly (clang, -O0):
<create_listening_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
cmp    -0x20(%rbp),%eax
je     1209 <create_listening_socket+0x49>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15bf <create_listening_socket+0x3ff>
mov    -0x18(%rbp),%rdi
call   1060 <htons@plt>
mov    %rax,-0x98(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    %esi,-0xac(%rbp)
mov    $0x28,%edx
mov    %edx,-0xa8(%rbp)
call   1070 <memset@plt>
mov    -0xac(%rbp),%esi
mov    -0xa8(%rbp),%edx
lea    -0x88(%rbp),%rdi
call   1070 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xa4(%rbp)
sub    $0x80,%eax
je     12b0 <create_listening_socket+0xf0>
jmp    1266 <create_listening_socket+0xa6>
mov    -0xa4(%rbp),%eax
sub    $0x81,%eax
jne    12e7 <create_listening_socket+0x127>
jmp    127c <create_listening_socket+0xbc>
mov    -0x98(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x90(%rbp)
movq   $0x28,-0xa0(%rbp)
jmp    12f2 <create_listening_socket+0x132>
mov    -0x98(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2d1e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x80(%rbp)
lea    -0x88(%rbp),%rax
mov    %rax,-0x90(%rbp)
movq   $0x28,-0xa0(%rbp)
jmp    12f2 <create_listening_socket+0x132>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15bf <create_listening_socket+0x3ff>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    131b <create_listening_socket+0x15b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15bf <create_listening_socket+0x3ff>
cmpl   $0x0,-0x24(%rbp)
je     1340 <create_listening_socket+0x180>
mov    -0x2c(%rbp),%edi
call   1040 <set_socket_options_low_volume@plt>
cmp    $0x0,%eax
jne    133b <create_listening_socket+0x17b>
jmp    1597 <create_listening_socket+0x3d7>
jmp    135b <create_listening_socket+0x19b>
mov    -0x2c(%rbp),%edi
call   10c0 <set_socket_options@plt>
cmp    $0x0,%eax
jne    1356 <create_listening_socket+0x196>
jmp    1597 <create_listening_socket+0x3d7>
jmp    135b <create_listening_socket+0x19b>
mov    -0x10(%rbp),%rsi
lea    0xc9e(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    13f4 <create_listening_socket+0x234>
mov    $0x81,%eax
cmp    -0x1c(%rbp),%eax
jne    13b9 <create_listening_socket+0x1f9>
mov    0x2c5f(%rip),%rax
mov    (%rax),%rdi
lea    -0x60(%rbp),%rdx
add    $0x4,%rdx
lea    0xc73(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     13b4 <create_listening_socket+0x1f4>
jmp    1597 <create_listening_socket+0x3d7>
jmp    13ef <create_listening_socket+0x22f>
mov    0x2c20(%rip),%rax
mov    (%rax),%rdi
lea    -0x88(%rbp),%rdx
lea    0xc47(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     13ea <create_listening_socket+0x22a>
jmp    1597 <create_listening_socket+0x3d7>
jmp    13ef <create_listening_socket+0x22f>
jmp    154d <create_listening_socket+0x38d>
mov    $0x81,%eax
cmp    -0x1c(%rbp),%eax
jne    147d <create_listening_socket+0x2bd>
mov    0x2bdf(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x60(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1478 <create_listening_socket+0x2b8>
mov    0x2bb7(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <get_ip_from_ifname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1451 <create_listening_socket+0x291>
jmp    1597 <create_listening_socket+0x3d7>
mov    0x2b90(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x60(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    14f6 <create_listening_socket+0x336>
mov    0x2b5c(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x88(%rbp),%rdx
call   10a0 <inet_pton@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    14f1 <create_listening_socket+0x331>
mov    0x2b35(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <get_ip_from_ifname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    14cb <create_listening_socket+0x30b>
jmp    1597 <create_listening_socket+0x3d7>
mov    0x2b0e(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x88(%rbp),%rdx
call   10a0 <inet_pton@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    14f6 <create_listening_socket+0x336>
mov    -0x28(%rbp),%eax
mov    %eax,-0xb0(%rbp)
sub    $0xffffffff,%eax
je     153e <create_listening_socket+0x37e>
jmp    150d <create_listening_socket+0x34d>
mov    -0xb0(%rbp),%eax
test   %eax,%eax
je     1539 <create_listening_socket+0x379>
jmp    1520 <create_listening_socket+0x360>
mov    -0xb0(%rbp),%eax
sub    $0x1,%eax
jne    1543 <create_listening_socket+0x383>
jmp    1534 <create_listening_socket+0x374>
jmp    1548 <create_listening_socket+0x388>
jmp    1597 <create_listening_socket+0x3d7>
jmp    1543 <create_listening_socket+0x383>
jmp    1597 <create_listening_socket+0x3d7>
jmp    154d <create_listening_socket+0x38d>
mov    -0x2c(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    -0xa0(%rbp),%rdx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     1572 <create_listening_socket+0x3b2>
jmp    1597 <create_listening_socket+0x3d7>
mov    -0x2c(%rbp),%edi
mov    0x2a54(%rip),%rax
mov    (%rax),%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     1592 <create_listening_socket+0x3d2>
jmp    1597 <create_listening_socket+0x3d7>
jmp    15b9 <create_listening_socket+0x3f9>
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
je     15b4 <create_listening_socket+0x3f4>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x2c(%rbp)
jmp    15b9 <create_listening_socket+0x3f9>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_listening_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    $0xffffffff,%r12d
test   %ecx,%ecx
je     11ea <create_listening_socket+0x2a>
mov    %r12d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r8d,%ebp
mov    %edx,%ebx
mov    %rdi,%r13
mov    %rsi,%rdi
call   1060 <htons@plt>
mov    %rax,%r15
lea    0x30(%rsp),%rdi
mov    %rdi,(%rsp)
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
cmp    $0x80,%ebx
je     124c <create_listening_socket+0x8c>
cmp    $0x81,%ebx
jne    11d8 <create_listening_socket+0x18>
mov    %r15,0x50(%rsp)
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x48(%rsp)
jmp    1264 <create_listening_socket+0xa4>
mov    %r15,0x18(%rsp)
mov    0x2d88(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %r14,(%rsp)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     11d8 <create_listening_socket+0x18>
mov    %eax,%r15d
mov    %eax,%edi
test   %ebp,%ebp
je     128f <create_listening_socket+0xcf>
call   1040 <set_socket_options_low_volume@plt>
test   %eax,%eax
jne    129c <create_listening_socket+0xdc>
jmp    13bd <create_listening_socket+0x1fd>
call   10c0 <set_socket_options@plt>
test   %eax,%eax
je     13bd <create_listening_socket+0x1fd>
lea    0xd61(%rip),%rdi
mov    %r13,%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     1300 <create_listening_socket+0x140>
cmp    $0x81,%ebx
jne    1320 <create_listening_socket+0x160>
mov    0x2d2a(%rip),%r14
mov    (%r14),%rdi
lea    0x34(%rsp),%rbx
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <inet_pton@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    136a <create_listening_socket+0x1aa>
mov    %rbx,%rbp
mov    (%r14),%rdi
mov    %r13,%rsi
call   1050 <get_ip_from_ifname@plt>
test   %rax,%rax
je     13bd <create_listening_socket+0x1fd>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
jmp    135b <create_listening_socket+0x19b>
cmp    $0x81,%ebx
jne    1371 <create_listening_socket+0x1b1>
mov    0x2cd9(%rip),%rax
mov    (%rax),%rdi
lea    0x34(%rsp),%rdx
lea    0xcf0(%rip),%rsi
jmp    1387 <create_listening_socket+0x1c7>
mov    0x2cb9(%rip),%r14
mov    (%r14),%rdi
lea    0x8(%rsp),%rdx
mov    %r13,%rsi
call   10a0 <inet_pton@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    136a <create_listening_socket+0x1aa>
mov    (%r14),%rdi
mov    %r13,%rsi
call   1050 <get_ip_from_ifname@plt>
test   %rax,%rax
je     13bd <create_listening_socket+0x1fd>
mov    %rax,%rbx
mov    (%r14),%rdi
lea    0x8(%rsp),%rdx
mov    %rax,%rsi
call   10a0 <inet_pton@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
cmp    $0x1,%ebp
je     1391 <create_listening_socket+0x1d1>
jmp    13bd <create_listening_socket+0x1fd>
mov    0x2c68(%rip),%rax
mov    (%rax),%rdi
lea    0xc96(%rip),%rsi
lea    0x8(%rsp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x1,%eax
jne    13bd <create_listening_socket+0x1fd>
mov    $0x28,%edx
mov    %r15d,%edi
mov    (%rsp),%rsi
call   10d0 <bind@plt>
test   %rax,%rax
jne    13bd <create_listening_socket+0x1fd>
mov    0x2c22(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   10b0 <listen@plt>
test   %rax,%rax
je     13ca <create_listening_socket+0x20a>
mov    %r15d,%edi
call   1080 <close@plt>
jmp    11d8 <create_listening_socket+0x18>
mov    %r15d,%r12d
jmp    11d8 <create_listening_socket+0x18>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i8*, i8*, i8*, i8* }
%struct.sockaddr_in6 = type { i32, i32, i8*, i8*, i8*, i8* }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = constant i32 0
@AF_INET = global i8* null
@AF_INET6 = global i8* null
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [4 x i8] c"::1\00"
@SOMAXCONN = global i32 0
define i32 @create_listening_socket(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca %struct.sockaddr_in
%16 = alloca %struct.sockaddr_in6
%17 = alloca %struct.sockaddr*
%18 = alloca i8*
%19 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i8* null, i8** %14
store i32 -1, i32* %13
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* %10
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = load i32, i32* %13
store i32 %24, i32* %6
br label %168
25: ; preds = %5
%26 = load i8*, i8** %8
%27 = call i8* @htons(i8* %26)
store i8* %27, i8** %18
%28 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr_in6*
%29 = call i32 @memset(%struct.sockaddr_in6* %28, i32 0, i32 40)
%30 = call i32 @memset(%struct.sockaddr_in6* %16, i32 0, i32 40)
%31 = load i32, i32* %9
switch i32 %31, label %44 [
i32 129, label %32
i32 128, label %38
]
32: ; preds = %25
%33 = load i8*, i8** %18
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 5
store i8* %33, i8** %34
%35 = load i8*, i8** @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 4
store i8* %35, i8** %36
%37 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
store %struct.sockaddr* %37, %struct.sockaddr** %17
store i64 40, i64* %19
br label %46
38: ; preds = %25
%39 = load i8*, i8** %18
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i32 0, i32 3
store i8* %39, i8** %40
%41 = load i8*, i8** @AF_INET6
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i32 0, i32 2
store i8* %41, i8** %42
%43 = bitcast %struct.sockaddr_in6* %16 to %struct.sockaddr*
store %struct.sockaddr* %43, %struct.sockaddr** %17
store i64 40, i64* %19
br label %46
44: ; preds = %25
%45 = load i32, i32* %13
store i32 %45, i32* %6
br label %168
46: ; preds = %38, %32
%47 = load i32, i32* %9
%48 = load i32, i32* %10
%49 = call i32 @socket(i32 %47, i32 %48, i32 0)
store i32 %49, i32* %13
%50 = load i32, i32* %13
%51 = icmp eq i32 -1, %50
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i32, i32* %13
store i32 %53, i32* %6
br label %168
54: ; preds = %46
%55 = load i32, i32* %11
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* %13
%59 = call i32 @set_socket_options_low_volume(i32 %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %57
br label %159
62: ; preds = %57
br label %69
63: ; preds = %54
%64 = load i32, i32* %13
%65 = call i32 @set_socket_options(i32 %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %63
br label %159
68: ; preds = %63
br label %69
69: ; preds = %68, %62
%70 = load i8*, i8** %7
%71 = call i32 @strcmp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %91, label %73
73: ; preds = %69
%74 = load i32, i32* %9
%75 = icmp eq i32 129, %74
br i1 %75, label %76, label %83
76: ; preds = %73
%77 = load i8*, i8** @AF_INET
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%79 = call i32 @inet_pton(i8* %77, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %78)
%80 = icmp ne i32 1, %79
br i1 %80, label %81, label %82
81: ; preds = %76
br label %159
82: ; preds = %76
br label %90
83: ; preds = %73
%84 = load i8*, i8** @AF_INET6
%85 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i32 0, i32 0
%86 = call i32 @inet_pton(i8* %84, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %85)
%87 = icmp ne i32 1, %86
br i1 %87, label %88, label %89
88: ; preds = %83
br label %159
89: ; preds = %83
br label %90
90: ; preds = %89, %82
br label %145
91: ; preds = %69
%92 = load i32, i32* %9
%93 = icmp eq i32 129, %92
br i1 %93, label %94, label %116
94: ; preds = %91
%95 = load i8*, i8** @AF_INET
%96 = load i8*, i8** %7
%97 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%98 = call i32 @inet_pton(i8* %95, i8* %96, i32* %97)
store i32 %98, i32* %12
%99 = load i32, i32* %12
%100 = icmp ne i32 %99, 0
br i1 %100, label %115, label %101
101: ; preds = %94
%102 = load i8*, i8** @AF_INET
%103 = load i8*, i8** %7
%104 = call i8* @get_ip_from_ifname(i8* %102, i8* %103)
store i8* %104, i8** %14
%105 = load i8*, i8** %14
%106 = icmp ne i8* %105, null
br i1 %106, label %108, label %107
107: ; preds = %101
br label %159
108: ; preds = %101
%109 = load i8*, i8** @AF_INET
%110 = load i8*, i8** %14
%111 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%112 = call i32 @inet_pton(i8* %109, i8* %110, i32* %111)
store i32 %112, i32* %12
%113 = load i8*, i8** %14
%114 = call i32 @free(i8* %113)
br label %115
115: ; preds = %108, %94
br label %138
116: ; preds = %91
%117 = load i8*, i8** @AF_INET6
%118 = load i8*, i8** %7
%119 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i32 0, i32 0
%120 = call i32 @inet_pton(i8* %117, i8* %118, i32* %119)
store i32 %120, i32* %12
%121 = load i32, i32* %12
%122 = icmp ne i32 %121, 0
br i1 %122, label %137, label %123
123: ; preds = %116
%124 = load i8*, i8** @AF_INET6
%125 = load i8*, i8** %7
%126 = call i8* @get_ip_from_ifname(i8* %124, i8* %125)
store i8* %126, i8** %14
%127 = load i8*, i8** %14
%128 = icmp ne i8* %127, null
br i1 %128, label %130, label %129
129: ; preds = %123
br label %159
130: ; preds = %123
%131 = load i8*, i8** @AF_INET6
%132 = load i8*, i8** %14
%133 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %16, i32 0, i32 0
%134 = call i32 @inet_pton(i8* %131, i8* %132, i32* %133)
store i32 %134, i32* %12
%135 = load i8*, i8** %14
%136 = call i32 @free(i8* %135)
br label %137
137: ; preds = %130, %116
br label %138
138: ; preds = %137, %115
%139 = load i32, i32* %12
switch i32 %139, label %143 [
i32 1, label %140
i32 0, label %141
i32 -1, label %142
]
140: ; preds = %138
br label %144
141: ; preds = %138
br label %159
142: ; preds = %138
br label %143
143: ; preds = %138, %142
br label %159
144: ; preds = %140
br label %145
145: ; preds = %144, %90
%146 = load i32, i32* %13
%147 = load %struct.sockaddr*, %struct.sockaddr** %17
%148 = load i64, i64* %19
%149 = call i64 @bind(i32 %146, %struct.sockaddr* %147, i64 %148)
%150 = icmp ne i64 %149, 0
br i1 %150, label %151, label %152
151: ; preds = %145
br label %159
152: ; preds = %145
%153 = load i32, i32* %13
%154 = load i32, i32* @SOMAXCONN
%155 = call i64 @listen(i32 %153, i32 %154)
%156 = icmp ne i64 %155, 0
br i1 %156, label %157, label %158
157: ; preds = %152
br label %159
158: ; preds = %152
br label %166
159: ; preds = %157, %151, %143, %141, %129, %107, %88, %81, %67, %61
%160 = load i32, i32* %13
%161 = icmp ne i32 -1, %160
br i1 %161, label %162, label %165
162: ; preds = %159
%163 = load i32, i32* %13
%164 = call i32 @close(i32 %163)
store i32 -1, i32* %13
br label %165
165: ; preds = %162, %159
br label %166
166: ; preds = %165, %158
%167 = load i32, i32* %13
store i32 %167, i32* %6
br label %168
168: ; preds = %166, %52, %44, %23
%169 = load i32, i32* %6
ret i32 %169
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i8*, i8*, i8*, i8* }
%struct.sockaddr_in6 = type { i32, i32, i8*, i8*, i8*, i8* }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = constant i32 0
@AF_INET = global i8* null
@AF_INET6 = global i8* null
@.str = [10 x i8] c"localhost\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [4 x i8] c"::1\00"
@SOMAXCONN = global i32 0
define i32 @create_listening_socket(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in6
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = bitcast %struct.sockaddr_in6* %7 to i8*
%10 = icmp eq i32 %3, 0
br i1 %10, label %11, label %94
11: ; preds = %5
%12 = tail call i8* @htons(i8* %1)
%13 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr_in6*
%14 = call i32 @memset(%struct.sockaddr_in6* %13, i32 0, i32 40)
%15 = call i32 @memset(%struct.sockaddr_in6* %7, i32 0, i32 40)
switch i32 %2, label %94 [
i32 129, label %16
i32 128, label %21
]
16: ; preds = %11
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 5
store i8* %12, i8** %17
%18 = load i8*, i8** @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 4
store i8* %18, i8** %19
%20 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %26
21: ; preds = %11
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 3
store i8* %12, i8** %22
%23 = load i8*, i8** @AF_INET6
%24 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 2
store i8* %23, i8** %24
%25 = bitcast %struct.sockaddr_in6* %7 to %struct.sockaddr*
br label %26
26: ; preds = %21, %16
%27 = phi %struct.sockaddr* [ %25, %21 ], [ %20, %16 ]
%28 = call i32 @socket(i32 %2, i32 0, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %94, label %30
30: ; preds = %26
%31 = icmp eq i32 %4, 0
br i1 %31, label %35, label %32
32: ; preds = %30
%33 = call i32 @set_socket_options_low_volume(i32 %28)
%34 = icmp eq i32 %33, 0
br i1 %34, label %92, label %38
35: ; preds = %30
%36 = call i32 @set_socket_options(i32 %28)
%37 = icmp eq i32 %36, 0
br i1 %37, label %92, label %38
38: ; preds = %35, %32
%39 = call i32 @strcmp(i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(1) %0)
%40 = icmp eq i32 %39, 0
%41 = icmp eq i32 %2, 129
br i1 %40, label %42, label %53
42: ; preds = %38
br i1 %41, label %43, label %48
43: ; preds = %42
%44 = load i8*, i8** @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%46 = call i32 @inet_pton(i8* %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %45)
%47 = icmp eq i32 %46, 1
br i1 %47, label %85, label %92
48: ; preds = %42
%49 = load i8*, i8** @AF_INET6
%50 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 0
%51 = call i32 @inet_pton(i8* %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %50)
%52 = icmp eq i32 %51, 1
br i1 %52, label %85, label %92
53: ; preds = %38
br i1 %41, label %54, label %66
54: ; preds = %53
%55 = load i8*, i8** @AF_INET
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%57 = call i32 @inet_pton(i8* %55, i8* %0, i32* %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %82
59: ; preds = %54
%60 = load i8*, i8** @AF_INET
%61 = call i8* @get_ip_from_ifname(i8* %60, i8* %0)
%62 = icmp eq i8* %61, null
br i1 %62, label %92, label %63
63: ; preds = %59
%64 = load i8*, i8** @AF_INET
%65 = call i32 @inet_pton(i8* %64, i8* %61, i32* %56)
br label %78
66: ; preds = %53
%67 = load i8*, i8** @AF_INET6
%68 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 0
%69 = call i32 @inet_pton(i8* %67, i8* %0, i32* %68)
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %82
71: ; preds = %66
%72 = load i8*, i8** @AF_INET6
%73 = call i8* @get_ip_from_ifname(i8* %72, i8* %0)
%74 = icmp eq i8* %73, null
br i1 %74, label %92, label %75
75: ; preds = %71
%76 = load i8*, i8** @AF_INET6
%77 = call i32 @inet_pton(i8* %76, i8* %73, i32* %68)
br label %78
78: ; preds = %63, %75
%79 = phi i8* [ %73, %75 ], [ %61, %63 ]
%80 = phi i32 [ %77, %75 ], [ %65, %63 ]
%81 = call i32 @free(i8* %79)
br label %82
82: ; preds = %78, %66, %54
%83 = phi i32 [ %57, %54 ], [ %69, %66 ], [ %80, %78 ]
%84 = icmp eq i32 %83, 1
br i1 %84, label %85, label %92
85: ; preds = %82, %43, %48
%86 = call i64 @bind(i32 %28, %struct.sockaddr* %27, i64 40)
%87 = icmp eq i64 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %85
%89 = load i32, i32* @SOMAXCONN
%90 = call i64 @listen(i32 %28, i32 %89)
%91 = icmp eq i64 %90, 0
br i1 %91, label %94, label %92
92: ; preds = %32, %35, %43, %48, %59, %71, %85, %82, %88
%93 = call i32 @close(i32 %28)
br label %94
94: ; preds = %88, %92, %26, %11, %5
%95 = phi i32 [ -1, %5 ], [ -1, %11 ], [ -1, %26 ], [ -1, %92 ], [ %28, %88 ]
ret i32 %95
}