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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, unsigned long);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ rem_headers(char *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int download_file(int socket, const char *filepath) {
  char buff[200];
  strcpy(buff, "GET /");
  strcat(buff, filepath);
  strcat(buff, " HTTP/1.0\n\n");
  char *bu = malloc(sizeof(char) * strlen(buff) + 1);
  strcat(bu, buff);
  if (send(socket, bu, strlen(bu), 0) < 1) {
    free(bu);
    return 1;
  }
  free(bu);
  FILE *file = fopen("download", "wb");
  if (!file)
    return 1;
  char buffer[4096];
  int size;
  int init = 1;
  int isinit = 1;
  int cont = 1;
  unsigned long total = 0;
  while (cont && ((size = recv(socket, buffer, sizeof(buffer) - 1, 0)) > 0)) {
    buffer[size] = 0;
    if (init) {
      size -= rem_headers(buffer);
      isinit = 0;
    }
    if (!init && strstr(buffer, "\r\n\r\n") != NULL) {
      cont = 0;
      size -= 8;
    }
    fwrite(buffer, 1, size, file);
    total += size;
    init = isinit;
  }
  fclose(file);
  printf("total size downloaded: %lu\n", total);
  return 0;
}


x86 assembly (clang, -O0):
<download_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1120,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xe0(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1060 <strcpy@plt>
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <strcat@plt>
lea    -0xe0(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   10d0 <strcat@plt>
lea    -0xe0(%rbp),%rdi
call   1080 <strlen@plt>
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0xe8(%rbp)
mov    -0xe8(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1114(%rbp)
mov    -0xe8(%rbp),%rax
mov    %rax,-0x1110(%rbp)
mov    -0xe8(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x1114(%rbp),%edi
mov    -0x1110(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
cmp    $0x1,%eax
jge    12a9 <download_file+0xd9>
mov    -0xe8(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1454 <download_file+0x284>
mov    -0xe8(%rbp),%rdi
call   1030 <free@plt>
lea    0xd56(%rip),%rdi
lea    0xd58(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0xf0(%rbp)
cmpq   $0x0,-0xf0(%rbp)
jne    12e9 <download_file+0x119>
movl   $0x1,-0x4(%rbp)
jmp    1454 <download_file+0x284>
movl   $0x1,-0x10f8(%rbp)
movl   $0x1,-0x10fc(%rbp)
movl   $0x1,-0x1100(%rbp)
movq   $0x0,-0x1108(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1100(%rbp)
mov    %al,-0x1115(%rbp)
je     134f <download_file+0x17f>
mov    -0x8(%rbp),%edi
lea    -0x10f0(%rbp),%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x10f4(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x1115(%rbp)
mov    -0x1115(%rbp),%al
test   $0x1,%al
jne    1362 <download_file+0x192>
jmp    142e <download_file+0x25e>
movslq -0x10f4(%rbp),%rax
movb   $0x0,-0x10f0(%rbp,%rax,1)
cmpl   $0x0,-0x10f8(%rbp)
je     13a7 <download_file+0x1d7>
lea    -0x10f0(%rbp),%rdi
call   1050 <rem_headers@plt>
mov    %rax,%rcx
movslq -0x10f4(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x10f4(%rbp)
movl   $0x0,-0x10fc(%rbp)
cmpl   $0x0,-0x10f8(%rbp)
jne    13ea <download_file+0x21a>
lea    -0x10f0(%rbp),%rdi
lea    0xc5c(%rip),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     13ea <download_file+0x21a>
movl   $0x0,-0x1100(%rbp)
mov    -0x10f4(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x10f4(%rbp)
lea    -0x10f0(%rbp),%rdi
mov    -0x10f4(%rbp),%edx
mov    -0xf0(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
movslq -0x10f4(%rbp),%rax
add    -0x1108(%rbp),%rax
mov    %rax,-0x1108(%rbp)
mov    -0x10fc(%rbp),%eax
mov    %eax,-0x10f8(%rbp)
jmp    1312 <download_file+0x142>
mov    -0xf0(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x1108(%rbp),%rsi
lea    0xbdb(%rip),%rdi
call   10a0 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<download_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10e8,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xe12(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   10d0 <strcat@plt>
lea    0xdf9(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <strcat@plt>
mov    %rbp,%rdi
call   1080 <strlen@plt>
lea    0x1(%rax),%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %r14d,0xc(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
test   %ebp,%ebp
jle    1332 <download_file+0x162>
lea    0xda5(%rip),%rdi
lea    0xda7(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1332 <download_file+0x162>
mov    %rax,%r15
xor    %ebp,%ebp
lea    0xe0(%rsp),%r13
xor    %r14d,%r14d
jmp    12de <download_file+0x10e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xd77(%rip),%rsi
call   10f0 <strstr@plt>
lea    -0x8(%rbx),%ecx
xor    %r12d,%r12d
test   %rax,%rax
sete   %r12b
cmove  %ebx,%ecx
mov    %ecx,%ebx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   10e0 <fwrite@plt>
movslq %ebx,%rax
add    %rax,%rbp
mov    $0x1,%r14b
test   %r12d,%r12d
je     1319 <download_file+0x149>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    $0xfff,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    1319 <download_file+0x149>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,0xe0(%rsp,%rax,1)
mov    %r13,%rdi
test   $0x1,%r14b
jne    12a0 <download_file+0xd0>
call   1050 <rem_headers@plt>
sub    %eax,%ebx
mov    $0x1,%r12d
jmp    12be <download_file+0xee>
mov    %r15,%rdi
call   1070 <fclose@plt>
lea    0xcfb(%rip),%rdi
mov    %rbp,%rsi
call   10a0 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"GET /\00"
@.str.1 = [12 x i8] c" HTTP/1.0\0A\0A\00"
@.str.2 = [9 x i8] c"download\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.5 = [28 x i8] c"total size downloaded: %lu\0A\00"
define i32 @download_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [200 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [4096 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%15 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = call i32 @strcat(i8* %17, i8* %18)
%20 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%21 = call i32 @strcat(i8* %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = mul i64 1, %24
%26 = add i64 %25, 1
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%31 = call i32 @strcat(i8* %29, i8* %30)
%32 = load i32, i32* %4
%33 = load i8*, i8** %7
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @send(i32 %32, i8* %33, i32 %35, i32 0)
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %2
%39 = load i8*, i8** %7
%40 = call i32 @free(i8* %39)
store i32 1, i32* %3
br label %97
41: ; preds = %2
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
%44 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %44, i32** %8
%45 = load i32*, i32** %8
%46 = icmp ne i32* %45, null
br i1 %46, label %48, label %47
47: ; preds = %41
store i32 1, i32* %3
br label %97
48: ; preds = %41
store i32 1, i32* %11
store i32 1, i32* %12
store i32 1, i32* %13
store i64 0, i64* %14
br label %49
49: ; preds = %82, %48
%50 = load i32, i32* %13
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32, i32* %4
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%55 = call i32 @recv(i32 %53, i8* %54, i32 4095, i32 0)
store i32 %55, i32* %10
%56 = icmp sgt i32 %55, 0
br label %57
57: ; preds = %52, %49
%58 = phi i1 [ false, %49 ], [ %56, %52 ]
br i1 %58, label %59, label %92
59: ; preds = %57
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %61
store i8 0, i8* %62
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %59
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%67 = call i64 @rem_headers(i8* %66)
%68 = load i32, i32* %10
%69 = sext i32 %68 to i64
%70 = sub nsw i64 %69, %67
%71 = trunc i64 %70 to i32
store i32 %71, i32* %10
store i32 0, i32* %12
br label %72
72: ; preds = %65, %59
%73 = load i32, i32* %11
%74 = icmp ne i32 %73, 0
br i1 %74, label %82, label %75
75: ; preds = %72
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%77 = call i32* @strstr(i8* %76, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%78 = icmp ne i32* %77, null
br i1 %78, label %79, label %82
79: ; preds = %75
store i32 0, i32* %13
%80 = load i32, i32* %10
%81 = sub nsw i32 %80, 8
store i32 %81, i32* %10
br label %82
82: ; preds = %79, %75, %72
%83 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%84 = load i32, i32* %10
%85 = load i32*, i32** %8
%86 = call i32 @fwrite(i8* %83, i32 1, i32 %84, i32* %85)
%87 = load i32, i32* %10
%88 = sext i32 %87 to i64
%89 = load i64, i64* %14
%90 = add i64 %89, %88
store i64 %90, i64* %14
%91 = load i32, i32* %12
store i32 %91, i32* %11
br label %49
92: ; preds = %57
%93 = load i32*, i32** %8
%94 = call i32 @fclose(i32* %93)
%95 = load i64, i64* %14
%96 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %95)
store i32 0, i32* %3
br label %97
97: ; preds = %92, %47, %38
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
@.str = [6 x i8] c"GET /\00"
@.str.1 = [12 x i8] c" HTTP/1.0\0A\0A\00"
@.str.2 = [9 x i8] c"download\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.5 = [28 x i8] c"total size downloaded: %lu\0A\00"
define i32 @download_file(i32 %0, i8* %1) {
%3 = alloca [200 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @strcat(i8* %5, i8* %1)
%8 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @strlen(i8* %5)
%10 = add i32 %9, 1
%11 = call i8* @malloc(i32 %10)
%12 = call i32 @strcat(i8* %11, i8* %5)
%13 = call i32 @strlen(i8* %11)
%14 = call i32 @send(i32 %0, i8* %11, i32 %13, i32 0)
%15 = icmp slt i32 %14, 1
%16 = call i32 @free(i8* %11)
br i1 %15, label %51, label %17
17: ; preds = %2
%18 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %51, label %20
20: ; preds = %17
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
br label %22
22: ; preds = %20, %40
%23 = phi i64 [ 0, %20 ], [ %45, %40 ]
%24 = phi i1 [ false, %20 ], [ true, %40 ]
%25 = call i32 @recv(i32 %0, i8* %21, i32 4095, i32 0)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %22
%28 = zext i32 %25 to i64
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %28
store i8 0, i8* %29
br i1 %24, label %34, label %30
30: ; preds = %27
%31 = call i64 @rem_headers(i8* %21)
%32 = trunc i64 %31 to i32
%33 = sub i32 %25, %32
br label %40
34: ; preds = %27
%35 = call i32* @strstr(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%36 = icmp eq i32* %35, null
%37 = add nsw i32 %25, -8
%38 = select i1 %36, i32 %25, i32 %37
%39 = zext i1 %36 to i32
br label %40
40: ; preds = %30, %34
%41 = phi i32 [ %33, %30 ], [ %38, %34 ]
%42 = phi i32 [ 1, %30 ], [ %39, %34 ]
%43 = call i32 @fwrite(i8* %21, i32 1, i32 %41, i32* %18)
%44 = sext i32 %41 to i64
%45 = add i64 %23, %44
%46 = icmp eq i32 %42, 0
br i1 %46, label %47, label %22
47: ; preds = %40, %22
%48 = phi i64 [ %45, %40 ], [ %23, %22 ]
%49 = call i32 @fclose(i32* %18)
%50 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %48)
br label %51
51: ; preds = %2, %47, %17
%52 = phi i32 [ 0, %47 ], [ 1, %17 ], [ 1, %2 ]
ret i32 %52
}