#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*/ 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*/ strtol(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");
    return 1;
  }
  char *remaining = NULL;
  uint32_t version = (uint32_t)strtol(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");
  }
  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);
  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    122a <main+0x3a>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    158b <main+0x39b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xea4(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    127a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xea7(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158b <main+0x39b>
mov    -0x18(%rbp),%rdi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x100,%rax
cmp    -0x20(%rbp),%rax
jae    12c9 <main+0xd9>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    158b <main+0x39b>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   10c0 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     131b <main+0x12b>
mov    -0x28(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    158b <main+0x39b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <strlen@plt>
cmp    $0x4,%eax
jle    1347 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x100,%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1372 <main+0x182>
lea    0xe14(%rip),%rdi
call   1100 <perror@plt>
mov    -0x38(%rbp),%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c48(%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   1040 <fread@plt>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    $0x400,%rsi
mov    -0x20(%rbp),%rdx
call   1060 <crc_update@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
mov    $0x20,%esi
call   10a0 <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   1090 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0x4,%edx
call   1030 <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    0x2a84(%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   1110 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@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     121c <main+0x1c>
lea    0xe90(%rip),%rdi
jmp    1265 <main+0x65>
mov    %rsi,%r12
mov    0x8(%rsi),%rdi
lea    0xdd6(%rip),%rsi
call   1100 <fopen@plt>
test   %rax,%rax
je     1449 <main+0x249>
mov    %rax,%r15
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %r15,%rdi
call   10c0 <ftell@plt>
cmp    $0xffffffffffffff00,%rax
jb     1274 <main+0x74>
lea    0xe33(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    1438 <main+0x238>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
mov    0x18(%r12),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   10d0 <strtol@plt>
mov    0x8(%rsp),%rsi
cmpb   $0x0,(%rsi)
je     12b3 <main+0xb3>
lea    0xd7f(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%ebp
jmp    1430 <main+0x230>
mov    %eax,0x4(%rsp)
mov    %rbx,%r14
add    $0x100,%rbx
mov    0x10(%r12),%rdi
call   1080 <strlen@plt>
cmp    $0x5,%eax
jl     12e3 <main+0xe3>
mov    0x10(%r12),%rsi
lea    0xd7d(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   10e0 <malloc@plt>
mov    %rbx,0x10(%rsp)
mov    %rax,%rbx
test   %rax,%rax
je     146f <main+0x26f>
mov    %rbx,%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
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   1050 <fread@plt>
mov    $0xffffffff,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   1070 <crc_update@plt>
mov    %eax,%edi
mov    $0x20,%esi
call   10b0 <crc_reflect@plt>
mov    %eax,%r13d
mov    %eax,(%rsp)
not    %r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   10a0 <memset@plt>
mov    0x10(%r12),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <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    0x2bc9(%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   1120 <fwrite@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r12),%rsi
lea    0xbaf(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xbf5(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%ebp
jmp    1438 <main+0x238>
lea    0xc1b(%rip),%rdi
call   1110 <perror@plt>
jmp    12fc <main+0xfc>

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 %192
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 %192
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 %41
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %192
41: ; preds = %29
store i8* null, i8** %8
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 3
%44 = load i8*, i8** %43
%45 = call i32 @strtol(i8* %44, i8** %8, i32 0)
store i32 %45, i32* %9
%46 = load i8*, i8** %8
%47 = load i8, i8* %46
%48 = icmp ne i8 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i8*, i8** %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i8* %50)
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
store i32 1, i32* %3
br label %192
54: ; preds = %41
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @strlen(i8* %57)
%59 = icmp sgt i32 %58, 4
br i1 %59, label %60, label %65
60: ; preds = %54
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %60, %54
%66 = load i64, i64* %7
%67 = add i64 %66, 256
%68 = call i32* @malloc(i64 %67)
store i32* %68, i32** %10
%69 = load i32*, i32** %10
%70 = icmp ne i32* %69, null
br i1 %70, label %73, label %71
71: ; preds = %65
%72 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %71, %65
%74 = load i32*, i32** %10
%75 = call i32 @memset(i32* %74, i32 255, i32 256)
%76 = load i32*, i32** %6
%77 = load i32, i32* @SEEK_SET
%78 = call i32 @fseek(i32* %76, i32 0, i32 %77)
%79 = load i32*, i32** %10
%80 = getelementptr inbounds i32, i32* %79, i64 256
%81 = load i64, i64* %7
%82 = load i32*, i32** %6
%83 = call i32 @fread(i32* %80, i32 1, i64 %81, i32* %82)
store i32 -1, i32* %11
%84 = load i32, i32* %11
%85 = load i32*, i32** %10
%86 = getelementptr inbounds i32, i32* %85, i64 256
%87 = load i64, i64* %7
%88 = call i32 @crc_update(i32 %84, i32* %86, i64 %87)
store i32 %88, i32* %11
%89 = load i32, i32* %11
%90 = call i32 @crc_reflect(i32 %89, i32 32)
store i32 %90, i32* %11
%91 = load i32, i32* %11
%92 = xor i32 %91, -1
store i32 %92, i32* %12
%93 = load i32*, i32** %10
%94 = call i32 @memset(i32* %93, i32 0, i32 4)
%95 = load i32*, i32** %10
%96 = bitcast i32* %95 to i8*
%97 = load i8**, i8*** %5
%98 = getelementptr inbounds i8*, i8** %97, i64 2
%99 = load i8*, i8** %98
%100 = call i32 @strncpy(i8* %96, i8* %99, i32 4)
%101 = load i32, i32* %9
%102 = and i32 %101, 255
%103 = load i32*, i32** %10
%104 = getelementptr inbounds i32, i32* %103, i64 4
store i32 %102, i32* %104
%105 = load i32, i32* %9
%106 = ashr i32 %105, 8
%107 = and i32 %106, 255
%108 = load i32*, i32** %10
%109 = getelementptr inbounds i32, i32* %108, i64 5
store i32 %107, i32* %109
%110 = load i32, i32* %9
%111 = ashr i32 %110, 16
%112 = and i32 %111, 255
%113 = load i32*, i32** %10
%114 = getelementptr inbounds i32, i32* %113, i64 6
store i32 %112, i32* %114
%115 = load i32, i32* %9
%116 = ashr i32 %115, 24
%117 = and i32 %116, 255
%118 = load i32*, i32** %10
%119 = getelementptr inbounds i32, i32* %118, i64 7
store i32 %117, i32* %119
%120 = load i64, i64* %7
%121 = and i64 %120, 255
%122 = trunc i64 %121 to i32
%123 = load i32*, i32** %10
%124 = getelementptr inbounds i32, i32* %123, i64 8
store i32 %122, i32* %124
%125 = load i64, i64* %7
%126 = lshr i64 %125, 8
%127 = and i64 %126, 255
%128 = trunc i64 %127 to i32
%129 = load i32*, i32** %10
%130 = getelementptr inbounds i32, i32* %129, i64 9
store i32 %128, i32* %130
%131 = load i64, i64* %7
%132 = lshr i64 %131, 16
%133 = and i64 %132, 255
%134 = trunc i64 %133 to i32
%135 = load i32*, i32** %10
%136 = getelementptr inbounds i32, i32* %135, i64 10
store i32 %134, i32* %136
%137 = load i64, i64* %7
%138 = lshr i64 %137, 24
%139 = and i64 %138, 255
%140 = trunc i64 %139 to i32
%141 = load i32*, i32** %10
%142 = getelementptr inbounds i32, i32* %141, i64 11
store i32 %140, i32* %142
%143 = load i32, i32* %12
%144 = and i32 %143, 255
%145 = load i32*, i32** %10
%146 = getelementptr inbounds i32, i32* %145, i64 12
store i32 %144, i32* %146
%147 = load i32, i32* %12
%148 = ashr i32 %147, 8
%149 = and i32 %148, 255
%150 = load i32*, i32** %10
%151 = getelementptr inbounds i32, i32* %150, i64 13
store i32 %149, i32* %151
%152 = load i32, i32* %12
%153 = ashr i32 %152, 16
%154 = and i32 %153, 255
%155 = load i32*, i32** %10
%156 = getelementptr inbounds i32, i32* %155, i64 14
store i32 %154, i32* %156
%157 = load i32, i32* %12
%158 = ashr i32 %157, 24
%159 = and i32 %158, 255
%160 = load i32*, i32** %10
%161 = getelementptr inbounds i32, i32* %160, i64 15
store i32 %159, i32* %161
%162 = load i32, i32* %11
%163 = and i32 %162, 255
%164 = load i32*, i32** %10
%165 = getelementptr inbounds i32, i32* %164, i64 16
store i32 %163, i32* %165
%166 = load i32, i32* %11
%167 = ashr i32 %166, 8
%168 = and i32 %167, 255
%169 = load i32*, i32** %10
%170 = getelementptr inbounds i32, i32* %169, i64 17
store i32 %168, i32* %170
%171 = load i32, i32* %11
%172 = ashr i32 %171, 16
%173 = and i32 %172, 255
%174 = load i32*, i32** %10
%175 = getelementptr inbounds i32, i32* %174, i64 18
store i32 %173, i32* %175
%176 = load i32, i32* %11
%177 = ashr i32 %176, 24
%178 = and i32 %177, 255
%179 = load i32*, i32** %10
%180 = getelementptr inbounds i32, i32* %179, i64 19
store i32 %178, i32* %180
%181 = load i32*, i32** %6
%182 = load i32, i32* @SEEK_SET
%183 = call i32 @fseek(i32* %181, i32 0, i32 %182)
%184 = load i32*, i32** %10
%185 = load i64, i64* %7
%186 = add i64 256, %185
%187 = trunc i64 %186 to i32
%188 = load i32*, i32** %6
%189 = call i32 @fwrite(i32* %184, i32 1, i32 %187, i32* %188)
%190 = load i32*, i32** %6
%191 = call i32 @fclose(i32* %190)
store i32 0, i32* %3
br label %192
192: ; preds = %73, %49, %39, %23, %15
%193 = load i32, i32* %3
ret i32 %193
}

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 %108
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 %108
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 %24
22: ; preds = %16
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %108
24: ; preds = %16
%25 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%26 = getelementptr inbounds i8*, i8** %1, i64 3
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i8** %3, i32 0)
%29 = load i8*, i8** %3
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i8* %29)
br label %105
34: ; preds = %24
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 @strlen(i8* %36)
%38 = icmp sgt i32 %37, 4
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i8*, i8** %35
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %34
%43 = tail call i32* @malloc(i64 %20)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = tail call i32 @memset(i32* %43, i32 255, i32 256)
%49 = load i32, i32* @SEEK_SET
%50 = tail call i32 @fseek(i32* %10, i32 0, i32 %49)
%51 = getelementptr inbounds i32, i32* %43, i64 256
%52 = tail call i32 @fread(i32* %51, i32 1, i64 %19, i32* %10)
%53 = tail call i32 @crc_update(i32 -1, i32* %51, i64 %19)
%54 = tail call i32 @crc_reflect(i32 %53, i32 32)
%55 = xor i32 %54, -1
%56 = tail call i32 @memset(i32* %43, i32 0, i32 4)
%57 = bitcast i32* %43 to i8*
%58 = load i8*, i8** %35
%59 = tail call i32 @strncpy(i8* %57, i8* %58, i32 4)
%60 = and i32 %28, 255
%61 = getelementptr inbounds i32, i32* %43, i64 4
store i32 %60, i32* %61
%62 = lshr i32 %28, 8
%63 = and i32 %62, 255
%64 = getelementptr inbounds i32, i32* %43, i64 5
store i32 %63, i32* %64
%65 = lshr i32 %28, 16
%66 = and i32 %65, 255
%67 = getelementptr inbounds i32, i32* %43, i64 6
store i32 %66, i32* %67
%68 = lshr i32 %28, 24
%69 = getelementptr inbounds i32, i32* %43, i64 7
store i32 %68, i32* %69
%70 = trunc i64 %19 to i32
%71 = and i32 %70, 255
%72 = getelementptr inbounds i32, i32* %43, i64 8
store i32 %71, i32* %72
%73 = lshr i32 %70, 8
%74 = and i32 %73, 255
%75 = getelementptr inbounds i32, i32* %43, i64 9
store i32 %74, i32* %75
%76 = lshr i32 %70, 16
%77 = and i32 %76, 255
%78 = getelementptr inbounds i32, i32* %43, i64 10
store i32 %77, i32* %78
%79 = lshr i32 %70, 24
%80 = getelementptr inbounds i32, i32* %43, i64 11
store i32 %79, i32* %80
%81 = and i32 %55, 255
%82 = getelementptr inbounds i32, i32* %43, i64 12
store i32 %81, i32* %82
%83 = lshr i32 %55, 8
%84 = and i32 %83, 255
%85 = getelementptr inbounds i32, i32* %43, i64 13
store i32 %84, i32* %85
%86 = lshr i32 %55, 16
%87 = and i32 %86, 255
%88 = getelementptr inbounds i32, i32* %43, i64 14
store i32 %87, i32* %88
%89 = lshr i32 %55, 24
%90 = getelementptr inbounds i32, i32* %43, i64 15
store i32 %89, i32* %90
%91 = and i32 %54, 255
%92 = getelementptr inbounds i32, i32* %43, i64 16
store i32 %91, i32* %92
%93 = lshr i32 %54, 8
%94 = and i32 %93, 255
%95 = getelementptr inbounds i32, i32* %43, i64 17
store i32 %94, i32* %95
%96 = lshr i32 %54, 16
%97 = and i32 %96, 255
%98 = getelementptr inbounds i32, i32* %43, i64 18
store i32 %97, i32* %98
%99 = lshr i32 %54, 24
%100 = getelementptr inbounds i32, i32* %43, i64 19
store i32 %99, i32* %100
%101 = load i32, i32* @SEEK_SET
%102 = tail call i32 @fseek(i32* %10, i32 0, i32 %101)
%103 = trunc i64 %20 to i32
%104 = tail call i32 @fwrite(i32* %43, i32 1, i32 %103, i32* %10)
br label %105
105: ; preds = %47, %32
%106 = phi i32 [ 1, %32 ], [ 0, %47 ]
%107 = tail call i32 @fclose(i32* %10)
br label %108
108: ; preds = %105, %22, %12, %5
%109 = phi i32 [ 1, %5 ], [ 1, %12 ], [ 1, %22 ], [ %106, %105 ]
ret i32 %109
}