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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc != 4) {
    fprintf(stderr, "Usage: %s INFILE OUTFILE KEYID\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  FILE *in = fopen(argv[1], "r");
  FILE *out = fopen(argv[2], "w");
  if (in == NULL || out == NULL) {
    fprintf(stderr, "Invalid input or output file path\n");
    exit(EXIT_FAILURE);
  }
  fseek(in, 0, SEEK_END);
  long in_len = ftell(in);
  rewind(in);
  unsigned char header[0x240] = {0};
  fread(header, 1, 0x240, in);
  char *endptr;
  long key_id = strtol(argv[3], &endptr, 0);
  if (*argv[3] == '\0' || *endptr != '\0') {
    fprintf(stderr, "Invalid key ID\n");
    exit(EXIT_FAILURE);
  }
  if (key_id != 0x2 && key_id != 0x82) {
    fprintf(stderr, "Warning: Key ID is neither 0x2 (NAND) nor 0x82 (USB)\n");
  }
  *((unsigned *)(header + 0x40)) = (unsigned)key_id;
  *((unsigned *)(header + 0x7c)) = (unsigned)in_len - 0x80;
  fwrite(header, 1, 0x240, out);
  unsigned char buffer[0x1000] = {0};
  size_t i;
  while ((i = fread(buffer, 1, 0x1000, in)) != 0)
    fwrite(buffer, 1, i, out);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1290,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11ef <main+0x4f>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe22(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe0c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1235 <main+0x95>
cmpq   $0x0,-0x20(%rbp)
jne    125a <main+0xba>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <rewind@plt>
lea    -0x270(%rbp),%rdi
xor    %esi,%esi
mov    $0x240,%edx
call   1050 <memset@plt>
lea    -0x270(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x240,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    -0x278(%rbp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,-0x280(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12f5 <main+0x155>
mov    -0x278(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     131a <main+0x17a>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpq   $0x2,-0x280(%rbp)
02
je     1350 <main+0x1b0>
cmpq   $0x82,-0x280(%rbp)
je     1350 <main+0x1b0>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x280(%rbp),%rax
mov    %eax,-0x230(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x80,%eax
mov    %eax,-0x1f4(%rbp)
lea    -0x270(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x240,%edx
call   10c0 <fwrite@plt>
lea    -0x1280(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x1280(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1288(%rbp)
cmp    $0x0,%rax
je     13e5 <main+0x245>
lea    -0x1280(%rbp),%rdi
mov    -0x1288(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
jmp    1399 <main+0x1f9>
mov    0x2bdc(%rip),%rax
mov    (%rax),%eax
add    $0x1290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1258,%rsp
mov    %rsi,%r15
cmp    $0x4,%edi
jne    135a <main+0x1ba>
mov    0x8(%r15),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbp
mov    0x10(%r15),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rbp,%rbp
je     1376 <main+0x1d6>
mov    %rax,%r14
test   %rax,%rax
je     1376 <main+0x1d6>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbp,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1040 <rewind@plt>
lea    0x10(%rsp),%rbx
mov    $0x240,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x240,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    0x18(%r15),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %rax,%r13
mov    0x18(%r15),%rax
cmpb   $0x0,(%rax)
je     1348 <main+0x1a8>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    1348 <main+0x1a8>
mov    %r13,%rax
and    $0xffffffffffffff7f,%rax
cmp    $0x2,%rax
je     1298 <main+0xf8>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r13d,0x50(%rsp)
add    $0xffffff80,%r12d
mov    %r12d,0x8c(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x240,%edx
mov    $0x1,%esi
mov    %r14,%rcx
call   10c0 <fwrite@plt>
lea    0x250(%rsp),%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     132d <main+0x18d>
lea    0x250(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    1300 <main+0x160>
mov    0x2c94(%rip),%rax
mov    (%rax),%eax
add    $0x1258,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
lea    0xcef(%rip),%rsi
jmp    1386 <main+0x1e6>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xc93(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    138d <main+0x1ed>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
lea    0xc9e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s INFILE OUTFILE KEYID\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Invalid input or output file path\0A\00"
@SEEK_END = global i32 0
@.str.4 = [16 x i8] c"Invalid key ID\0A\00"
@.str.5 = [54 x i8] c"Warning: Key ID is neither 0x2 (NAND) nor 0x82 (USB)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca [576 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [4096 x 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, 4
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %7
%33 = load i32*, i32** %6
%34 = icmp eq i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %24
%36 = load i32*, i32** %7
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %43
38: ; preds = %35, %24
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %35
%44 = load i32*, i32** %6
%45 = load i32, i32* @SEEK_END
%46 = call i32 @fseek(i32* %44, i32 0, i32 %45)
%47 = load i32*, i32** %6
%48 = call i64 @ftell(i32* %47)
store i64 %48, i64* %8
%49 = load i32*, i32** %6
%50 = call i32 @rewind(i32* %49)
%51 = bitcast [576 x i8]* %9 to i8*
%52 = getelementptr inbounds [576 x i8], [576 x i8]* %9, i64 0, i64 0
%53 = load i32*, i32** %6
%54 = call i64 @fread(i8* %52, i32 1, i32 576, i32* %53)
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 3
%57 = load i8*, i8** %56
%58 = call i64 @strtol(i8* %57, i8** %10, i32 0)
store i64 %58, i64* %11
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 0
br i1 %64, label %70, label %65
65: ; preds = %43
%66 = load i8*, i8** %10
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %65, %43
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %65
%76 = load i64, i64* %11
%77 = icmp ne i64 %76, 2
br i1 %77, label %78, label %84
78: ; preds = %75
%79 = load i64, i64* %11
%80 = icmp ne i64 %79, 130
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %84
84: ; preds = %81, %78, %75
%85 = load i64, i64* %11
%86 = trunc i64 %85 to i32
%87 = getelementptr inbounds [576 x i8], [576 x i8]* %9, i64 0, i64 0
%88 = getelementptr inbounds i8, i8* %87, i64 64
%89 = bitcast i8* %88 to i32*
store i32 %86, i32* %89
%90 = load i64, i64* %8
%91 = trunc i64 %90 to i32
%92 = sub i32 %91, 128
%93 = getelementptr inbounds [576 x i8], [576 x i8]* %9, i64 0, i64 0
%94 = getelementptr inbounds i8, i8* %93, i64 124
%95 = bitcast i8* %94 to i32*
store i32 %92, i32* %95
%96 = getelementptr inbounds [576 x i8], [576 x i8]* %9, i64 0, i64 0
%97 = load i32*, i32** %7
%98 = call i32 @fwrite(i8* %96, i32 1, i64 576, i32* %97)
%99 = bitcast [4096 x i8]* %12 to i8*
br label %100
100: ; preds = %105, %84
%101 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%102 = load i32*, i32** %6
%103 = call i64 @fread(i8* %101, i32 1, i32 4096, i32* %102)
store i64 %103, i64* %13
%104 = icmp ne i64 %103, 0
br i1 %104, label %105, label %110
105: ; preds = %100
%106 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%107 = load i64, i64* %13
%108 = load i32*, i32** %7
%109 = call i32 @fwrite(i8* %106, i32 1, i64 %107, i32* %108)
br label %100
110: ; preds = %100
%111 = load i32, i32* @EXIT_SUCCESS
ret i32 %111
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s INFILE OUTFILE KEYID\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"Invalid input or output file path\0A\00"
@SEEK_END = global i32 0
@.str.4 = [16 x i8] c"Invalid key ID\0A\00"
@.str.5 = [54 x i8] c"Warning: Key ID is neither 0x2 (NAND) nor 0x82 (USB)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [576 x i8]
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = icmp eq i32 %0, 4
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %16, null
%21 = icmp eq i32* %19, null
%22 = or i1 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %13
%29 = load i32, i32* @SEEK_END
%30 = tail call i32 @fseek(i32* %16, i32 0, i32 %29)
%31 = tail call i64 @ftell(i32* %16)
%32 = tail call i32 @rewind(i32* %16)
%33 = getelementptr inbounds [576 x i8], [576 x i8]* %3, i64 0, i64 0
%34 = call i64 @fread(i8* %33, i32 1, i32 576, i32* %16)
%35 = bitcast i8** %4 to i8*
%36 = getelementptr inbounds i8*, i8** %1, i64 3
%37 = load i8*, i8** %36
%38 = call i64 @strtol(i8* %37, i8** %4, i32 0)
%39 = load i8*, i8** %36
%40 = load i8, i8* %39
%41 = icmp eq i8 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %28
%43 = load i8*, i8** %4
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 0
br i1 %45, label %51, label %46
46: ; preds = %42, %28
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %42
%52 = and i64 %38, -129
%53 = icmp eq i64 %52, 2
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %54, %51
%58 = trunc i64 %38 to i32
%59 = getelementptr inbounds [576 x i8], [576 x i8]* %3, i64 0, i64 64
%60 = bitcast i8* %59 to i32*
store i32 %58, i32* %60
%61 = trunc i64 %31 to i32
%62 = add i32 %61, -128
%63 = getelementptr inbounds [576 x i8], [576 x i8]* %3, i64 0, i64 124
%64 = bitcast i8* %63 to i32*
store i32 %62, i32* %64
%65 = call i32 @fwrite(i8* %33, i32 1, i64 576, i32* %19)
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%67 = call i64 @fread(i8* %66, i32 1, i32 4096, i32* %16)
%68 = icmp eq i64 %67, 0
br i1 %68, label %74, label %69
69: ; preds = %57, %69
%70 = phi i64 [ %72, %69 ], [ %67, %57 ]
%71 = call i32 @fwrite(i8* %66, i32 1, i64 %70, i32* %19)
%72 = call i64 @fread(i8* %66, i32 1, i32 4096, i32* %16)
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %69
74: ; preds = %69, %57
%75 = load i32, i32* @EXIT_SUCCESS
ret i32 %75
}