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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char *, int);
int main() {
  struct addrinfo hints, *res;
  int sockfd;
  int fileSize = 0, i;
  FILE *fp;
  char name[20];
  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);
  printf("\nEnter filename: ");
  fgets(name, 20, stdin);
  write(sockfd, name, 20);
  char buf[1024];
  int num = read(sockfd, buf, 1024);
  buf[num] = 0;
  fileSize = atoi(buf);
  printf("\nFileSize: %d", fileSize);
  num = read(sockfd, buf, fileSize);
  buf[num + 1] = '\n';
  printf("\n Received buf %s", buf);
  fp = fopen("recv.txt", "w+");
  fputs(buf, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xdf8(%rip),%rdi
lea    0xdfb(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10d0 <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   10e0 <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   10c0 <connect@plt>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    $0x14,%esi
call   1090 <fgets@plt>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x454(%rbp)
movslq -0x454(%rbp),%rax
movb   $0x0,-0x450(%rbp,%rax,1)
lea    -0x450(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x454(%rbp)
mov    -0x454(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0xa,-0x450(%rbp,%rax,1)
0a
lea    -0x450(%rbp),%rsi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd2d(%rip),%rdi
lea    0xd2f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x450(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x440,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe00(%rip),%rdi
lea    0xe03(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdx
call   10d0 <getaddrinfo@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10e0 <socket@plt>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10c0 <connect@plt>
lea    0xdd2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x14,%esi
call   1090 <fgets@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1030 <write@plt>
lea    0x40(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x40(%rsp,%rax,1)
mov    %r14,%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
lea    0xd87(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1080 <read@plt>
cltq
movb   $0xa,0x1(%rax,%r14,1)
lea    0xd71(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd72(%rip),%rdi
lea    0xd74(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbx
pop    %r14
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 = [18 x i8] c"\0AEnter filename: \00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"\0AFileSize: %d\00"
@.str.4 = [18 x i8] c"\0A Received buf %s\00"
@.str.5 = [9 x i8] c"recv.txt\00"
@.str.6 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [20 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%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 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%37 = load i32, i32* @stdin
%38 = call i32 @fgets(i8* %36, i32 20, i32 %37)
%39 = load i32, i32* %4
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 20)
%42 = load i32, i32* %4
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 1024)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%49 = call i32 @atoi(i8* %48)
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %4
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* %5
%55 = call i32 @read(i32 %52, i8* %53, i32 %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %58
store i8 10, i8* %59
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %62, i32** %7
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%64 = load i32*, i32** %7
%65 = call i32 @fputs(i8* %63, i32* %64)
%66 = load i32*, i32** %7
%67 = call i32 @fclose(i32* %66)
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 = [18 x i8] c"\0AEnter filename: \00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"\0AFileSize: %d\00"
@.str.4 = [18 x i8] c"\0A Received buf %s\00"
@.str.5 = [9 x i8] c"recv.txt\00"
@.str.6 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca %struct.addrinfo
%2 = alloca %struct.addrinfo*
%3 = alloca [20 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast %struct.addrinfo* %1 to i8*
%6 = bitcast %struct.addrinfo** %2 to i8*
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 20)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = 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)
%14 = load %struct.addrinfo*, %struct.addrinfo** %2
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 3
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %16, i32 %18, i32 %20)
%22 = load %struct.addrinfo*, %struct.addrinfo** %2
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @connect(i32 %21, i32 %24, i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @stdin
%30 = call i32 @fgets(i8* %7, i32 20, i32 %29)
%31 = call i32 @write(i32 %21, i8* %7, i32 20)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %21, i8* %32, i32 1024)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
%36 = call i32 @atoi(i8* %32)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = call i32 @read(i32 %21, i8* %32, i32 %36)
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %40
store i8 10, i8* %41
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%43 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%44 = call i32 @fputs(i8* %32, i32* %43)
%45 = call i32 @fclose(i32* %43)
ret i32 0
}