#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*/ ucl_header;
typedef int /*<<< orphan*/ start_addr;
typedef int /*<<< orphan*/ reset_vec;
typedef int /*<<< orphan*/ magic;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned long Read32(unsigned char *);
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ Write32(unsigned char *, unsigned long);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long fread(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ memcmp(unsigned char const *, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
unsigned long PlaceImage(char *filename, unsigned long pos,
                         unsigned char *pFirmware, unsigned long limit) {
  unsigned long size, read;
  FILE *pFile;
  unsigned long align;
  unsigned long flags;
  unsigned long load_addr = 0x09000000, exec_addr = 0x09000200;
  static const unsigned char magic[8] = {0x00, 0xe9, 0x55, 0x43,
                                         0x4c, 0xff, 0x01, 0x1a};
  unsigned char ucl_header[26];
  pFile = fopen(filename, "rb");
  if (pFile == NULL) {
    printf("Image file %s not found!\n", filename);
    exit(5);
  }
  fseek(pFile, 0, SEEK_END);
  size = ftell(pFile);
  fseek(pFile, 0, SEEK_SET);
  flags = 0x00000000;
  fread(ucl_header, 1, sizeof(ucl_header), pFile);
  if (memcmp(magic, ucl_header, sizeof(magic)) == 0) {
    if (ucl_header[12] != 0x2E && ucl_header[12] != 0x2B) {
      printf("UCL compressed files must use algorithm 2e, not %d\n",
             ucl_header[12]);
      printf("Generate with: uclpack --best --2e rockbox.bin %s\n", filename);
      exit(6);
    }
    size = Read32(ucl_header + 22);
    if (Read32(ucl_header + 18) > size) {
      flags = 0x00000001;
    }
    if (ucl_header[12] == 0x2B) {
      unsigned char start_addr[4];
      unsigned char reset_vec[4];
      fread(start_addr, 1, sizeof(start_addr), pFile);
      fread(reset_vec, 1, sizeof(reset_vec), pFile);
      fseek(pFile, 0 - sizeof(start_addr) - sizeof(reset_vec), SEEK_CUR);
      load_addr = Read32(start_addr);
      if (load_addr != 0x02000000 + pos + 16) {
        printf(
            "Error: Rombox .ucl file is linked to 0x%08X instead of 0x%08X\n",
            load_addr, 0x02000000 + pos + 16);
        exit(11);
      }
      exec_addr = Read32(reset_vec);
    }
  } else {
    fseek(pFile, 0, SEEK_SET);
  }
  if (pos + 16 + size > limit) {
    printf("Exceeding maximum image size %d\n", limit);
    exit(7);
  }
  align = (pos + 16 + size + 4096 - 1) & ~(4096 - 1);
  Write32(pFirmware + pos, load_addr);
  Write32(pFirmware + pos + 4, align - (pos + 16));
  Write32(pFirmware + pos + 8, exec_addr);
  Write32(pFirmware + pos + 12, flags);
  pos += 16;
  read = fread(pFirmware + pos, 1, size, pFile);
  if (read != size) {
    printf("Read error, expecting %d bytes, got only %d\n", size, read);
    exit(8);
  }
  fclose(pFile);
  pos += size;
  return pos;
}


x86 assembly (clang, -O0):
<PlaceImage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x9000000,-0x50(%rbp)
09
movq   $0x9000200,-0x58(%rbp)
09
mov    -0x8(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1206 <PlaceImage+0x66>
mov    -0x8(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
movq   $0x0,-0x48(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1a,%edx
call   1030 <fread@plt>
lea    -0x80(%rbp),%rsi
lea    0xd9b(%rip),%rdi
mov    $0x8,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    13b5 <PlaceImage+0x215>
movzbl -0x74(%rbp),%eax
cmp    $0x2e,%eax
je     12c1 <PlaceImage+0x121>
movzbl -0x74(%rbp),%eax
cmp    $0x2b,%eax
je     12c1 <PlaceImage+0x121>
movzbl -0x74(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x6,%edi
call   10c0 <exit@plt>
lea    -0x80(%rbp),%rdi
add    $0x16,%rdi
call   10a0 <Read32@plt>
mov    %rax,-0x28(%rbp)
lea    -0x80(%rbp),%rdi
add    $0x12,%rdi
call   10a0 <Read32@plt>
cmp    -0x28(%rbp),%rax
jbe    12f1 <PlaceImage+0x151>
movq   $0x1,-0x48(%rbp)
movzbl -0x74(%rbp),%eax
cmp    $0x2b,%eax
jne    13b0 <PlaceImage+0x210>
lea    -0x84(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
lea    -0x88(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    -0x38(%rbp),%rdi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffff8,%esi
call   1090 <fseek@plt>
lea    -0x84(%rbp),%rdi
call   10a0 <Read32@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    $0x2000000,%rcx
add    $0x10,%rcx
cmp    %rcx,%rax
je     13a0 <PlaceImage+0x200>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    $0x2000000,%rdx
add    $0x10,%rdx
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xb,%edi
call   10c0 <exit@plt>
lea    -0x88(%rbp),%rdi
call   10a0 <Read32@plt>
mov    %rax,-0x58(%rbp)
jmp    13c9 <PlaceImage+0x229>
mov    -0x38(%rbp),%rdi
mov    0x2c10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
add    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    13fb <PlaceImage+0x25b>
mov    -0x20(%rbp),%rsi
lea    0xce1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x7,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
add    -0x28(%rbp),%rax
add    $0x1000,%rax
sub    $0x1,%rax
and    $0xfffffffffffff000,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1060 <Write32@plt>
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
add    $0x4,%rdi
mov    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%rax
add    $0x10,%rax
sub    %rax,%rsi
call   1060 <Write32@plt>
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
add    $0x8,%rdi
mov    -0x58(%rbp),%rsi
call   1060 <Write32@plt>
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
add    $0xc,%rdi
mov    -0x48(%rbp),%rsi
call   1060 <Write32@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     14ce <PlaceImage+0x32e>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xc2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x8,%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PlaceImage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,0x10(%rsp)
mov    %rdx,(%rsp)
mov    %rsi,%r13
mov    %rdi,%r12
lea    0xe44(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     13de <PlaceImage+0x23e>
mov    %rax,%rbx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r14
mov    0x2dd6(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x20(%rsp),%rbp
mov    $0x1a,%edx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xdd8(%rip),%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     1261 <PlaceImage+0xc1>
mov    (%r15),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    $0x9000200,%eax
mov    %rax,0x8(%rsp)
mov    $0x9000000,%ecx
mov    %r14,%r15
jmp    1323 <PlaceImage+0x183>
movzbl 0x2c(%rsp),%esi
cmp    $0x2b,%esi
je     1275 <PlaceImage+0xd5>
cmp    $0x2e,%sil
jne    142f <PlaceImage+0x28f>
lea    0x36(%rsp),%rdi
call   10a0 <Read32@plt>
mov    %rax,%r15
lea    0x32(%rsp),%rdi
call   10a0 <Read32@plt>
cmp    %r15,%rax
seta   %r12b
cmpb   $0x2b,0x2c(%rsp)
jne    1314 <PlaceImage+0x174>
lea    0x1c(%rsp),%rbp
mov    $0x4,%edx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0x18(%rsp),%rdi
mov    $0x4,%edx
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xfffffff8,%esi
call   1090 <fseek@plt>
mov    %rbp,%rdi
call   10a0 <Read32@plt>
lea    0x2000010(%r13),%rdx
mov    %rax,%rbp
cmp    %rdx,%rax
jne    1458 <PlaceImage+0x2b8>
lea    0x18(%rsp),%rdi
call   10a0 <Read32@plt>
mov    %rax,0x8(%rsp)
mov    (%rsp),%rax
mov    %rbp,%rcx
jmp    1327 <PlaceImage+0x187>
mov    $0x9000200,%eax
mov    %rax,0x8(%rsp)
mov    $0x9000000,%ecx
mov    (%rsp),%rax
mov    %rbx,(%rsp)
lea    (%r15,%r13,1),%rbp
add    $0x10,%rbp
mov    0x10(%rsp),%rsi
cmp    %rsi,%rbp
ja     13f9 <PlaceImage+0x259>
lea    0x10(%r13),%rbx
movzbl %r12b,%edx
mov    %rdx,0x10(%rsp)
mov    %r13,%r14
lea    0xfff(%rbp),%r13
and    $0xfffffffffffff000,%r13
mov    %rax,%r12
lea    (%rax,%r14,1),%rdi
mov    %rcx,%rsi
call   1060 <Write32@plt>
lea    (%r12,%r14,1),%rdi
add    $0x4,%rdi
sub    %rbx,%r13
mov    %r13,%rsi
call   1060 <Write32@plt>
lea    (%r12,%r14,1),%rdi
add    $0x8,%rdi
mov    0x8(%rsp),%rsi
call   1060 <Write32@plt>
lea    (%r12,%r14,1),%rdi
add    $0xc,%rdi
mov    0x10(%rsp),%rsi
call   1060 <Write32@plt>
add    %rbx,%r12
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    (%rsp),%rbx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    1411 <PlaceImage+0x271>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc26(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   10c0 <exit@plt>
lea    0xccb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7,%edi
call   10c0 <exit@plt>
lea    0xcd4(%rip),%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x8,%edi
call   10c0 <exit@plt>
lea    0xbef(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc15(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x6,%edi
call   10c0 <exit@plt>
lea    0xc2d(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xb,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@PlaceImage.magic = internal constant [8 x i8] c"\00\E9UCL\FF\01\1A"
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"Image file %s not found!\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [52 x i8] c"UCL compressed files must use algorithm 2e, not %d\0A\00"
@.str.3 = [51 x i8] c"Generate with: uclpack --best --2e rockbox.bin %s\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [63 x i8] c"Error: Rombox .ucl file is linked to 0x%08X instead of 0x%08X\0A\00"
@.str.5 = [33 x i8] c"Exceeding maximum image size %d\0A\00"
@.str.6 = [45 x i8] c"Read error, expecting %d bytes, got only %d\0A\00"
define i64 @PlaceImage(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca [26 x i8]
%17 = alloca [4 x i8]
%18 = alloca [4 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i64 150994944, i64* %14
store i64 150995456, i64* %15
%19 = load i8*, i8** %5
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %11
%21 = load i32*, i32** %11
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %4
%24 = load i8*, i8** %5
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 5)
unreachable
27: ; preds = %4
%28 = load i32*, i32** %11
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %11
%32 = call i64 @ftell(i32* %31)
store i64 %32, i64* %9
%33 = load i32*, i32** %11
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
store i64 0, i64* %13
%36 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 0
%37 = load i32*, i32** %11
%38 = call i64 @fread(i8* %36, i32 1, i64 26, i32* %37)
%39 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 0
%40 = call i64 @memcmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @PlaceImage.magic, i64 0, i64 0), i8* %39, i32 8)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %103
42: ; preds = %27
%43 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 12
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = icmp ne i32 %45, 46
br i1 %46, label %47, label %60
47: ; preds = %42
%48 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 12
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = icmp ne i32 %50, 43
br i1 %51, label %52, label %60
52: ; preds = %47
%53 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 12
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = load i8*, i8** %5
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 6)
unreachable
60: ; preds = %47, %42
%61 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 0
%62 = getelementptr inbounds i8, i8* %61, i64 22
%63 = call i64 @Read32(i8* %62)
store i64 %63, i64* %9
%64 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 0
%65 = getelementptr inbounds i8, i8* %64, i64 18
%66 = call i64 @Read32(i8* %65)
%67 = load i64, i64* %9
%68 = icmp ugt i64 %66, %67
br i1 %68, label %69, label %70
69: ; preds = %60
store i64 1, i64* %13
br label %70
70: ; preds = %69, %60
%71 = getelementptr inbounds [26 x i8], [26 x i8]* %16, i64 0, i64 12
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = icmp eq i32 %73, 43
br i1 %74, label %75, label %102
75: ; preds = %70
%76 = getelementptr inbounds [4 x i8], [4 x i8]* %17, i64 0, i64 0
%77 = load i32*, i32** %11
%78 = call i64 @fread(i8* %76, i32 1, i64 4, i32* %77)
%79 = getelementptr inbounds [4 x i8], [4 x i8]* %18, i64 0, i64 0
%80 = load i32*, i32** %11
%81 = call i64 @fread(i8* %79, i32 1, i64 4, i32* %80)
%82 = load i32*, i32** %11
%83 = load i32, i32* @SEEK_CUR
%84 = call i32 @fseek(i32* %82, i32 -8, i32 %83)
%85 = getelementptr inbounds [4 x i8], [4 x i8]* %17, i64 0, i64 0
%86 = call i64 @Read32(i8* %85)
store i64 %86, i64* %14
%87 = load i64, i64* %14
%88 = load i64, i64* %6
%89 = add i64 33554432, %88
%90 = add i64 %89, 16
%91 = icmp ne i64 %87, %90
br i1 %91, label %92, label %99
92: ; preds = %75
%93 = load i64, i64* %14
%94 = load i64, i64* %6
%95 = add i64 33554432, %94
%96 = add i64 %95, 16
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i64 %93, i64 %96)
%98 = call i32 @exit(i32 11)
unreachable
99: ; preds = %75
%100 = getelementptr inbounds [4 x i8], [4 x i8]* %18, i64 0, i64 0
%101 = call i64 @Read32(i8* %100)
store i64 %101, i64* %15
br label %102
102: ; preds = %99, %70
br label %107
103: ; preds = %27
%104 = load i32*, i32** %11
%105 = load i32, i32* @SEEK_SET
%106 = call i32 @fseek(i32* %104, i32 0, i32 %105)
br label %107
107: ; preds = %103, %102
%108 = load i64, i64* %6
%109 = add i64 %108, 16
%110 = load i64, i64* %9
%111 = add i64 %109, %110
%112 = load i64, i64* %8
%113 = icmp ugt i64 %111, %112
br i1 %113, label %114, label %118
114: ; preds = %107
%115 = load i64, i64* %8
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %115)
%117 = call i32 @exit(i32 7)
unreachable
118: ; preds = %107
%119 = load i64, i64* %6
%120 = add i64 %119, 16
%121 = load i64, i64* %9
%122 = add i64 %120, %121
%123 = add i64 %122, 4096
%124 = sub i64 %123, 1
%125 = and i64 %124, -4096
store i64 %125, i64* %12
%126 = load i8*, i8** %7
%127 = load i64, i64* %6
%128 = getelementptr inbounds i8, i8* %126, i64 %127
%129 = load i64, i64* %14
%130 = call i32 @Write32(i8* %128, i64 %129)
%131 = load i8*, i8** %7
%132 = load i64, i64* %6
%133 = getelementptr inbounds i8, i8* %131, i64 %132
%134 = getelementptr inbounds i8, i8* %133, i64 4
%135 = load i64, i64* %12
%136 = load i64, i64* %6
%137 = add i64 %136, 16
%138 = sub i64 %135, %137
%139 = call i32 @Write32(i8* %134, i64 %138)
%140 = load i8*, i8** %7
%141 = load i64, i64* %6
%142 = getelementptr inbounds i8, i8* %140, i64 %141
%143 = getelementptr inbounds i8, i8* %142, i64 8
%144 = load i64, i64* %15
%145 = call i32 @Write32(i8* %143, i64 %144)
%146 = load i8*, i8** %7
%147 = load i64, i64* %6
%148 = getelementptr inbounds i8, i8* %146, i64 %147
%149 = getelementptr inbounds i8, i8* %148, i64 12
%150 = load i64, i64* %13
%151 = call i32 @Write32(i8* %149, i64 %150)
%152 = load i64, i64* %6
%153 = add i64 %152, 16
store i64 %153, i64* %6
%154 = load i8*, i8** %7
%155 = load i64, i64* %6
%156 = getelementptr inbounds i8, i8* %154, i64 %155
%157 = load i64, i64* %9
%158 = load i32*, i32** %11
%159 = call i64 @fread(i8* %156, i32 1, i64 %157, i32* %158)
store i64 %159, i64* %10
%160 = load i64, i64* %10
%161 = load i64, i64* %9
%162 = icmp ne i64 %160, %161
br i1 %162, label %163, label %168
163: ; preds = %118
%164 = load i64, i64* %9
%165 = load i64, i64* %10
%166 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i64 %164, i64 %165)
%167 = call i32 @exit(i32 8)
unreachable
168: ; preds = %118
%169 = load i32*, i32** %11
%170 = call i32 @fclose(i32* %169)
%171 = load i64, i64* %9
%172 = load i64, i64* %6
%173 = add i64 %172, %171
store i64 %173, i64* %6
%174 = load i64, i64* %6
ret i64 %174
}

LLVM IR (-O2):
@PlaceImage.magic = internal constant [8 x i8] c"\00\E9UCL\FF\01\1A"
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"Image file %s not found!\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [52 x i8] c"UCL compressed files must use algorithm 2e, not %d\0A\00"
@.str.3 = [51 x i8] c"Generate with: uclpack --best --2e rockbox.bin %s\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [63 x i8] c"Error: Rombox .ucl file is linked to 0x%08X instead of 0x%08X\0A\00"
@.str.5 = [33 x i8] c"Exceeding maximum image size %d\0A\00"
@.str.6 = [45 x i8] c"Read error, expecting %d bytes, got only %d\0A\00"
define i64 @PlaceImage(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca [26 x i8]
%6 = alloca [4 x i8]
%7 = alloca [4 x i8]
%8 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 0
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %4
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 5)
unreachable
14: ; preds = %4
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %9, i32 0, i32 %15)
%17 = tail call i64 @ftell(i32* %9)
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @fseek(i32* %9, i32 0, i32 %18)
%20 = call i64 @fread(i8* %8, i32 1, i64 26, i32* %9)
%21 = call i64 @memcmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @PlaceImage.magic, i64 0, i64 0), i8* %8, i32 8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %54
23: ; preds = %14
%24 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 12
%25 = load i8, i8* %24
switch i8 %25, label %26 [
i8 46, label %31
i8 43, label %31
]
26: ; preds = %23
%27 = zext i8 %25 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%30 = call i32 @exit(i32 6)
unreachable
31: ; preds = %23, %23
%32 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 22
%33 = call i64 @Read32(i8* %32)
%34 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 18
%35 = call i64 @Read32(i8* %34)
%36 = icmp ugt i64 %35, %33
%37 = load i8, i8* %24
%38 = icmp eq i8 %37, 43
br i1 %38, label %39, label %57
39: ; preds = %31
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%42 = call i64 @fread(i8* %40, i32 1, i64 4, i32* %9)
%43 = call i64 @fread(i8* %41, i32 1, i64 4, i32* %9)
%44 = load i32, i32* @SEEK_CUR
%45 = call i32 @fseek(i32* %9, i32 -8, i32 %44)
%46 = call i64 @Read32(i8* %40)
%47 = add i64 %1, 33554448
%48 = icmp eq i64 %46, %47
br i1 %48, label %52, label %49
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.4, i64 0, i64 0), i64 %46, i64 %47)
%51 = call i32 @exit(i32 11)
unreachable
52: ; preds = %39
%53 = call i64 @Read32(i8* %41)
br label %57
54: ; preds = %14
%55 = load i32, i32* @SEEK_SET
%56 = call i32 @fseek(i32* %9, i32 0, i32 %55)
br label %57
57: ; preds = %31, %52, %54
%58 = phi i64 [ %33, %52 ], [ %33, %31 ], [ %17, %54 ]
%59 = phi i1 [ %36, %52 ], [ %36, %31 ], [ false, %54 ]
%60 = phi i64 [ %46, %52 ], [ 150994944, %31 ], [ 150994944, %54 ]
%61 = phi i64 [ %53, %52 ], [ 150995456, %31 ], [ 150995456, %54 ]
%62 = add i64 %1, 16
%63 = add i64 %58, %62
%64 = icmp ugt i64 %63, %3
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %3)
%67 = call i32 @exit(i32 7)
unreachable
68: ; preds = %57
%69 = zext i1 %59 to i64
%70 = add i64 %63, 4095
%71 = and i64 %70, -4096
%72 = getelementptr inbounds i8, i8* %2, i64 %1
%73 = call i32 @Write32(i8* %72, i64 %60)
%74 = getelementptr inbounds i8, i8* %72, i64 4
%75 = sub i64 %71, %62
%76 = call i32 @Write32(i8* %74, i64 %75)
%77 = getelementptr inbounds i8, i8* %72, i64 8
%78 = call i32 @Write32(i8* %77, i64 %61)
%79 = getelementptr inbounds i8, i8* %72, i64 12
%80 = call i32 @Write32(i8* %79, i64 %69)
%81 = getelementptr inbounds i8, i8* %2, i64 %62
%82 = call i64 @fread(i8* %81, i32 1, i64 %58, i32* %9)
%83 = icmp eq i64 %82, %58
br i1 %83, label %87, label %84
84: ; preds = %68
%85 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i64 %58, i64 %82)
%86 = call i32 @exit(i32 8)
unreachable
87: ; preds = %68
%88 = call i32 @fclose(i32* %9)
ret i64 %63
}