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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned long lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ process_chunk(int, int *, int);
int /*<<< orphan*/ stderr;
int swap32(int);
int main(int argc, char **argv) {
  unsigned long flen, fpos;
  uint32_t *fptr;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "\n\tusage: %s <dump.ge>\n\n", argv[0]);
    return -1;
  }
  fd = open(argv[1], O_RDONLY, 0);
  flen = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  fptr = (uint32_t *)mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0);
  fpos = 0;
  flen /= 4;
  while (fpos < flen - 2) {
    uint32_t tag = swap32(fptr[fpos]);
    uint32_t len = swap32(fptr[fpos + 1]);
    if (fpos + len / 4 > flen) {
      fprintf(stderr, "**** unexpected end of file! ****\n");
      break;
    }
    if (len == 0)
      break;
    process_chunk(tag, &fptr[fpos + 2], len - 2 * 4);
    fpos += len / 4;
  }
  munmap((void *)fptr, flen);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ca <main+0x4a>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1334 <main+0x1b4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x18(%rbp),%rsi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x2,%rcx
cmp    %rcx,%rax
jae    1318 <main+0x198>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1080 <swap32@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x4(%rax,%rcx,4),%edi
call   1080 <swap32@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
cmp    -0x18(%rbp),%rax
jbe    12ce <main+0x14e>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1318 <main+0x198>
cmpl   $0x0,-0x34(%rbp)
jne    12dd <main+0x15d>
jmp    1318 <main+0x198>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rax
add    $0x2,%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
sub    $0x8,%edx
call   1070 <process_chunk@plt>
mov    -0x34(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    124d <main+0xcd>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1261 <main+0xe1>
mov    0x8(%rsi),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,%r12
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,0x4(%rsp)
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
shr    $0x2,%r12
mov    %r12,%r13
add    $0xfffffffffffffffe,%r13
je     1299 <main+0x119>
xor    %r15d,%r15d
xchg   %ax,%ax
mov    0x0(%rbp,%r15,4),%edi
call   1080 <swap32@plt>
mov    %eax,%ebx
mov    0x4(%rbp,%r15,4),%edi
call   1080 <swap32@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x2,%ecx
movslq %ecx,%r14
add    %r15,%r14
cmp    %r12,%r14
ja     1282 <main+0x102>
test   %eax,%eax
je     1299 <main+0x119>
lea    0x8(,%r15,4),%rsi
add    %rbp,%rsi
add    $0xfffffff8,%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1070 <process_chunk@plt>
mov    %r14,%r15
cmp    %r13,%r14
jb     1210 <main+0x90>
jmp    1299 <main+0x119>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12af <main+0x12f>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <munmap@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"\0A\09usage: %s <dump.ge>\0A\0A\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [35 x i8] c"**** unexpected end of file! ****\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %92
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24, i32 0)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = load i32, i32* @SEEK_END
%28 = call i64 @lseek(i32 %26, i32 0, i32 %27)
store i64 %28, i64* %6
%29 = load i32, i32* %9
%30 = load i32, i32* @SEEK_SET
%31 = call i64 @lseek(i32 %29, i32 0, i32 %30)
%32 = load i64, i64* %6
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @MAP_PRIVATE
%35 = load i32, i32* %9
%36 = call i64 @mmap(i32* null, i64 %32, i32 %33, i32 %34, i32 %35, i32 0)
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %8
store i64 0, i64* %7
%38 = load i64, i64* %6
%39 = udiv i64 %38, 4
store i64 %39, i64* %6
br label %40
40: ; preds = %71, %20
%41 = load i64, i64* %7
%42 = load i64, i64* %6
%43 = sub i64 %42, 2
%44 = icmp ult i64 %41, %43
br i1 %44, label %45, label %85
45: ; preds = %40
%46 = load i32*, i32** %8
%47 = load i64, i64* %7
%48 = getelementptr inbounds i32, i32* %46, i64 %47
%49 = load i32, i32* %48
%50 = call i32 @swap32(i32 %49)
store i32 %50, i32* %10
%51 = load i32*, i32** %8
%52 = load i64, i64* %7
%53 = add i64 %52, 1
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = call i32 @swap32(i32 %55)
store i32 %56, i32* %11
%57 = load i64, i64* %7
%58 = load i32, i32* %11
%59 = sdiv i32 %58, 4
%60 = sext i32 %59 to i64
%61 = add i64 %57, %60
%62 = load i64, i64* %6
%63 = icmp ugt i64 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %45
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %85
67: ; preds = %45
%68 = load i32, i32* %11
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %67
br label %85
71: ; preds = %67
%72 = load i32, i32* %10
%73 = load i32*, i32** %8
%74 = load i64, i64* %7
%75 = add i64 %74, 2
%76 = getelementptr inbounds i32, i32* %73, i64 %75
%77 = load i32, i32* %11
%78 = sub nsw i32 %77, 8
%79 = call i32 @process_chunk(i32 %72, i32* %76, i32 %78)
%80 = load i32, i32* %11
%81 = sdiv i32 %80, 4
%82 = sext i32 %81 to i64
%83 = load i64, i64* %7
%84 = add i64 %83, %82
store i64 %84, i64* %7
br label %40
85: ; preds = %70, %64, %40
%86 = load i32*, i32** %8
%87 = bitcast i32* %86 to i8*
%88 = load i64, i64* %6
%89 = call i32 @munmap(i8* %87, i64 %88)
%90 = load i32, i32* %9
%91 = call i32 @close(i32 %90)
store i32 0, i32* %3
br label %92
92: ; preds = %85, %14
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"\0A\09usage: %s <dump.ge>\0A\0A\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [35 x i8] c"**** unexpected end of file! ****\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %52
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11, i32 0)
%13 = load i32, i32* @SEEK_END
%14 = tail call i64 @lseek(i32 %12, i32 0, i32 %13)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @lseek(i32 %12, i32 0, i32 %15)
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_PRIVATE
%19 = tail call i64 @mmap(i32* null, i64 %14, i32 %17, i32 %18, i32 %12, i32 0)
%20 = inttoptr i64 %19 to i32*
%21 = lshr i64 %14, 2
%22 = add nsw i64 %21, -2
%23 = icmp eq i64 %22, 0
br i1 %23, label %48, label %24
24: ; preds = %8, %42
%25 = phi i64 [ %35, %42 ], [ 0, %8 ]
%26 = getelementptr inbounds i32, i32* %20, i64 %25
%27 = load i32, i32* %26
%28 = tail call i32 @swap32(i32 %27)
%29 = add nuw i64 %25, 1
%30 = getelementptr inbounds i32, i32* %20, i64 %29
%31 = load i32, i32* %30
%32 = tail call i32 @swap32(i32 %31)
%33 = sdiv i32 %32, 4
%34 = sext i32 %33 to i64
%35 = add i64 %25, %34
%36 = icmp ugt i64 %35, %21
br i1 %36, label %37, label %40
37: ; preds = %24
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %48
40: ; preds = %24
%41 = icmp eq i32 %32, 0
br i1 %41, label %48, label %42
42: ; preds = %40
%43 = add i64 %25, 2
%44 = getelementptr inbounds i32, i32* %20, i64 %43
%45 = add nsw i32 %32, -8
%46 = tail call i32 @process_chunk(i32 %28, i32* %44, i32 %45)
%47 = icmp ult i64 %35, %22
br i1 %47, label %24, label %48
48: ; preds = %42, %40, %8, %37
%49 = inttoptr i64 %19 to i8*
%50 = tail call i32 @munmap(i8* %49, i64 %21)
%51 = tail call i32 @close(i32 %12)
br label %52
52: ; preds = %48, %4
%53 = phi i32 [ -1, %4 ], [ 0, %48 ]
ret i32 %53
}