#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*/ FseekEnd(int /*<<< orphan*/ *, long);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
char ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
long negoffset(long, long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  FILE *fp, *fw1, *fw2;
  long offset1, offset2;
  if (argc != 4 && argc != 6)
    usage(argv[0]);
  fp = fopen(argv[1], "rb+");
  if (fp == NULL) {
    puts("Error opening firmware image!");
    exit(1);
  }
  if (argc == 6) {
    fw1 = fopen(argv[2], "rb");
    fw2 = fopen(argv[3], "rb");
    offset1 = strtoul(argv[4], NULL, 0);
    offset2 = strtoul(argv[5], NULL, 0);
    if (fw1 == NULL || fw2 == NULL) {
      puts("Error opening file!");
      exit(1);
    }
  } else {
    fw1 = NULL;
    fw2 = NULL;
    offset1 = strtoul(argv[2], NULL, 0);
    offset2 = strtoul(argv[3], NULL, 0);
  }
  if ((offset1 & 0xff) || (offset2 & 0xff)) {
    puts("The offsets must be aligned to 0x100");
    exit(1);
  }
  long romsz;
  FseekEnd(fp, -1);
  romsz = ftell(fp) + 1;
  printf("size of %s: 0x%lx\n", argv[1], romsz);
  if (romsz & 0xff) {
    puts("The ROM size must be multiple of 0x100");
    exit(1);
  }
  offset1 = negoffset(offset1, romsz);
  offset2 = negoffset(offset2, romsz);
  char offs[8];
  long os;
  os = 0x1000000 + offset1;
  offs[0] = os >> 16;
  offs[1] = os >> 8;
  offs[2] = 0xff - offs[0];
  offs[3] = 0xff - offs[1];
  os = 0x1000000 + offset2;
  offs[4] = os >> 16;
  offs[5] = os >> 8;
  offs[6] = 0xff - offs[4];
  offs[7] = 0xff - offs[5];
  for (size_t i = 0; i < 8; i++)
    printf("%02hhx ", offs[i]);
  puts("");
  FseekEnd(fp, -0x100);
  printf("writing to 0x%lx\n", ftell(fp));
  fwrite(offs, 1, 8, fp);
  if (argc == 6) {
    char c;
    FseekEnd(fp, offset1);
    printf("writing to 0x%lx\n", ftell(fp));
    while (fread(&c, 1, 1, fw1) == 1)
      fwrite(&c, 1, 1, fp);
    FseekEnd(fp, offset2);
    printf("writing to 0x%lx\n", ftell(fp));
    while (fread(&c, 1, 1, fw2) == 1)
      fwrite(&c, 1, 1, fp);
    fclose(fw1);
    fclose(fw2);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11f6 <main+0x36>
cmpl   $0x6,-0x8(%rbp)
je     11f6 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfb(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122f <main+0x6f>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x6,-0x8(%rbp)
jne    12c8 <main+0x108>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdda(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdc2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ad <main+0xed>
cmpq   $0x0,-0x28(%rbp)
jne    12c3 <main+0x103>
lea    0xd71(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1306 <main+0x146>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
and    $0xff,%rax
cmp    $0x0,%rax
jne    132e <main+0x16e>
mov    -0x38(%rbp),%rax
and    $0xff,%rax
cmp    $0x0,%rax
je     1344 <main+0x184>
lea    0xd04(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0xffffffffffffffff,%rsi
call   10e0 <FseekEnd@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
movsbl %al,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x40(%rbp),%rdx
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%rax
and    $0xff,%rax
cmp    $0x0,%rax
je     13ad <main+0x1ed>
lea    0xcd3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <negoffset@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <negoffset@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1000000,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
sar    $0x10,%rax
mov    %al,-0x48(%rbp)
mov    -0x50(%rbp),%rax
sar    $0x8,%rax
mov    %al,-0x47(%rbp)
movsbl -0x48(%rbp),%ecx
mov    $0xff,%eax
sub    %ecx,%eax
mov    %al,-0x46(%rbp)
movsbl -0x47(%rbp),%ecx
mov    $0xff,%eax
sub    %ecx,%eax
mov    %al,-0x45(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1000000,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
sar    $0x10,%rax
mov    %al,-0x44(%rbp)
mov    -0x50(%rbp),%rax
sar    $0x8,%rax
mov    %al,-0x43(%rbp)
movsbl -0x44(%rbp),%ecx
mov    $0xff,%eax
sub    %ecx,%eax
mov    %al,-0x42(%rbp)
movsbl -0x43(%rbp),%ecx
mov    $0xff,%eax
sub    %ecx,%eax
mov    %al,-0x41(%rbp)
movq   $0x0,-0x58(%rbp)
cmpq   $0x8,-0x58(%rbp)
jae    148a <main+0x2ca>
mov    -0x58(%rbp),%rax
movsbl -0x48(%rbp,%rax,1),%esi
lea    0xc26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1457 <main+0x297>
lea    0xbdf(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rdi
mov    $0xffffffffffffff00,%rsi
call   10e0 <FseekEnd@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
movsbl %al,%esi
lea    0xbe7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   10d0 <fwrite@plt>
cmpl   $0x6,-0x8(%rbp)
jne    15ad <main+0x3ed>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10e0 <FseekEnd@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
movsbl %al,%esi
lea    0xb9f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rcx
lea    -0x59(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    153e <main+0x37e>
mov    -0x18(%rbp),%rcx
lea    -0x59(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10d0 <fwrite@plt>
jmp    1508 <main+0x348>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10e0 <FseekEnd@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
movsbl %al,%esi
lea    0xb42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rcx
lea    -0x59(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    159b <main+0x3db>
mov    -0x18(%rbp),%rcx
lea    -0x59(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10d0 <fwrite@plt>
jmp    1565 <main+0x3a5>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %edi,%eax
and    $0xfffffffd,%eax
cmp    $0x4,%eax
je     1206 <main+0x26>
mov    0x0(%rbp),%rdi
call   10b0 <usage@plt>
mov    0x8(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1580 <main+0x3a0>
mov    %rax,%r15
mov    0x10(%rbp),%rdi
mov    %ebx,0xc(%rsp)
cmp    $0x6,%ebx
jne    1284 <main+0xa4>
lea    0xdec(%rip),%r14
mov    %r14,%rsi
call   10c0 <fopen@plt>
mov    %rax,%r12
mov    0x18(%rbp),%rdi
mov    %r14,%rsi
call   10c0 <fopen@plt>
mov    %rax,%r14
mov    0x20(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <strtoul@plt>
mov    %rax,%rbx
mov    0x28(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <strtoul@plt>
test   %r12,%r12
je     159b <main+0x3bb>
test   %r14,%r14
je     159b <main+0x3bb>
mov    %rax,%r13
jmp    12a6 <main+0xc6>
xor    %r14d,%r14d
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <strtoul@plt>
mov    %rax,%rbx
mov    0x18(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <strtoul@plt>
mov    %rax,%r13
xor    %r12d,%r12d
mov    %ebx,%eax
or     %r13b,%al
jne    1589 <main+0x3a9>
mov    %r12,0x20(%rsp)
mov    %r14,0x28(%rsp)
mov    %r15,%rdi
mov    $0xffffffffffffffff,%rsi
call   1100 <FseekEnd@plt>
mov    %r15,%rdi
call   10a0 <ftell@plt>
movsbq %al,%r14
add    $0x1,%r14
mov    0x8(%rbp),%rsi
lea    0xd79(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14b,%r14b
mov    %r15,0x30(%rsp)
jne    1592 <main+0x3b2>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <negoffset@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <negoffset@plt>
mov    %rbx,0x10(%rsp)
add    $0x1000000,%rbx
mov    %rbx,%rcx
shr    $0x10,%rcx
mov    %cl,0x4(%rsp)
shr    $0x8,%rbx
mov    %bl,0x5(%rsp)
movsbl %cl,%esi
mov    %ecx,%r12d
not    %r12b
mov    %r12b,0x6(%rsp)
mov    %ebx,%ebp
not    %bpl
mov    %bpl,0x7(%rsp)
mov    %rax,0x18(%rsp)
lea    0x1000000(%rax),%r15
mov    %r15,%r14
shr    $0x10,%r14
mov    %r14b,0x8(%rsp)
shr    $0x8,%r15
mov    %r15b,0x9(%rsp)
mov    %r14d,%r13d
not    %r13b
mov    %r13b,0xa(%rsp)
mov    %r15d,%eax
not    %al
mov    %al,0x3(%rsp)
mov    %al,0xb(%rsp)
lea    0xd0b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %bl,%esi
lea    0xcfa(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %r12b,%esi
lea    0xce8(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %bpl,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %r14b,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %r15b,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl %r13b,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x3(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xffffffffffffff00,%rsi
call   1100 <FseekEnd@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
movsbl %al,%esi
lea    0xc6f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   10f0 <fwrite@plt>
cmpl   $0x6,0xc(%rsp)
jne    1567 <main+0x387>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1100 <FseekEnd@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
movsbl %al,%esi
lea    0xc27(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x2(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    0x20(%rsp),%r15
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    14d7 <main+0x2f7>
lea    0x2(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
movsbl 0x2(%rsp),%edi
mov    %rbx,%rsi
call   1090 <fputc@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     14b0 <main+0x2d0>
mov    %rbx,%rdi
mov    0x18(%rsp),%rsi
call   1100 <FseekEnd@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
movsbl %al,%esi
lea    0xbaa(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x2(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    0x28(%rsp),%rbp
mov    %rbp,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    1557 <main+0x377>
lea    0x2(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x2(%rsp),%edi
mov    %rbx,%rsi
call   1090 <fputc@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1530 <main+0x350>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xa7d(%rip),%rdi
jmp    15a2 <main+0x3c2>
lea    0xaa9(%rip),%rdi
jmp    15a2 <main+0x3c2>
lea    0xad8(%rip),%rdi
jmp    15a2 <main+0x3c2>
lea    0xa83(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"rb+\00"
@.str.1 = [30 x i8] c"Error opening firmware image!\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [20 x i8] c"Error opening file!\00"
@.str.4 = [37 x i8] c"The offsets must be aligned to 0x100\00"
@.str.5 = [19 x i8] c"size of %s: 0x%lx\0A\00"
@.str.6 = [39 x i8] c"The ROM size must be multiple of 0x100\00"
@.str.7 = [8 x i8] c"%02hhx \00"
@.str.8 = [1 x i8] zeroinitializer
@.str.9 = [18 x i8] c"writing to 0x%lx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca [8 x i8]
%13 = alloca i64
%14 = alloca i64
%15 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 6
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @usage(i8* %24)
br label %26
26: ; preds = %21, %18, %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 @puts(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 6
br i1 %38, label %39, label %65
39: ; preds = %36
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %43, i32** %7
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 3
%46 = load i8*, i8** %45
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %8
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 4
%50 = load i8*, i8** %49
%51 = call i64 @strtoul(i8* %50, i32* null, i32 0)
store i64 %51, i64* %9
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 5
%54 = load i8*, i8** %53
%55 = call i64 @strtoul(i8* %54, i32* null, i32 0)
store i64 %55, i64* %10
%56 = load i32*, i32** %7
%57 = icmp eq i32* %56, null
br i1 %57, label %61, label %58
58: ; preds = %39
%59 = load i32*, i32** %8
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58, %39
%62 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %58
br label %74
65: ; preds = %36
store i32* null, i32** %7
store i32* null, i32** %8
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = call i64 @strtoul(i8* %68, i32* null, i32 0)
store i64 %69, i64* %9
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 3
%72 = load i8*, i8** %71
%73 = call i64 @strtoul(i8* %72, i32* null, i32 0)
store i64 %73, i64* %10
br label %74
74: ; preds = %65, %64
%75 = load i64, i64* %9
%76 = and i64 %75, 255
%77 = icmp ne i64 %76, 0
br i1 %77, label %82, label %78
78: ; preds = %74
%79 = load i64, i64* %10
%80 = and i64 %79, 255
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %78, %74
%83 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %78
%86 = load i32*, i32** %6
%87 = call i32 @FseekEnd(i32* %86, i64 -1)
%88 = load i32*, i32** %6
%89 = call signext i8 @ftell(i32* %88)
%90 = sext i8 %89 to i32
%91 = add nsw i32 %90, 1
%92 = sext i32 %91 to i64
store i64 %92, i64* %11
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 1
%95 = load i8*, i8** %94
%96 = load i64, i64* %11
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %95, i64 %96)
%98 = load i64, i64* %11
%99 = and i64 %98, 255
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %85
%102 = call i32 @puts(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %85
%105 = load i64, i64* %9
%106 = load i64, i64* %11
%107 = call i64 @negoffset(i64 %105, i64 %106)
store i64 %107, i64* %9
%108 = load i64, i64* %10
%109 = load i64, i64* %11
%110 = call i64 @negoffset(i64 %108, i64 %109)
store i64 %110, i64* %10
%111 = load i64, i64* %9
%112 = add nsw i64 16777216, %111
store i64 %112, i64* %13
%113 = load i64, i64* %13
%114 = ashr i64 %113, 16
%115 = trunc i64 %114 to i8
%116 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
store i8 %115, i8* %116
%117 = load i64, i64* %13
%118 = ashr i64 %117, 8
%119 = trunc i64 %118 to i8
%120 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 1
store i8 %119, i8* %120
%121 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%122 = load i8, i8* %121
%123 = sext i8 %122 to i32
%124 = sub nsw i32 255, %123
%125 = trunc i32 %124 to i8
%126 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 2
store i8 %125, i8* %126
%127 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 1
%128 = load i8, i8* %127
%129 = sext i8 %128 to i32
%130 = sub nsw i32 255, %129
%131 = trunc i32 %130 to i8
%132 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 3
store i8 %131, i8* %132
%133 = load i64, i64* %10
%134 = add nsw i64 16777216, %133
store i64 %134, i64* %13
%135 = load i64, i64* %13
%136 = ashr i64 %135, 16
%137 = trunc i64 %136 to i8
%138 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 4
store i8 %137, i8* %138
%139 = load i64, i64* %13
%140 = ashr i64 %139, 8
%141 = trunc i64 %140 to i8
%142 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 5
store i8 %141, i8* %142
%143 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 4
%144 = load i8, i8* %143
%145 = sext i8 %144 to i32
%146 = sub nsw i32 255, %145
%147 = trunc i32 %146 to i8
%148 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 6
store i8 %147, i8* %148
%149 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 5
%150 = load i8, i8* %149
%151 = sext i8 %150 to i32
%152 = sub nsw i32 255, %151
%153 = trunc i32 %152 to i8
%154 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 7
store i8 %153, i8* %154
store i64 0, i64* %14
br label %155
155: ; preds = %164, %104
%156 = load i64, i64* %14
%157 = icmp ult i64 %156, 8
br i1 %157, label %158, label %167
158: ; preds = %155
%159 = load i64, i64* %14
%160 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 %159
%161 = load i8, i8* %160
%162 = sext i8 %161 to i32
%163 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %162)
br label %164
164: ; preds = %158
%165 = load i64, i64* %14
%166 = add i64 %165, 1
store i64 %166, i64* %14
br label %155
167: ; preds = %155
%168 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%169 = load i32*, i32** %6
%170 = call i32 @FseekEnd(i32* %169, i64 -256)
%171 = load i32*, i32** %6
%172 = call signext i8 @ftell(i32* %171)
%173 = sext i8 %172 to i32
%174 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %173)
%175 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%176 = load i32*, i32** %6
%177 = call i32 @fwrite(i8* %175, i32 1, i32 8, i32* %176)
%178 = load i32, i32* %4
%179 = icmp eq i32 %178, 6
br i1 %179, label %180, label %215
180: ; preds = %167
%181 = load i32*, i32** %6
%182 = load i64, i64* %9
%183 = call i32 @FseekEnd(i32* %181, i64 %182)
%184 = load i32*, i32** %6
%185 = call signext i8 @ftell(i32* %184)
%186 = sext i8 %185 to i32
%187 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %186)
br label %188
188: ; preds = %192, %180
%189 = load i32*, i32** %7
%190 = call i32 @fread(i8* %15, i32 1, i32 1, i32* %189)
%191 = icmp eq i32 %190, 1
br i1 %191, label %192, label %195
192: ; preds = %188
%193 = load i32*, i32** %6
%194 = call i32 @fwrite(i8* %15, i32 1, i32 1, i32* %193)
br label %188
195: ; preds = %188
%196 = load i32*, i32** %6
%197 = load i64, i64* %10
%198 = call i32 @FseekEnd(i32* %196, i64 %197)
%199 = load i32*, i32** %6
%200 = call signext i8 @ftell(i32* %199)
%201 = sext i8 %200 to i32
%202 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %201)
br label %203
203: ; preds = %207, %195
%204 = load i32*, i32** %8
%205 = call i32 @fread(i8* %15, i32 1, i32 1, i32* %204)
%206 = icmp eq i32 %205, 1
br i1 %206, label %207, label %210
207: ; preds = %203
%208 = load i32*, i32** %6
%209 = call i32 @fwrite(i8* %15, i32 1, i32 1, i32* %208)
br label %203
210: ; preds = %203
%211 = load i32*, i32** %7
%212 = call i32 @fclose(i32* %211)
%213 = load i32*, i32** %8
%214 = call i32 @fclose(i32* %213)
br label %215
215: ; preds = %210, %167
%216 = load i32*, i32** %6
%217 = call i32 @fclose(i32* %216)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"rb+\00"
@.str.1 = [30 x i8] c"Error opening firmware image!\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [20 x i8] c"Error opening file!\00"
@.str.4 = [37 x i8] c"The offsets must be aligned to 0x100\00"
@.str.5 = [19 x i8] c"size of %s: 0x%lx\0A\00"
@.str.6 = [39 x i8] c"The ROM size must be multiple of 0x100\00"
@.str.7 = [8 x i8] c"%02hhx \00"
@.str.9 = [18 x i8] c"writing to 0x%lx\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = alloca i8
%5 = and i32 %0, -3
%6 = icmp eq i32 %5, 4
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 @usage(i8* %8)
br label %10
10: ; preds = %7, %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = icmp eq i32 %0, 6
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
br i1 %19, label %22, label %39
22: ; preds = %18
%23 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds i8*, i8** %1, i64 4
%28 = load i8*, i8** %27
%29 = tail call i64 @strtoul(i8* nocapture %28, i32* null, i32 0)
%30 = getelementptr inbounds i8*, i8** %1, i64 5
%31 = load i8*, i8** %30
%32 = tail call i64 @strtoul(i8* nocapture %31, i32* null, i32 0)
%33 = icmp eq i32* %23, null
%34 = icmp eq i32* %26, null
%35 = or i1 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %22
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %18
%40 = tail call i64 @strtoul(i8* nocapture %21, i32* null, i32 0)
%41 = getelementptr inbounds i8*, i8** %1, i64 3
%42 = load i8*, i8** %41
%43 = tail call i64 @strtoul(i8* nocapture %42, i32* null, i32 0)
br label %44
44: ; preds = %22, %39
%45 = phi i64 [ %29, %22 ], [ %40, %39 ]
%46 = phi i64 [ %32, %22 ], [ %43, %39 ]
%47 = phi i32* [ %26, %22 ], [ null, %39 ]
%48 = phi i32* [ %23, %22 ], [ null, %39 ]
%49 = and i64 %45, 255
%50 = icmp eq i64 %49, 0
%51 = and i64 %46, 255
%52 = icmp eq i64 %51, 0
%53 = select i1 %50, i1 %52, i1 false
br i1 %53, label %57, label %54
54: ; preds = %44
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%56 = tail call i32 @exit(i32 1)
unreachable
57: ; preds = %44
%58 = tail call i32 @FseekEnd(i32* %13, i64 -1)
%59 = tail call signext i8 @ftell(i32* %13)
%60 = sext i8 %59 to i64
%61 = add nsw i64 %60, 1
%62 = load i8*, i8** %11
%63 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %62, i64 %61)
%64 = and i64 %61, 255
%65 = icmp eq i64 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %57
%67 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
%68 = tail call i32 @exit(i32 1)
unreachable
69: ; preds = %57
%70 = tail call i64 @negoffset(i64 %45, i64 %61)
%71 = tail call i64 @negoffset(i64 %46, i64 %61)
%72 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%73 = add nsw i64 %70, 16777216
%74 = lshr i64 %73, 16
%75 = trunc i64 %74 to i8
store i8 %75, i8* %72
%76 = lshr i64 %73, 8
%77 = trunc i64 %76 to i8
%78 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
store i8 %77, i8* %78
%79 = xor i8 %75, -1
%80 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 2
store i8 %79, i8* %80
%81 = xor i8 %77, -1
%82 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 3
store i8 %81, i8* %82
%83 = add nsw i64 %71, 16777216
%84 = lshr i64 %83, 16
%85 = trunc i64 %84 to i8
%86 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 4
store i8 %85, i8* %86
%87 = lshr i64 %83, 8
%88 = trunc i64 %87 to i8
%89 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 5
store i8 %88, i8* %89
%90 = xor i8 %85, -1
%91 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 6
store i8 %90, i8* %91
%92 = xor i8 %88, -1
%93 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 7
store i8 %92, i8* %93
%94 = sext i8 %75 to i32
%95 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %94)
%96 = sext i8 %77 to i32
%97 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %96)
%98 = sext i8 %79 to i32
%99 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %98)
%100 = sext i8 %81 to i32
%101 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %100)
%102 = sext i8 %85 to i32
%103 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %102)
%104 = sext i8 %88 to i32
%105 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %104)
%106 = sext i8 %90 to i32
%107 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %106)
%108 = sext i8 %92 to i32
%109 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %108)
%110 = tail call i32 @putchar(i32 10)
%111 = tail call i32 @FseekEnd(i32* %13, i64 -256)
%112 = tail call signext i8 @ftell(i32* %13)
%113 = sext i8 %112 to i32
%114 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %113)
%115 = call i32 @fwrite(i8* %72, i32 1, i32 8, i32* %13)
br i1 %19, label %116, label %145
116: ; preds = %69
%117 = tail call i32 @FseekEnd(i32* %13, i64 %70)
%118 = tail call signext i8 @ftell(i32* %13)
%119 = sext i8 %118 to i32
%120 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %119)
%121 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %48)
%122 = icmp eq i32 %121, 1
br i1 %122, label %123, label %129
123: ; preds = %116, %123
%124 = load i8, i8* %4
%125 = sext i8 %124 to i32
%126 = tail call i32 @fputc(i32 %125, i32* %13)
%127 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %48)
%128 = icmp eq i32 %127, 1
br i1 %128, label %123, label %129
129: ; preds = %123, %116
%130 = tail call i32 @FseekEnd(i32* %13, i64 %71)
%131 = tail call signext i8 @ftell(i32* %13)
%132 = sext i8 %131 to i32
%133 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %132)
%134 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %47)
%135 = icmp eq i32 %134, 1
br i1 %135, label %136, label %142
136: ; preds = %129, %136
%137 = load i8, i8* %4
%138 = sext i8 %137 to i32
%139 = tail call i32 @fputc(i32 %138, i32* %13)
%140 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %47)
%141 = icmp eq i32 %140, 1
br i1 %141, label %136, label %142
142: ; preds = %136, %129
%143 = tail call i32 @fclose(i32* %48)
%144 = tail call i32 @fclose(i32* %47)
br label %145
145: ; preds = %142, %69
%146 = tail call i32 @fclose(i32* %13)
ret i32 0
}