#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;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
char *malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  struct addrinfo hints, *res;
  int sockfd;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  getaddrinfo("localhost", "8080", &hints, &res);
  sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  connect(sockfd, res->ai_addr, res->ai_addrlen);
  char filename[100];
  strcpy(filename, "yo.txt");
  write(sockfd, filename, 100);
  char filesize[1024];
  int num = read(sockfd, filesize, 1024);
  filesize[num] = 0;
  int fsize = atoi(filesize);
  printf("Received buf %d\n", fsize);
  char *data = malloc(fsize * sizeof(char));
  int i = 0;
  for (i = 0; i < fsize; i++)
    read(sockfd, (char)data[i], 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe1f(%rip),%rdi
lea    0xe22(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   10a0 <connect@plt>
lea    -0x90(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
lea    -0x490(%rbp),%rsi
mov    $0x400,%edx
mov    $0x0,%al
call   1070 <read@plt>
mov    %eax,-0x494(%rbp)
movslq -0x494(%rbp),%rax
movb   $0x0,-0x490(%rbp,%rax,1)
lea    -0x490(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x498(%rbp)
mov    -0x498(%rbp),%esi
lea    0xd7e(%rip),%rdi
call   1050 <printf@plt>
movslq -0x498(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x4a0(%rbp)
movl   $0x0,-0x4a4(%rbp)
movl   $0x0,-0x4a4(%rbp)
mov    -0x4a4(%rbp),%eax
cmp    -0x498(%rbp),%eax
jge    1311 <main+0x171>
mov    -0x24(%rbp),%edi
mov    -0x4a0(%rbp),%rax
movslq -0x4a4(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
mov    $0x1,%edx
mov    $0x0,%al
call   1070 <read@plt>
mov    -0x4a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4a4(%rbp)
jmp    12ca <main+0x12a>
xor    %eax,%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x490,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe1c(%rip),%rdi
lea    0xe1f(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   10b0 <getaddrinfo@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
mov    %eax,%r15d
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r15d,%edi
call   10a0 <connect@plt>
lea    0xdec(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
lea    0x90(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %eax,%eax
call   1070 <read@plt>
cltq
movb   $0x0,0x90(%rsp,%rax,1)
mov    %rbx,%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
lea    0xda0(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r14d,%edi
call   1080 <malloc@plt>
test   %r14d,%r14d
jle    12cd <main+0x12d>
mov    %rax,%r12
mov    %r15d,%edi
xor    %esi,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1070 <read@plt>
cmp    $0x1,%r14d
je     12cd <main+0x12d>
mov    %r14d,%ebp
mov    $0x1,%ebx
nopl   0x0(%rax)
movsbl (%r12,%rbx,1),%esi
mov    %r15d,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1070 <read@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12b0 <main+0x110>
xor    %eax,%eax
add    $0x490,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"8080\00"
@.str.2 = [7 x i8] c"yo.txt\00"
@.str.3 = [17 x i8] c"Received buf %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %1
%11 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 3
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 4
%19 = load i32, i32* %18
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 3
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %3
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %19, i32 %22, i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = load %struct.addrinfo*, %struct.addrinfo** %3
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @connect(i32 %27, i32 %30, i32 %33)
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %4
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%39 = call i32 @write(i32 %37, i8* %38, i32 100)
%40 = load i32, i32* %4
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i32 (i32, ...) @read(i32 %40, i8* %41, i32 1024)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = mul i64 %51, 1
%53 = trunc i64 %52 to i32
%54 = call i8* @malloc(i32 %53)
store i8* %54, i8** %9
store i32 0, i32* %10
store i32 0, i32* %10
br label %55
55: ; preds = %68, %0
%56 = load i32, i32* %10
%57 = load i32, i32* %8
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i32, i32* %4
%61 = load i8*, i8** %9
%62 = load i32, i32* %10
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = call i32 (i32, ...) @read(i32 %60, i32 %66, i32 1)
br label %68
68: ; preds = %59
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %55
71: ; preds = %55
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
@.str.1 = [5 x i8] c"8080\00"
@.str.2 = [7 x i8] c"yo.txt\00"
@.str.3 = [17 x i8] c"Received buf %d\0A\00"
define i32 @main() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca [100 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast %struct.addrinfo* %1 to i8*
%6 = bitcast %struct.addrinfo** %2 to i8*
%7 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 20)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%13 = load %struct.addrinfo*, %struct.addrinfo** %2
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 4
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 3
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 2
%19 = load i32, i32* %18
%20 = call i32 @socket(i32 %15, i32 %17, i32 %19)
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @connect(i32 %20, i32 %23, i32 %25)
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @write(i32 %20, i8* %27, i32 100)
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%31 = call i32 (i32, ...) @read(i32 %20, i8* %30, i32 1024)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 @atoi(i8* %30)
%35 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i8* @malloc(i32 %34)
%37 = icmp sgt i32 %34, 0
br i1 %37, label %38, label %50
38: ; preds = %0
%39 = zext i32 %34 to i64
%40 = call i32 (i32, ...) @read(i32 %20, i32 0, i32 1)
%41 = icmp eq i32 %34, 1
br i1 %41, label %50, label %42
42: ; preds = %38, %42
%43 = phi i64 [ %48, %42 ], [ 1, %38 ]
%44 = getelementptr inbounds i8, i8* %36, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = call i32 (i32, ...) @read(i32 %20, i32 %46, i32 1)
%48 = add nuw nsw i64 %43, 1
%49 = icmp eq i64 %48, %39
br i1 %49, label %50, label %42
50: ; preds = %42, %38, %0
ret i32 0
}