#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_in {
  int /*<<< orphan*/ sin_port;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ prepareHints(struct addrinfo *, int);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ waitLoop(int);
int main(int argc, char **argv) {
  const char *port = (argc >= 2) ? argv[1] : "7474";
  struct addrinfo hints, *res;
  prepareHints(&hints, 1);
  int status = getaddrinfo(NULL, port, &hints, &res);
  if (status != 0) {
    printf("Error on getaddrinfo\n");
    exit(1);
  }
  int socketfd;
  struct addrinfo *cur;
  for (cur = res; cur != NULL; cur = cur->ai_next) {
    if ((socketfd =
             socket(res->ai_family, res->ai_socktype, res->ai_protocol)) >= 0)
      break;
  }
  if (socketfd == -1) {
    printf("Error on socket\n");
    exit(1);
  }
  int yes = 1;
  if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    perror("setsockopt");
    exit(1);
  }
  int bindres = bind(socketfd, cur->ai_addr, cur->ai_addrlen);
  if (bindres != 0) {
    perror("bind");
    exit(1);
  }
  if (listen(socketfd, 5) < 0) {
    printf("error on listen\n");
    exit(1);
  }
  printf("success, listening on socket %d, port %d\n", socketfd,
         ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port));
  waitLoop(socketfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11e1 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    11f1 <main+0x41>
lea    0xe18(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    11f1 <main+0x41>
mov    -0x60(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <prepareHints@plt>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1241 <main+0x91>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1295 <main+0xe5>
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jl     127f <main+0xcf>
jmp    1295 <main+0xe5>
jmp    1284 <main+0xd4>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1249 <main+0x99>
cmpl   $0xffffffff,-0x48(%rbp)
jne    12b7 <main+0x107>
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
mov    0x2d07(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1301 <main+0x151>
lea    0xd3a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <bind@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     133a <main+0x18a>
lea    0xd0c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1369 <main+0x1b9>
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x64(%rbp),%esi
mov    %eax,%edx
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%edi
call   1050 <waitLoop@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
cmp    $0x2,%edi
jl     11d3 <main+0x13>
mov    0x8(%rsi),%rbx
jmp    11da <main+0x1a>
lea    0xe26(%rip),%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
call   10d0 <prepareHints@plt>
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
jne    12dc <main+0x11c>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12d3 <main+0x113>
mov    0x14(%rbx),%edi
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%esi
nopw   0x0(%rax,%rax,1)
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    124e <main+0x8e>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1243 <main+0x83>
mov    (%rsp),%rax
mov    0x14(%rax),%edi
mov    0xc(%rax),%edx
mov    0x10(%rax),%esi
jmp    1220 <main+0x60>
cmp    $0xffffffff,%ebp
je     12d3 <main+0x113>
xor    %ebx,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12e5 <main+0x125>
mov    (%rbx),%rsi
mov    0x8(%rbx),%edx
mov    %ebp,%edi
call   1090 <bind@plt>
test   %eax,%eax
jne    12ee <main+0x12e>
mov    %ebp,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     1304 <main+0x144>
mov    (%rsp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1080 <ntohs@plt>
lea    0xd5f(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <waitLoop@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd75(%rip),%rdi
jmp    130b <main+0x14b>
lea    0xd7c(%rip),%rdi
jmp    130b <main+0x14b>
lea    0xd19(%rip),%rdi
jmp    12f5 <main+0x135>
lea    0xd1b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd34(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
@.str = [5 x i8] c"7474\00"
@.str.1 = [22 x i8] c"Error on getaddrinfo\0A\00"
@.str.2 = [17 x i8] c"Error on socket\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.5 = [17 x i8] c"error on listen\0A\00"
@.str.6 = [42 x i8] c"success, listening on socket %d, port %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.addrinfo*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
br label %21
20: ; preds = %2
br label %21
21: ; preds = %20, %16
%22 = phi i8* [ %19, %16 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %20 ]
store i8* %22, i8** %6
%23 = call i32 @prepareHints(%struct.addrinfo* %7, i32 1)
%24 = load i8*, i8** %6
%25 = call i32 @getaddrinfo(i32* null, i8* %24, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %32, %struct.addrinfo** %11
br label %33
33: ; preds = %50, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %11
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %54
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 4
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %39, i32 %42, i32 %45)
store i32 %46, i32* %10
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %36
br label %54
49: ; preds = %36
br label %50
50: ; preds = %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %11
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 5
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
store %struct.addrinfo* %53, %struct.addrinfo** %11
br label %33
54: ; preds = %48, %33
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %54
store i32 1, i32* %12
%61 = load i32, i32* %10
%62 = load i32, i32* @SOL_SOCKET
%63 = load i32, i32* @SO_REUSEADDR
%64 = call i32 @setsockopt(i32 %61, i32 %62, i32 %63, i32* %12, i32 4)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %60
%70 = load i32, i32* %10
%71 = load %struct.addrinfo*, %struct.addrinfo** %11
%72 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %71, i32 0, i32 0
%73 = load i64, i64* %72
%74 = load %struct.addrinfo*, %struct.addrinfo** %11
%75 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %74, i32 0, i32 1
%76 = load i32, i32* %75
%77 = call i32 @bind(i32 %70, i64 %73, i32 %76)
store i32 %77, i32* %13
%78 = load i32, i32* %13
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %69
%81 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %69
%84 = load i32, i32* %10
%85 = call i64 @listen(i32 %84, i32 5)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%89 = call i32 @exit(i32 1)
unreachable
90: ; preds = %83
%91 = load i32, i32* %10
%92 = load %struct.addrinfo*, %struct.addrinfo** %8
%93 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %92, i32 0, i32 0
%94 = load i64, i64* %93
%95 = inttoptr i64 %94 to %struct.sockaddr_in*
%96 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %95, i32 0, i32 0
%97 = load i32, i32* %96
%98 = call i32 @ntohs(i32 %97)
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %91, i32 %98)
%100 = load i32, i32* %10
%101 = call i32 @waitLoop(i32 %100)
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo* }
%struct.sockaddr_in = type { i32 }
@.str = [5 x i8] c"7474\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [11 x i8] c"setsockopt\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.6 = [42 x i8] c"success, listening on socket %d, port %d\0A\00"
@str = [16 x i8] c"error on listen\00"
@str.7 = [16 x i8] c"Error on socket\00"
@str.8 = [21 x i8] c"Error on getaddrinfo\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %10
10: ; preds = %2, %7
%11 = phi i8* [ %9, %7 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
%12 = bitcast %struct.addrinfo* %3 to i8*
%13 = bitcast %struct.addrinfo** %4 to i8*
%14 = call i32 @prepareHints(%struct.addrinfo* %3, i32 1)
%15 = call i32 @getaddrinfo(i32* null, i8* %11, %struct.addrinfo* %3, %struct.addrinfo** %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %10
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %50, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 @socket(i32 %22, i32 %24, i32 %26)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %53, label %32
29: ; preds = %10
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20, %38
%33 = phi i32 [ %46, %38 ], [ %27, %20 ]
%34 = phi %struct.addrinfo* [ %36, %38 ], [ %18, %20 ]
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 5
%36 = load %struct.addrinfo*, %struct.addrinfo** %35
%37 = icmp eq %struct.addrinfo* %36, null
br i1 %37, label %48, label %38
38: ; preds = %32
%39 = load %struct.addrinfo*, %struct.addrinfo** %4
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 4
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 3
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 2
%45 = load i32, i32* %44
%46 = call i32 @socket(i32 %41, i32 %43, i32 %45)
%47 = icmp sgt i32 %46, -1
br i1 %47, label %53, label %32
48: ; preds = %32
%49 = icmp eq i32 %33, -1
br i1 %49, label %50, label %53
50: ; preds = %17, %48
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %38, %20, %48
%54 = phi i32 [ %33, %48 ], [ %27, %20 ], [ %46, %38 ]
%55 = phi %struct.addrinfo* [ null, %48 ], [ %18, %20 ], [ %36, %38 ]
%56 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_REUSEADDR
%59 = call i32 @setsockopt(i32 %54, i32 %57, i32 %58, i32* %5, i32 4)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %53
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 0
%66 = load i64, i64* %65
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i64 0, i32 1
%68 = load i32, i32* %67
%69 = call i32 @bind(i32 %54, i64 %66, i32 %68)
%70 = icmp eq i32 %69, 0
br i1 %70, label %74, label %71
71: ; preds = %64
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %64
%75 = call i64 @listen(i32 %54, i32 5)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %74
%81 = load %struct.addrinfo*, %struct.addrinfo** %4
%82 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %81, i64 0, i32 0
%83 = load i64, i64* %82
%84 = inttoptr i64 %83 to %struct.sockaddr_in*
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %84, i64 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @ntohs(i32 %86)
%88 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %87)
%89 = call i32 @waitLoop(i32 %54)
ret i32 0
}