#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*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ its_birthday(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_cake();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  FILE *firmware;
  unsigned short data;
  unsigned short checksum;
  unsigned short flashmever;
  int version = 0;
  firmware = fopen("/dev/firmware", "r");
  if (!firmware) {
    perror("/dev/firmware");
    exit(1);
  }
  if (its_birthday(firmware))
    print_cake();
  fseek(firmware, 0x17c, SEEK_SET);
  fread(&data, sizeof(short), 1, firmware);
  fseek(firmware, 0x6, SEEK_SET);
  fread(&checksum, sizeof(short), 1, firmware);
  fseek(firmware, 0x3f7fc, SEEK_SET);
  fread(&flashmever, sizeof(short), 1, firmware);
  fclose(firmware);
  switch (checksum) {
  case 0x2C7A:
    version = 1;
    break;
  case 0xE0CE:
    version = 2;
    break;
  case 0xBFBA:
    version = 3;
    break;
  case 0xDFC7:
    version = 4;
    break;
  case 0x73b3:
    version = 5;
    break;
  case 0xF96D:
    printf("iQue firmware detected.\n");
    version = -1;
    break;
  case 0xE843:
    printf("DS Lite firmware detected.  It is safe to run online games.\n");
    version = -1;
    break;
  case 0x0f1f:
    printf("European DS Lite firmware detected.\nIt is safe to run online "
           "games.\n");
    version = -1;
    break;
  }
  if (version > 0) {
    printf("Nintendo firmware v%d detected.  It is safe to run online games.\n",
           version);
  }
  if (version >= 4) {
    printf("Your PassMe2 seems to be working!\n");
  }
  if (version == 0) {
    if (data == 1) {
      printf("Old FlashMe detected.  Upgrade before running online games.\n");
    } else if (data == 2) {
      printf("FlashMe v%d detected.  It is safe to run online games.\n",
             flashmever + 3);
    } else {
      printf("Unknown firmware.\n");
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe53(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11de <main+0x4e>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <its_birthday@plt>
cmp    $0x0,%rax
je     11f8 <main+0x68>
mov    $0x0,%al
call   10b0 <print_cake@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    (%rax),%edx
mov    $0x17c,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x12(%rbp),%rdi
mov    $0x2,%esi
mov    %esi,-0x28(%rbp)
mov    $0x1,%edx
mov    %edx,-0x24(%rbp)
call   1030 <fread@plt>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    (%rax),%edx
mov    $0x6,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
call   1030 <fread@plt>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    (%rax),%edx
mov    $0x3f7fc,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    -0x16(%rbp),%rdi
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movzwl -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xf1f,%eax
je     1393 <main+0x203>
jmp    129e <main+0x10e>
mov    -0x20(%rbp),%eax
sub    $0x2c7a,%eax
je     1323 <main+0x193>
jmp    12b1 <main+0x121>
mov    -0x20(%rbp),%eax
sub    $0x73b3,%eax
je     1353 <main+0x1c3>
jmp    12c4 <main+0x134>
mov    -0x20(%rbp),%eax
sub    $0xbfba,%eax
je     133b <main+0x1ab>
jmp    12d7 <main+0x147>
mov    -0x20(%rbp),%eax
sub    $0xdfc7,%eax
je     1347 <main+0x1b7>
jmp    12ea <main+0x15a>
mov    -0x20(%rbp),%eax
sub    $0xe0ce,%eax
je     132f <main+0x19f>
jmp    12fd <main+0x16d>
mov    -0x20(%rbp),%eax
sub    $0xe843,%eax
je     1379 <main+0x1e9>
jmp    1310 <main+0x180>
mov    -0x20(%rbp),%eax
sub    $0xf96d,%eax
je     135f <main+0x1cf>
jmp    13a8 <main+0x218>
movl   $0x1,-0x1c(%rbp)
jmp    13a8 <main+0x218>
movl   $0x2,-0x1c(%rbp)
jmp    13a8 <main+0x218>
movl   $0x3,-0x1c(%rbp)
jmp    13a8 <main+0x218>
movl   $0x4,-0x1c(%rbp)
jmp    13a8 <main+0x218>
movl   $0x5,-0x1c(%rbp)
jmp    13a8 <main+0x218>
lea    0xcaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    13a8 <main+0x218>
lea    0xca9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    13a8 <main+0x218>
lea    0xccc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    13c3 <main+0x233>
mov    -0x1c(%rbp),%esi
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x4,-0x1c(%rbp)
jl     13db <main+0x24b>
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1444 <main+0x2b4>
movzwl -0x12(%rbp),%eax
cmp    $0x1,%eax
jne    1405 <main+0x275>
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    143f <main+0x2af>
movzwl -0x12(%rbp),%eax
cmp    $0x2,%eax
jne    142c <main+0x29c>
movzwl -0x16(%rbp),%esi
add    $0x3,%esi
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    143a <main+0x2aa>
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    143f <main+0x2af>
jmp    1444 <main+0x2b4>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe51(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
jne    11d6 <main+0x36>
lea    0xe39(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <its_birthday@plt>
test   %rax,%rax
je     11ed <main+0x4d>
xor    %eax,%eax
call   10c0 <print_cake@plt>
mov    0x2dec(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x17c,%esi
call   1080 <fseek@plt>
lea    0xe(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1080 <fseek@plt>
lea    0xc(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x3f7fc,%esi
call   1080 <fseek@plt>
lea    0xa(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movswl 0xc(%rsp),%eax
movswl %ax,%ecx
cmp    $0xfffff96c,%eax
jg     12a9 <main+0x109>
movzwl %ax,%eax
cmp    $0xffffe0cd,%ecx
jg     12ce <main+0x12e>
cmp    $0xbfba,%eax
je     1312 <main+0x172>
mov    $0x4,%esi
cmp    $0xdfc7,%eax
je     12fb <main+0x15b>
jmp    1341 <main+0x1a1>
movzwl %ax,%eax
cmp    $0x2c79,%ecx
jg     12e8 <main+0x148>
cmp    $0xf96d,%eax
je     131a <main+0x17a>
cmp    $0xf1f,%eax
jne    1341 <main+0x1a1>
lea    0xdc1(%rip),%rdi
jmp    1373 <main+0x1d3>
cmp    $0xe0ce,%eax
je     1323 <main+0x183>
cmp    $0xe843,%eax
jne    1341 <main+0x1a1>
lea    0xe5b(%rip),%rdi
jmp    1373 <main+0x1d3>
cmp    $0x2c7a,%eax
je     1329 <main+0x189>
cmp    $0x73b3,%eax
jne    1341 <main+0x1a1>
mov    $0x5,%esi
lea    0xd0e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe0c(%rip),%rdi
jmp    1373 <main+0x1d3>
mov    $0x3,%r14d
jmp    1329 <main+0x189>
lea    0xe59(%rip),%rdi
jmp    1373 <main+0x1d3>
mov    $0x2,%r14d
lea    0xce0(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
movzwl 0xe(%rsp),%eax
cmp    $0x2,%ax
je     135b <main+0x1bb>
cmp    $0x1,%ax
jne    136c <main+0x1cc>
lea    0xd87(%rip),%rdi
jmp    1373 <main+0x1d3>
movzwl 0xa(%rsp),%esi
add    $0x3,%esi
lea    0xce8(%rip),%rdi
jmp    1333 <main+0x193>
lea    0xd5b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/firmware\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.2 = [25 x i8] c"iQue firmware detected.\0A\00"
@.str.3 = [61 x i8] c"DS Lite firmware detected. It is safe to run online games.\0A\00"
@.str.4 = [69 x i8] c"European DS Lite firmware detected.\0AIt is safe to run online games.\0A\00"
@.str.5 = [66 x i8] c"Nintendo firmware v%d detected. It is safe to run online games.\0A\00"
@.str.6 = [35 x i8] c"Your PassMe2 seems to be working!\0A\00"
@.str.7 = [61 x i8] c"Old FlashMe detected. Upgrade before running online games.\0A\00"
@.str.8 = [56 x i8] c"FlashMe v%d detected. It is safe to run online games.\0A\00"
@.str.9 = [19 x i8] c"Unknown firmware.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
%7 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32*, i32** %2
%15 = call i64 @its_birthday(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 (...) @print_cake()
br label %19
19: ; preds = %17, %13
%20 = load i32*, i32** %2
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 380, i32 %21)
%23 = load i32*, i32** %2
%24 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %23)
%25 = load i32*, i32** %2
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 6, i32 %26)
%28 = load i32*, i32** %2
%29 = call i32 @fread(i16* %4, i32 2, i32 1, i32* %28)
%30 = load i32*, i32** %2
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %30, i32 260092, i32 %31)
%33 = load i32*, i32** %2
%34 = call i32 @fread(i16* %5, i32 2, i32 1, i32* %33)
%35 = load i32*, i32** %2
%36 = call i32 @fclose(i32* %35)
%37 = load i16, i16* %4
%38 = zext i16 %37 to i32
switch i32 %38, label %50 [
i32 11386, label %39
i32 57550, label %40
i32 49082, label %41
i32 57287, label %42
i32 29619, label %43
i32 63853, label %44
i32 59459, label %46
i32 3871, label %48
]
39: ; preds = %19
store i32 1, i32* %6
br label %50
40: ; preds = %19
store i32 2, i32* %6
br label %50
41: ; preds = %19
store i32 3, i32* %6
br label %50
42: ; preds = %19
store i32 4, i32* %6
br label %50
43: ; preds = %19
store i32 5, i32* %6
br label %50
44: ; preds = %19
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %50
46: ; preds = %19
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %50
48: ; preds = %19
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
br label %50
50: ; preds = %19, %48, %46, %44, %43, %42, %41, %40, %39
%51 = load i32, i32* %6
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %6
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %53, %50
%57 = load i32, i32* %6
%58 = icmp sge i32 %57, 4
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %83
64: ; preds = %61
%65 = load i16, i16* %3
%66 = zext i16 %65 to i32
%67 = icmp eq i32 %66, 1
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.7, i64 0, i64 0))
br label %82
70: ; preds = %64
%71 = load i16, i16* %3
%72 = zext i16 %71 to i32
%73 = icmp eq i32 %72, 2
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i16, i16* %5
%76 = zext i16 %75 to i32
%77 = add nsw i32 %76, 3
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.8, i64 0, i64 0), i32 %77)
br label %81
79: ; preds = %70
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
br label %81
81: ; preds = %79, %74
br label %82
82: ; preds = %81, %68
br label %83
83: ; preds = %82, %61
%84 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/firmware\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.5 = [66 x i8] c"Nintendo firmware v%d detected. It is safe to run online games.\0A\00"
@.str.8 = [56 x i8] c"FlashMe v%d detected. It is safe to run online games.\0A\00"
@str = [68 x i8] c"European DS Lite firmware detected.\0AIt is safe to run online games.\00"
@str.10 = [18 x i8] c"Unknown firmware.\00"
@str.11 = [60 x i8] c"Old FlashMe detected. Upgrade before running online games.\00"
@str.12 = [34 x i8] c"Your PassMe2 seems to be working!\00"
@str.13 = [60 x i8] c"DS Lite firmware detected. It is safe to run online games.\00"
@str.14 = [24 x i8] c"iQue firmware detected.\00"
define i32 @main() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i16
%4 = bitcast i16* %1 to i8*
%5 = bitcast i16* %2 to i8*
%6 = bitcast i16* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i64 @its_birthday(i32* %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 (...) @print_cake()
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @fseek(i32* %7, i32 380, i32 %18)
%20 = call i32 @fread(i16* %1, i32 2, i32 1, i32* %7)
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @fseek(i32* %7, i32 6, i32 %21)
%23 = call i32 @fread(i16* %2, i32 2, i32 1, i32* %7)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %7, i32 260092, i32 %24)
%26 = call i32 @fread(i16* %3, i32 2, i32 1, i32* %7)
%27 = tail call i32 @fclose(i32* %7)
%28 = load i16, i16* %2
switch i16 %28, label %45 [
i16 11386, label %38
i16 -7986, label %29
i16 -16454, label %30
i16 -8249, label %41
i16 29619, label %31
i16 -1683, label %32
i16 -6077, label %34
i16 3871, label %36
]
29: ; preds = %17
br label %38
30: ; preds = %17
br label %38
31: ; preds = %17
br label %41
32: ; preds = %17
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.14, i64 0, i64 0))
br label %56
34: ; preds = %17
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.13, i64 0, i64 0))
br label %56
36: ; preds = %17
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str, i64 0, i64 0))
br label %56
38: ; preds = %29, %30, %17
%39 = phi i32 [ 3, %30 ], [ 2, %29 ], [ 1, %17 ]
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %39)
br label %56
41: ; preds = %17, %31
%42 = phi i32 [ 5, %31 ], [ 4, %17 ]
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %42)
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.12, i64 0, i64 0))
br label %56
45: ; preds = %17
%46 = load i16, i16* %1
switch i16 %46, label %54 [
i16 1, label %47
i16 2, label %49
]
47: ; preds = %45
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.11, i64 0, i64 0))
br label %56
49: ; preds = %45
%50 = load i16, i16* %3
%51 = zext i16 %50 to i32
%52 = add nuw nsw i32 %51, 3
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.8, i64 0, i64 0), i32 %52)
br label %56
54: ; preds = %45
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
br label %56
56: ; preds = %36, %34, %32, %38, %41, %47, %54, %49
%57 = tail call i32 @exit(i32 0)
unreachable
}