#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 scalar_t__ uint32_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ filesize(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
int get_uint32le(unsigned char *);
unsigned char *malloc(scalar_t__);
int open(char *, int, ...);
scalar_t__ read(int, unsigned char *, scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
scalar_t__ write(int, unsigned char *, scalar_t__);
int main(int argc, char *argv[]) {
  char *infile, *outfile;
  int fdin, fdout;
  off_t len;
  uint32_t n;
  unsigned char *buf;
  uint32_t firmware_size;
  if (argc != 3) {
    usage();
  }
  infile = argv[1];
  outfile = argv[2];
  fdin = open(infile, O_RDONLY | 0);
  if (fdin < 0) {
    fprintf(stderr, "[ERR]  Could not open %s for reading\n", infile);
    return 1;
  }
  if ((len = filesize(fdin)) < 0)
    return 1;
  if ((buf = malloc(len)) == NULL) {
    fprintf(stderr,
            "[ERR]  Could not allocate buffer for input file (%d bytes)\n",
            (int)len);
    return 1;
  }
  n = read(fdin, buf, len);
  if (n != (uint32_t)len) {
    fprintf(stderr, "[ERR] Could not read firmware file\n");
    return 1;
  }
  close(fdin);
  if (get_uint32le(&buf[0x04]) == 0x0000f000)
    firmware_size = get_uint32le(&buf[0x10]);
  else
    firmware_size = get_uint32le(&buf[0x0c]);
  fdout = open(outfile, O_CREAT | O_TRUNC | O_WRONLY | 0, 0666);
  if (fdout < 0) {
    fprintf(stderr, "[ERR]  Could not open %s for writing\n", outfile);
    return 1;
  }
  n = write(fdout, buf + 0x400, firmware_size);
  if (n != (uint32_t)firmware_size) {
    fprintf(stderr, "[ERR] Could not write firmware block\n");
    return 1;
  }
  close(fdout);
  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   $0x3,-0x8(%rbp)
je     11c7 <main+0x27>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122a <main+0x8a>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x24(%rbp),%edi
call   10c0 <filesize@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    124c <main+0xac>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    128c <main+0xec>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x24(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12d1 <main+0x131>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%rdi
add    $0x4,%rdi
call   1090 <get_uint32le@plt>
cmp    $0xf000,%eax
jne    1309 <main+0x169>
mov    -0x40(%rbp),%rdi
add    $0x10,%rdi
call   1090 <get_uint32le@plt>
cltq
mov    %rax,-0x48(%rbp)
jmp    131c <main+0x17c>
mov    -0x40(%rbp),%rdi
add    $0xc,%rdi
call   1090 <get_uint32le@plt>
cltq
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2cd1(%rip),%rax
mov    (%rax),%esi
mov    0x2cc0(%rip),%rax
or     (%rax),%esi
mov    0x2c7f(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    137e <main+0x1de>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x28(%rbp),%edi
mov    -0x40(%rbp),%rsi
add    $0x400,%rsi
mov    -0x48(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x48(%rbp),%rax
je     13ca <main+0x22a>
mov    0x2c1a(%rip),%rax
mov    (%rax),%edi
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <main+0x242>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%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   %r12
push   %rbx
mov    %rsi,%rbp
cmp    $0x3,%edi
je     11b7 <main+0x17>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    0x8(%rbp),%rbx
mov    0x10(%rbp),%r14
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
js     12b4 <main+0x114>
mov    %eax,%ebp
mov    %eax,%edi
call   10c0 <filesize@plt>
mov    $0x1,%r15d
test   %rax,%rax
js     1336 <main+0x196>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12d6 <main+0x136>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
cmp    %rbx,%rax
jne    12f1 <main+0x151>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0x4(%r12),%rdi
call   1090 <get_uint32le@plt>
xor    %ecx,%ecx
cmp    $0xf000,%eax
sete   %cl
lea    (%r12,%rcx,4),%rdi
add    $0xc,%rdi
call   1090 <get_uint32le@plt>
mov    0x2daa(%rip),%rcx
mov    0x2d9b(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%ebp
mov    0x2d56(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
js     1303 <main+0x163>
mov    %eax,%ebx
movslq %ebp,%rbp
mov    %r12,%rsi
add    $0x400,%rsi
mov    %eax,%edi
mov    %rbp,%rdx
call   1040 <write@plt>
cmp    %rbp,%rax
jne    131f <main+0x17f>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    1336 <main+0x196>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%r15d
jmp    1336 <main+0x196>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1336 <main+0x196>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
jmp    132f <main+0x18f>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1336 <main+0x196>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"[ERR] Could not open %s for reading\0A\00"
@.str.1 = [60 x i8] c"[ERR] Could not allocate buffer for input file (%d bytes)\0A\00"
@.str.2 = [36 x i8] c"[ERR] Could not read firmware file\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [38 x i8] c"[ERR] Could not open %s for writing\0A\00"
@.str.4 = [38 x i8] c"[ERR] Could not write firmware block\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @usage()
br label %18
18: ; preds = %16, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = load i32, i32* @O_RDONLY
%27 = or i32 %26, 0
%28 = call i32 (i8*, i32, ...) @open(i8* %25, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %18
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %6
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 1, i32* %3
br label %109
35: ; preds = %18
%36 = load i32, i32* %8
%37 = call i64 @filesize(i32 %36)
store i64 %37, i64* %10
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 1, i32* %3
br label %109
40: ; preds = %35
%41 = load i64, i64* %10
%42 = call i8* @malloc(i64 %41)
store i8* %42, i8** %12
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i64, i64* %10
%47 = trunc i64 %46 to i32
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %47)
store i32 1, i32* %3
br label %109
49: ; preds = %40
%50 = load i32, i32* %8
%51 = load i8*, i8** %12
%52 = load i64, i64* %10
%53 = call i64 @read(i32 %50, i8* %51, i64 %52)
store i64 %53, i64* %11
%54 = load i64, i64* %11
%55 = load i64, i64* %10
%56 = icmp ne i64 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %109
60: ; preds = %49
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %12
%64 = getelementptr inbounds i8, i8* %63, i64 4
%65 = call i32 @get_uint32le(i8* %64)
%66 = icmp eq i32 %65, 61440
br i1 %66, label %67, label %72
67: ; preds = %60
%68 = load i8*, i8** %12
%69 = getelementptr inbounds i8, i8* %68, i64 16
%70 = call i32 @get_uint32le(i8* %69)
%71 = sext i32 %70 to i64
store i64 %71, i64* %13
br label %77
72: ; preds = %60
%73 = load i8*, i8** %12
%74 = getelementptr inbounds i8, i8* %73, i64 12
%75 = call i32 @get_uint32le(i8* %74)
%76 = sext i32 %75 to i64
store i64 %76, i64* %13
br label %77
77: ; preds = %72, %67
%78 = load i8*, i8** %7
%79 = load i32, i32* @O_CREAT
%80 = load i32, i32* @O_TRUNC
%81 = or i32 %79, %80
%82 = load i32, i32* @O_WRONLY
%83 = or i32 %81, %82
%84 = or i32 %83, 0
%85 = call i32 (i8*, i32, ...) @open(i8* %78, i32 %84, i32 438)
store i32 %85, i32* %9
%86 = load i32, i32* %9
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %77
%89 = load i32, i32* @stderr
%90 = load i8*, i8** %7
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %90)
store i32 1, i32* %3
br label %109
92: ; preds = %77
%93 = load i32, i32* %9
%94 = load i8*, i8** %12
%95 = getelementptr inbounds i8, i8* %94, i64 1024
%96 = load i64, i64* %13
%97 = call i64 @write(i32 %93, i8* %95, i64 %96)
store i64 %97, i64* %11
%98 = load i64, i64* %11
%99 = load i64, i64* %13
%100 = icmp ne i64 %98, %99
br i1 %100, label %101, label %104
101: ; preds = %92
%102 = load i32, i32* @stderr
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %102, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %109
104: ; preds = %92
%105 = load i32, i32* %9
%106 = call i32 @close(i32 %105)
%107 = load i8*, i8** %12
%108 = call i32 @free(i8* %107)
store i32 0, i32* %3
br label %109
109: ; preds = %104, %101, %88, %57, %44, %39, %31
%110 = load i32, i32* %3
ret i32 %110
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"[ERR] Could not open %s for reading\0A\00"
@.str.1 = [60 x i8] c"[ERR] Could not allocate buffer for input file (%d bytes)\0A\00"
@.str.2 = [36 x i8] c"[ERR] Could not read firmware file\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [38 x i8] c"[ERR] Could not open %s for writing\0A\00"
@.str.4 = [38 x i8] c"[ERR] Could not write firmware block\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %62
17: ; preds = %6
%18 = tail call i64 @filesize(i32 %12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %62, label %20
20: ; preds = %17
%21 = tail call i8* @malloc(i64 %18)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = trunc i64 %18 to i32
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %62
27: ; preds = %20
%28 = tail call i64 @read(i32 %12, i8* %21, i64 %18)
%29 = icmp eq i64 %28, %18
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %62
33: ; preds = %27
%34 = tail call i32 @close(i32 %12)
%35 = getelementptr inbounds i8, i8* %21, i64 4
%36 = tail call i32 @get_uint32le(i8* %35)
%37 = icmp eq i32 %36, 61440
%38 = select i1 %37, i64 16, i64 12
%39 = getelementptr inbounds i8, i8* %21, i64 %38
%40 = tail call i32 @get_uint32le(i8* %39)
%41 = sext i32 %40 to i64
%42 = load i32, i32* @O_CREAT
%43 = load i32, i32* @O_TRUNC
%44 = or i32 %43, %42
%45 = load i32, i32* @O_WRONLY
%46 = or i32 %44, %45
%47 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %46, i32 438)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %33
%50 = load i32, i32* @stderr
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %62
52: ; preds = %33
%53 = getelementptr inbounds i8, i8* %21, i64 1024
%54 = tail call i64 @write(i32 %47, i8* %53, i64 %41)
%55 = icmp eq i64 %54, %41
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %62
59: ; preds = %52
%60 = tail call i32 @close(i32 %47)
%61 = tail call i32 @free(i8* %21)
br label %62
62: ; preds = %17, %59, %56, %49, %30, %23, %14
%63 = phi i32 [ 1, %14 ], [ 1, %23 ], [ 1, %30 ], [ 1, %49 ], [ 1, %56 ], [ 0, %59 ], [ 1, %17 ]
ret i32 %63
}