// File: cpearce[P]OggIndex/src/Decoder.cpp
// Function: SkeletonDecoder::Decode(ogg_page*, long)
bool SkeletonDecoder::Decode(ogg_page* page, ogg_int64_t offset) {
  int ret = ogg_stream_pagein(&mState, page);
  assert(ret == 0);

  ogg_packet packet;
  int num_packets = 0;
  while (true) {

    ret = ogg_stream_packetout(&mState, &packet);
    if (ret == 0) {
      // We need another page to decode more packets.
      return true;
    }
    if (ret == -1) {
      // Some kind of error?
      return true;
    }
    assert(ret == 1);
    num_packets++;

    if (IsIndexPacket(&packet)) {
      assert(!packet.e_o_s);
      if (SKELETON_VERSION(SKELETON_VERSION_MAJOR,SKELETON_VERSION_MINOR) != mVersion) {
        cerr << "WARNING: Encountered an index packet of version " 
             << mVersionMajor << "." << mVersionMinor
             << ". I can only read version "
             << SKELETON_VERSION_MAJOR << "." << SKELETON_VERSION_MINOR 
             << ", so skipping index packet." << endl;
      } else if (!::DecodeIndex(mIndex, &packet)) {
        cerr << "WARNING: Index packet " << packet.packetno << " of stream "
             << ogg_page_serialno(page) << " failed to parse." << endl;
      }
    } else if (IsSkeletonPacket(&packet)) {
      assert(!IsIndexPacket(&packet));
      // Don't record index packets, we'll recompute them.
      mPackets.push_back(Clone(&packet));
    }
    
    // Check if the skeleton version is 3.x, fail otherwise.
    if (IsFisheadPacket(&packet)) {
      mVersionMajor = LEUint16(packet.packet + 8);
      mVersionMinor = LEUint16(packet.packet + 10);
      mVersion = SKELETON_VERSION(mVersionMajor, mVersionMinor);
      if (mVersion < SKELETON_VERSION(3,0) ||
          mVersion > SKELETON_VERSION(4,0)) { 
        cerr << "FAIL: Skeleton version " << mVersionMajor << "." << mVersionMinor   
             << " detected. I can only handle version [3.x,4.0]" << endl;
        exit(-1);
      }
    }
    
    // We've read all headers when we receive the EOS packet.
    if (packet.e_o_s) {
      mGotAllHeaders = true;
      return true;
    }
  }
  return true;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq 0x78(%rsp), %rdi
movq %rdi, 0x18(%rsp)
addq $0x8, %rdi
movq 0x70(%rsp), %rsi
callq 0x5620
movl %eax, 0x64(%rsp)
cmpl $0x0, 0x64(%rsp)
jne 0x5f0b
jmp 0x5f2a
leaq 0x170f2(%rip), %rdi     # 0x1d004
leaq 0x170f4(%rip), %rsi     # 0x1d00d
movl $0x3b1, %edx            # imm = 0x3B1
leaq 0x17117(%rip), %rcx     # 0x1d03c
callq 0x5280
movl $0x0, 0x2c(%rsp)
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
leaq 0x30(%rsp), %rsi
callq 0x5680
movl %eax, 0x64(%rsp)
cmpl $0x0, 0x64(%rsp)
jne 0x5f5d
movb $0x1, 0x87(%rsp)
jmp 0x62b4
cmpl $-0x1, 0x64(%rsp)
jne 0x5f71
movb $0x1, 0x87(%rsp)
jmp 0x62b4
cmpl $0x1, 0x64(%rsp)
jne 0x5f7a
jmp 0x5f99
leaq 0x170f9(%rip), %rdi     # 0x1d07a
leaq 0x17085(%rip), %rsi     # 0x1d00d
movl $0x3c0, %edx            # imm = 0x3C0
leaq 0x170a8(%rip), %rcx     # 0x1d03c
callq 0x5280
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x15ec0
testb $0x1, %al
jne 0x5fb7
jmp 0x6127
cmpq $0x0, 0x48(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x5fc8
jmp 0x5fca
jmp 0x5fe9
leaq 0x170b2(%rip), %rdi     # 0x1d083
leaq 0x17035(%rip), %rsi     # 0x1d00d
movl $0x3c4, %edx            # imm = 0x3C4
leaq 0x17058(%rip), %rcx     # 0x1d03c
callq 0x5280
movq 0x18(%rsp), %rcx
movl $0x40000, %eax          # imm = 0x40000
cmpl 0x220(%rcx), %eax
je 0x60a1
movq 0x21fea(%rip), %rdi     # 0x27ff0
leaq 0x17084(%rip), %rsi     # 0x1d091
callq 0x53d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movzwl 0x21a(%rax), %esi
callq 0x5740
movq %rax, %rdi
leaq 0x17b1d(%rip), %rsi     # 0x1db4d
callq 0x53d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movzwl 0x21c(%rax), %esi
callq 0x5740
movq %rax, %rdi
leaq 0x1706f(%rip), %rsi     # 0x1d0c2
callq 0x53d0
movq %rax, %rdi
movl $0x4, %esi
callq 0x57a0
movq %rax, %rdi
leaq 0x17ade(%rip), %rsi     # 0x1db4d
callq 0x53d0
movq %rax, %rdi
xorl %esi, %esi
callq 0x57a0
movq %rax, %rdi
leaq 0x17055(%rip), %rsi     # 0x1d0dd
callq 0x53d0
movq %rax, %rdi
movq 0x21f29(%rip), %rsi     # 0x27fc0
callq 0x5430
jmp 0x6125
movq 0x18(%rsp), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
leaq 0x30(%rsp), %rsi
callq 0x62d0
testb $0x1, %al
jne 0x6123
movq 0x21f2e(%rip), %rdi     # 0x27ff0
leaq 0x17030(%rip), %rsi     # 0x1d0f9
callq 0x53d0
movq %rax, %rdi
movq 0x58(%rsp), %rsi
callq 0x5810
movq %rax, %rdi
leaq 0x1702b(%rip), %rsi     # 0x1d110
callq 0x53d0
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rdi
callq 0x5350
movq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x57a0
movq %rax, %rdi
leaq 0x1700d(%rip), %rsi     # 0x1d11c
callq 0x53d0
movq %rax, %rdi
movq 0x21ea2(%rip), %rsi     # 0x27fc0
callq 0x5430
jmp 0x6125
jmp 0x619a
leaq 0x30(%rsp), %rdi
callq 0x66c0
testb $0x1, %al
jne 0x6137
jmp 0x6198
leaq 0x30(%rsp), %rdi
callq 0x15ec0
xorb $-0x1, %al
testb $0x1, %al
jne 0x6149
jmp 0x614b
jmp 0x616a
leaq 0x16fdc(%rip), %rdi     # 0x1d12e
leaq 0x16eb4(%rip), %rsi     # 0x1d00d
movl $0x3d0, %edx            # imm = 0x3D0
leaq 0x16ed7(%rip), %rcx     # 0x1d03c
callq 0x5280
movq 0x18(%rsp), %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x15e10
movq 0x8(%rsp), %rdi
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rsi
callq 0x6a90
jmp 0x619a
leaq 0x30(%rsp), %rdi
callq 0x168f0
testb $0x1, %al
jne 0x61ad
jmp 0x6291
movq 0x30(%rsp), %rdi
addq $0x8, %rdi
callq 0x16f90
movw %ax, %cx
movq 0x18(%rsp), %rax
movw %cx, 0x21a(%rax)
movq 0x30(%rsp), %rdi
addq $0xa, %rdi
callq 0x16f90
movw %ax, %cx
movq 0x18(%rsp), %rax
movw %cx, 0x21c(%rax)
movzwl 0x21a(%rax), %ecx
shll $0x10, %ecx
movzwl 0x21c(%rax), %edx
orl %edx, %ecx
movl %ecx, 0x220(%rax)
cmpl $0x30000, 0x220(%rax)   # imm = 0x30000
jb 0x621d
movq 0x18(%rsp), %rax
cmpl $0x40000, 0x220(%rax)   # imm = 0x40000
jbe 0x628f
movq 0x21dcc(%rip), %rdi     # 0x27ff0
leaq 0x16f1b(%rip), %rsi     # 0x1d146
callq 0x53d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movzwl 0x21a(%rax), %esi
callq 0x5740
movq %rax, %rdi
leaq 0x178ff(%rip), %rsi     # 0x1db4d
callq 0x53d0
movq %rax, %rdi
movq 0x18(%rsp), %rax
movzwl 0x21c(%rax), %esi
callq 0x5740
movq %rax, %rdi
leaq 0x16eed(%rip), %rsi     # 0x1d15e
callq 0x53d0
movq %rax, %rdi
movq 0x21d40(%rip), %rsi     # 0x27fc0
callq 0x5430
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x54c0
jmp 0x6291
cmpq $0x0, 0x48(%rsp)
je 0x62af
movq 0x18(%rsp), %rax
movb $0x1, 0x218(%rax)
movb $0x1, 0x87(%rsp)
jmp 0x62b4
jmp 0x5f32
movb 0x87(%rsp), %al
andb $0x1, %al
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)