#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 */

/* Variables and functions */
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, size_t);
void check_mmm01_format(unsigned char *file_data, size_t size) {
  if (size < 0x8000) {
    return;
  }
  unsigned char const *header_data = file_data + (size - 0x8000);
  unsigned char rom_code = header_data[0x147];
  if (header_data[0x104] == 0xCE && header_data[0x105] == 0xED &&
      header_data[0x106] == 0x66 && header_data[0x107] == 0x66 &&
      header_data[0x108] == 0xCC && header_data[0x109] == 0x0D &&
      rom_code >= 0xB && rom_code <= 0xD) {
    unsigned char temp[0x8000];
    memcpy(temp, header_data, 0x8000);
    memmove(file_data + 0x8000, file_data, size - 0x8000);
    memcpy(file_data, temp, 0x8000);
  }
}


x86 assembly (clang, -O0):
<check_mmm01_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x8000,-0x10(%rbp)
jae    1146 <check_mmm01_format+0x26>
jmp    124a <check_mmm01_format+0x12a>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x8000,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x147(%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0x104(%rax),%eax
cmp    $0xce,%eax
jne    124a <check_mmm01_format+0x12a>
mov    -0x18(%rbp),%rax
movzbl 0x105(%rax),%eax
cmp    $0xed,%eax
jne    124a <check_mmm01_format+0x12a>
mov    -0x18(%rbp),%rax
movzbl 0x106(%rax),%eax
cmp    $0x66,%eax
jne    124a <check_mmm01_format+0x12a>
mov    -0x18(%rbp),%rax
movzbl 0x107(%rax),%eax
cmp    $0x66,%eax
jne    124a <check_mmm01_format+0x12a>
mov    -0x18(%rbp),%rax
movzbl 0x108(%rax),%eax
cmp    $0xcc,%eax
jne    124a <check_mmm01_format+0x12a>
mov    -0x18(%rbp),%rax
movzbl 0x109(%rax),%eax
cmp    $0xd,%eax
jne    124a <check_mmm01_format+0x12a>
movzbl -0x19(%rbp),%eax
cmp    $0xb,%eax
jl     124a <check_mmm01_format+0x12a>
movzbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jg     124a <check_mmm01_format+0x12a>
lea    -0x8020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x8000,%edx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rdi
add    $0x8000,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x8000,%rdx
call   1040 <memmove@plt>
mov    -0x8(%rbp),%rdi
lea    -0x8020(%rbp),%rsi
mov    $0x8000,%edx
call   1030 <memcpy@plt>
add    $0x8020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_mmm01_format>:
push   %r15
push   %r14
push   %rbx
sub    $0x8000,%rsp
cmp    $0x8000,%rsi
jb     11ce <check_mmm01_format+0xae>
mov    %rsi,%rbx
mov    %rdi,%r14
cmpb   $0xce,-0x7efc(%rdi,%rsi,1)
ce
jne    11ce <check_mmm01_format+0xae>
lea    (%r14,%rbx,1),%rsi
add    $0xffffffffffff8000,%rsi
cmpb   $0xed,0x105(%rsi)
jne    11ce <check_mmm01_format+0xae>
cmpb   $0x66,0x106(%rsi)
jne    11ce <check_mmm01_format+0xae>
cmpb   $0x66,0x107(%rsi)
jne    11ce <check_mmm01_format+0xae>
cmpb   $0xcc,0x108(%rsi)
jne    11ce <check_mmm01_format+0xae>
cmpb   $0xd,0x109(%rsi)
jne    11ce <check_mmm01_format+0xae>
mov    -0x7eb9(%r14,%rbx,1),%al
ff
cmp    $0xb,%al
jb     11ce <check_mmm01_format+0xae>
cmp    $0xd,%al
ja     11ce <check_mmm01_format+0xae>
add    $0xffffffffffff8000,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x8000,%edx
call   1030 <memcpy@plt>
lea    0x8000(%r14),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memmove@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x8000,%edx
call   1030 <memcpy@plt>
add    $0x8000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @check_mmm01_format(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8
%7 = alloca [32768 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i64, i64* %4
%9 = icmp ult i64 %8, 32768
br i1 %9, label %10, label %11
10: ; preds = %2
br label %75
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = sub i64 %13, 32768
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 327
%18 = load i8, i8* %17
store i8 %18, i8* %6
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 260
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %22, 206
br i1 %23, label %24, label %75
24: ; preds = %11
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 261
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %28, 237
br i1 %29, label %30, label %75
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 262
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = icmp eq i32 %34, 102
br i1 %35, label %36, label %75
36: ; preds = %30
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 263
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %40, 102
br i1 %41, label %42, label %75
42: ; preds = %36
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 264
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %46, 204
br i1 %47, label %48, label %75
48: ; preds = %42
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i64 265
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = icmp eq i32 %52, 13
br i1 %53, label %54, label %75
54: ; preds = %48
%55 = load i8, i8* %6
%56 = zext i8 %55 to i32
%57 = icmp sge i32 %56, 11
br i1 %57, label %58, label %75
58: ; preds = %54
%59 = load i8, i8* %6
%60 = zext i8 %59 to i32
%61 = icmp sle i32 %60, 13
br i1 %61, label %62, label %75
62: ; preds = %58
%63 = getelementptr inbounds [32768 x i8], [32768 x i8]* %7, i64 0, i64 0
%64 = load i8*, i8** %5
%65 = call i32 @memcpy(i8* %63, i8* %64, i32 32768)
%66 = load i8*, i8** %3
%67 = getelementptr inbounds i8, i8* %66, i64 32768
%68 = load i8*, i8** %3
%69 = load i64, i64* %4
%70 = sub i64 %69, 32768
%71 = call i32 @memmove(i8* %67, i8* %68, i64 %70)
%72 = load i8*, i8** %3
%73 = getelementptr inbounds [32768 x i8], [32768 x i8]* %7, i64 0, i64 0
%74 = call i32 @memcpy(i8* %72, i8* %73, i32 32768)
br label %75
75: ; preds = %10, %62, %58, %54, %48, %42, %36, %30, %24, %11
ret void
}

LLVM IR (-O2):
define void @check_mmm01_format(i8* %0, i64 %1) {
%3 = alloca [32768 x i8]
%4 = icmp ult i64 %1, 32768
br i1 %4, label %43, label %5
5: ; preds = %2
%6 = add i64 %1, -32768
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 327
%9 = load i8, i8* %8
%10 = getelementptr inbounds i8, i8* %7, i64 260
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, -50
br i1 %12, label %13, label %43
13: ; preds = %5
%14 = getelementptr inbounds i8, i8* %7, i64 261
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, -19
br i1 %16, label %17, label %43
17: ; preds = %13
%18 = getelementptr inbounds i8, i8* %7, i64 262
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 102
br i1 %20, label %21, label %43
21: ; preds = %17
%22 = getelementptr inbounds i8, i8* %7, i64 263
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 102
br i1 %24, label %25, label %43
25: ; preds = %21
%26 = getelementptr inbounds i8, i8* %7, i64 264
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, -52
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = getelementptr inbounds i8, i8* %7, i64 265
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 13
%33 = icmp ugt i8 %9, 10
%34 = select i1 %32, i1 %33, i1 false
%35 = icmp ult i8 %9, 14
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %37, label %43
37: ; preds = %29
%38 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
%39 = call i32 @memcpy(i8* %38, i8* %7, i32 32768)
%40 = getelementptr inbounds i8, i8* %0, i64 32768
%41 = call i32 @memmove(i8* %40, i8* %0, i64 %6)
%42 = call i32 @memcpy(i8* %0, i8* %38, i32 32768)
br label %43
43: ; preds = %5, %13, %17, %21, %25, %29, %37, %2
ret void
}