#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *, char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void LoadRAM(const char *file, const char *ramfile) {
  unsigned char buff[256 * 1024];
  FILE *fp = fopen(file, "rb");
  FILE *ram = fopen(ramfile, "wb");
  fseek(fp, 0, SEEK_END);
  int size = ftell(fp);
  rewind(fp);
  int block = 0, read = 0;
  while (read < size) {
    if (read + 256 * 1024 > size)
      block = size - read;
    else
      block = 256 * 1024;
    fread(buff, 1, 256 * 1024, fp);
    fwrite(buff, 1, 256 * 1024, ram);
    read += 256 * 1024;
  }
  printf("%s loaded at %s\n", file, ramfile);
  fclose(fp);
  fclose(ram);
}


x86 assembly (clang, -O0):
<LoadRAM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40018(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40020(%rbp)
mov    -0x40018(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x40018(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x40024(%rbp)
mov    -0x40018(%rbp),%rdi
call   1060 <rewind@plt>
movl   $0x0,-0x40028(%rbp)
movl   $0x0,-0x4002c(%rbp)
mov    -0x4002c(%rbp),%eax
cmp    -0x40024(%rbp),%eax
jge    12a4 <LoadRAM+0x124>
mov    -0x4002c(%rbp),%eax
add    $0x40000,%eax
cmp    -0x40024(%rbp),%eax
jle    124a <LoadRAM+0xca>
mov    -0x40024(%rbp),%eax
sub    -0x4002c(%rbp),%eax
mov    %eax,-0x40028(%rbp)
jmp    1254 <LoadRAM+0xd4>
movl   $0x40000,-0x40028(%rbp)
lea    -0x40010(%rbp),%rdi
mov    -0x40018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40000,%edx
call   1030 <fread@plt>
lea    -0x40010(%rbp),%rdi
mov    -0x40020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40000,%edx
call   10a0 <fwrite@plt>
mov    -0x4002c(%rbp),%eax
add    $0x40000,%eax
mov    %eax,-0x4002c(%rbp)
jmp    120a <LoadRAM+0x8a>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd53(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40018(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40020(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadRAM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40018,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r13
lea    0xe56(%rip),%rsi
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r12
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r13,%rdi
call   1070 <ftell@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1060 <rewind@plt>
test   %ebp,%ebp
jle    1226 <LoadRAM+0xa6>
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
add    $0x40000,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x40000,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x40000,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
cmp    %ebp,%r14d
jl     11f0 <LoadRAM+0x70>
lea    0xdd9(%rip),%rdi
mov    %r15,%rsi
mov    0x8(%rsp),%rdx
call   1050 <printf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
add    $0x40018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@.str.2 = [17 x i8] c"%s loaded at %s\0A\00"
define void @LoadRAM(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [262144 x i8]
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %6
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %6
%19 = call i32 @ftell(i32* %18)
store i32 %19, i32* %8
%20 = load i32*, i32** %6
%21 = call i32 @rewind(i32* %20)
store i32 0, i32* %9
store i32 0, i32* %10
br label %22
22: ; preds = %36, %2
%23 = load i32, i32* %10
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %45
26: ; preds = %22
%27 = load i32, i32* %10
%28 = add nsw i32 %27, 262144
%29 = load i32, i32* %8
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* %8
%33 = load i32, i32* %10
%34 = sub nsw i32 %32, %33
store i32 %34, i32* %9
br label %36
35: ; preds = %26
store i32 262144, i32* %9
br label %36
36: ; preds = %35, %31
%37 = getelementptr inbounds [262144 x i8], [262144 x i8]* %5, i64 0, i64 0
%38 = load i32*, i32** %6
%39 = call i32 @fread(i8* %37, i32 1, i32 262144, i32* %38)
%40 = getelementptr inbounds [262144 x i8], [262144 x i8]* %5, i64 0, i64 0
%41 = load i32*, i32** %7
%42 = call i32 @fwrite(i8* %40, i32 1, i32 262144, i32* %41)
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 262144
store i32 %44, i32* %10
br label %22
45: ; preds = %22
%46 = load i8*, i8** %3
%47 = load i8*, i8** %4
%48 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %47)
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
%51 = load i32*, i32** %7
%52 = call i32 @fclose(i32* %51)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@.str.2 = [17 x i8] c"%s loaded at %s\0A\00"
define void @LoadRAM(i8* %0, i8* %1) {
%3 = alloca [262144 x i8]
%4 = getelementptr inbounds [262144 x i8], [262144 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %5, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %5)
%10 = tail call i32 @rewind(i32* %5)
%11 = icmp sgt i32 %9, 0
br i1 %11, label %12, label %18
12: ; preds = %2, %12
%13 = phi i32 [ %14, %12 ], [ 0, %2 ]
%14 = add nuw nsw i32 %13, 262144
%15 = call i32 @fread(i8* %4, i32 1, i32 262144, i32* %5)
%16 = call i32 @fwrite(i8* %4, i32 1, i32 262144, i32* %6)
%17 = icmp slt i32 %14, %9
br i1 %17, label %12, label %18
18: ; preds = %12, %2
%19 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%20 = tail call i32 @fclose(i32* %5)
%21 = tail call i32 @fclose(i32* %6)
ret void
}