#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 {
  scalar_t__ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ portstr;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int make_listen_socket(struct addrinfo *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int snprintf(char *, int, char *, int);
int get_listen_socket(const char *addr, int port, int *s, int *ipv6) {
  char portstr[8];
  struct addrinfo hints;
  struct addrinfo *result;
  struct addrinfo *iter;
  struct addrinfo *ipv4_addr;
  struct addrinfo *ipv6_addr;
  int ret;
  int fd;
  int ipv6_flag;
  assert(s != NULL);
  assert(ipv6 != NULL);
  ipv4_addr = NULL;
  ipv6_addr = NULL;
  fd = -1;
  ipv6_flag = 0;
  if (port <= 0 || port > 65535)
    return -1;
  ret = snprintf(portstr, sizeof(portstr), "%d", port);
  assert(ret > 0);
  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_PASSIVE;
  result = NULL;
  ret = getaddrinfo(addr, portstr, &hints, &result);
  if (ret != 0)
    return -1;
  for (iter = result; iter != NULL; iter = iter->ai_next) {
    if (iter->ai_family == AF_INET6)
      ipv6_addr = iter;
    else if (iter->ai_family == AF_INET)
      ipv4_addr = iter;
  }
  if (ipv6_addr != NULL) {
    fd = make_listen_socket(ipv6_addr);
    if (fd != -1)
      ipv6_flag = 1;
  }
  if (fd == -1 && ipv4_addr != NULL)
    fd = make_listen_socket(ipv4_addr);
  freeaddrinfo(result);
  if (fd == -1)
    return -1;
  *s = fd;
  *ipv6 = ipv6_flag;
  return 0;
}


x86 assembly (clang, -O0):
<get_listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
movl   $0xffffffff,-0x70(%rbp)
movl   $0x0,-0x74(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11d3 <get_listen_socket+0x73>
cmpl   $0xffff,-0x14(%rbp)
jle    11df <get_listen_socket+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <get_listen_socket+0x203>
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x8,%esi
lea    0xe0e(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
je     126c <get_listen_socket+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <get_listen_socket+0x203>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12dd <get_listen_socket+0x17d>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    12a3 <get_listen_socket+0x143>
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    12c7 <get_listen_socket+0x167>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
jne    12c2 <get_listen_socket+0x162>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    12c7 <get_listen_socket+0x167>
jmp    12cc <get_listen_socket+0x16c>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1274 <get_listen_socket+0x114>
cmpq   $0x0,-0x68(%rbp)
je     130a <get_listen_socket+0x1aa>
mov    -0x68(%rbp),%rdi
call   1030 <make_listen_socket@plt>
mov    %eax,-0x70(%rbp)
cmpl   $0xffffffff,-0x70(%rbp)
je     1305 <get_listen_socket+0x1a5>
movl   $0x1,-0x74(%rbp)
jmp    130a <get_listen_socket+0x1aa>
cmpl   $0xffffffff,-0x70(%rbp)
jne    132b <get_listen_socket+0x1cb>
cmpq   $0x0,-0x60(%rbp)
je     132b <get_listen_socket+0x1cb>
mov    -0x60(%rbp),%rdi
call   1030 <make_listen_socket@plt>
mov    %eax,-0x70(%rbp)
mov    -0x50(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpl   $0xffffffff,-0x70(%rbp)
jne    134a <get_listen_socket+0x1ea>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <get_listen_socket+0x203>
mov    -0x70(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x74(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_listen_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r13
xor    %edi,%edi
test   %rdx,%rdx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
lea    -0x10000(%rbx),%eax
mov    $0xffffffff,%ebp
cmp    $0xffff0001,%eax
jb     121a <get_listen_socket+0xba>
lea    0xe52(%rip),%rdx
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    $0x8,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
or     %eax,0x18(%rsp)
movq   $0x0,(%rsp)
mov    %rsp,%rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     122b <get_listen_socket+0xcb>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
test   %rax,%rax
je     1291 <get_listen_socket+0x131>
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2d9b(%rip),%rdx
mov    (%rdx),%rdx
xor    %edi,%edi
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %rbx,%rsi
mov    (%rax),%rbp
cmp    %rdx,%rbp
cmove  %rax,%rbx
cmp    %rcx,%rbp
cmove  %rax,%rdi
mov    0x8(%rax),%rbp
cmove  %rsi,%rbx
mov    %rbp,%rax
test   %rbp,%rbp
jne    1250 <get_listen_socket+0xf0>
test   %rdi,%rdi
je     129f <get_listen_socket+0x13f>
call   1030 <make_listen_socket@plt>
mov    %eax,%ebp
xor    %r12d,%r12d
cmp    $0xffffffff,%eax
setne  %r12b
cmp    $0xffffffff,%ebp
je     12ac <get_listen_socket+0x14c>
jmp    12bb <get_listen_socket+0x15b>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
jmp    121a <get_listen_socket+0xba>
xor    %r12d,%r12d
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%ebp
jne    12bb <get_listen_socket+0x15b>
test   %rbx,%rbx
je     12bb <get_listen_socket+0x15b>
mov    %rbx,%rdi
call   1030 <make_listen_socket@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
cmp    $0xffffffff,%ebp
je     12d6 <get_listen_socket+0x176>
mov    %ebp,(%r15)
mov    %r12d,(%r14)
xor    %ebp,%ebp
jmp    121a <get_listen_socket+0xba>
mov    $0xffffffff,%ebp
jmp    121a <get_listen_socket+0xba>

LLVM IR (-O0):
%struct.addrinfo = type { i64, %struct.addrinfo*, i32, i32 }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define i32 @get_listen_socket(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [8 x i8]
%11 = alloca %struct.addrinfo
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.addrinfo*
%15 = alloca %struct.addrinfo*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32*, i32** %9
%24 = icmp ne i32* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
store %struct.addrinfo* null, %struct.addrinfo** %14
store %struct.addrinfo* null, %struct.addrinfo** %15
store i32 -1, i32* %17
store i32 0, i32* %18
%27 = load i32, i32* %7
%28 = icmp sle i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %4
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 65535
br i1 %31, label %32, label %33
32: ; preds = %29, %4
store i32 -1, i32* %5
br label %111
33: ; preds = %29
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%35 = load i32, i32* %7
%36 = call i32 @snprintf(i8* %34, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 %36, i32* %16
%37 = load i32, i32* %16
%38 = icmp sgt i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = call i32 @memset(%struct.addrinfo* %11, i32 0, i32 24)
%42 = load i32, i32* @SOCK_STREAM
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 3
store i32 %42, i32* %43
%44 = load i32, i32* @AI_PASSIVE
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 2
%46 = load i32, i32* %45
%47 = or i32 %46, %44
store i32 %47, i32* %45
store %struct.addrinfo* null, %struct.addrinfo** %12
%48 = load i8*, i8** %6
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%50 = call i32 @getaddrinfo(i8* %48, i8* %49, %struct.addrinfo* %11, %struct.addrinfo** %12)
store i32 %50, i32* %16
%51 = load i32, i32* %16
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %33
store i32 -1, i32* %5
br label %111
54: ; preds = %33
%55 = load %struct.addrinfo*, %struct.addrinfo** %12
store %struct.addrinfo* %55, %struct.addrinfo** %13
br label %56
56: ; preds = %77, %54
%57 = load %struct.addrinfo*, %struct.addrinfo** %13
%58 = icmp ne %struct.addrinfo* %57, null
br i1 %58, label %59, label %81
59: ; preds = %56
%60 = load %struct.addrinfo*, %struct.addrinfo** %13
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 0
%62 = load i64, i64* %61
%63 = load i64, i64* @AF_INET6
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %66, %struct.addrinfo** %15
br label %76
67: ; preds = %59
%68 = load %struct.addrinfo*, %struct.addrinfo** %13
%69 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %68, i32 0, i32 0
%70 = load i64, i64* %69
%71 = load i64, i64* @AF_INET
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %75
73: ; preds = %67
%74 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %74, %struct.addrinfo** %14
br label %75
75: ; preds = %73, %67
br label %76
76: ; preds = %75, %65
br label %77
77: ; preds = %76
%78 = load %struct.addrinfo*, %struct.addrinfo** %13
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %78, i32 0, i32 1
%80 = load %struct.addrinfo*, %struct.addrinfo** %79
store %struct.addrinfo* %80, %struct.addrinfo** %13
br label %56
81: ; preds = %56
%82 = load %struct.addrinfo*, %struct.addrinfo** %15
%83 = icmp ne %struct.addrinfo* %82, null
br i1 %83, label %84, label %91
84: ; preds = %81
%85 = load %struct.addrinfo*, %struct.addrinfo** %15
%86 = call i32 @make_listen_socket(%struct.addrinfo* %85)
store i32 %86, i32* %17
%87 = load i32, i32* %17
%88 = icmp ne i32 %87, -1
br i1 %88, label %89, label %90
89: ; preds = %84
store i32 1, i32* %18
br label %90
90: ; preds = %89, %84
br label %91
91: ; preds = %90, %81
%92 = load i32, i32* %17
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %100
94: ; preds = %91
%95 = load %struct.addrinfo*, %struct.addrinfo** %14
%96 = icmp ne %struct.addrinfo* %95, null
br i1 %96, label %97, label %100
97: ; preds = %94
%98 = load %struct.addrinfo*, %struct.addrinfo** %14
%99 = call i32 @make_listen_socket(%struct.addrinfo* %98)
store i32 %99, i32* %17
br label %100
100: ; preds = %97, %94, %91
%101 = load %struct.addrinfo*, %struct.addrinfo** %12
%102 = call i32 @freeaddrinfo(%struct.addrinfo* %101)
%103 = load i32, i32* %17
%104 = icmp eq i32 %103, -1
br i1 %104, label %105, label %106
105: ; preds = %100
store i32 -1, i32* %5
br label %111
106: ; preds = %100
%107 = load i32, i32* %17
%108 = load i32*, i32** %8
store i32 %107, i32* %108
%109 = load i32, i32* %18
%110 = load i32*, i32** %9
store i32 %109, i32* %110
store i32 0, i32* %5
br label %111
111: ; preds = %106, %105, %53, %32
%112 = load i32, i32* %5
ret i32 %112
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, %struct.addrinfo*, i32, i32 }
@.str = [3 x i8] c"%d\00"
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET6 = global i64 0
@AF_INET = global i64 0
define i32 @get_listen_socket(i8* %0, i32 %1, i32* writeonly %2, i32* writeonly %3) {
%5 = alloca [8 x i8]
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%9 = bitcast %struct.addrinfo* %6 to i8*
%10 = bitcast %struct.addrinfo** %7 to i8*
%11 = icmp ne i32* %2, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = icmp ne i32* %3, null
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = add i32 %1, -65536
%18 = icmp ult i32 %17, -65535
br i1 %18, label %76, label %19
19: ; preds = %4
%20 = call i32 @snprintf(i8* %8, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%21 = icmp sgt i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%25 = load i32, i32* @SOCK_STREAM
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @AI_PASSIVE
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 2
%29 = load i32, i32* %28
%30 = or i32 %29, %27
store i32 %30, i32* %28
store %struct.addrinfo* null, %struct.addrinfo** %7
%31 = call i32 @getaddrinfo(i8* %0, i8* %8, %struct.addrinfo* %6, %struct.addrinfo** %7)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %76
33: ; preds = %19
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = icmp eq %struct.addrinfo* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
br label %76
39: ; preds = %33
%40 = load i64, i64* @AF_INET6
%41 = load i64, i64* @AF_INET
br label %42
42: ; preds = %39, %42
%43 = phi %struct.addrinfo* [ %34, %39 ], [ %54, %42 ]
%44 = phi %struct.addrinfo* [ null, %39 ], [ %52, %42 ]
%45 = phi %struct.addrinfo* [ null, %39 ], [ %51, %42 ]
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 0
%47 = load i64, i64* %46
%48 = icmp eq i64 %47, %40
%49 = icmp eq i64 %47, %41
%50 = select i1 %49, %struct.addrinfo* %43, %struct.addrinfo* %45
%51 = select i1 %48, %struct.addrinfo* %45, %struct.addrinfo* %50
%52 = select i1 %48, %struct.addrinfo* %43, %struct.addrinfo* %44
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 1
%54 = load %struct.addrinfo*, %struct.addrinfo** %53
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %42
56: ; preds = %42
%57 = icmp eq %struct.addrinfo* %52, null
br i1 %57, label %62, label %58
58: ; preds = %56
%59 = call i32 @make_listen_socket(%struct.addrinfo* %52)
%60 = icmp ne i32 %59, -1
%61 = zext i1 %60 to i32
br label %62
62: ; preds = %58, %56
%63 = phi i32 [ -1, %56 ], [ %59, %58 ]
%64 = phi i32 [ 0, %56 ], [ %61, %58 ]
%65 = icmp eq i32 %63, -1
%66 = icmp ne %struct.addrinfo* %51, null
%67 = select i1 %65, i1 %66, i1 false
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @make_listen_socket(%struct.addrinfo* %51)
br label %70
70: ; preds = %68, %62
%71 = phi i32 [ %69, %68 ], [ %63, %62 ]
%72 = load %struct.addrinfo*, %struct.addrinfo** %7
%73 = call i32 @freeaddrinfo(%struct.addrinfo* %72)
%74 = icmp eq i32 %71, -1
br i1 %74, label %76, label %75
75: ; preds = %70
store i32 %71, i32* %2
store i32 %64, i32* %3
br label %76
76: ; preds = %36, %70, %19, %4, %75
%77 = phi i32 [ 0, %75 ], [ -1, %4 ], [ -1, %19 ], [ -1, %70 ], [ -1, %36 ]
ret i32 %77
}