#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 ai_socktype;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int set_reuseport(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int tcp_listen_fd(const char *addr, int port, int backlog) {
  int ret, listen_fd;
  char _port[6];
  struct addrinfo hints, *res, *res_save;
  snprintf(_port, 6, "%d", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(addr, _port, &hints, &res);
  do {
    if ((ret != 0)) {
      fprintf(stderr, "[ERROR in %s:%d]: %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/zhaolei1023/muse/refs/heads/master/"
              "src/net.c",
              60, (gai_strerror(ret)));
      return ((-1));
    }
  } while (0);
  for (res_save = res; res; res = res->ai_next) {
    listen_fd = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
                       res->ai_protocol);
    if (listen_fd == -1)
      continue;
    if (set_reuseport(listen_fd) == (-1)) {
      close(listen_fd);
      continue;
    }
    if (bind(listen_fd, res->ai_addr, res->ai_addrlen) == -1) {
      close(listen_fd);
      continue;
    }
    if (listen(listen_fd, backlog) == -1) {
      close(listen_fd);
      continue;
    }
    break;
  }
  freeaddrinfo(res_save);
  return res ? listen_fd : (-1);
}


x86 assembly (clang, -O0):
<tcp_listen_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x26(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xe2b(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x26(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     126e <tcp_listen_fd+0xbe>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%r8
lea    0xdb2(%rip),%rsi
lea    0xdc1(%rip),%rdx
mov    $0x3c,%ecx
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136d <tcp_listen_fd+0x1bd>
jmp    1273 <tcp_listen_fd+0xc3>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     133b <tcp_listen_fd+0x18b>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
or     (%rax),%esi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12ba <tcp_listen_fd+0x10a>
jmp    132a <tcp_listen_fd+0x17a>
mov    -0x20(%rbp),%edi
call   1080 <set_reuseport@plt>
cmp    $0xffffffff,%eax
jne    12d8 <tcp_listen_fd+0x128>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    132a <tcp_listen_fd+0x17a>
mov    -0x20(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1304 <tcp_listen_fd+0x154>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    132a <tcp_listen_fd+0x17a>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1325 <tcp_listen_fd+0x175>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
jmp    132a <tcp_listen_fd+0x17a>
jmp    133b <tcp_listen_fd+0x18b>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    127b <tcp_listen_fd+0xcb>
mov    -0x60(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
je     135a <tcp_listen_fd+0x1aa>
mov    -0x20(%rbp),%eax
mov    %eax,-0x68(%rbp)
jmp    1367 <tcp_listen_fd+0x1b7>
mov    $0xffffffff,%eax
mov    %eax,-0x68(%rbp)
jmp    1367 <tcp_listen_fd+0x1b7>
mov    -0x68(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %esi,%ecx
mov    %rdi,%rbp
lea    0xe37(%rip),%rdx
lea    0xa(%rsp),%r14
mov    %r14,%rdi
mov    $0x6,%esi
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
je     1262 <tcp_listen_fd+0xb2>
mov    0x2d96(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdc1(%rip),%rsi
lea    0xdd0(%rip),%rdx
mov    %ebx,%edi
mov    $0x3c,%ecx
mov    %rax,%r8
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12f2 <tcp_listen_fd+0x142>
mov    (%rsp),%r14
test   %r14,%r14
je     12e1 <tcp_listen_fd+0x131>
mov    0x2d5e(%rip),%rbx
mov    %r14,%rax
jmp    1298 <tcp_listen_fd+0xe8>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%rax
mov    0x18(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     12e1 <tcp_listen_fd+0x131>
mov    0xc(%rax),%edx
mov    0x10(%rax),%edi
mov    (%rbx),%esi
or     (%rax),%esi
call   10d0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1287 <tcp_listen_fd+0xd7>
mov    %ebp,%edi
call   1080 <set_reuseport@plt>
cmp    $0xffffffff,%eax
je     1280 <tcp_listen_fd+0xd0>
mov    (%rsp),%rax
mov    0x4(%rax),%edx
mov    0x8(%rax),%esi
mov    %ebp,%edi
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1280 <tcp_listen_fd+0xd0>
mov    %ebp,%edi
mov    %r15d,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     1280 <tcp_listen_fd+0xd0>
jmp    12e1 <tcp_listen_fd+0x131>
mov    %r14,%rdi
call   10c0 <freeaddrinfo@plt>
cmpq   $0x1,(%rsp)
sbb    %eax,%eax
or     %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@.str = [3 x i8] c"%d\00"
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"[ERROR in %s:%d]: %s\0A\00"
@.str.2 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zhaolei1023/muse/refs/heads/master/src/net.c\00"
@SOCK_NONBLOCK = global i32 0
define i32 @tcp_listen_fd(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [6 x i8]
%11 = alloca %struct.addrinfo
%12 = alloca %struct.addrinfo*
%13 = alloca %struct.addrinfo*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 0
%15 = load i32, i32* %6
%16 = call i32 @snprintf(i8* %14, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32 @memset(%struct.addrinfo* %11, i32 0, i32 40)
%18 = load i32, i32* @AI_PASSIVE
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 6
store i32 %18, i32* %19
%20 = load i32, i32* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 4
store i32 %20, i32* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %5
%25 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 0
%26 = call i32 @getaddrinfo(i8* %24, i8* %25, %struct.addrinfo* %11, %struct.addrinfo** %12)
store i32 %26, i32* %8
br label %27
27: ; preds = %3
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i32, i32* %8
%33 = call i8* @gai_strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.2, i64 0, i64 0), i32 60, i8* %33)
store i32 -1, i32* %4
br label %100
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %12
store %struct.addrinfo* %37, %struct.addrinfo** %13
br label %38
38: ; preds = %86, %36
%39 = load %struct.addrinfo*, %struct.addrinfo** %12
%40 = icmp ne %struct.addrinfo* %39, null
br i1 %40, label %41, label %90
41: ; preds = %38
%42 = load %struct.addrinfo*, %struct.addrinfo** %12
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 4
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %12
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i32, i32* @SOCK_NONBLOCK
%49 = or i32 %47, %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %12
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 3
%52 = load i32, i32* %51
%53 = call i32 @socket(i32 %44, i32 %49, i32 %52)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %57
56: ; preds = %41
br label %86
57: ; preds = %41
%58 = load i32, i32* %9
%59 = call i32 @set_reuseport(i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
br label %86
64: ; preds = %57
%65 = load i32, i32* %9
%66 = load %struct.addrinfo*, %struct.addrinfo** %12
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 2
%68 = load i32, i32* %67
%69 = load %struct.addrinfo*, %struct.addrinfo** %12
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 1
%71 = load i32, i32* %70
%72 = call i32 @bind(i32 %65, i32 %68, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %64
%75 = load i32, i32* %9
%76 = call i32 @close(i32 %75)
br label %86
77: ; preds = %64
%78 = load i32, i32* %9
%79 = load i32, i32* %7
%80 = call i32 @listen(i32 %78, i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %85
82: ; preds = %77
%83 = load i32, i32* %9
%84 = call i32 @close(i32 %83)
br label %86
85: ; preds = %77
br label %90
86: ; preds = %82, %74, %61, %56
%87 = load %struct.addrinfo*, %struct.addrinfo** %12
%88 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %87, i32 0, i32 5
%89 = load %struct.addrinfo*, %struct.addrinfo** %88
store %struct.addrinfo* %89, %struct.addrinfo** %12
br label %38
90: ; preds = %85, %38
%91 = load %struct.addrinfo*, %struct.addrinfo** %13
%92 = call i32 @freeaddrinfo(%struct.addrinfo* %91)
%93 = load %struct.addrinfo*, %struct.addrinfo** %12
%94 = icmp ne %struct.addrinfo* %93, null
br i1 %94, label %95, label %97
95: ; preds = %90
%96 = load i32, i32* %9
br label %98
97: ; preds = %90
br label %98
98: ; preds = %97, %95
%99 = phi i32 [ %96, %95 ], [ -1, %97 ]
store i32 %99, i32* %4
br label %100
100: ; preds = %98, %30
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@.str = [3 x i8] c"%d\00"
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"[ERROR in %s:%d]: %s\0A\00"
@.str.2 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zhaolei1023/muse/refs/heads/master/src/net.c\00"
@SOCK_NONBLOCK = global i32 0
define i32 @tcp_listen_fd(i8* %0, i32 %1, i32 %2) {
%4 = alloca [6 x i8]
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = bitcast %struct.addrinfo** %6 to i8*
%10 = call i32 @snprintf(i8* %7, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%11 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 40)
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 6
store i32 %12, i32* %13
%14 = load i32, i32* @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %16, i32* %17
%18 = call i32 @getaddrinfo(i8* %0, i8* %7, %struct.addrinfo* %5, %struct.addrinfo** %6)
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = call i8* @gai_strerror(i32 %18)
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.2, i64 0, i64 0), i32 60, i8* %22)
br label %66
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %60, label %27
27: ; preds = %24, %55
%28 = phi %struct.addrinfo* [ %58, %55 ], [ %25, %24 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%32 = load i32, i32* %31
%33 = load i32, i32* @SOCK_NONBLOCK
%34 = or i32 %33, %32
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %55, label %39
39: ; preds = %27
%40 = call i32 @set_reuseport(i32 %37)
%41 = icmp eq i32 %40, -1
br i1 %41, label %53, label %42
42: ; preds = %39
%43 = load %struct.addrinfo*, %struct.addrinfo** %6
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 1
%47 = load i32, i32* %46
%48 = call i32 @bind(i32 %37, i32 %45, i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %53, label %50
50: ; preds = %42
%51 = call i32 @listen(i32 %37, i32 %2)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %60
53: ; preds = %50, %42, %39
%54 = call i32 @close(i32 %37)
br label %55
55: ; preds = %53, %27
%56 = load %struct.addrinfo*, %struct.addrinfo** %6
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i64 0, i32 5
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
store %struct.addrinfo* %58, %struct.addrinfo** %6
%59 = icmp eq %struct.addrinfo* %58, null
br i1 %59, label %60, label %27
60: ; preds = %55, %50, %24
%61 = phi i32 [ undef, %24 ], [ %37, %50 ], [ %37, %55 ]
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
%63 = load %struct.addrinfo*, %struct.addrinfo** %6
%64 = icmp eq %struct.addrinfo* %63, null
%65 = select i1 %64, i32 -1, i32 %61
br label %66
66: ; preds = %60, %20
%67 = phi i32 [ -1, %20 ], [ %65, %60 ]
ret i32 %67
}