#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 cart_direct_write(unsigned char *, int, int /*<<< orphan*/, int, int,
                      int /*<<< orphan*/, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *prepare_loadandwrite_sram(char const *, int, int);
int /*<<< orphan*/ printerr(char *, int, char const *, int);
int f2au_loadandwrite_sram(const char *file, int offset, int size) {
  int ret;
  unsigned char *sram;
  if (size != 65536) {
    printerr(
        "Could not write %i bytes from file %s to SRAM, %i bytes expected\n",
        size, file, 65536);
    return -1;
  }
  if ((sram = prepare_loadandwrite_sram(file, offset, size)) == NULL)
    return -1;
  ret = cart_direct_write(sram, (0x0e000000 + 0x0f0000), 0, 65536, 1024, 0,
                          65536);
  free(sram);
  return ret;
}


x86 assembly (clang, -O0):
<f2au_loadandwrite_sram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x10000,-0x18(%rbp)
je     1183 <f2au_loadandwrite_sram+0x43>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe93(%rip),%rdi
mov    $0x10000,%ecx
call   1050 <printerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <f2au_loadandwrite_sram+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <prepare_loadandwrite_sram@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ac <f2au_loadandwrite_sram+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <f2au_loadandwrite_sram+0xa4>
mov    -0x28(%rbp),%rdi
mov    $0xe0f0000,%esi
xor    %r9d,%r9d
mov    $0x10000,%ecx
mov    $0x400,%r8d
mov    %r9d,%edx
movl   $0x10000,(%rsp)
call   1060 <cart_direct_write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f2au_loadandwrite_sram>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rax
cmp    $0x10000,%edx
jne    1195 <f2au_loadandwrite_sram+0x55>
mov    %rax,%rdi
mov    $0x10000,%edx
call   1040 <prepare_loadandwrite_sram@plt>
test   %rax,%rax
je     11ab <f2au_loadandwrite_sram+0x6b>
mov    %rax,%rbx
movl   $0x10000,(%rsp)
mov    %rax,%rdi
mov    $0xe0f0000,%esi
xor    %edx,%edx
mov    $0x10000,%ecx
mov    $0x400,%r8d
xor    %r9d,%r9d
call   1060 <cart_direct_write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    11b0 <f2au_loadandwrite_sram+0x70>
lea    0xe64(%rip),%rdi
mov    %edx,%esi
mov    %rax,%rdx
mov    $0x10000,%ecx
call   1050 <printerr@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [66 x i8] c"Could not write %i bytes from file %s to SRAM, %i bytes expected\0A\00"
define i32 @f2au_loadandwrite_sram(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 65536
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* %7
%14 = load i8*, i8** %5
%15 = call i32 @printerr(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14, i32 65536)
store i32 -1, i32* %4
br label %29
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = call i8* @prepare_loadandwrite_sram(i8* %17, i32 %18, i32 %19)
store i8* %20, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %4
br label %29
23: ; preds = %16
%24 = load i8*, i8** %9
%25 = call i32 @cart_direct_write(i8* %24, i32 235864064, i32 0, i32 65536, i32 1024, i32 0, i32 65536)
store i32 %25, i32* %8
%26 = load i8*, i8** %9
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %29
29: ; preds = %23, %22, %12
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [66 x i8] c"Could not write %i bytes from file %s to SRAM, %i bytes expected\0A\00"
define i32 @f2au_loadandwrite_sram(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 65536
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @printerr(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i32 %2, i8* %0, i32 65536)
br label %13
7: ; preds = %3
%8 = tail call i8* @prepare_loadandwrite_sram(i8* %0, i32 %1, i32 65536)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = tail call i32 @cart_direct_write(i8* %8, i32 235864064, i32 0, i32 65536, i32 1024, i32 0, i32 65536)
%12 = tail call i32 @free(i8* %8)
br label %13
13: ; preds = %7, %10, %5
%14 = phi i32 [ -1, %5 ], [ %11, %10 ], [ -1, %7 ]
ret i32 %14
}