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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int crc_reflect(int, int);
int crc_update(int, int *, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int *malloc(size_t);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ strtoul(char *, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  FILE *f;
  size_t flen;
  if (argc < 3) {
    printf("Usage: genhdr <input file> <signature> <version>\n"
           "  input file: file to be headered\n"
           "  signature : magic value at start of header (4-char string)\n"
           "  version   : firmware version (decimal uint32)\n"
           "Output is written in place.\n");
    return 1;
  }
  if ((f = fopen(argv[1], "rb+")) == NULL) {
    printf("Unable to open input file %s", argv[1]);
    perror("");
    return 1;
  }
  fseek(f, 0, SEEK_END);
  flen = ftell(f);
  if (flen + 256 < flen) {
    printf("File too large ;)\n");
    fclose(f);
    return 1;
  }
  char *remaining = NULL;
  uint32_t version = (uint32_t)strtoul(argv[3], &remaining, 0);
  if (*remaining) {
    printf("could not parse version number (remaining portion: %s)\n",
           remaining);
    fclose(f);
    return 1;
  }
  if (strlen(argv[2]) > 4) {
    printf("Magic string '%s' too long. Truncated to 4 characters.\n", argv[2]);
  }
  uint8_t *buf = malloc(flen + 256);
  if (!buf) {
    perror("malloc");
    fclose(f);
    return -1;
  }
  memset(buf, 0xff, 256);
  fseek(f, 0, SEEK_SET);
  fread(buf + 256, 1, flen, f);
  uint32_t crcc = 0xffffffff, crc;
  crcc = crc_update(crcc, buf + 256, flen);
  crcc = crc_reflect(crcc, 32);
  crc = crcc ^ 0xffffffff;
  memset(buf, 0, 4);
  strncpy((char *)buf, argv[2], 4);
  buf[4] = (version & 0xff);
  buf[5] = ((version >> 8) & 0xff);
  buf[6] = ((version >> 16) & 0xff);
  buf[7] = ((version >> 24) & 0xff);
  buf[8] = (flen & 0xff);
  buf[9] = ((flen >> 8) & 0xff);
  buf[10] = ((flen >> 16) & 0xff);
  buf[11] = ((flen >> 24) & 0xff);
  buf[12] = (crc & 0xff);
  buf[13] = ((crc >> 8) & 0xff);
  buf[14] = ((crc >> 16) & 0xff);
  buf[15] = ((crc >> 24) & 0xff);
  buf[16] = (crcc & 0xff);
  buf[17] = ((crcc >> 8) & 0xff);
  buf[18] = ((crcc >> 16) & 0xff);
  buf[19] = ((crcc >> 24) & 0xff);
  fseek(f, 0, SEEK_SET);
  fwrite(buf, 1, 256 + flen, f);
  fclose(f);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    123a <main+0x3a>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c2 <main+0x3c2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe94(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    128a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    0xe97(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c2 <main+0x3c2>
mov    -0x18(%rbp),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x100,%rax
cmp    -0x20(%rbp),%rax
jae    12e2 <main+0xe2>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c2 <main+0x3c2>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1110 <strtoul@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1334 <main+0x134>
mov    -0x28(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    15c2 <main+0x3c2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <strlen@plt>
cmp    $0x4,%eax
jle    1360 <main+0x160>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x100,%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    13a0 <main+0x1a0>
lea    0xdfb(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15c2 <main+0x3c2>
mov    -0x38(%rbp),%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x38(%rbp),%rdi
add    $0x400,%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    $0x400,%rsi
mov    -0x20(%rbp),%rdx
call   1070 <crc_update@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    $0x20,%esi
call   10b0 <crc_reflect@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   10a0 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x2c(%rbp),%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x2c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    -0x2c(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x2c(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x1c(%rax)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    -0x20(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x24(%rax)
mov    -0x20(%rbp),%rax
shr    $0x10,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x28(%rax)
mov    -0x20(%rbp),%rax
shr    $0x18,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x2c(%rax)
mov    -0x40(%rbp),%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x30(%rax)
mov    -0x40(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x34(%rax)
mov    -0x40(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x38(%rax)
mov    -0x40(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x3c(%rax)
mov    -0x3c(%rbp),%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x40(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x44(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x48(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4c(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2a56(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
add    $0x100,%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1120 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jg     1234 <main+0x24>
lea    0xe80(%rip),%rdi
call   1050 <puts@plt>
jmp    12c0 <main+0xb0>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xdbe(%rip),%rsi
call   1100 <fopen@plt>
test   %rax,%rax
je     1468 <main+0x258>
mov    %rax,%r15
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %r15,%rdi
call   10d0 <ftell@plt>
cmp    $0xffffffffffffff00,%rax
jb     1284 <main+0x74>
lea    0xe1b(%rip),%rdi
call   1050 <puts@plt>
jmp    12b8 <main+0xa8>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
mov    0x18(%rbp),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1120 <strtoul@plt>
mov    0x8(%rsp),%rsi
cmpb   $0x0,(%rsi)
je     12d8 <main+0xc8>
lea    0xd70(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,0x4(%rsp)
mov    %rbx,%r14
lea    0x100(%rbx),%r12
mov    0x10(%rbp),%rdi
call   1090 <strlen@plt>
cmp    $0x5,%eax
jl     1306 <main+0xf6>
mov    0x10(%rbp),%rsi
lea    0xd5a(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r12,%rdi
call   10e0 <malloc@plt>
test   %rax,%rax
je     148b <main+0x27b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   10b0 <memset@plt>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edx
mov    %r12,0x10(%rsp)
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbx,%r13
add    $0x400,%r13
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <fread@plt>
mov    $0xffffffff,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1080 <crc_update@plt>
mov    %eax,%edi
mov    $0x20,%esi
call   10c0 <crc_reflect@plt>
mov    %eax,%r13d
mov    %eax,(%rsp)
not    %r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   10b0 <memset@plt>
mov    0x10(%rbp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    0x4(%rsp),%ecx
movzbl %cl,%eax
mov    %eax,0x10(%rbx)
movzbl %ch,%eax
mov    %eax,0x14(%rbx)
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x18(%rbx)
shr    $0x18,%ecx
mov    %ecx,0x1c(%rbx)
mov    %r14,%rcx
movzbl %cl,%eax
mov    %eax,0x20(%rbx)
movzbl %ch,%eax
mov    %eax,0x24(%rbx)
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x28(%rbx)
shr    $0x18,%ecx
mov    %ecx,0x2c(%rbx)
mov    %r13d,%ecx
movzbl %cl,%eax
mov    %eax,0x30(%rbx)
movzbl %ch,%eax
mov    %eax,0x34(%rbx)
mov    %r13d,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x38(%rbx)
shr    $0x18,%ecx
mov    %ecx,0x3c(%rbx)
mov    (%rsp),%ecx
movzbl %cl,%eax
mov    %eax,0x40(%rbx)
movzbl %ch,%eax
mov    %eax,0x44(%rbx)
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x48(%rbx)
shr    $0x18,%ecx
mov    %ecx,0x4c(%rbx)
mov    0x2ba6(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    0x10(%rsp),%rdx
mov    %r15,%rcx
call   1130 <fwrite@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    12c6 <main+0xb6>
mov    0x8(%rbp),%rsi
lea    0xb91(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0xbd7(%rip),%rdi
call   1110 <perror@plt>
jmp    12c0 <main+0xb0>
lea    0xbff(%rip),%rdi
call   1110 <perror@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    $0xffffffff,%r12d
jmp    12c6 <main+0xb6>

LLVM IR (-O0):
@.str = [221 x i8] c"Usage: genhdr <input file> <signature> <version>\0A input file: file to be headered\0A signature : magic value at start of header (4-char string)\0A version : firmware version (decimal uint32)\0AOutput is written in place.\0A\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [29 x i8] c"Unable to open input file %s\00"
@.str.3 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.4 = [19 x i8] c"File too large ;)\0A\00"
@.str.5 = [56 x i8] c"could not parse version number (remaining portion: %s)\0A\00"
@.str.6 = [56 x i8] c"Magic string '%s' too long. Truncated to 4 characters.\0A\00"
@.str.7 = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([221 x i8], [221 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %198
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %198
29: ; preds = %17
%30 = load i32*, i32** %6
%31 = load i32, i32* @SEEK_END
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i32*, i32** %6
%34 = call i64 @ftell(i32* %33)
store i64 %34, i64* %7
%35 = load i64, i64* %7
%36 = add i64 %35, 256
%37 = load i64, i64* %7
%38 = icmp ult i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %3
br label %198
43: ; preds = %29
store i8* null, i8** %8
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 3
%46 = load i8*, i8** %45
%47 = call i32 @strtoul(i8* %46, i8** %8, i32 0)
store i32 %47, i32* %9
%48 = load i8*, i8** %8
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %43
%52 = load i8*, i8** %8
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
store i32 1, i32* %3
br label %198
56: ; preds = %43
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 2
%59 = load i8*, i8** %58
%60 = call i32 @strlen(i8* %59)
%61 = icmp sgt i32 %60, 4
br i1 %61, label %62, label %67
62: ; preds = %56
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 2
%65 = load i8*, i8** %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %62, %56
%68 = load i64, i64* %7
%69 = add i64 %68, 256
%70 = call i32* @malloc(i64 %69)
store i32* %70, i32** %10
%71 = load i32*, i32** %10
%72 = icmp ne i32* %71, null
br i1 %72, label %77, label %73
73: ; preds = %67
%74 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%75 = load i32*, i32** %6
%76 = call i32 @fclose(i32* %75)
store i32 -1, i32* %3
br label %198
77: ; preds = %67
%78 = load i32*, i32** %10
%79 = call i32 @memset(i32* %78, i32 255, i32 256)
%80 = load i32*, i32** %6
%81 = load i32, i32* @SEEK_SET
%82 = call i32 @fseek(i32* %80, i32 0, i32 %81)
%83 = load i32*, i32** %10
%84 = getelementptr inbounds i32, i32* %83, i64 256
%85 = load i64, i64* %7
%86 = load i32*, i32** %6
%87 = call i32 @fread(i32* %84, i32 1, i64 %85, i32* %86)
store i32 -1, i32* %11
%88 = load i32, i32* %11
%89 = load i32*, i32** %10
%90 = getelementptr inbounds i32, i32* %89, i64 256
%91 = load i64, i64* %7
%92 = call i32 @crc_update(i32 %88, i32* %90, i64 %91)
store i32 %92, i32* %11
%93 = load i32, i32* %11
%94 = call i32 @crc_reflect(i32 %93, i32 32)
store i32 %94, i32* %11
%95 = load i32, i32* %11
%96 = xor i32 %95, -1
store i32 %96, i32* %12
%97 = load i32*, i32** %10
%98 = call i32 @memset(i32* %97, i32 0, i32 4)
%99 = load i32*, i32** %10
%100 = bitcast i32* %99 to i8*
%101 = load i8**, i8*** %5
%102 = getelementptr inbounds i8*, i8** %101, i64 2
%103 = load i8*, i8** %102
%104 = call i32 @strncpy(i8* %100, i8* %103, i32 4)
%105 = load i32, i32* %9
%106 = and i32 %105, 255
%107 = load i32*, i32** %10
%108 = getelementptr inbounds i32, i32* %107, i64 4
store i32 %106, i32* %108
%109 = load i32, i32* %9
%110 = ashr i32 %109, 8
%111 = and i32 %110, 255
%112 = load i32*, i32** %10
%113 = getelementptr inbounds i32, i32* %112, i64 5
store i32 %111, i32* %113
%114 = load i32, i32* %9
%115 = ashr i32 %114, 16
%116 = and i32 %115, 255
%117 = load i32*, i32** %10
%118 = getelementptr inbounds i32, i32* %117, i64 6
store i32 %116, i32* %118
%119 = load i32, i32* %9
%120 = ashr i32 %119, 24
%121 = and i32 %120, 255
%122 = load i32*, i32** %10
%123 = getelementptr inbounds i32, i32* %122, i64 7
store i32 %121, i32* %123
%124 = load i64, i64* %7
%125 = and i64 %124, 255
%126 = trunc i64 %125 to i32
%127 = load i32*, i32** %10
%128 = getelementptr inbounds i32, i32* %127, i64 8
store i32 %126, i32* %128
%129 = load i64, i64* %7
%130 = lshr i64 %129, 8
%131 = and i64 %130, 255
%132 = trunc i64 %131 to i32
%133 = load i32*, i32** %10
%134 = getelementptr inbounds i32, i32* %133, i64 9
store i32 %132, i32* %134
%135 = load i64, i64* %7
%136 = lshr i64 %135, 16
%137 = and i64 %136, 255
%138 = trunc i64 %137 to i32
%139 = load i32*, i32** %10
%140 = getelementptr inbounds i32, i32* %139, i64 10
store i32 %138, i32* %140
%141 = load i64, i64* %7
%142 = lshr i64 %141, 24
%143 = and i64 %142, 255
%144 = trunc i64 %143 to i32
%145 = load i32*, i32** %10
%146 = getelementptr inbounds i32, i32* %145, i64 11
store i32 %144, i32* %146
%147 = load i32, i32* %12
%148 = and i32 %147, 255
%149 = load i32*, i32** %10
%150 = getelementptr inbounds i32, i32* %149, i64 12
store i32 %148, i32* %150
%151 = load i32, i32* %12
%152 = ashr i32 %151, 8
%153 = and i32 %152, 255
%154 = load i32*, i32** %10
%155 = getelementptr inbounds i32, i32* %154, i64 13
store i32 %153, i32* %155
%156 = load i32, i32* %12
%157 = ashr i32 %156, 16
%158 = and i32 %157, 255
%159 = load i32*, i32** %10
%160 = getelementptr inbounds i32, i32* %159, i64 14
store i32 %158, i32* %160
%161 = load i32, i32* %12
%162 = ashr i32 %161, 24
%163 = and i32 %162, 255
%164 = load i32*, i32** %10
%165 = getelementptr inbounds i32, i32* %164, i64 15
store i32 %163, i32* %165
%166 = load i32, i32* %11
%167 = and i32 %166, 255
%168 = load i32*, i32** %10
%169 = getelementptr inbounds i32, i32* %168, i64 16
store i32 %167, i32* %169
%170 = load i32, i32* %11
%171 = ashr i32 %170, 8
%172 = and i32 %171, 255
%173 = load i32*, i32** %10
%174 = getelementptr inbounds i32, i32* %173, i64 17
store i32 %172, i32* %174
%175 = load i32, i32* %11
%176 = ashr i32 %175, 16
%177 = and i32 %176, 255
%178 = load i32*, i32** %10
%179 = getelementptr inbounds i32, i32* %178, i64 18
store i32 %177, i32* %179
%180 = load i32, i32* %11
%181 = ashr i32 %180, 24
%182 = and i32 %181, 255
%183 = load i32*, i32** %10
%184 = getelementptr inbounds i32, i32* %183, i64 19
store i32 %182, i32* %184
%185 = load i32*, i32** %6
%186 = load i32, i32* @SEEK_SET
%187 = call i32 @fseek(i32* %185, i32 0, i32 %186)
%188 = load i32*, i32** %10
%189 = load i64, i64* %7
%190 = add i64 256, %189
%191 = trunc i64 %190 to i32
%192 = load i32*, i32** %6
%193 = call i32 @fwrite(i32* %188, i32 1, i32 %191, i32* %192)
%194 = load i32*, i32** %6
%195 = call i32 @fclose(i32* %194)
%196 = load i32*, i32** %10
%197 = call i32 @free(i32* %196)
store i32 0, i32* %3
br label %198
198: ; preds = %77, %73, %51, %39, %23, %15
%199 = load i32, i32* %3
ret i32 %199
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [29 x i8] c"Unable to open input file %s\00"
@.str.3 = [1 x i8] zeroinitializer
@SEEK_END = global i32 0
@.str.5 = [56 x i8] c"could not parse version number (remaining portion: %s)\0A\00"
@.str.6 = [56 x i8] c"Magic string '%s' too long. Truncated to 4 characters.\0A\00"
@.str.7 = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
@str = [18 x i8] c"File too large ;)\00"
@str.8 = [220 x i8] c"Usage: genhdr <input file> <signature> <version>\0A input file: file to be headered\0A signature : magic value at start of header (4-char string)\0A version : firmware version (decimal uint32)\0AOutput is written in place.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([220 x i8], [220 x i8]* @str.8, i64 0, i64 0))
br label %112
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %112
16: ; preds = %7
%17 = load i32, i32* @SEEK_END
%18 = tail call i32 @fseek(i32* %10, i32 0, i32 %17)
%19 = tail call i64 @ftell(i32* %10)
%20 = add i64 %19, 256
%21 = icmp ugt i64 %19, -257
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @fclose(i32* %10)
br label %112
25: ; preds = %16
%26 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%27 = getelementptr inbounds i8*, i8** %1, i64 3
%28 = load i8*, i8** %27
%29 = call i32 @strtoul(i8* %28, i8** %3, i32 0)
%30 = load i8*, i8** %3
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %25
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i8* %30)
%35 = tail call i32 @fclose(i32* %10)
br label %110
36: ; preds = %25
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32 @strlen(i8* %38)
%40 = icmp sgt i32 %39, 4
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %37
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %36
%45 = tail call i32* @malloc(i64 %20)
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%49 = tail call i32 @fclose(i32* %10)
br label %110
50: ; preds = %44
%51 = tail call i32 @memset(i32* %45, i32 255, i32 256)
%52 = load i32, i32* @SEEK_SET
%53 = tail call i32 @fseek(i32* %10, i32 0, i32 %52)
%54 = getelementptr inbounds i32, i32* %45, i64 256
%55 = tail call i32 @fread(i32* %54, i32 1, i64 %19, i32* %10)
%56 = tail call i32 @crc_update(i32 -1, i32* %54, i64 %19)
%57 = tail call i32 @crc_reflect(i32 %56, i32 32)
%58 = xor i32 %57, -1
%59 = tail call i32 @memset(i32* %45, i32 0, i32 4)
%60 = bitcast i32* %45 to i8*
%61 = load i8*, i8** %37
%62 = tail call i32 @strncpy(i8* %60, i8* %61, i32 4)
%63 = and i32 %29, 255
%64 = getelementptr inbounds i32, i32* %45, i64 4
store i32 %63, i32* %64
%65 = lshr i32 %29, 8
%66 = and i32 %65, 255
%67 = getelementptr inbounds i32, i32* %45, i64 5
store i32 %66, i32* %67
%68 = lshr i32 %29, 16
%69 = and i32 %68, 255
%70 = getelementptr inbounds i32, i32* %45, i64 6
store i32 %69, i32* %70
%71 = lshr i32 %29, 24
%72 = getelementptr inbounds i32, i32* %45, i64 7
store i32 %71, i32* %72
%73 = trunc i64 %19 to i32
%74 = and i32 %73, 255
%75 = getelementptr inbounds i32, i32* %45, i64 8
store i32 %74, i32* %75
%76 = lshr i32 %73, 8
%77 = and i32 %76, 255
%78 = getelementptr inbounds i32, i32* %45, i64 9
store i32 %77, i32* %78
%79 = lshr i32 %73, 16
%80 = and i32 %79, 255
%81 = getelementptr inbounds i32, i32* %45, i64 10
store i32 %80, i32* %81
%82 = lshr i32 %73, 24
%83 = getelementptr inbounds i32, i32* %45, i64 11
store i32 %82, i32* %83
%84 = and i32 %58, 255
%85 = getelementptr inbounds i32, i32* %45, i64 12
store i32 %84, i32* %85
%86 = lshr i32 %58, 8
%87 = and i32 %86, 255
%88 = getelementptr inbounds i32, i32* %45, i64 13
store i32 %87, i32* %88
%89 = lshr i32 %58, 16
%90 = and i32 %89, 255
%91 = getelementptr inbounds i32, i32* %45, i64 14
store i32 %90, i32* %91
%92 = lshr i32 %58, 24
%93 = getelementptr inbounds i32, i32* %45, i64 15
store i32 %92, i32* %93
%94 = and i32 %57, 255
%95 = getelementptr inbounds i32, i32* %45, i64 16
store i32 %94, i32* %95
%96 = lshr i32 %57, 8
%97 = and i32 %96, 255
%98 = getelementptr inbounds i32, i32* %45, i64 17
store i32 %97, i32* %98
%99 = lshr i32 %57, 16
%100 = and i32 %99, 255
%101 = getelementptr inbounds i32, i32* %45, i64 18
store i32 %100, i32* %101
%102 = lshr i32 %57, 24
%103 = getelementptr inbounds i32, i32* %45, i64 19
store i32 %102, i32* %103
%104 = load i32, i32* @SEEK_SET
%105 = tail call i32 @fseek(i32* %10, i32 0, i32 %104)
%106 = trunc i64 %20 to i32
%107 = tail call i32 @fwrite(i32* %45, i32 1, i32 %106, i32* %10)
%108 = tail call i32 @fclose(i32* %10)
%109 = tail call i32 @free(i32* %45)
br label %110
110: ; preds = %47, %50, %33
%111 = phi i32 [ 1, %33 ], [ 0, %50 ], [ -1, %47 ]
br label %112
112: ; preds = %110, %22, %12, %5
%113 = phi i32 [ 1, %5 ], [ 1, %12 ], [ 1, %22 ], [ %111, %110 ]
ret i32 %113
}