#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  struct stat *st;
  char *rom_buf;
  int file_size, rom_size, area, area_start, area_end, area_sum, i, in, out;
  unsigned char checksum;
  if (argc < 6 || argc % 3 != 0) {
    fprintf(stderr, "Usage: %s <input_file> <output_file>\n", argv[0]);
    fprintf(stderr, "          <area1_start> <area1_end> <area1_sumaddr>\n");
    fprintf(stderr,
            "          [<area2_start> <area2_end> <area2_sumaddr>] ..\n\n");
    fprintf(stderr, "<input_file>  - File name of the input image file.\n");
    fprintf(stderr, "<output_file> - File name of the output image file.\n");
    fprintf(stderr,
            "<areaX_start> - Hexadecimal start address of a ROM area X\n");
    fprintf(stderr,
            "<areaX_end>   - Hexadecimal end address of a ROM area X\n");
    fprintf(
        stderr,
        "<areaX_sum>   - Hexadecimal address of the byte to be changed to\n");
    fprintf(stderr, "                correct the checksum for area X.\n");
    exit(1);
  }
  st = malloc(sizeof(struct stat));
  if (stat(argv[1], st) == -1) {
    fprintf(stderr, "Failed to stat '%s'\n", argv[1]);
    exit(2);
  }
  file_size = st->st_size;
  printf("DEBUG: ROM file size is %d\n", file_size);
  if (file_size < 5) {
    fprintf(stderr, "File is too short\n");
    exit(2);
  }
  rom_buf = malloc(file_size);
  if ((in = open(argv[1], O_RDONLY)) == -1) {
    fprintf(stderr, "Failed to open '%s'\n", argv[1]);
    exit(2);
  }
  if (read(in, rom_buf, file_size) != file_size) {
    fprintf(stderr, "Short read\n");
    exit(2);
  }
  close(in);
  rom_size = rom_buf[2] * 512;
  printf("DEBUG: ROM code size is %d\n", rom_size);
  if (rom_size > file_size) {
    fprintf(stderr, "ROM code size is bigger than ROM file size\n");
    exit(2);
  }
  for (area = 1; area < argc / 3; area++) {
    sscanf(argv[area * 3], "%x", &area_start);
    sscanf(argv[area * 3 + 1], "%x", &area_end);
    sscanf(argv[area * 3 + 2], "%x", &area_sum);
    if (area_start > area_end) {
      fprintf(stderr,
              "Area %d start address %04X is bigger than end address %04X\n",
              area, area_start, area_end);
      exit(1);
    }
    if (area_sum < area_start || area_sum > area_end) {
      fprintf(
          stderr,
          "Area %d checksum address %04X is not within the area %04X - %04X\n",
          area, area_sum, area_start, area_end);
      exit(1);
    }
    checksum = 0;
    for (i = area_start; i <= area_end; i++) {
      if (i != area_sum)
        checksum += rom_buf[i];
    }
    printf("DEBUG: Area %d: Original checksum: 0x%02X\n", area, checksum);
    rom_buf[area_sum] = -checksum;
    checksum = 0;
    for (i = area_start; i <= area_end; i++)
      checksum += rom_buf[i];
    printf("DEBUG: Area %d: Fixed checksum: 0x%02X\n", area, checksum);
  }
  if ((out = creat(argv[2], 0777)) == -1) {
    fprintf(stderr, "Failed to open '%s'\n", argv[2]);
    exit(3);
  }
  if (write(out, rom_buf, file_size) != file_size) {
    fprintf(stderr, "Short write\n");
    exit(3);
  }
  close(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
jl     11e4 <main+0x34>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12c4 <main+0x114>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xed7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xf02(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x4,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1315 <main+0x165>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xee3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x5,-0x24(%rbp)
jge    135a <main+0x1aa>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
lea    0xecf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c73(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
jne    13b1 <main+0x201>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe8b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
cmp    -0x24(%rbp),%eax
je     13ea <main+0x23a>
mov    0x2c08(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
shl    $0x9,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    143e <main+0x28e>
mov    0x2bb4(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
cmp    %ecx,%eax
jge    1614 <main+0x464>
mov    -0x10(%rbp),%rax
imul   $0x3,-0x2c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe1b(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   10a0 <sscanf@plt>
mov    -0x10(%rbp),%rax
imul   $0x3,-0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdf9(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <sscanf@plt>
mov    -0x10(%rbp),%rax
imul   $0x3,-0x2c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdd7(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   10a0 <sscanf@plt>
mov    -0x30(%rbp),%eax
cmp    -0x34(%rbp),%eax
jle    14fd <main+0x34d>
mov    0x2aff(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xdab(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%eax
cmp    -0x30(%rbp),%eax
jl     1515 <main+0x365>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jle    1544 <main+0x394>
mov    0x2abc(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    -0x34(%rbp),%r9d
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movb   $0x0,-0x45(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     158e <main+0x3de>
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     157b <main+0x3cb>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
movzbl -0x45(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x45(%rbp)
jmp    1580 <main+0x3d0>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    154e <main+0x39e>
mov    -0x2c(%rbp),%esi
movzbl -0x45(%rbp),%edx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movzbl -0x45(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movb   $0x0,-0x45(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     15f1 <main+0x441>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
movzbl -0x45(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x45(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    15c2 <main+0x412>
mov    -0x2c(%rbp),%esi
movzbl -0x45(%rbp),%edx
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1445 <main+0x295>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x1ff,%esi
call   10c0 <creat@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    165b <main+0x4ab>
mov    0x299f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xbe1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    -0x44(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     1694 <main+0x4e4>
mov    0x295e(%rip),%rax
mov    (%rax),%edi
lea    0xce5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbp
cmp    $0x6,%edi
jl     16f8 <main+0x548>
mov    %edi,%r14d
movslq %edi,%rax
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
jne    16f8 <main+0x548>
mov    $0x4,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rbp),%rdi
mov    %rax,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     179d <main+0x5ed>
mov    (%rbx),%r12d
lea    0xff1(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x4,%r12d
jle    17b3 <main+0x603>
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    0x8(%rbp),%rdi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     17c5 <main+0x615>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <read@plt>
cmp    %r12d,%eax
jne    17ea <main+0x63a>
mov    %ebx,%edi
call   1050 <close@plt>
movsbl 0x2(%r15),%ebx
shl    $0x9,%ebx
lea    0xfd9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,0x24(%rsp)
cmp    %r12d,%ebx
jg     17fc <main+0x64c>
mov    %r14d,%eax
mov    $0xaaaaaaab,%ecx
imul   %rax,%rcx
shr    $0x21,%rcx
mov    %rcx,0x40(%rsp)
lea    0x3(%r15),%rax
mov    %rax,0x38(%rsp)
lea    0x70(%r15),%rax
mov    %rax,0x28(%rsp)
lea    0x10(%r15),%rax
mov    %rax,0x30(%rsp)
mov    $0x1,%r12d
mov    %rbp,%r14
mov    %rbp,0x18(%rsp)
jmp    1304 <main+0x154>
mov    0x18(%rsp),%r14
nop
movzbl %r13b,%edx
lea    0x1065(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r12
cmp    0x40(%rsp),%r12
je     1661 <main+0x4b1>
lea    (%r12,%r12,2),%rbx
mov    (%r14,%rbx,8),%rdi
lea    0xf91(%rip),%rbp
mov    %rbp,%rsi
lea    0x14(%rsp),%rdx
call   10a0 <sscanf@plt>
mov    0x8(%r14,%rbx,8),%rdi
mov    %rbp,%rsi
lea    0x10(%rsp),%rdx
call   10a0 <sscanf@plt>
mov    0x10(%r14,%rbx,8),%rdi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
call   10a0 <sscanf@plt>
movslq 0x14(%rsp),%r8
mov    0x10(%rsp),%r9d
cmp    %r8d,%r9d
jl     16aa <main+0x4fa>
mov    0xc(%rsp),%ecx
cmp    %r8d,%ecx
jl     16d4 <main+0x524>
cmp    %r9d,%ecx
jg     16d4 <main+0x524>
movslq %ecx,%r11
mov    %r9d,%eax
sub    %r8d,%eax
lea    0x1(%rax),%r10d
cmp    $0x3,%eax
jae    1390 <main+0x1e0>
xor    %ecx,%ecx
mov    %r8,%rsi
jmp    1408 <main+0x258>
nopl   0x0(%rax)
mov    0x38(%rsp),%rax
lea    (%rax,%r8,1),%rdi
mov    %r11,%rax
sub    %r8,%rax
lea    -0x3(%rax),%r14
lea    -0x2(%rax),%rbp
lea    -0x1(%rax),%rbx
mov    %r10d,%edx
and    $0xfffffffc,%edx
xor    %esi,%esi
xor    %ecx,%ecx
jmp    13c8 <main+0x218>
nopw   0x0(%rax,%rax,1)
add    $0x4,%rsi
cmp    %esi,%edx
je     1400 <main+0x250>
cmp    %rsi,%rax
jne    13e0 <main+0x230>
cmp    %rsi,%rbx
jne    13e9 <main+0x239>
cmp    %rsi,%rbp
jne    13f2 <main+0x242>
cmp    %rsi,%r14
je     13c0 <main+0x210>
jmp    13fb <main+0x24b>
xchg   %ax,%ax
add    -0x3(%rdi,%rsi,1),%cl
cmp    %rsi,%rbx
je     13d2 <main+0x222>
add    -0x2(%rdi,%rsi,1),%cl
cmp    %rsi,%rbp
je     13d7 <main+0x227>
add    -0x1(%rdi,%rsi,1),%cl
cmp    %rsi,%r14
je     13c0 <main+0x210>
add    (%rdi,%rsi,1),%cl
jmp    13c0 <main+0x210>
add    %r8,%rsi
mov    0x18(%rsp),%r14
mov    %r14,%rbx
test   $0x3,%r10b
je     1450 <main+0x2a0>
lea    (%r15,%rsi,1),%rax
sub    %rsi,%r11
sub    %r8b,%r9b
add    $0x1,%r9b
movzbl %r9b,%edx
and    $0x3,%edx
xor    %esi,%esi
jmp    1438 <main+0x288>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rsi
cmp    %esi,%edx
je     1450 <main+0x2a0>
cmp    %rsi,%r11
je     1430 <main+0x280>
add    (%rax,%rsi,1),%cl
jmp    1430 <main+0x280>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl %cl,%r14d
xor    %r13d,%r13d
lea    0xec7(%rip),%rdi
mov    %r12d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
neg    %r14b
movslq 0xc(%rsp),%rax
mov    %r14b,(%r15,%rax,1)
movslq 0x14(%rsp),%rcx
mov    0x10(%rsp),%r8d
cmp    %ecx,%r8d
mov    %rbx,%r14
jl     12e0 <main+0x130>
mov    %r8d,%esi
sub    %ecx,%esi
cmp    $0x7,%esi
jae    14b0 <main+0x300>
xor    %r13d,%r13d
mov    %rcx,%rax
jmp    1640 <main+0x490>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x1f,%esi
jae    14bf <main+0x30f>
xor    %r13d,%r13d
xor    %edx,%edx
jmp    15ca <main+0x41a>
add    $0x1,%rsi
mov    %rsi,%rdx
movabs $0x1ffffffe0,%rax
and    %rax,%rdx
lea    -0x20(%rdx),%rax
mov    %rax,%rdi
shr    $0x5,%rdi
add    $0x1,%rdi
mov    %edi,%ebp
and    $0x3,%ebp
cmp    $0x60,%rax
jae    14f9 <main+0x349>
pxor   %xmm0,%xmm0
xor    %eax,%eax
pxor   %xmm1,%xmm1
jmp    1569 <main+0x3b9>
and    $0xfffffffffffffffc,%rdi
mov    0x28(%rsp),%rax
lea    (%rax,%rcx,1),%rbx
pxor   %xmm0,%xmm0
xor    %eax,%eax
pxor   %xmm1,%xmm1
movdqu -0x70(%rbx,%rax,1),%xmm2
paddb  %xmm0,%xmm2
movdqu -0x60(%rbx,%rax,1),%xmm0
paddb  %xmm1,%xmm0
movdqu -0x50(%rbx,%rax,1),%xmm1
movdqu -0x40(%rbx,%rax,1),%xmm3
movdqu -0x30(%rbx,%rax,1),%xmm4
paddb  %xmm1,%xmm4
paddb  %xmm2,%xmm4
movdqu -0x20(%rbx,%rax,1),%xmm2
paddb  %xmm3,%xmm2
paddb  %xmm0,%xmm2
movdqu -0x10(%rbx,%rax,1),%xmm0
paddb  %xmm4,%xmm0
movdqu (%rbx,%rax,1),%xmm1
paddb  %xmm2,%xmm1
sub    $0xffffffffffffff80,%rax
add    $0xfffffffffffffffc,%rdi
jne    1510 <main+0x360>
test   %rbp,%rbp
je     159c <main+0x3ec>
add    %rcx,%rax
add    0x30(%rsp),%rax
shl    $0x5,%rbp
xor    %edi,%edi
nopl   0x0(%rax)
movdqu -0x10(%rax,%rdi,1),%xmm2
paddb  %xmm2,%xmm0
movdqu (%rax,%rdi,1),%xmm2
paddb  %xmm2,%xmm1
add    $0x20,%rdi
cmp    %rdi,%rbp
jne    1580 <main+0x3d0>
paddb  %xmm1,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddb  %xmm0,%xmm1
psadbw 0xa4f(%rip),%xmm1
movd   %xmm1,%r13d
cmp    %rdx,%rsi
je     12da <main+0x12a>
test   $0x18,%sil
mov    0x18(%rsp),%r14
je     162f <main+0x47f>
mov    %r8d,%esi
sub    %ecx,%esi
add    $0x1,%rsi
movabs $0x1ffffffe0,%rax
lea    0x18(%rax),%rdi
and    %rsi,%rdi
lea    (%rdi,%rcx,1),%rax
movzbl %r13b,%ebp
movd   %ebp,%xmm0
add    %r15,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movq   (%rcx,%rdx,1),%xmm1
paddb  %xmm1,%xmm0
add    $0x8,%rdx
cmp    %rdx,%rdi
jne    1600 <main+0x450>
psadbw 0x9e6(%rip),%xmm0
movd   %xmm0,%r13d
cmp    %rdi,%rsi
mov    0x18(%rsp),%r14
je     12e0 <main+0x130>
jmp    1640 <main+0x490>
add    %rcx,%rdx
mov    %rdx,%rax
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r8d
mov    %r8d,%ecx
nopw   0x0(%rax,%rax,1)
add    (%r15,%rax,1),%r13b
add    $0x1,%rax
cmp    %eax,%ecx
jne    1650 <main+0x4a0>
jmp    12e0 <main+0x130>
mov    0x10(%r14),%rdi
mov    $0x1ff,%esi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
je     181d <main+0x66d>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    0x24(%rsp),%ebp
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    1842 <main+0x692>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2927(%rip),%rax
mov    (%rax),%edi
lea    0xbed(%rip),%rsi
mov    %r12d,%edx
mov    %r8d,%ecx
mov    %r9d,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x28fd(%rip),%rax
mov    (%rax),%edi
lea    0xbff(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x28d9(%rip),%rbx
mov    (%rbx),%edi
mov    0x0(%rbp),%rdx
lea    0x904(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x91a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x93f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x96a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x98e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x9b3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0x9de(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xa07(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xa39(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2834(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xa46(%rip),%rsi
jmp    17d9 <main+0x629>
mov    0x281e(%rip),%rax
mov    (%rax),%edi
lea    0xa65(%rip),%rsi
jmp    180c <main+0x65c>
mov    0x280c(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xa62(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    0x27e7(%rip),%rax
mov    (%rax),%edi
lea    0xa56(%rip),%rsi
jmp    180c <main+0x65c>
mov    0x27d5(%rip),%rax
mov    (%rax),%edi
lea    0xa6c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    0x27b4(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r14),%rdx
lea    0xa0a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    0x278f(%rip),%rax
mov    (%rax),%edi
lea    0xb26(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <input_file> <output_file>\0A\00"
@.str.1 = [53 x i8] c" <area1_start> <area1_end> <area1_sumaddr>\0A\00"
@.str.2 = [59 x i8] c" [<area2_start> <area2_end> <area2_sumaddr>] ..\0A\0A\00"
@.str.3 = [52 x i8] c"<input_file> - File name of the input image file.\0A\00"
@.str.4 = [53 x i8] c"<output_file> - File name of the output image file.\0A\00"
@.str.5 = [59 x i8] c"<areaX_start> - Hexadecimal start address of a ROM area X\0A\00"
@.str.6 = [57 x i8] c"<areaX_end> - Hexadecimal end address of a ROM area X\0A\00"
@.str.7 = [66 x i8] c"<areaX_sum> - Hexadecimal address of the byte to be changed to\0A\00"
@.str.8 = [50 x i8] c" correct the checksum for area X.\0A\00"
@.str.9 = [21 x i8] c"Failed to stat '%s'\0A\00"
@.str.10 = [28 x i8] c"DEBUG: ROM file size is %d\0A\00"
@.str.11 = [19 x i8] c"File is too short\0A\00"
@O_RDONLY = global i32 0
@.str.12 = [21 x i8] c"Failed to open '%s'\0A\00"
@.str.13 = [12 x i8] c"Short read\0A\00"
@.str.14 = [28 x i8] c"DEBUG: ROM code size is %d\0A\00"
@.str.15 = [44 x i8] c"ROM code size is bigger than ROM file size\0A\00"
@.str.16 = [3 x i8] c"%x\00"
@.str.17 = [60 x i8] c"Area %d start address %04X is bigger than end address %04X\0A\00"
@.str.18 = [66 x i8] c"Area %d checksum address %04X is not within the area %04X - %04X\0A\00"
@.str.19 = [43 x i8] c"DEBUG: Area %d: Original checksum: 0x%02X\0A\00"
@.str.20 = [40 x i8] c"DEBUG: Area %d: Fixed checksum: 0x%02X\0A\00"
@.str.21 = [13 x i8] c"Short write\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 6
br i1 %19, label %24, label %20
20: ; preds = %2
%21 = load i32, i32* %4
%22 = srem i32 %21, 3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %47
24: ; preds = %20, %2
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.8, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %20
%48 = call i8* @malloc(i32 4)
%49 = bitcast i8* %48 to %struct.stat*
store %struct.stat* %49, %struct.stat** %6
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = load %struct.stat*, %struct.stat** %6
%54 = call i32 @stat(i8* %52, %struct.stat* %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %63
56: ; preds = %47
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 2)
unreachable
63: ; preds = %47
%64 = load %struct.stat*, %struct.stat** %6
%65 = getelementptr inbounds %struct.stat, %struct.stat* %64, i32 0, i32 0
%66 = load i32, i32* %65
store i32 %66, i32* %8
%67 = load i32, i32* %8
%68 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %8
%70 = icmp slt i32 %69, 5
br i1 %70, label %71, label %75
71: ; preds = %63
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0))
%74 = call i32 @exit(i32 2)
unreachable
75: ; preds = %63
%76 = load i32, i32* %8
%77 = call i8* @malloc(i32 %76)
store i8* %77, i8** %7
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = load i32, i32* @O_RDONLY
%82 = call i32 @open(i8* %80, i32 %81)
store i32 %82, i32* %15
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %91
84: ; preds = %75
%85 = load i32, i32* @stderr
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 1
%88 = load i8*, i8** %87
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i8* %88)
%90 = call i32 @exit(i32 2)
unreachable
91: ; preds = %75
%92 = load i32, i32* %15
%93 = load i8*, i8** %7
%94 = load i32, i32* %8
%95 = call i32 @read(i32 %92, i8* %93, i32 %94)
%96 = load i32, i32* %8
%97 = icmp ne i32 %95, %96
br i1 %97, label %98, label %102
98: ; preds = %91
%99 = load i32, i32* @stderr
%100 = call i32 (i32, i8*, ...) @fprintf(i32 %99, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%101 = call i32 @exit(i32 2)
unreachable
102: ; preds = %91
%103 = load i32, i32* %15
%104 = call i32 @close(i32 %103)
%105 = load i8*, i8** %7
%106 = getelementptr inbounds i8, i8* %105, i64 2
%107 = load i8, i8* %106
%108 = sext i8 %107 to i32
%109 = mul nsw i32 %108, 512
store i32 %109, i32* %9
%110 = load i32, i32* %9
%111 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.14, i64 0, i64 0), i32 %110)
%112 = load i32, i32* %9
%113 = load i32, i32* %8
%114 = icmp sgt i32 %112, %113
br i1 %114, label %115, label %119
115: ; preds = %102
%116 = load i32, i32* @stderr
%117 = call i32 (i32, i8*, ...) @fprintf(i32 %116, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.15, i64 0, i64 0))
%118 = call i32 @exit(i32 2)
unreachable
119: ; preds = %102
store i32 1, i32* %10
br label %120
120: ; preds = %237, %119
%121 = load i32, i32* %10
%122 = load i32, i32* %4
%123 = sdiv i32 %122, 3
%124 = icmp slt i32 %121, %123
br i1 %124, label %125, label %240
125: ; preds = %120
%126 = load i8**, i8*** %5
%127 = load i32, i32* %10
%128 = mul nsw i32 %127, 3
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds i8*, i8** %126, i64 %129
%131 = load i8*, i8** %130
%132 = call i32 @sscanf(i8* %131, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %11)
%133 = load i8**, i8*** %5
%134 = load i32, i32* %10
%135 = mul nsw i32 %134, 3
%136 = add nsw i32 %135, 1
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds i8*, i8** %133, i64 %137
%139 = load i8*, i8** %138
%140 = call i32 @sscanf(i8* %139, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %12)
%141 = load i8**, i8*** %5
%142 = load i32, i32* %10
%143 = mul nsw i32 %142, 3
%144 = add nsw i32 %143, 2
%145 = sext i32 %144 to i64
%146 = getelementptr inbounds i8*, i8** %141, i64 %145
%147 = load i8*, i8** %146
%148 = call i32 @sscanf(i8* %147, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %13)
%149 = load i32, i32* %11
%150 = load i32, i32* %12
%151 = icmp sgt i32 %149, %150
br i1 %151, label %152, label %159
152: ; preds = %125
%153 = load i32, i32* @stderr
%154 = load i32, i32* %10
%155 = load i32, i32* %11
%156 = load i32, i32* %12
%157 = call i32 (i32, i8*, ...) @fprintf(i32 %153, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.17, i64 0, i64 0), i32 %154, i32 %155, i32 %156)
%158 = call i32 @exit(i32 1)
unreachable
159: ; preds = %125
%160 = load i32, i32* %13
%161 = load i32, i32* %11
%162 = icmp slt i32 %160, %161
br i1 %162, label %167, label %163
163: ; preds = %159
%164 = load i32, i32* %13
%165 = load i32, i32* %12
%166 = icmp sgt i32 %164, %165
br i1 %166, label %167, label %175
167: ; preds = %163, %159
%168 = load i32, i32* @stderr
%169 = load i32, i32* %10
%170 = load i32, i32* %13
%171 = load i32, i32* %11
%172 = load i32, i32* %12
%173 = call i32 (i32, i8*, ...) @fprintf(i32 %168, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.18, i64 0, i64 0), i32 %169, i32 %170, i32 %171, i32 %172)
%174 = call i32 @exit(i32 1)
unreachable
175: ; preds = %163
store i8 0, i8* %17
%176 = load i32, i32* %11
store i32 %176, i32* %14
br label %177
177: ; preds = %197, %175
%178 = load i32, i32* %14
%179 = load i32, i32* %12
%180 = icmp sle i32 %178, %179
br i1 %180, label %181, label %200
181: ; preds = %177
%182 = load i32, i32* %14
%183 = load i32, i32* %13
%184 = icmp ne i32 %182, %183
br i1 %184, label %185, label %196
185: ; preds = %181
%186 = load i8*, i8** %7
%187 = load i32, i32* %14
%188 = sext i32 %187 to i64
%189 = getelementptr inbounds i8, i8* %186, i64 %188
%190 = load i8, i8* %189
%191 = sext i8 %190 to i32
%192 = load i8, i8* %17
%193 = zext i8 %192 to i32
%194 = add nsw i32 %193, %191
%195 = trunc i32 %194 to i8
store i8 %195, i8* %17
br label %196
196: ; preds = %185, %181
br label %197
197: ; preds = %196
%198 = load i32, i32* %14
%199 = add nsw i32 %198, 1
store i32 %199, i32* %14
br label %177
200: ; preds = %177
%201 = load i32, i32* %10
%202 = load i8, i8* %17
%203 = zext i8 %202 to i32
%204 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.19, i64 0, i64 0), i32 %201, i32 %203)
%205 = load i8, i8* %17
%206 = zext i8 %205 to i32
%207 = sub nsw i32 0, %206
%208 = trunc i32 %207 to i8
%209 = load i8*, i8** %7
%210 = load i32, i32* %13
%211 = sext i32 %210 to i64
%212 = getelementptr inbounds i8, i8* %209, i64 %211
store i8 %208, i8* %212
store i8 0, i8* %17
%213 = load i32, i32* %11
store i32 %213, i32* %14
br label %214
214: ; preds = %229, %200
%215 = load i32, i32* %14
%216 = load i32, i32* %12
%217 = icmp sle i32 %215, %216
br i1 %217, label %218, label %232
218: ; preds = %214
%219 = load i8*, i8** %7
%220 = load i32, i32* %14
%221 = sext i32 %220 to i64
%222 = getelementptr inbounds i8, i8* %219, i64 %221
%223 = load i8, i8* %222
%224 = sext i8 %223 to i32
%225 = load i8, i8* %17
%226 = zext i8 %225 to i32
%227 = add nsw i32 %226, %224
%228 = trunc i32 %227 to i8
store i8 %228, i8* %17
br label %229
229: ; preds = %218
%230 = load i32, i32* %14
%231 = add nsw i32 %230, 1
store i32 %231, i32* %14
br label %214
232: ; preds = %214
%233 = load i32, i32* %10
%234 = load i8, i8* %17
%235 = zext i8 %234 to i32
%236 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.20, i64 0, i64 0), i32 %233, i32 %235)
br label %237
237: ; preds = %232
%238 = load i32, i32* %10
%239 = add nsw i32 %238, 1
store i32 %239, i32* %10
br label %120
240: ; preds = %120
%241 = load i8**, i8*** %5
%242 = getelementptr inbounds i8*, i8** %241, i64 2
%243 = load i8*, i8** %242
%244 = call i32 @creat(i8* %243, i32 511)
store i32 %244, i32* %16
%245 = icmp eq i32 %244, -1
br i1 %245, label %246, label %253
246: ; preds = %240
%247 = load i32, i32* @stderr
%248 = load i8**, i8*** %5
%249 = getelementptr inbounds i8*, i8** %248, i64 2
%250 = load i8*, i8** %249
%251 = call i32 (i32, i8*, ...) @fprintf(i32 %247, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i8* %250)
%252 = call i32 @exit(i32 3)
unreachable
253: ; preds = %240
%254 = load i32, i32* %16
%255 = load i8*, i8** %7
%256 = load i32, i32* %8
%257 = call i32 @write(i32 %254, i8* %255, i32 %256)
%258 = load i32, i32* %8
%259 = icmp ne i32 %257, %258
br i1 %259, label %260, label %264
260: ; preds = %253
%261 = load i32, i32* @stderr
%262 = call i32 (i32, i8*, ...) @fprintf(i32 %261, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.21, i64 0, i64 0))
%263 = call i32 @exit(i32 3)
unreachable
264: ; preds = %253
%265 = load i32, i32* %16
%266 = call i32 @close(i32 %265)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Usage: %s <input_file> <output_file>\0A\00"
@.str.1 = [53 x i8] c" <area1_start> <area1_end> <area1_sumaddr>\0A\00"
@.str.2 = [59 x i8] c" [<area2_start> <area2_end> <area2_sumaddr>] ..\0A\0A\00"
@.str.3 = [52 x i8] c"<input_file> - File name of the input image file.\0A\00"
@.str.4 = [53 x i8] c"<output_file> - File name of the output image file.\0A\00"
@.str.5 = [59 x i8] c"<areaX_start> - Hexadecimal start address of a ROM area X\0A\00"
@.str.6 = [57 x i8] c"<areaX_end> - Hexadecimal end address of a ROM area X\0A\00"
@.str.7 = [66 x i8] c"<areaX_sum> - Hexadecimal address of the byte to be changed to\0A\00"
@.str.8 = [50 x i8] c" correct the checksum for area X.\0A\00"
@.str.9 = [21 x i8] c"Failed to stat '%s'\0A\00"
@.str.10 = [28 x i8] c"DEBUG: ROM file size is %d\0A\00"
@.str.11 = [19 x i8] c"File is too short\0A\00"
@O_RDONLY = global i32 0
@.str.12 = [21 x i8] c"Failed to open '%s'\0A\00"
@.str.13 = [12 x i8] c"Short read\0A\00"
@.str.14 = [28 x i8] c"DEBUG: ROM code size is %d\0A\00"
@.str.15 = [44 x i8] c"ROM code size is bigger than ROM file size\0A\00"
@.str.16 = [3 x i8] c"%x\00"
@.str.17 = [60 x i8] c"Area %d start address %04X is bigger than end address %04X\0A\00"
@.str.18 = [66 x i8] c"Area %d checksum address %04X is not within the area %04X - %04X\0A\00"
@.str.19 = [43 x i8] c"DEBUG: Area %d: Original checksum: 0x%02X\0A\00"
@.str.20 = [40 x i8] c"DEBUG: Area %d: Fixed checksum: 0x%02X\0A\00"
@.str.21 = [13 x i8] c"Short write\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp sgt i32 %0, 5
%10 = srem i32 %0, 3
%11 = icmp eq i32 %10, 0
%12 = and i1 %9, %11
br i1 %12, label %34, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %1
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.7, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.8, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %2
%35 = tail call dereferenceable_or_null(4) i8* @malloc(i32 4)
%36 = bitcast i8* %35 to %struct.stat*
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = tail call i32 @stat(i8* %38, %struct.stat* %36)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %37
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i8* %43)
%45 = tail call i32 @exit(i32 2)
unreachable
46: ; preds = %34
%47 = getelementptr inbounds %struct.stat, %struct.stat* %36, i64 0, i32 0
%48 = load i32, i32* %47
%49 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0), i32 %48)
%50 = icmp slt i32 %48, 5
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.11, i64 0, i64 0))
%54 = tail call i32 @exit(i32 2)
unreachable
55: ; preds = %46
%56 = tail call i8* @malloc(i32 %48)
%57 = load i8*, i8** %37
%58 = load i32, i32* @O_RDONLY
%59 = tail call i32 @open(i8* %57, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %37
%64 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i8* %63)
%65 = tail call i32 @exit(i32 2)
unreachable
66: ; preds = %55
%67 = tail call i32 @read(i32 %59, i8* %56, i32 %48)
%68 = icmp eq i32 %67, %48
br i1 %68, label %73, label %69
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = tail call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%72 = tail call i32 @exit(i32 2)
unreachable
73: ; preds = %66
%74 = tail call i32 @close(i32 %59)
%75 = getelementptr inbounds i8, i8* %56, i64 2
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = shl nsw i32 %77, 9
%79 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.14, i64 0, i64 0), i32 %78)
%80 = icmp sgt i32 %78, %48
br i1 %80, label %84, label %81
81: ; preds = %73
%82 = udiv i32 %0, 3
%83 = zext i32 %82 to i64
br label %88
84: ; preds = %73
%85 = load i32, i32* @stderr
%86 = tail call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.15, i64 0, i64 0))
%87 = tail call i32 @exit(i32 2)
unreachable
88: ; preds = %81, %332
%89 = phi i64 [ 1, %81 ], [ %336, %332 ]
%90 = mul nuw nsw i64 %89, 3
%91 = getelementptr inbounds i8*, i8** %1, i64 %90
%92 = load i8*, i8** %91
%93 = call i32 @sscanf(i8* %92, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %3)
%94 = add nuw nsw i64 %90, 1
%95 = getelementptr inbounds i8*, i8** %1, i64 %94
%96 = load i8*, i8** %95
%97 = call i32 @sscanf(i8* %96, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %4)
%98 = add nuw nsw i64 %90, 2
%99 = getelementptr inbounds i8*, i8** %1, i64 %98
%100 = load i8*, i8** %99
%101 = call i32 @sscanf(i8* %100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32* %5)
%102 = load i32, i32* %3
%103 = load i32, i32* %4
%104 = icmp slt i32 %103, %102
br i1 %104, label %105, label %110
105: ; preds = %88
%106 = trunc i64 %89 to i32
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.17, i64 0, i64 0), i32 %106, i32 %102, i32 %103)
%109 = call i32 @exit(i32 1)
unreachable
110: ; preds = %88
%111 = load i32, i32* %5
%112 = icmp slt i32 %111, %102
%113 = icmp sgt i32 %111, %103
%114 = select i1 %112, i1 true, i1 %113
br i1 %114, label %125, label %115
115: ; preds = %110
%116 = sext i32 %102 to i64
%117 = sext i32 %111 to i64
%118 = add i32 %103, 1
%119 = sub i32 %118, %102
%120 = sub i32 %103, %102
%121 = and i32 %119, 3
%122 = icmp ult i32 %120, 3
br i1 %122, label %168, label %123
123: ; preds = %115
%124 = and i32 %119, -4
br label %130
125: ; preds = %110
%126 = trunc i64 %89 to i32
%127 = load i32, i32* @stderr
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.18, i64 0, i64 0), i32 %126, i32 %111, i32 %102, i32 %103)
%129 = call i32 @exit(i32 1)
unreachable
130: ; preds = %163, %123
%131 = phi i64 [ %116, %123 ], [ %165, %163 ]
%132 = phi i8 [ 0, %123 ], [ %164, %163 ]
%133 = phi i32 [ 0, %123 ], [ %166, %163 ]
%134 = icmp eq i64 %131, %117
br i1 %134, label %139, label %135
135: ; preds = %130
%136 = getelementptr inbounds i8, i8* %56, i64 %131
%137 = load i8, i8* %136
%138 = add i8 %137, %132
br label %139
139: ; preds = %130, %135
%140 = phi i8 [ %138, %135 ], [ %132, %130 ]
%141 = add nsw i64 %131, 1
%142 = icmp eq i64 %141, %117
br i1 %142, label %147, label %143
143: ; preds = %139
%144 = getelementptr inbounds i8, i8* %56, i64 %141
%145 = load i8, i8* %144
%146 = add i8 %145, %140
br label %147
147: ; preds = %143, %139
%148 = phi i8 [ %146, %143 ], [ %140, %139 ]
%149 = add nsw i64 %131, 2
%150 = icmp eq i64 %149, %117
br i1 %150, label %155, label %151
151: ; preds = %147
%152 = getelementptr inbounds i8, i8* %56, i64 %149
%153 = load i8, i8* %152
%154 = add i8 %153, %148
br label %155
155: ; preds = %151, %147
%156 = phi i8 [ %154, %151 ], [ %148, %147 ]
%157 = add nsw i64 %131, 3
%158 = icmp eq i64 %157, %117
br i1 %158, label %163, label %159
159: ; preds = %155
%160 = getelementptr inbounds i8, i8* %56, i64 %157
%161 = load i8, i8* %160
%162 = add i8 %161, %156
br label %163
163: ; preds = %159, %155
%164 = phi i8 [ %162, %159 ], [ %156, %155 ]
%165 = add nsw i64 %131, 4
%166 = add i32 %133, 4
%167 = icmp eq i32 %166, %124
br i1 %167, label %168, label %130
168: ; preds = %163, %115
%169 = phi i8 [ undef, %115 ], [ %164, %163 ]
%170 = phi i64 [ %116, %115 ], [ %165, %163 ]
%171 = phi i8 [ 0, %115 ], [ %164, %163 ]
%172 = icmp eq i32 %121, 0
br i1 %172, label %187, label %173
173: ; preds = %168, %182
%174 = phi i64 [ %184, %182 ], [ %170, %168 ]
%175 = phi i8 [ %183, %182 ], [ %171, %168 ]
%176 = phi i32 [ %185, %182 ], [ 0, %168 ]
%177 = icmp eq i64 %174, %117
br i1 %177, label %182, label %178
178: ; preds = %173
%179 = getelementptr inbounds i8, i8* %56, i64 %174
%180 = load i8, i8* %179
%181 = add i8 %180, %175
br label %182
182: ; preds = %178, %173
%183 = phi i8 [ %181, %178 ], [ %175, %173 ]
%184 = add nsw i64 %174, 1
%185 = add i32 %176, 1
%186 = icmp eq i32 %185, %121
br i1 %186, label %187, label %173
187: ; preds = %182, %168
%188 = phi i8 [ %169, %168 ], [ %183, %182 ]
%189 = zext i8 %188 to i32
%190 = trunc i64 %89 to i32
%191 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.19, i64 0, i64 0), i32 %190, i32 %189)
%192 = sub i8 0, %188
%193 = load i32, i32* %5
%194 = sext i32 %193 to i64
%195 = getelementptr inbounds i8, i8* %56, i64 %194
store i8 %192, i8* %195
%196 = load i32, i32* %3
%197 = load i32, i32* %4
%198 = icmp slt i32 %197, %196
br i1 %198, label %332, label %199
199: ; preds = %187
%200 = sext i32 %196 to i64
%201 = add i32 %197, 1
%202 = sub i32 %197, %196
%203 = zext i32 %202 to i64
%204 = add nuw nsw i64 %203, 1
%205 = icmp ult i32 %202, 7
br i1 %205, label %320, label %206
206: ; preds = %199
%207 = icmp ult i32 %202, 31
br i1 %207, label %298, label %208
208: ; preds = %206
%209 = and i64 %204, 8589934560
%210 = add nsw i64 %209, -32
%211 = lshr exact i64 %210, 5
%212 = add nuw nsw i64 %211, 1
%213 = and i64 %212, 3
%214 = icmp ult i64 %210, 96
br i1 %214, label %264, label %215
215: ; preds = %208
%216 = and i64 %212, 1152921504606846972
br label %217
217: ; preds = %217, %215
%218 = phi i64 [ 0, %215 ], [ %261, %217 ]
%219 = phi <16 x i8> [ zeroinitializer, %215 ], [ %259, %217 ]
%220 = phi <16 x i8> [ zeroinitializer, %215 ], [ %260, %217 ]
%221 = phi i64 [ 0, %215 ], [ %262, %217 ]
%222 = add i64 %218, %200
%223 = getelementptr inbounds i8, i8* %56, i64 %222
%224 = bitcast i8* %223 to <16 x i8>*
%225 = load <16 x i8>, <16 x i8>* %224
%226 = getelementptr inbounds i8, i8* %223, i64 16
%227 = bitcast i8* %226 to <16 x i8>*
%228 = load <16 x i8>, <16 x i8>* %227
%229 = add <16 x i8> %225, %219
%230 = add <16 x i8> %228, %220
%231 = or i64 %218, 32
%232 = add i64 %231, %200
%233 = getelementptr inbounds i8, i8* %56, i64 %232
%234 = bitcast i8* %233 to <16 x i8>*
%235 = load <16 x i8>, <16 x i8>* %234
%236 = getelementptr inbounds i8, i8* %233, i64 16
%237 = bitcast i8* %236 to <16 x i8>*
%238 = load <16 x i8>, <16 x i8>* %237
%239 = add <16 x i8> %235, %229
%240 = add <16 x i8> %238, %230
%241 = or i64 %218, 64
%242 = add i64 %241, %200
%243 = getelementptr inbounds i8, i8* %56, i64 %242
%244 = bitcast i8* %243 to <16 x i8>*
%245 = load <16 x i8>, <16 x i8>* %244
%246 = getelementptr inbounds i8, i8* %243, i64 16
%247 = bitcast i8* %246 to <16 x i8>*
%248 = load <16 x i8>, <16 x i8>* %247
%249 = add <16 x i8> %245, %239
%250 = add <16 x i8> %248, %240
%251 = or i64 %218, 96
%252 = add i64 %251, %200
%253 = getelementptr inbounds i8, i8* %56, i64 %252
%254 = bitcast i8* %253 to <16 x i8>*
%255 = load <16 x i8>, <16 x i8>* %254
%256 = getelementptr inbounds i8, i8* %253, i64 16
%257 = bitcast i8* %256 to <16 x i8>*
%258 = load <16 x i8>, <16 x i8>* %257
%259 = add <16 x i8> %255, %249
%260 = add <16 x i8> %258, %250
%261 = add nuw i64 %218, 128
%262 = add i64 %221, 4
%263 = icmp eq i64 %262, %216
br i1 %263, label %264, label %217
264: ; preds = %217, %208
%265 = phi <16 x i8> [ undef, %208 ], [ %259, %217 ]
%266 = phi <16 x i8> [ undef, %208 ], [ %260, %217 ]
%267 = phi i64 [ 0, %208 ], [ %261, %217 ]
%268 = phi <16 x i8> [ zeroinitializer, %208 ], [ %259, %217 ]
%269 = phi <16 x i8> [ zeroinitializer, %208 ], [ %260, %217 ]
%270 = icmp eq i64 %213, 0
br i1 %270, label %288, label %271
271: ; preds = %264, %271
%272 = phi i64 [ %285, %271 ], [ %267, %264 ]
%273 = phi <16 x i8> [ %283, %271 ], [ %268, %264 ]
%274 = phi <16 x i8> [ %284, %271 ], [ %269, %264 ]
%275 = phi i64 [ %286, %271 ], [ 0, %264 ]
%276 = add i64 %272, %200
%277 = getelementptr inbounds i8, i8* %56, i64 %276
%278 = bitcast i8* %277 to <16 x i8>*
%279 = load <16 x i8>, <16 x i8>* %278
%280 = getelementptr inbounds i8, i8* %277, i64 16
%281 = bitcast i8* %280 to <16 x i8>*
%282 = load <16 x i8>, <16 x i8>* %281
%283 = add <16 x i8> %279, %273
%284 = add <16 x i8> %282, %274
%285 = add nuw i64 %272, 32
%286 = add i64 %275, 1
%287 = icmp eq i64 %286, %213
br i1 %287, label %288, label %271
288: ; preds = %271, %264
%289 = phi <16 x i8> [ %265, %264 ], [ %283, %271 ]
%290 = phi <16 x i8> [ %266, %264 ], [ %284, %271 ]
%291 = add <16 x i8> %290, %289
%292 = call i8 @llvm.vector.reduce.add.v16i8(<16 x i8> %291)
%293 = icmp eq i64 %204, %209
br i1 %293, label %332, label %294
294: ; preds = %288
%295 = add nsw i64 %209, %200
%296 = and i64 %204, 24
%297 = icmp eq i64 %296, 0
br i1 %297, label %320, label %298
298: ; preds = %206, %294
%299 = phi i8 [ 0, %206 ], [ %292, %294 ]
%300 = phi i64 [ 0, %206 ], [ %209, %294 ]
%301 = sub i32 %197, %196
%302 = zext i32 %301 to i64
%303 = add nuw nsw i64 %302, 1
%304 = and i64 %303, 8589934584
%305 = add nsw i64 %304, %200
%306 = insertelement <8 x i8> <i8 poison, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, i8 %299, i64 0
br label %307
307: ; preds = %307, %298
%308 = phi i64 [ %300, %298 ], [ %315, %307 ]
%309 = phi <8 x i8> [ %306, %298 ], [ %314, %307 ]
%310 = add i64 %308, %200
%311 = getelementptr inbounds i8, i8* %56, i64 %310
%312 = bitcast i8* %311 to <8 x i8>*
%313 = load <8 x i8>, <8 x i8>* %312
%314 = add <8 x i8> %313, %309
%315 = add nuw i64 %308, 8
%316 = icmp eq i64 %315, %304
br i1 %316, label %317, label %307
317: ; preds = %307
%318 = call i8 @llvm.vector.reduce.add.v8i8(<8 x i8> %314)
%319 = icmp eq i64 %303, %304
br i1 %319, label %332, label %320
320: ; preds = %199, %294, %317
%321 = phi i64 [ %200, %199 ], [ %295, %294 ], [ %305, %317 ]
%322 = phi i8 [ 0, %199 ], [ %292, %294 ], [ %318, %317 ]
br label %323
323: ; preds = %320, %323
%324 = phi i64 [ %329, %323 ], [ %321, %320 ]
%325 = phi i8 [ %328, %323 ], [ %322, %320 ]
%326 = getelementptr inbounds i8, i8* %56, i64 %324
%327 = load i8, i8* %326
%328 = add i8 %327, %325
%329 = add nsw i64 %324, 1
%330 = trunc i64 %329 to i32
%331 = icmp eq i32 %201, %330
br i1 %331, label %332, label %323
332: ; preds = %323, %288, %317, %187
%333 = phi i8 [ 0, %187 ], [ %292, %288 ], [ %318, %317 ], [ %328, %323 ]
%334 = zext i8 %333 to i32
%335 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.20, i64 0, i64 0), i32 %190, i32 %334)
%336 = add nuw nsw i64 %89, 1
%337 = icmp eq i64 %336, %83
br i1 %337, label %338, label %88
338: ; preds = %332
%339 = getelementptr inbounds i8*, i8** %1, i64 2
%340 = load i8*, i8** %339
%341 = call i32 @creat(i8* %340, i32 511)
%342 = icmp eq i32 %341, -1
br i1 %342, label %343, label %348
343: ; preds = %338
%344 = load i32, i32* @stderr
%345 = load i8*, i8** %339
%346 = call i32 (i32, i8*, ...) @fprintf(i32 %344, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0), i8* %345)
%347 = call i32 @exit(i32 3)
unreachable
348: ; preds = %338
%349 = call i32 @write(i32 %341, i8* %56, i32 %48)
%350 = icmp eq i32 %349, %48
br i1 %350, label %355, label %351
351: ; preds = %348
%352 = load i32, i32* @stderr
%353 = call i32 (i32, i8*, ...) @fprintf(i32 %352, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.21, i64 0, i64 0))
%354 = call i32 @exit(i32 3)
unreachable
355: ; preds = %348
%356 = call i32 @close(i32 %341)
ret i32 0
}