#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 */

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long long *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void get_file(char *filename, int sck) {
  char buffer[1024];
  memset(buffer, 0, 1024);
  char *cmd = malloc((strlen(filename) + 6) * sizeof(char));
  memset(cmd, 0, (strlen(filename) + 6));
  strcpy(cmd, "GET ");
  strcat(cmd, filename);
  printf("cmd: %s\n", cmd);
  write(sck, cmd, strlen(cmd) + 1);
  free(cmd);
  char r;
  int i = 0;
  do {
    read(sck, &r, 1);
    buffer[i++] = r;
  } while (r != '\n');
  if (buffer[0] == 'O') {
    unsigned long long bytes;
    sscanf(buffer + 3, "%llu\n", &bytes);
    int fd = open(filename, O_WRONLY | O_CREAT);
    int n;
    while (bytes > 0) {
      n = read(sck, buffer, 1024);
      bytes -= n;
      write(fd, buffer, n);
    }
    printf("%s saved\n", filename);
  } else {
    printf("%s not found!\n", filename);
  }
}


x86 assembly (clang, -O0):
<get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x6,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    %eax,%edx
add    $0x6,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x418(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x418(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x418(%rbp),%rsi
lea    0xdb5(%rip),%rdi
call   1070 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x418(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <write@plt>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x420(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x419(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
mov    -0x419(%rbp),%cl
mov    -0x420(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x420(%rbp)
cltq
mov    %cl,-0x410(%rbp,%rax,1)
movsbl -0x419(%rbp),%eax
cmp    $0xa,%eax
jne    12a5 <get_file+0xf5>
movsbl -0x410(%rbp),%eax
cmp    $0x4f,%eax
jne    13aa <get_file+0x1fa>
lea    -0x410(%rbp),%rdi
add    $0x3,%rdi
lea    0xd03(%rip),%rsi
lea    -0x428(%rbp),%rdx
call   10b0 <sscanf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
mov    0x2ccd(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x42c(%rbp)
cmpq   $0x0,-0x428(%rbp)
jbe    1395 <get_file+0x1e5>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x430(%rbp)
movslq -0x430(%rbp),%rcx
mov    -0x428(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x428(%rbp)
mov    -0x42c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x430(%rbp),%edx
call   1050 <write@plt>
jmp    1338 <get_file+0x188>
mov    -0x8(%rbp),%rsi
lea    0xc74(%rip),%rdi
call   1070 <printf@plt>
jmp    13ba <get_file+0x20a>
mov    -0x8(%rbp),%rsi
lea    0xc69(%rip),%rdi
call   1070 <printf@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x6(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x6(%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xdf8(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10d0 <strcat@plt>
lea    0xde3(%rip),%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r12d,%edi
mov    %r15,%rsi
call   1050 <write@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0x7(%rsp),%rbp
nopl   (%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1250 <get_file+0xa0>
cmpb   $0x4f,0x10(%rsp)
jne    12f4 <get_file+0x144>
lea    0x13(%rsp),%rdi
lea    0xd8c(%rip),%rsi
lea    0x8(%rsp),%rdx
call   10b0 <sscanf@plt>
mov    0x2d3d(%rip),%rax
mov    0x2d5e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
call   10c0 <open@plt>
cmpq   $0x0,0x8(%rsp)
je     12eb <get_file+0x13b>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
movslq %eax,%rdx
sub    %rdx,0x8(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
cmpq   $0x0,0x8(%rsp)
jne    12c0 <get_file+0x110>
lea    0xd22(%rip),%rdi
jmp    12fb <get_file+0x14b>
lea    0xd23(%rip),%rdi
mov    %r14,%rsi
call   1070 <printf@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"GET \00"
@.str.1 = [9 x i8] c"cmd: %s\0A\00"
@.str.2 = [6 x i8] c"%llu\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [10 x i8] c"%s saved\0A\00"
@.str.4 = [15 x i8] c"%s not found!\0A\00"
define void @get_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 1024)
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 6
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 6
%25 = call i32 @memset(i8* %21, i32 0, i32 %24)
%26 = load i8*, i8** %6
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %6
%29 = load i8*, i8** %3
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = load i8*, i8** %6
%32 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %4
%34 = load i8*, i8** %6
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* %35)
%37 = add nsw i32 %36, 1
%38 = call i32 @write(i32 %33, i8* %34, i32 %37)
%39 = load i8*, i8** %6
%40 = call i32 @free(i8* %39)
store i32 0, i32* %8
br label %41
41: ; preds = %49, %2
%42 = load i32, i32* %4
%43 = call i32 @read(i32 %42, i8* %7, i32 1)
%44 = load i8, i8* %7
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %47
store i8 %44, i8* %48
br label %49
49: ; preds = %41
%50 = load i8, i8* %7
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 10
br i1 %52, label %41, label %53
53: ; preds = %49
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 79
br i1 %57, label %58, label %85
58: ; preds = %53
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%60 = getelementptr inbounds i8, i8* %59, i64 3
%61 = call i32 @sscanf(i8* %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64* %9)
%62 = load i8*, i8** %3
%63 = load i32, i32* @O_WRONLY
%64 = load i32, i32* @O_CREAT
%65 = or i32 %63, %64
%66 = call i32 @open(i8* %62, i32 %65)
store i32 %66, i32* %10
br label %67
67: ; preds = %70, %58
%68 = load i64, i64* %9
%69 = icmp ugt i64 %68, 0
br i1 %69, label %70, label %82
70: ; preds = %67
%71 = load i32, i32* %4
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%73 = call i32 @read(i32 %71, i8* %72, i32 1024)
store i32 %73, i32* %11
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = load i64, i64* %9
%77 = sub i64 %76, %75
store i64 %77, i64* %9
%78 = load i32, i32* %10
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%80 = load i32, i32* %11
%81 = call i32 @write(i32 %78, i8* %79, i32 %80)
br label %67
82: ; preds = %67
%83 = load i8*, i8** %3
%84 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %83)
br label %88
85: ; preds = %53
%86 = load i8*, i8** %3
%87 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %86)
br label %88
88: ; preds = %85, %82
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"GET \00"
@.str.1 = [9 x i8] c"cmd: %s\0A\00"
@.str.2 = [6 x i8] c"%llu\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [10 x i8] c"%s saved\0A\00"
@.str.4 = [15 x i8] c"%s not found!\0A\00"
define void @get_file(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca i8
%5 = alloca i64
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 1024)
%8 = call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 6
%10 = call i8* @malloc(i32 %9)
%11 = call i32 @strlen(i8* %0)
%12 = add nsw i32 %11, 6
%13 = call i32 @memset(i8* %10, i32 0, i32 %12)
%14 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @strcat(i8* %10, i8* %0)
%16 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%17 = call i32 @strlen(i8* %10)
%18 = add nsw i32 %17, 1
%19 = call i32 @write(i32 %1, i8* %10, i32 %18)
%20 = call i32 @free(i8* %10)
br label %21
21: ; preds = %21, %2
%22 = phi i64 [ %25, %21 ], [ 0, %2 ]
%23 = call i32 @read(i32 %1, i8* %4, i32 1)
%24 = load i8, i8* %4
%25 = add nuw i64 %22, 1
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %22
store i8 %24, i8* %26
%27 = icmp eq i8 %24, 10
br i1 %27, label %28, label %21
28: ; preds = %21
%29 = load i8, i8* %6
%30 = icmp eq i8 %29, 79
br i1 %30, label %31, label %51
31: ; preds = %28
%32 = bitcast i64* %5 to i8*
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 3
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64* %5)
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %36, %35
%38 = call i32 @open(i8* %0, i32 %37)
%39 = load i64, i64* %5
%40 = icmp eq i64 %39, 0
br i1 %40, label %49, label %41
41: ; preds = %31, %41
%42 = call i32 @read(i32 %1, i8* %6, i32 1024)
%43 = sext i32 %42 to i64
%44 = load i64, i64* %5
%45 = sub i64 %44, %43
store i64 %45, i64* %5
%46 = call i32 @write(i32 %38, i8* %6, i32 %42)
%47 = load i64, i64* %5
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %41
49: ; preds = %41, %31
%50 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %53
51: ; preds = %28
%52 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %53
53: ; preds = %51, %49
ret void
}