#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_flags;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int NI_MAXHOST;
int NI_MAXSERV;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ getnameinfo(struct sockaddr *, int, char *, int, char *, int,
                               int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void main(int args, char *argv[]) {
  struct addrinfo hints, *res;
  struct sockaddr_storage cli;
  char host[NI_MAXHOST], serv[NI_MAXSERV];
  char buf[81];
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  getaddrinfo(argv[1], argv[2], &hints, &res);
  int sd = socket(res->ai_family, res->ai_socktype, 0);
  bind(sd, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
  freeaddrinfo(res);
  listen(sd, 5);
  while (1) {
    socklen_t clen = sizeof(cli);
    int cli_sd = accept(sd, (struct sockaddr *)&cli, &clen);
    getnameinfo((struct sockaddr *)&cli, clen, host, NI_MAXHOST, serv,
                NI_MAXSERV, NI_NUMERICHOST);
    printf("%s::%s connected\n", host, serv);
    while (1) {
      int c = recv(cli_sd, buf, 80, 0);
      buf[c] = '\0';
      send(cli_sd, buf, c, 0);
    }
    close(cli_sd);
  }
  close(sd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xe0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xd8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0xc4(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
jmp    12a4 <main+0x104>
movl   $0x4,-0xc8(%rbp)
mov    -0xc4(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0xc8(%rbp),%rdx
call   1090 <accept@plt>
mov    -0xe0(%rbp),%rdx
mov    -0xd8(%rbp),%r8
mov    %eax,-0xcc(%rbp)
lea    -0x48(%rbp),%rdi
mov    -0xc8(%rbp),%esi
mov    0x2ccf(%rip),%rax
mov    (%rax),%ecx
mov    0x2ce6(%rip),%rax
mov    (%rax),%r9d
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
mov    -0xe0(%rbp),%rsi
mov    -0xd8(%rbp),%rdx
add    $0x10,%rsp
lea    0xcdd(%rip),%rdi
call   1050 <printf@plt>
jmp    132d <main+0x18d>
mov    -0xcc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xd0(%rbp)
movslq -0xd0(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
mov    -0xcc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    -0xd0(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
jmp    1328 <main+0x188>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e03(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%esi
mov    0x14(%rax),%edi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rsi
mov    (%rax),%edx
mov    %ebx,%edi
call   1080 <bind@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0x4,-0x2c(%rbp)
lea    -0x40(%rbp),%r12
lea    -0x2c(%rbp),%rdx
mov    %ebx,%edi
mov    %r12,%rsi
call   1090 <accept@plt>
mov    %eax,%ebx
mov    -0x2c(%rbp),%esi
mov    0x2d2f(%rip),%rax
mov    (%rax),%ecx
mov    0x0(%r13),%r9d
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r12,%rdi
mov    %r14,%rdx
mov    %r15,%r8
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
lea    0xd4a(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <printf@plt>
lea    -0xc0(%rbp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
movslq %eax,%rdx
movb   $0x0,-0xc0(%rbp,%rdx,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
call   1040 <send@plt>
jmp    12d0 <main+0x130>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, i32, i32, i64, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@NI_NUMERICHOST = global i32 0
@.str = [18 x i8] c"%s::%s connected\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.sockaddr_storage
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca [81 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%16 = load i32, i32* @NI_MAXHOST
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %8
%19 = alloca i8, i64 %17
store i64 %17, i64* %9
%20 = load i32, i32* @NI_MAXSERV
%21 = zext i32 %20 to i64
%22 = alloca i8, i64 %21
store i64 %21, i64* %10
%23 = load i32, i32* @AI_PASSIVE
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 5
store i32 %23, i32* %24
%25 = load i32, i32* @AF_UNSPEC
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 4
store i64 0, i64* %29
%30 = load i8**, i8*** %4
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %4
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @getaddrinfo(i8* %32, i8* %35, %struct.addrinfo* %5, %struct.addrinfo** %6)
%37 = load %struct.addrinfo*, %struct.addrinfo** %6
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 3
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %6
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 2
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %39, i32 %42, i32 0)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = load %struct.addrinfo*, %struct.addrinfo** %6
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = inttoptr i64 %47 to %struct.sockaddr*
%49 = load %struct.addrinfo*, %struct.addrinfo** %6
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @bind(i32 %44, %struct.sockaddr* %48, i32 %51)
%53 = load %struct.addrinfo*, %struct.addrinfo** %6
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
%55 = load i32, i32* %12
%56 = call i32 @listen(i32 %55, i32 5)
br label %57
57: ; preds = %2
br label %58
58: ; preds = %57
store i32 4, i32* %13
%59 = load i32, i32* %12
%60 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%61 = call i32 @accept(i32 %59, %struct.sockaddr* %60, i32* %13)
store i32 %61, i32* %14
%62 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%63 = load i32, i32* %13
%64 = load i32, i32* @NI_MAXHOST
%65 = load i32, i32* @NI_MAXSERV
%66 = load i32, i32* @NI_NUMERICHOST
%67 = call i32 @getnameinfo(%struct.sockaddr* %62, i32 %63, i8* %19, i32 %64, i8* %22, i32 %65, i32 %66)
%68 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %22)
br label %69
69: ; preds = %70, %58
br label %70
70: ; preds = %69
%71 = load i32, i32* %14
%72 = getelementptr inbounds [81 x i8], [81 x i8]* %11, i64 0, i64 0
%73 = call i32 @recv(i32 %71, i8* %72, i32 80, i32 0)
store i32 %73, i32* %15
%74 = load i32, i32* %15
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [81 x i8], [81 x i8]* %11, i64 0, i64 %75
store i8 0, i8* %76
%77 = load i32, i32* %14
%78 = getelementptr inbounds [81 x i8], [81 x i8]* %11, i64 0, i64 0
%79 = load i32, i32* %15
%80 = call i32 @send(i32 %77, i8* %78, i32 %79, i32 0)
br label %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, i32, i32, i64, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@NI_NUMERICHOST = global i32 0
@.str = [18 x i8] c"%s::%s connected\0A\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.sockaddr_storage
%6 = alloca [81 x i8]
%7 = alloca i32
%8 = bitcast %struct.addrinfo* %3 to i8*
%9 = bitcast %struct.addrinfo** %4 to i8*
%10 = bitcast %struct.sockaddr_storage* %5 to i8*
%11 = load i32, i32* @NI_MAXHOST
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = load i32, i32* @NI_MAXSERV
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
%17 = getelementptr inbounds [81 x i8], [81 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @AI_PASSIVE
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %18, i32* %19
%20 = load i32, i32* @AF_UNSPEC
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* @SOCK_STREAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i64 0, i64* %24
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @getaddrinfo(i8* %26, i8* %28, %struct.addrinfo* %3, %struct.addrinfo** %4)
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %32, i32 %34, i32 0)
%36 = load %struct.addrinfo*, %struct.addrinfo** %4
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 1
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to %struct.sockaddr*
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @bind(i32 %35, %struct.sockaddr* %39, i32 %41)
%43 = load %struct.addrinfo*, %struct.addrinfo** %4
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
%45 = call i32 @listen(i32 %35, i32 5)
%46 = bitcast i32* %7 to i8*
store i32 4, i32* %7
%47 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%48 = call i32 @accept(i32 %35, %struct.sockaddr* %47, i32* %7)
%49 = load i32, i32* %7
%50 = load i32, i32* @NI_MAXHOST
%51 = load i32, i32* @NI_MAXSERV
%52 = load i32, i32* @NI_NUMERICHOST
%53 = call i32 @getnameinfo(%struct.sockaddr* %47, i32 %49, i8* %13, i32 %50, i8* %16, i32 %51, i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %16)
br label %55
55: ; preds = %55, %2
%56 = call i32 @recv(i32 %48, i8* %17, i32 80, i32 0)
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [81 x i8], [81 x i8]* %6, i64 0, i64 %57
store i8 0, i8* %58
%59 = call i32 @send(i32 %48, i8* %17, i32 %56, i32 0)
br label %55
}