#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
unsigned long fwrite(char *, int, unsigned long, int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ printf(char *, char *, unsigned long);
int /*<<< orphan*/ stderr;
void ExpandRom(unsigned long int size, char *file) {
  FILE *FP;
  char *buff;
  buff = (char *)malloc(size * sizeof(char));
  if (buff == NULL) {
    fprintf(stderr, "Error no memory was allocated!\n");
    exit(1);
  }
  FP = fopen(file, "ab");
  if (FP == NULL) {
    fprintf(stderr, "Error opening file!\n");
    exit(1);
  }
  if ((fwrite(buff, sizeof(char), size, FP)) != size) {
    fprintf(stderr, "Error expanding file!\n");
    exit(1);
  }
  fclose(FP);
  printf("\n%s has been expanded by %ld bytes\n\n", file, size);
}


x86 assembly (clang, -O0):
<ExpandRom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bb <ExpandRom+0x4b>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f9 <ExpandRom+0x89>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x8(%rbp),%rax
je     1238 <ExpandRom+0xc8>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ExpandRom>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <malloc@plt>
test   %rax,%rax
je     11df <ExpandRom+0x6f>
mov    %rax,%r12
lea    0xe8e(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f1 <ExpandRom+0x81>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1090 <fwrite@plt>
cmp    %r15,%rax
jne    1203 <ExpandRom+0x93>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe86(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <printf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
jmp    1213 <ExpandRom+0xa3>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
jmp    1213 <ExpandRom+0xa3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Error no memory was allocated!\0A\00"
@.str.1 = [3 x i8] c"ab\00"
@.str.2 = [21 x i8] c"Error opening file!\0A\00"
@.str.3 = [23 x i8] c"Error expanding file!\0A\00"
@.str.4 = [37 x i8] c"\0A%s has been expanded by %ld bytes\0A\0A\00"
define void @ExpandRom(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i64 %0, i64* %3
store i8* %1, i8** %4
%7 = load i64, i64* %3
%8 = mul i64 %7, 1
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i8*, i8** %6
%28 = load i64, i64* %3
%29 = load i32*, i32** %5
%30 = call i64 @fwrite(i8* %27, i32 1, i64 %28, i32* %29)
%31 = load i64, i64* %3
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %26
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %4
%41 = load i64, i64* %3
%42 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %40, i64 %41)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Error no memory was allocated!\0A\00"
@.str.1 = [3 x i8] c"ab\00"
@.str.2 = [21 x i8] c"Error opening file!\0A\00"
@.str.3 = [23 x i8] c"Error expanding file!\0A\00"
@.str.4 = [37 x i8] c"\0A%s has been expanded by %ld bytes\0A\0A\00"
define void @ExpandRom(i64 %0, i8* %1) {
%3 = tail call i64 @malloc(i64 %0)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i64 @fwrite(i8* %4, i32 1, i64 %0, i32* %11)
%19 = icmp eq i64 %18, %0
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = tail call i32 @fclose(i32* %11)
%26 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %1, i64 %0)
ret void
}