#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 {
  int dummy;
};
struct addrinfo {
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hint;
typedef int /*<<< orphan*/ enable;

/* Variables and functions */
int /*<<< orphan*/ AI_NUMERICHOST;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_REUSEPORT;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int make_listen_socket(const char *addr, const char *port) {
  struct addrinfo hint;
  struct addrinfo *res;
  int ret;
  memset(&hint, 0, sizeof(hint));
  hint.ai_family = PF_UNSPEC;
  hint.ai_flags = AI_NUMERICHOST;
  ret = getaddrinfo(addr, port, &hint, &res);
  if (ret) {
    fprintf(stderr, "couldn't get addrinfo for addr and port");
    exit(1);
  }
  int listen_fd = 0;
  if ((listen_fd = socket(res->ai_family, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "failed creating socket\n");
    exit(1);
  }
  int enable = 1;
  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <
      0) {
    fprintf(stderr, "error setting SO_REUSEADDR");
    exit(1);
  }
  if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable)) <
      0) {
    fprintf(stderr, "error setting SO_REUSEPORT");
    exit(1);
  }
  if (bind(listen_fd, (struct sockaddr *)res->ai_addr, res->ai_addrlen) < 0) {
    fprintf(stderr, "can't bind the foo\n");
    exit(1);
  }
  do {
    struct addrinfo *next = res->ai_next;
    free(res);
    res = next;
  } while (res);
  return listen_fd;
}


x86 assembly (clang, -O0):
<make_listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11f9 <make_listen_socket+0x79>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    0x18(%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jge    1242 <make_listen_socket+0xc2>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x1,-0x44(%rbp)
mov    -0x40(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    1296 <make_listen_socket+0x116>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    12e3 <make_listen_socket+0x163>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1323 <make_listen_socket+0x1a3>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1328 <make_listen_socket+0x1a8>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1328 <make_listen_socket+0x1a8>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_listen_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    12a2 <make_listen_socket+0x122>
mov    0x10(%rsp),%rax
mov    0x18(%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     12b4 <make_listen_socket+0x134>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2dde(%rip),%r14
mov    (%r14),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     12c6 <make_listen_socket+0x146>
mov    (%r14),%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     12d8 <make_listen_socket+0x158>
mov    0x10(%rsp),%rax
mov    0x10(%rax),%rsi
mov    0x8(%rax),%edx
mov    %ebp,%edi
call   1070 <bind@plt>
test   %rax,%rax
js     12ea <make_listen_socket+0x16a>
mov    0x10(%rsp),%rdi
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rdi),%rbx
call   1030 <free@plt>
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
test   %rbx,%rbx
jne    1280 <make_listen_socket+0x100>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
jmp    12fa <make_listen_socket+0x17a>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
jmp    12fa <make_listen_socket+0x17a>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
jmp    12fa <make_listen_socket+0x17a>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
jmp    12fa <make_listen_socket+0x17a>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { %struct.addrinfo*, i32, i64, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNSPEC = global i32 0
@AI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"couldn't get addrinfo for addr and port\00"
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"failed creating socket\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [27 x i8] c"error setting SO_REUSEADDR\00"
@SO_REUSEPORT = global i32 0
@.str.3 = [27 x i8] c"error setting SO_REUSEPORT\00"
@.str.4 = [20 x i8] c"can't bind the foo\0A\00"
define i32 @make_listen_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
%9 = alloca i32
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%12 = load i32, i32* @PF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_NUMERICHOST
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = call i32 @getaddrinfo(i8* %16, i8* %17, %struct.addrinfo* %5, %struct.addrinfo** %6)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
store i32 0, i32* %8
%26 = load %struct.addrinfo*, %struct.addrinfo** %6
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 3
%28 = load i32, i32* %27
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
store i32 1, i32* %9
%37 = load i32, i32* %8
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_REUSEADDR
%40 = call i64 @setsockopt(i32 %37, i32 %38, i32 %39, i32* %9, i32 4)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = load i32, i32* %8
%48 = load i32, i32* @SOL_SOCKET
%49 = load i32, i32* @SO_REUSEPORT
%50 = call i64 @setsockopt(i32 %47, i32 %48, i32 %49, i32* %9, i32 4)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %46
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %46
%57 = load i32, i32* %8
%58 = load %struct.addrinfo*, %struct.addrinfo** %6
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 2
%60 = load i64, i64* %59
%61 = inttoptr i64 %60 to %struct.sockaddr*
%62 = load %struct.addrinfo*, %struct.addrinfo** %6
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 1
%64 = load i32, i32* %63
%65 = call i64 @bind(i32 %57, %struct.sockaddr* %61, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %56
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %56
br label %72
72: ; preds = %79, %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %6
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 0
%75 = load %struct.addrinfo*, %struct.addrinfo** %74
store %struct.addrinfo* %75, %struct.addrinfo** %10
%76 = load %struct.addrinfo*, %struct.addrinfo** %6
%77 = call i32 @free(%struct.addrinfo* %76)
%78 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %78, %struct.addrinfo** %6
br label %79
79: ; preds = %72
%80 = load %struct.addrinfo*, %struct.addrinfo** %6
%81 = icmp ne %struct.addrinfo* %80, null
br i1 %81, label %72, label %82
82: ; preds = %79
%83 = load i32, i32* %8
ret i32 %83
}

LLVM IR (-O2):
%struct.addrinfo = type { %struct.addrinfo*, i32, i64, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNSPEC = global i32 0
@AI_NUMERICHOST = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"couldn't get addrinfo for addr and port\00"
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"failed creating socket\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [27 x i8] c"error setting SO_REUSEADDR\00"
@SO_REUSEPORT = global i32 0
@.str.3 = [27 x i8] c"error setting SO_REUSEPORT\00"
@.str.4 = [20 x i8] c"can't bind the foo\0A\00"
define i32 @make_listen_socket(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 32)
%9 = load i32, i32* @PF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_NUMERICHOST
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%22 = load i32, i32* %21
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i64 @setsockopt(i32 %24, i32 %32, i32 %33, i32* %5, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEPORT
%43 = call i64 @setsockopt(i32 %24, i32 %41, i32 %42, i32* %5, i32 4)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
%50 = load %struct.addrinfo*, %struct.addrinfo** %4
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i64 0, i32 2
%52 = load i64, i64* %51
%53 = inttoptr i64 %52 to %struct.sockaddr*
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i64 0, i32 1
%55 = load i32, i32* %54
%56 = call i64 @bind(i32 %24, %struct.sockaddr* %53, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %49
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %64
60: ; preds = %49
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %58, %64
%65 = phi %struct.addrinfo* [ %59, %58 ], [ %67, %64 ]
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i64 0, i32 0
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
%68 = call i32 @free(%struct.addrinfo* %65)
store %struct.addrinfo* %67, %struct.addrinfo** %4
%69 = icmp eq %struct.addrinfo* %67, null
br i1 %69, label %70, label %64
70: ; preds = %64
ret i32 %24
}