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

/* Variables and functions */
int /*<<< orphan*/ LH5Decode(unsigned char *, unsigned int, unsigned char *,
                             unsigned int);
int LH5HeaderParse(unsigned char *, int, unsigned int *, unsigned int *,
                   char **, unsigned short *);
unsigned char *MMapOutputFile(char *, unsigned int);
unsigned char *memmem(unsigned char *, int, char *, int);
int /*<<< orphan*/ munmap(unsigned char *, unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int AwardExtract(unsigned char *BIOSImage, int BIOSLength, int BIOSOffset,
                 uint32_t Offset1, uint32_t BCPSegmentOffset) {
  unsigned char *p, *Buffer;
  int HeaderSize;
  unsigned int BufferSize, PackedSize;
  char *filename;
  unsigned short crc;
  printf("Found Award BIOS.\n");
  p = BIOSImage;
  while (p) {
    p = memmem(p, BIOSLength - (p - BIOSImage), "-lh5-", 5);
    if (!p)
      break;
    p -= 2;
    HeaderSize = LH5HeaderParse(p, BIOSLength - (p - BIOSImage), &BufferSize,
                                &PackedSize, &filename, &crc);
    if (!HeaderSize)
      return 0;
    printf("0x%05X (%6d bytes)    ->    %s  \t(%6d bytes)\n",
           (unsigned int)(p - BIOSImage), HeaderSize + PackedSize, filename,
           BufferSize);
    Buffer = MMapOutputFile(filename, BufferSize);
    if (!Buffer)
      return 0;
    LH5Decode(p + HeaderSize, PackedSize, Buffer, BufferSize);
    munmap(Buffer, BufferSize);
    p += HeaderSize + PackedSize;
  }
  return 1;
}


x86 assembly (clang, -O0):
<AwardExtract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12b7 <AwardExtract+0x157>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%esi
lea    0xe5a(%rip),%rdx
mov    $0x5,%ecx
call   1060 <memmem@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d7 <AwardExtract+0x77>
jmp    12b7 <AwardExtract+0x157>
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x48(%rbp),%r8
lea    -0x4a(%rbp),%r9
call   1080 <LH5HeaderParse@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1229 <AwardExtract+0xc9>
movl   $0x0,-0x4(%rbp)
jmp    12be <AwardExtract+0x15e>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x34(%rbp),%edx
add    -0x3c(%rbp),%edx
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%r8d
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1030 <MMapOutputFile@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1279 <AwardExtract+0x119>
movl   $0x0,-0x4(%rbp)
jmp    12be <AwardExtract+0x15e>
mov    -0x28(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x3c(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%ecx
call   1050 <LH5Decode@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x34(%rbp),%ecx
add    -0x3c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    118f <AwardExtract+0x2f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AwardExtract>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xeaa(%rip),%rdi
call   1040 <puts@plt>
test   %rbx,%rbx
je     128a <AwardExtract+0x11a>
lea    0xe61(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x5,%ecx
call   1070 <memmem@plt>
test   %rax,%rax
je     128a <AwardExtract+0x11a>
mov    %rax,%r14
mov    %ebp,0x8(%rsp)
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xfffffffffffffffe,%r14
mov    %r14d,%r13d
sub    %ebx,%r13d
mov    %ebp,%esi
sub    %r13d,%esi
mov    %r14,%rdi
mov    %rsp,%rdx
lea    0x4(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xe(%rsp),%r9
call   1090 <LH5HeaderParse@plt>
test   %eax,%eax
je     12a2 <AwardExtract+0x132>
mov    %eax,%r15d
mov    0x4(%rsp),%edx
add    %eax,%edx
mov    0x10(%rsp),%rcx
mov    (%rsp),%r8d
lea    0xdec(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rdi
mov    (%rsp),%esi
call   1030 <MMapOutputFile@plt>
test   %rax,%rax
je     1290 <AwardExtract+0x120>
mov    %rax,%rbp
movslq %r15d,%rdi
add    %r14,%rdi
mov    0x4(%rsp),%esi
mov    (%rsp),%ecx
mov    %rax,%rdx
call   1060 <LH5Decode@plt>
mov    (%rsp),%esi
mov    %rbp,%rdi
call   1080 <munmap@plt>
mov    0x4(%rsp),%edi
add    %r15d,%edi
add    %r14,%rdi
mov    %ebx,%esi
sub    %edi,%esi
mov    0x8(%rsp),%ebp
add    %ebp,%esi
lea    0xd8c(%rip),%rdx
mov    $0x5,%ecx
call   1070 <memmem@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <AwardExtract+0x60>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
jmp    1290 <AwardExtract+0x120>

LLVM IR (-O0):
@.str = [19 x i8] c"Found Award BIOS.\0A\00"
@.str.1 = [6 x i8] c"-lh5-\00"
@.str.2 = [46 x i8] c"0x%05X (%6d bytes) -> %s \09(%6d bytes)\0A\00"
define i32 @AwardExtract(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i8*
%18 = alloca i16
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %7
store i8* %20, i8** %12
br label %21
21: ; preds = %75, %5
%22 = load i8*, i8** %12
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %93
24: ; preds = %21
%25 = load i8*, i8** %12
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = load i8*, i8** %12
%29 = load i8*, i8** %7
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = sub nsw i64 %27, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @memmem(i8* %25, i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i8* %35, i8** %12
%36 = load i8*, i8** %12
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %24
br label %93
39: ; preds = %24
%40 = load i8*, i8** %12
%41 = getelementptr inbounds i8, i8* %40, i64 -2
store i8* %41, i8** %12
%42 = load i8*, i8** %12
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i8*, i8** %12
%46 = load i8*, i8** %7
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = sub nsw i64 %44, %49
%51 = trunc i64 %50 to i32
%52 = call i32 @LH5HeaderParse(i8* %42, i32 %51, i32* %15, i32* %16, i8** %17, i16* %18)
store i32 %52, i32* %14
%53 = load i32, i32* %14
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %39
store i32 0, i32* %6
br label %94
56: ; preds = %39
%57 = load i8*, i8** %12
%58 = load i8*, i8** %7
%59 = ptrtoint i8* %57 to i64
%60 = ptrtoint i8* %58 to i64
%61 = sub i64 %59, %60
%62 = trunc i64 %61 to i32
%63 = load i32, i32* %14
%64 = load i32, i32* %16
%65 = add i32 %63, %64
%66 = load i8*, i8** %17
%67 = load i32, i32* %15
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %62, i32 %65, i8* %66, i32 %67)
%69 = load i8*, i8** %17
%70 = load i32, i32* %15
%71 = call i8* @MMapOutputFile(i8* %69, i32 %70)
store i8* %71, i8** %13
%72 = load i8*, i8** %13
%73 = icmp ne i8* %72, null
br i1 %73, label %75, label %74
74: ; preds = %56
store i32 0, i32* %6
br label %94
75: ; preds = %56
%76 = load i8*, i8** %12
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = load i32, i32* %16
%81 = load i8*, i8** %13
%82 = load i32, i32* %15
%83 = call i32 @LH5Decode(i8* %79, i32 %80, i8* %81, i32 %82)
%84 = load i8*, i8** %13
%85 = load i32, i32* %15
%86 = call i32 @munmap(i8* %84, i32 %85)
%87 = load i32, i32* %14
%88 = load i32, i32* %16
%89 = add i32 %87, %88
%90 = load i8*, i8** %12
%91 = zext i32 %89 to i64
%92 = getelementptr inbounds i8, i8* %90, i64 %91
store i8* %92, i8** %12
br label %21
93: ; preds = %38, %21
store i32 1, i32* %6
br label %94
94: ; preds = %93, %74, %55
%95 = load i32, i32* %6
ret i32 %95
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"-lh5-\00"
@.str.2 = [46 x i8] c"0x%05X (%6d bytes) -> %s \09(%6d bytes)\0A\00"
@str = [18 x i8] c"Found Award BIOS.\00"
define i32 @AwardExtract(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i16
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast i8** %8 to i8*
%13 = bitcast i16* %9 to i8*
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%15 = icmp eq i8* %0, null
br i1 %15, label %57, label %16
16: ; preds = %5
%17 = ptrtoint i8* %0 to i64
%18 = call i8* @memmem(i8* %0, i32 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%19 = icmp eq i8* %18, null
br i1 %19, label %57, label %20
20: ; preds = %16, %39
%21 = phi i8* [ %55, %39 ], [ %18, %16 ]
%22 = getelementptr inbounds i8, i8* %21, i64 -2
%23 = ptrtoint i8* %22 to i64
%24 = sub i64 %23, %17
%25 = trunc i64 %24 to i32
%26 = sub i32 %1, %25
%27 = call i32 @LH5HeaderParse(i8* %22, i32 %26, i32* %6, i32* %7, i8** %8, i16* %9)
%28 = icmp eq i32 %27, 0
br i1 %28, label %57, label %29
29: ; preds = %20
%30 = load i32, i32* %7
%31 = add i32 %30, %27
%32 = load i8*, i8** %8
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %31, i8* %32, i32 %33)
%35 = load i8*, i8** %8
%36 = load i32, i32* %6
%37 = call i8* @MMapOutputFile(i8* %35, i32 %36)
%38 = icmp eq i8* %37, null
br i1 %38, label %57, label %39
39: ; preds = %29
%40 = sext i32 %27 to i64
%41 = getelementptr inbounds i8, i8* %22, i64 %40
%42 = load i32, i32* %7
%43 = load i32, i32* %6
%44 = call i32 @LH5Decode(i8* %41, i32 %42, i8* %37, i32 %43)
%45 = load i32, i32* %6
%46 = call i32 @munmap(i8* %37, i32 %45)
%47 = load i32, i32* %7
%48 = add i32 %47, %27
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %22, i64 %49
%51 = ptrtoint i8* %50 to i64
%52 = sub i64 %17, %51
%53 = trunc i64 %52 to i32
%54 = add i32 %53, %1
%55 = call i8* @memmem(i8* %50, i32 %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %20
57: ; preds = %39, %29, %20, %16, %5
%58 = phi i32 [ 1, %5 ], [ 1, %16 ], [ 0, %20 ], [ 0, %29 ], [ 1, %39 ]
ret i32 %58
}