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

/* Variables and functions */
int EINVAL;
int EIO;
int /*<<< orphan*/ check_hex_line(char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int hex_to_int(char *);
int lhex_to_int(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  unsigned int addr = 0;
  FILE *ifp, *ofp;
  char *rbuf;
  ssize_t len, i;
  size_t buflen;
  if (argc != 3) {
    printf("Usage: %s <input hex file> <output file>\n", argv[0]);
    return 0;
  }
  ifp = fopen(argv[1], "r");
  ofp = fopen(argv[2], "w");
  if ((ifp == NULL) || (ofp == NULL)) {
    puts("failed to open file.");
    return -EIO;
  }
  rbuf = NULL;
  while ((len = getline(&rbuf, &buflen, ifp)) > 0) {
    int type;
    char obuf[7];
    unsigned int dest_addr;
    while ((rbuf[len - 1] == '\r') || (rbuf[len - 1] == '\n'))
      len--;
    printf("%d, %s\n", (int)len, rbuf);
    if (!check_hex_line(rbuf, len))
      break;
    type = hex_to_int(rbuf + 7);
    switch (type) {
    case 4:
      addr = lhex_to_int(rbuf + 9) * 0x10000;
      printf("bump addr to 0x%08X\n", addr);
      break;
    case 0:
      dest_addr = addr + lhex_to_int(rbuf + 3);
      obuf[0] = 0x4c;
      obuf[1] = 0xfc;
      obuf[2] = hex_to_int(rbuf + 1) + 4;
      obuf[3] = dest_addr;
      obuf[4] = dest_addr >> 8;
      obuf[5] = dest_addr >> 16;
      obuf[6] = dest_addr >> 24;
      if (fwrite(obuf, 7, 1, ofp) != 1)
        goto output_err;
      for (i = 0; i < hex_to_int(rbuf + 1); i++) {
        obuf[0] = hex_to_int(rbuf + 9 + i * 2);
        if (fwrite(obuf, 1, 1, ofp) != 1)
          goto output_err;
      }
      break;
    case 1:
      if (fwrite("\x4e\xfc\x04\xff\xff\xff\xff", 7, 1, ofp) != 1)
        goto output_err;
      goto end;
    default:
      return -EINVAL;
    }
  }
  puts("hex file formatting error");
  return -EINVAL;
output_err:
  puts("error on writing output file");
  return -EIO;
end:
  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)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    14cf <main+0x34f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xea7(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     120e <main+0x8e>
cmpq   $0x0,-0x28(%rbp)
jne    122d <main+0xad>
lea    0xe17(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14cf <main+0x34f>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    148a <main+0x30a>
jmp    1257 <main+0xd7>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xd,%ecx
mov    %al,-0x51(%rbp)
je     1290 <main+0x110>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    12a0 <main+0x120>
jmp    12ae <main+0x12e>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1257 <main+0xd7>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1030 <check_hex_line@plt>
cmp    $0x0,%eax
jne    12dd <main+0x15d>
jmp    148a <main+0x30a>
mov    -0x30(%rbp),%rdi
add    $0x7,%rdi
call   1070 <hex_to_int@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x58(%rbp)
test   %eax,%eax
je     134b <main+0x1cb>
jmp    1300 <main+0x180>
mov    -0x58(%rbp),%eax
sub    $0x1,%eax
je     1445 <main+0x2c5>
jmp    1311 <main+0x191>
mov    -0x58(%rbp),%eax
sub    $0x4,%eax
jne    1472 <main+0x2f2>
jmp    1322 <main+0x1a2>
mov    -0x30(%rbp),%rdi
add    $0x9,%rdi
call   1060 <lhex_to_int@plt>
shl    $0x10,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1485 <main+0x305>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x3,%rdi
call   1060 <lhex_to_int@plt>
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x50(%rbp)
movb   $0x4c,-0x4b(%rbp)
movb   $0xfc,-0x4a(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <hex_to_int@plt>
add    $0x4,%eax
mov    %al,-0x49(%rbp)
mov    -0x50(%rbp),%eax
mov    %al,-0x48(%rbp)
mov    -0x50(%rbp),%eax
shr    $0x8,%eax
mov    %al,-0x47(%rbp)
mov    -0x50(%rbp),%eax
shr    $0x10,%eax
mov    %al,-0x46(%rbp)
mov    -0x50(%rbp),%eax
shr    $0x18,%eax
mov    %al,-0x45(%rbp)
lea    -0x4b(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x7,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     13c9 <main+0x249>
jmp    14a9 <main+0x329>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <hex_to_int@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
cmp    %ecx,%eax
jge    1440 <main+0x2c0>
mov    -0x30(%rbp),%rdi
add    $0x9,%rdi
mov    -0x38(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
call   1070 <hex_to_int@plt>
mov    %al,-0x4b(%rbp)
lea    -0x4b(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     142d <main+0x2ad>
jmp    14a9 <main+0x329>
jmp    1432 <main+0x2b2>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    13d0 <main+0x250>
jmp    1485 <main+0x305>
mov    -0x28(%rbp),%rcx
lea    0xc0e(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
je     146d <main+0x2ed>
jmp    14a9 <main+0x329>
jmp    14c8 <main+0x348>
xor    %eax,%eax
mov    0x2b5d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14cf <main+0x34f>
jmp    1235 <main+0xb5>
lea    0xbd5(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
mov    0x2b39(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14cf <main+0x34f>
lea    0xbd0(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
mov    0x2b22(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    14cf <main+0x34f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1204 <main+0x84>
mov    0x8(%rbx),%rdi
lea    0xedd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
test   %r14,%r14
je     121c <main+0x9c>
mov    %rax,%r15
test   %rax,%rax
je     121c <main+0x9c>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %r14,%rdx
call   1090 <getline@plt>
test   %eax,%eax
jle    13bf <main+0x23f>
lea    0xe51(%rip),%rbx
lea    0x8(%rsp),%r12
lea    0x20(%rsp),%r13
movl   $0x0,0x1c(%rsp)
jmp    124c <main+0xcc>
mov    (%rbx),%rsi
lea    0xdf2(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1412 <main+0x292>
lea    0xe09(%rip),%rdi
jmp    1402 <main+0x282>
mov    %r14,%rbx
mov    %r12,%r14
mov    %r13,%r12
lea    0x20(%rsp),%r13
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1090 <getline@plt>
test   %eax,%eax
jle    13bf <main+0x23f>
mov    0x8(%rsp),%rdx
mov    %eax,%ebp
jmp    1264 <main+0xe4>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffffffffffff,%rbp
movzbl -0x1(%rdx,%rbp,1),%eax
cmp    $0xd,%al
je     1260 <main+0xe0>
cmp    $0xa,%al
je     1260 <main+0xe0>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1030 <check_hex_line@plt>
test   %eax,%eax
je     13bf <main+0x23f>
mov    0x8(%rsp),%rdi
add    $0x7,%rdi
call   1070 <hex_to_int@plt>
test   %eax,%eax
je     12e0 <main+0x160>
cmp    $0x4,%eax
jne    13d4 <main+0x254>
mov    0x8(%rsp),%rdi
add    $0x9,%rdi
call   1060 <lhex_to_int@plt>
shl    $0x10,%eax
lea    0xd85(%rip),%rdi
mov    %eax,0x1c(%rsp)
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1236 <main+0xb6>
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%r13
mov    %r14,%r12
mov    %rbx,%r14
mov    0x8(%rsp),%rdi
add    $0x3,%rdi
call   1060 <lhex_to_int@plt>
mov    %eax,%ebx
add    0x1c(%rsp),%ebx
movw   $0xfc4c,0x15(%rsp)
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1070 <hex_to_int@plt>
add    $0x4,%al
mov    %al,0x17(%rsp)
mov    %bl,0x18(%rsp)
mov    %bh,0x19(%rsp)
mov    %ebx,%eax
shr    $0x10,%eax
mov    %al,0x1a(%rsp)
shr    $0x18,%ebx
mov    %bl,0x1b(%rsp)
lea    0x15(%rsp),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    13fb <main+0x27b>
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1070 <hex_to_int@plt>
test   %eax,%eax
jle    1228 <main+0xa8>
mov    $0x9,%ebx
xor    %ebp,%ebp
nopl   (%rax)
mov    0x8(%rsp),%rdi
add    %rbx,%rdi
call   1070 <hex_to_int@plt>
mov    %al,0x15(%rsp)
lea    0x15(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    13fb <main+0x27b>
add    $0x1,%rbp
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1070 <hex_to_int@plt>
cltq
add    $0x2,%rbx
cmp    %rax,%rbp
jl     1370 <main+0x1f0>
jmp    1228 <main+0xa8>
lea    0xca0(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c06(%rip),%rax
jmp    140e <main+0x28e>
cmp    $0x1,%eax
jne    13cb <main+0x24b>
lea    0xc7e(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    13fb <main+0x27b>
xor    %ebx,%ebx
jmp    1412 <main+0x292>
lea    0xc7e(%rip),%rdi
call   1040 <puts@plt>
mov    0x2bd2(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Usage: %s <input hex file> <output file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"failed to open file.\00"
@EIO = global i32 0
@.str.4 = [8 x i8] c"%d, %s\0A\00"
@.str.5 = [21 x i8] c"bump addr to 0x%08X\0A\00"
@.str.6 = [8 x i8] c"N\FC\04\FF\FF\FF\FF\00"
@EINVAL = global i32 0
@.str.7 = [26 x i8] c"hex file formatting error\00"
@.str.8 = [29 x i8] c"error on writing output file\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 i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca [7 x i8]
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 0, i32* %3
br label %171
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %7
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %8
%32 = load i32*, i32** %7
%33 = icmp eq i32* %32, null
br i1 %33, label %37, label %34
34: ; preds = %23
%35 = load i32*, i32** %8
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %34, %23
%38 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EIO
%40 = sub nsw i32 0, %39
store i32 %40, i32* %3
br label %171
41: ; preds = %34
store i8* null, i8** %9
br label %42
42: ; preds = %161, %41
%43 = load i32*, i32** %7
%44 = call i32 @getline(i8** %9, i64* %12, i32* %43)
store i32 %44, i32* %10
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %162
46: ; preds = %42
br label %47
47: ; preds = %67, %46
%48 = load i8*, i8** %9
%49 = load i32, i32* %10
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 13
br i1 %55, label %65, label %56
56: ; preds = %47
%57 = load i8*, i8** %9
%58 = load i32, i32* %10
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br label %65
65: ; preds = %56, %47
%66 = phi i1 [ true, %47 ], [ %64, %56 ]
br i1 %66, label %67, label %70
67: ; preds = %65
%68 = load i32, i32* %10
%69 = add nsw i32 %68, -1
store i32 %69, i32* %10
br label %47
70: ; preds = %65
%71 = load i32, i32* %10
%72 = load i8*, i8** %9
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %71, i8* %72)
%74 = load i8*, i8** %9
%75 = load i32, i32* %10
%76 = call i32 @check_hex_line(i8* %74, i32 %75)
%77 = icmp ne i32 %76, 0
br i1 %77, label %79, label %78
78: ; preds = %70
br label %162
79: ; preds = %70
%80 = load i8*, i8** %9
%81 = getelementptr inbounds i8, i8* %80, i64 7
%82 = call i32 @hex_to_int(i8* %81)
store i32 %82, i32* %13
%83 = load i32, i32* %13
switch i32 %83, label %158 [
i32 4, label %84
i32 0, label %91
i32 1, label %152
]
84: ; preds = %79
%85 = load i8*, i8** %9
%86 = getelementptr inbounds i8, i8* %85, i64 9
%87 = call i32 @lhex_to_int(i8* %86)
%88 = mul nsw i32 %87, 65536
store i32 %88, i32* %6
%89 = load i32, i32* %6
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %89)
br label %161
91: ; preds = %79
%92 = load i32, i32* %6
%93 = load i8*, i8** %9
%94 = getelementptr inbounds i8, i8* %93, i64 3
%95 = call i32 @lhex_to_int(i8* %94)
%96 = add i32 %92, %95
store i32 %96, i32* %15
%97 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 0
store i8 76, i8* %97
%98 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 1
store i8 -4, i8* %98
%99 = load i8*, i8** %9
%100 = getelementptr inbounds i8, i8* %99, i64 1
%101 = call i32 @hex_to_int(i8* %100)
%102 = add nsw i32 %101, 4
%103 = trunc i32 %102 to i8
%104 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 2
store i8 %103, i8* %104
%105 = load i32, i32* %15
%106 = trunc i32 %105 to i8
%107 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 3
store i8 %106, i8* %107
%108 = load i32, i32* %15
%109 = lshr i32 %108, 8
%110 = trunc i32 %109 to i8
%111 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 4
store i8 %110, i8* %111
%112 = load i32, i32* %15
%113 = lshr i32 %112, 16
%114 = trunc i32 %113 to i8
%115 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 5
store i8 %114, i8* %115
%116 = load i32, i32* %15
%117 = lshr i32 %116, 24
%118 = trunc i32 %117 to i8
%119 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 6
store i8 %118, i8* %119
%120 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 0
%121 = load i32*, i32** %8
%122 = call i32 @fwrite(i8* %120, i32 7, i32 1, i32* %121)
%123 = icmp ne i32 %122, 1
br i1 %123, label %124, label %125
124: ; preds = %91
br label %166
125: ; preds = %91
store i32 0, i32* %11
br label %126
126: ; preds = %148, %125
%127 = load i32, i32* %11
%128 = load i8*, i8** %9
%129 = getelementptr inbounds i8, i8* %128, i64 1
%130 = call i32 @hex_to_int(i8* %129)
%131 = icmp slt i32 %127, %130
br i1 %131, label %132, label %151
132: ; preds = %126
%133 = load i8*, i8** %9
%134 = getelementptr inbounds i8, i8* %133, i64 9
%135 = load i32, i32* %11
%136 = mul nsw i32 %135, 2
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds i8, i8* %134, i64 %137
%139 = call i32 @hex_to_int(i8* %138)
%140 = trunc i32 %139 to i8
%141 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 0
store i8 %140, i8* %141
%142 = getelementptr inbounds [7 x i8], [7 x i8]* %14, i64 0, i64 0
%143 = load i32*, i32** %8
%144 = call i32 @fwrite(i8* %142, i32 1, i32 1, i32* %143)
%145 = icmp ne i32 %144, 1
br i1 %145, label %146, label %147
146: ; preds = %132
br label %166
147: ; preds = %132
br label %148
148: ; preds = %147
%149 = load i32, i32* %11
%150 = add nsw i32 %149, 1
store i32 %150, i32* %11
br label %126
151: ; preds = %126
br label %161
152: ; preds = %79
%153 = load i32*, i32** %8
%154 = call i32 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7, i32 1, i32* %153)
%155 = icmp ne i32 %154, 1
br i1 %155, label %156, label %157
156: ; preds = %152
br label %166
157: ; preds = %152
br label %170
158: ; preds = %79
%159 = load i32, i32* @EINVAL
%160 = sub nsw i32 0, %159
store i32 %160, i32* %3
br label %171
161: ; preds = %151, %84
br label %42
162: ; preds = %78, %42
%163 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%164 = load i32, i32* @EINVAL
%165 = sub nsw i32 0, %164
store i32 %165, i32* %3
br label %171
166: ; preds = %156, %146, %124
%167 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%168 = load i32, i32* @EIO
%169 = sub nsw i32 0, %168
store i32 %169, i32* %3
br label %171
170: ; preds = %157
store i32 0, i32* %3
br label %171
171: ; preds = %170, %166, %162, %158, %37, %18
%172 = load i32, i32* %3
ret i32 %172
}

LLVM IR (-O2):
@.str = [42 x i8] c"Usage: %s <input hex file> <output file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"failed to open file.\00"
@EIO = global i32 0
@.str.4 = [8 x i8] c"%d, %s\0A\00"
@.str.5 = [21 x i8] c"bump addr to 0x%08X\0A\00"
@.str.6 = [8 x i8] c"N\FC\04\FF\FF\FF\FF\00"
@EINVAL = global i32 0
@.str.7 = [26 x i8] c"hex file formatting error\00"
@.str.8 = [29 x i8] c"error on writing output file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [7 x i8]
%6 = bitcast i8** %3 to i8*
%7 = bitcast i64* %4 to i8*
%8 = icmp eq i32 %0, 3
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %125
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %15, null
%20 = icmp eq i32* %18, null
%21 = or i1 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EIO
%25 = sub nsw i32 0, %24
br label %125
26: ; preds = %12
store i8* null, i8** %3
%27 = call i32 @getline(i8** %3, i64* %4, i32* %15)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %116
29: ; preds = %26
%30 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%31 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 1
%32 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 2
%33 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 3
%34 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 4
%35 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 5
%36 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 6
br label %37
37: ; preds = %29, %112
%38 = phi i32 [ %27, %29 ], [ %114, %112 ]
%39 = phi i32 [ 0, %29 ], [ %113, %112 ]
%40 = load i8*, i8** %3
%41 = zext i32 %38 to i64
br label %42
42: ; preds = %47, %37
%43 = phi i64 [ %41, %37 ], [ %44, %47 ]
%44 = add nsw i64 %43, -1
%45 = getelementptr inbounds i8, i8* %40, i64 %44
%46 = load i8, i8* %45
switch i8 %46, label %48 [
i8 13, label %47
i8 10, label %47
]
47: ; preds = %42, %42
br label %42
48: ; preds = %42
%49 = trunc i64 %43 to i32
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %49, i8* %40)
%51 = load i8*, i8** %3
%52 = call i32 @check_hex_line(i8* %51, i32 %49)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %48
br label %116
55: ; preds = %48
%56 = load i8*, i8** %3
%57 = getelementptr inbounds i8, i8* %56, i64 7
%58 = call i32 @hex_to_int(i8* %57)
switch i32 %58, label %109 [
i32 4, label %59
i32 0, label %65
i32 1, label %106
]
59: ; preds = %55
%60 = load i8*, i8** %3
%61 = getelementptr inbounds i8, i8* %60, i64 9
%62 = call i32 @lhex_to_int(i8* %61)
%63 = shl nsw i32 %62, 16
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %63)
br label %112
65: ; preds = %55
%66 = load i8*, i8** %3
%67 = getelementptr inbounds i8, i8* %66, i64 3
%68 = call i32 @lhex_to_int(i8* %67)
%69 = add i32 %68, %39
store i8 76, i8* %30
store i8 -4, i8* %31
%70 = load i8*, i8** %3
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = call i32 @hex_to_int(i8* %71)
%73 = trunc i32 %72 to i8
%74 = add i8 %73, 4
store i8 %74, i8* %32
%75 = trunc i32 %69 to i8
store i8 %75, i8* %33
%76 = lshr i32 %69, 8
%77 = trunc i32 %76 to i8
store i8 %77, i8* %34
%78 = lshr i32 %69, 16
%79 = trunc i32 %78 to i8
store i8 %79, i8* %35
%80 = lshr i32 %69, 24
%81 = trunc i32 %80 to i8
store i8 %81, i8* %36
%82 = call i32 @fwrite(i8* %30, i32 7, i32 1, i32* %18)
%83 = icmp eq i32 %82, 1
br i1 %83, label %84, label %120
84: ; preds = %65
%85 = load i8*, i8** %3
%86 = getelementptr inbounds i8, i8* %85, i64 1
%87 = call i32 @hex_to_int(i8* %86)
%88 = icmp sgt i32 %87, 0
br i1 %88, label %95, label %112
89: ; preds = %95
%90 = load i8*, i8** %3
%91 = getelementptr inbounds i8, i8* %90, i64 1
%92 = call i32 @hex_to_int(i8* %91)
%93 = sext i32 %92 to i64
%94 = icmp slt i64 %105, %93
br i1 %94, label %95, label %112
95: ; preds = %84, %89
%96 = phi i64 [ %105, %89 ], [ 0, %84 ]
%97 = load i8*, i8** %3
%98 = getelementptr inbounds i8, i8* %97, i64 9
%99 = shl nuw nsw i64 %96, 1
%100 = getelementptr inbounds i8, i8* %98, i64 %99
%101 = call i32 @hex_to_int(i8* %100)
%102 = trunc i32 %101 to i8
store i8 %102, i8* %30
%103 = call i32 @fwrite(i8* %30, i32 1, i32 1, i32* %18)
%104 = icmp eq i32 %103, 1
%105 = add nuw nsw i64 %96, 1
br i1 %104, label %89, label %120
106: ; preds = %55
%107 = call i32 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7, i32 1, i32* %18)
%108 = icmp eq i32 %107, 1
br i1 %108, label %124, label %120
109: ; preds = %55
%110 = load i32, i32* @EINVAL
%111 = sub nsw i32 0, %110
br label %125
112: ; preds = %89, %84, %59
%113 = phi i32 [ %63, %59 ], [ %39, %84 ], [ %39, %89 ]
%114 = call i32 @getline(i8** %3, i64* %4, i32* %15)
%115 = icmp sgt i32 %114, 0
br i1 %115, label %37, label %116
116: ; preds = %112, %26, %54
%117 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%118 = load i32, i32* @EINVAL
%119 = sub nsw i32 0, %118
br label %125
120: ; preds = %65, %95, %106
%121 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%122 = load i32, i32* @EIO
%123 = sub nsw i32 0, %122
br label %125
124: ; preds = %106
br label %125
125: ; preds = %109, %124, %120, %116, %22, %9
%126 = phi i32 [ 0, %9 ], [ %25, %22 ], [ 0, %124 ], [ %123, %120 ], [ %119, %116 ], [ %111, %109 ]
ret i32 %126
}