#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;
};
typedef int /*<<< orphan*/ so_opt;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *build_msg(char *, char const *);
struct addrinfo *conn_build_addrinfo(char const *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gen_err_msg(char *, char *, char *, int);
int listen(int, int);
int /*<<< orphan*/ put_err(int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int conn_setup_listen(const char *port) {
  int new_sock = 0;
  if ((new_sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    if (!0)
      put_err(gen_err_msg(
          "[1;31mERROR:[0m ", build_msg("socket creation, Port: %s", port),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/janLo/htw-computer-networks-mailserver/"
          "refs/heads/master/connection.c",
          148));
    return -1;
  }
  struct addrinfo *info = conn_build_addrinfo(port);
  int so_opt = 1;
  setsockopt(new_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&so_opt,
             sizeof(so_opt));
  if (bind(new_sock, info->ai_addr, info->ai_addrlen) == -1) {
    if (!0)
      put_err(gen_err_msg(
          "[1;31mERROR:[0m ", build_msg("socket binding, Port: %s", port),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/janLo/htw-computer-networks-mailserver/"
          "refs/heads/master/connection.c",
          158));
    return -1;
  }
  freeaddrinfo(info);
  if (listen(new_sock, 2) == -1) {
    if (!0)
      put_err(gen_err_msg(
          "[1;31mERROR:[0m ", "socket listening",
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/janLo/htw-computer-networks-mailserver/"
          "refs/heads/master/connection.c",
          165));
    return -1;
  }
  return new_sock;
}


x86 assembly (clang, -O0):
<conn_setup_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <conn_setup_listen+0x76>
mov    -0x10(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1070 <build_msg@plt>
mov    %rax,%rsi
lea    0xe1e(%rip),%rdi
lea    0xe44(%rip),%rdx
mov    $0x94,%ecx
call   1040 <gen_err_msg@plt>
mov    %eax,%edi
call   1050 <put_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <conn_setup_listen+0x161>
mov    -0x10(%rbp),%rdi
call   1080 <conn_build_addrinfo@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    129a <conn_setup_listen+0x10a>
mov    -0x10(%rbp),%rsi
lea    0xe66(%rip),%rdi
call   1070 <build_msg@plt>
mov    %rax,%rsi
lea    0xd8a(%rip),%rdi
lea    0xdb0(%rip),%rdx
mov    $0x9e,%ecx
call   1040 <gen_err_msg@plt>
mov    %eax,%edi
call   1050 <put_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <conn_setup_listen+0x161>
mov    -0x20(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12eb <conn_setup_listen+0x15b>
lea    0xd40(%rip),%rdi
lea    0xe1f(%rip),%rsi
lea    0xd5f(%rip),%rdx
mov    $0xa5,%ecx
call   1040 <gen_err_msg@plt>
mov    %eax,%edi
call   1050 <put_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f1 <conn_setup_listen+0x161>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_setup_listen>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     123f <conn_setup_listen+0xaf>
mov    %eax,%ebx
mov    %r14,%rdi
call   1080 <conn_build_addrinfo@plt>
mov    %rax,%r15
movl   $0x1,0xc(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r15),%edx
mov    0x4(%r15),%esi
mov    %ebx,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1266 <conn_setup_listen+0xd6>
mov    %r15,%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    129c <conn_setup_listen+0x10c>
lea    0xdd6(%rip),%rdi
lea    0xeb5(%rip),%rsi
lea    0xdf5(%rip),%rdx
mov    $0xa5,%ecx
jmp    128b <conn_setup_listen+0xfb>
lea    0xdcd(%rip),%rdi
mov    %r14,%rsi
call   1070 <build_msg@plt>
lea    0xdab(%rip),%rdi
lea    0xdd1(%rip),%rdx
mov    %rax,%rsi
mov    $0x94,%ecx
jmp    128b <conn_setup_listen+0xfb>
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
call   1070 <build_msg@plt>
lea    0xd84(%rip),%rdi
lea    0xdaa(%rip),%rdx
mov    %rax,%rsi
mov    $0x9e,%ecx
call   1040 <gen_err_msg@plt>
mov    %eax,%edi
call   1050 <put_err@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"\1B[1;31mERROR:\1B[0m \00"
@.str.1 = [26 x i8] c"socket creation, Port: %s\00"
@.str.2 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/janLo/htw-computer-networks-mailserver/refs/heads/master/connection.c\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [25 x i8] c"socket binding, Port: %s\00"
@.str.4 = [17 x i8] c"socket listening\00"
define i32 @conn_setup_listen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i8* @build_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %13, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 148)
%15 = call i32 @put_err(i32 %14)
store i32 -1, i32* %2
br label %49
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call %struct.addrinfo* @conn_build_addrinfo(i8* %17)
store %struct.addrinfo* %18, %struct.addrinfo** %5
store i32 1, i32* %6
%19 = load i32, i32* %4
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = bitcast i32* %6 to i8*
%23 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i8* %22, i32 4)
%24 = load i32, i32* %4
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load %struct.addrinfo*, %struct.addrinfo** %5
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @bind(i32 %24, i32 %27, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %16
%34 = load i8*, i8** %3
%35 = call i8* @build_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %35, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 158)
%37 = call i32 @put_err(i32 %36)
store i32 -1, i32* %2
br label %49
38: ; preds = %16
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%40 = call i32 @freeaddrinfo(%struct.addrinfo* %39)
%41 = load i32, i32* %4
%42 = call i32 @listen(i32 %41, i32 2)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 165)
%46 = call i32 @put_err(i32 %45)
store i32 -1, i32* %2
br label %49
47: ; preds = %38
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %44, %33, %11
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [19 x i8] c"\1B[1;31mERROR:\1B[0m \00"
@.str.1 = [26 x i8] c"socket creation, Port: %s\00"
@.str.2 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/janLo/htw-computer-networks-mailserver/refs/heads/master/connection.c\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [25 x i8] c"socket binding, Port: %s\00"
@.str.4 = [17 x i8] c"socket listening\00"
define i32 @conn_setup_listen(i8* %0) {
%2 = alloca i32
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i8* @build_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 148)
%10 = tail call i32 @put_err(i32 %9)
br label %37
11: ; preds = %1
%12 = tail call %struct.addrinfo* @conn_build_addrinfo(i8* %0)
%13 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %5, i32 %14, i32 %15, i8* %13, i32 4)
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @bind(i32 %5, i32 %18, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = call i8* @build_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%25 = call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %24, i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 158)
br label %32
26: ; preds = %11
%27 = call i32 @freeaddrinfo(%struct.addrinfo* %12)
%28 = call i32 @listen(i32 %5, i32 2)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = call i32 @gen_err_msg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.2, i64 0, i64 0), i32 165)
br label %32
32: ; preds = %23, %30
%33 = phi i32 [ %31, %30 ], [ %25, %23 ]
%34 = call i32 @put_err(i32 %33)
br label %35
35: ; preds = %32, %26
%36 = phi i32 [ %5, %26 ], [ -1, %32 ]
br label %37
37: ; preds = %35, %7
%38 = phi i32 [ -1, %7 ], [ %36, %35 ]
ret i32 %38
}