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

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void recv_file_from(int sock_fd, const char *file_size, const char *file_path) {
  int bytes_recv, i_file_size = atoi(file_size);
  char buffer[(512)];
  FILE *file_stream;
  file_stream = fopen(file_path, "w");
  if (file_stream == NULL) {
    fprintf(stderr, "[ERROR] Could not create file: %s\n", file_path);
    exit(-1);
  }
  memset(buffer, '\0', (512));
  while (i_file_size > 0 &&
         (bytes_recv = recv(sock_fd, buffer, (512), 0)) > 0) {
    fwrite(buffer, sizeof(char), bytes_recv, file_stream);
    i_file_size -= bytes_recv;
    fprintf(stdout, "[INFO] %d bytes received - %d remaining\n", bytes_recv,
            i_file_size);
    memset(buffer, '\0', (512));
  }
  if (i_file_size > 0) {
    fprintf(stderr,
            "[WARNING] Maybe something happened while transfering:"
            " %s\n"
            "          Apparently the transfer was not completed: "
            "%i bytes missing.\n",
            file_path, i_file_size);
  }
  if (fclose(file_stream) != 0) {
    fprintf(stderr,
            "[ERROR] An error occurred while trying to close"
            "        the file: %s\n",
            file_path);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<recv_file_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    11ec <recv_file_from+0x6c>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x229(%rbp)
jle    1236 <recv_file_from+0xb6>
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x229(%rbp)
mov    -0x229(%rbp),%al
test   $0x1,%al
jne    1249 <recv_file_from+0xc9>
jmp    12a4 <recv_file_from+0x124>
lea    -0x220(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x228(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
jmp    11ff <recv_file_from+0x7f>
cmpl   $0x0,-0x20(%rbp)
jle    12cc <recv_file_from+0x14c>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x228(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1307 <recv_file_from+0x187>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_file_from>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdx,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
lea    0xe58(%rip),%rsi
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1289 <recv_file_from+0x109>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
test   %ebp,%ebp
jle    126a <recv_file_from+0xea>
lea    0x10(%rsp),%rbx
lea    0xe3f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    124c <recv_file_from+0xcc>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%ecx
sub    %r14d,%ecx
mov    %r13,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
sub    %r14d,%ebp
jg     11f0 <recv_file_from+0x70>
jmp    126a <recv_file_from+0xea>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    129b <recv_file_from+0x11b>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
jmp    12ab <recv_file_from+0x12b>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    0x8(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"[ERROR] Could not create file: %s\0A\00"
@stdout = global i32 0
@.str.2 = [41 x i8] c"[INFO] %d bytes received - %d remaining\0A\00"
@.str.3 = [129 x i8] c"[WARNING] Maybe something happened while transfering: %s\0A Apparently the transfer was not completed: %i bytes missing.\0A\00"
@.str.4 = [69 x i8] c"[ERROR] An error occurred while trying to close the file: %s\0A\00"
define void @recv_file_from(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %5
%12 = call i32 @atoi(i8* %11)
store i32 %12, i32* %8
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %6
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %3
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i8 signext 0, i32 512)
br label %25
25: ; preds = %35, %22
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i32, i32* %4
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%31 = call i32 @recv(i32 %29, i8* %30, i32 512, i32 0)
store i32 %31, i32* %7
%32 = icmp sgt i32 %31, 0
br label %33
33: ; preds = %28, %25
%34 = phi i1 [ false, %25 ], [ %32, %28 ]
br i1 %34, label %35, label %49
35: ; preds = %33
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %7
%38 = load i32*, i32** %10
%39 = call i32 @fwrite(i8* %36, i32 1, i32 %37, i32* %38)
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %8
%43 = load i32, i32* @stdout
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%48 = call i32 @memset(i8* %47, i8 signext 0, i32 512)
br label %25
49: ; preds = %33
%50 = load i32, i32* %8
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %6
%55 = load i32, i32* %8
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([129 x i8], [129 x i8]* @.str.3, i64 0, i64 0), i8* %54, i32 %55)
br label %57
57: ; preds = %52, %49
%58 = load i32*, i32** %10
%59 = call i64 @fclose(i32* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %6
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %57
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"[ERROR] Could not create file: %s\0A\00"
@stdout = global i32 0
@.str.2 = [41 x i8] c"[INFO] %d bytes received - %d remaining\0A\00"
@.str.3 = [129 x i8] c"[WARNING] Maybe something happened while transfering: %s\0A Apparently the transfer was not completed: %i bytes missing.\0A\00"
@.str.4 = [69 x i8] c"[ERROR] An error occurred while trying to close the file: %s\0A\00"
define void @recv_file_from(i32 %0, i8* nocapture %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = tail call i32 @atoi(i8* %1)
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %3
%14 = call i32 @memset(i8* %6, i8 signext 0, i32 512)
%15 = icmp sgt i32 %5, 0
br i1 %15, label %16, label %30
16: ; preds = %13, %20
%17 = phi i32 [ %22, %20 ], [ %5, %13 ]
%18 = call i32 @recv(i32 %0, i8* %6, i32 512, i32 0)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = call i32 @fwrite(i8* %6, i32 1, i32 %18, i32* %7)
%22 = sub nsw i32 %17, %18
%23 = load i32, i32* @stdout
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %22)
%25 = call i32 @memset(i8* %6, i8 signext 0, i32 512)
%26 = icmp sgt i32 %22, 0
br i1 %26, label %16, label %30
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([129 x i8], [129 x i8]* @.str.3, i64 0, i64 0), i8* %2, i32 %17)
br label %30
30: ; preds = %20, %13, %27
%31 = call i64 @fclose(i32* %7)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %30
ret void
}