#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 main(int argc, char **argv) {
  FILE *f;
  size_t flen;
  if (argc < 4) {
    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 (string) - 32-bit magic generated "
           "internally\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;
  }
  uint32_t version = 0xffffffff;
  version = crc_update(version, (uint8_t *)argv[3], strlen(argv[3]));
  version = crc_reflect(version, 32);
  version ^= 0xffffffff;
  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    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    122a <main+0x3a>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b1 <main+0x3c1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xec0(%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    0xea2(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    0xec3(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b1 <main+0x3c1>
mov    -0x18(%rbp),%rdi
mov    0x2d63(%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    12d2 <main+0xe2>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    15b1 <main+0x3c1>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
movslq %eax,%rdx
call   1070 <crc_update@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x20,%esi
call   10b0 <crc_reflect@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <strlen@plt>
cmp    $0x4,%eax
jle    134f <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x20(%rbp),%rdi
add    $0x100,%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    138f <main+0x19f>
lea    0xdf0(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    15b1 <main+0x3c1>
mov    -0x30(%rbp),%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c2b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x30(%rbp),%rdi
add    $0x400,%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    $0x400,%rsi
mov    -0x20(%rbp),%rdx
call   1070 <crc_update@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    $0x20,%esi
call   10b0 <crc_reflect@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   10a0 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x24(%rbp),%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x1c(%rax)
mov    -0x20(%rbp),%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    -0x20(%rbp),%rax
shr    $0x8,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x24(%rax)
mov    -0x20(%rbp),%rax
shr    $0x10,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x28(%rax)
mov    -0x20(%rbp),%rax
shr    $0x18,%rax
and    $0xff,%rax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x2c(%rax)
mov    -0x38(%rbp),%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x30(%rax)
mov    -0x38(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x34(%rax)
mov    -0x38(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x38(%rax)
mov    -0x38(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x3c(%rax)
mov    -0x34(%rbp),%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x40(%rax)
mov    -0x34(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x44(%rax)
mov    -0x34(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x48(%rax)
mov    -0x34(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4c(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2a67(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x30(%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   1060 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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    $0x18,%rsp
cmp    $0x3,%edi
jg     122a <main+0x2a>
lea    0xe58(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r13d
jmp    1449 <main+0x249>
mov    %rsi,%r12
mov    0x8(%rsi),%rdi
lea    0xdc8(%rip),%rsi
call   1100 <fopen@plt>
test   %rax,%rax
je     145b <main+0x25b>
mov    %rax,%r15
mov    0x2d98(%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     128b <main+0x8b>
lea    0xded(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    $0x1,%r13d
jmp    1449 <main+0x249>
mov    %rax,0x10(%rsp)
lea    0x100(%rax),%r13
mov    0x18(%r12),%rbx
mov    %rbx,%rdi
call   1090 <strlen@plt>
movslq %eax,%rdx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1080 <crc_update@plt>
mov    %eax,%edi
mov    $0x20,%esi
call   10c0 <crc_reflect@plt>
mov    %eax,%ebp
mov    0x10(%r12),%rdi
call   1090 <strlen@plt>
cmp    $0x5,%eax
jl     12e4 <main+0xe4>
mov    0x10(%r12),%rsi
lea    0xd44(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r13,%rdi
call   10e0 <malloc@plt>
test   %rax,%rax
je     1482 <main+0x282>
mov    %rax,%rbx
not    %ebp
mov    %ebp,0x4(%rsp)
mov    %rax,%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   10b0 <memset@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edx
mov    %r13,0x8(%rsp)
xor    %r13d,%r13d
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbx,%rbp
add    $0x400,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    0x10(%rsp),%r14
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <fread@plt>
mov    $0xffffffff,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1080 <crc_update@plt>
mov    %eax,%edi
mov    $0x20,%esi
call   10c0 <crc_reflect@plt>
mov    %eax,%ebp
mov    %eax,(%rsp)
not    %ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   10b0 <memset@plt>
mov    0x10(%r12),%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    %ebp,%ecx
movzbl %cl,%eax
mov    %eax,0x30(%rbx)
movzbl %ch,%eax
mov    %eax,0x34(%rbx)
mov    %ebp,%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    0x2bc0(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%rdx
mov    %r15,%rcx
call   1120 <fwrite@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r12),%rsi
lea    0xb9d(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0xbe3(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%r13d
jmp    1449 <main+0x249>
lea    0xbd0(%rip),%rdi
call   1110 <perror@plt>
mov    %r15,%rdi
call   1070 <fclose@plt>
mov    $0xffffffff,%r13d
jmp    1449 <main+0x249>

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

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