// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x8(%rdi), %r15
movq %r15, %rdi
movq %rsi, (%rsp)
callq 0x4550
testl %eax, %eax
jne 0x503b
leaq 0x1d0(%rbx), %r12
leaq 0x1e8(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x18(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x45a0
cmpl $0x1, %eax
jne 0x4f65
movq %rbp, %rdi
callq 0xc3a2
testb %al, %al
je 0x4da9
cmpq $0x0, 0x30(%rsp)
jne 0x4f9c
cmpl $0x40000, 0x220(%rbx)   # imm = 0x40000
jne 0x4e23
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x505a
testb %al, %al
jne 0x4ef6
movl $0x16, %edx
movq 0x122a4(%rip), %r14     # 0x16ff0
movq %r14, %rdi
leaq 0xb3a3(%rip), %rsi      # 0x100f9
callq 0x4410
movq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x4340
movq %rax, %r13
movl $0xb, %edx
movq %rax, %rdi
leaq 0xb396(%rip), %rsi      # 0x10110
callq 0x4410
movq (%rsp), %rdi
callq 0x42d0
movq %r13, %rdi
movl %eax, %esi
callq 0x46a0
movq %rax, %r13
movl $0x11, %edx
movq %rax, %rdi
leaq 0xb378(%rip), %rsi      # 0x1011c
jmp 0x4ec9
cmpq $0x0, 0x30(%rsp)
je 0x4db9
cmpq $0x0, 0x20(%rsp)
je 0x4df4
cmpq $0x9, 0x20(%rsp)
jl 0x4de4
movq 0x18(%rsp), %rax
movabsq $0x64616568736966, %rcx # imm = 0x64616568736966
cmpq %rcx, (%rax)
je 0x4df4
movabsq $0x656e6f62736966, %rcx # imm = 0x656E6F62736966
cmpq %rcx, (%rax)
je 0x4df4
movq %rbp, %rdi
callq 0xc3a2
testb %al, %al
je 0x4ef6
movq %rbp, %rdi
callq 0xc3a2
testb %al, %al
jne 0x501c
movq %rbp, %rdi
callq 0xc345
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x76ac
jmp 0x4ef6
movl $0x30, %edx
movq 0x121c1(%rip), %r14     # 0x16ff0
movq %r14, %rdi
leaq 0xb258(%rip), %rsi      # 0x10091
callq 0x4410
movzwl 0x21a(%rbx), %esi
movq %r14, %rdi
callq 0x4240
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xbd0e(%rip), %r14      # 0x10b6d
movq %r14, %rsi
callq 0x4410
movzwl 0x21c(%rbx), %esi
movq %r13, %rdi
callq 0x4240
movq %rax, %r13
movl $0x1a, %edx
movq %rax, %rdi
leaq 0xb23a(%rip), %rsi      # 0x100c2
callq 0x4410
movq %r13, %rdi
movl $0x4, %esi
callq 0x46a0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x4410
movq %r13, %rdi
xorl %esi, %esi
callq 0x46a0
movq %rax, %r13
movl $0x1b, %edx
movq %rax, %rdi
leaq 0xb214(%rip), %rsi      # 0x100dd
callq 0x4410
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4360
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4250
movq %rbp, %rdi
callq 0xca6a
testb %al, %al
je 0x4f50
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
callq 0xcd11
movw %ax, 0x21a(%rbx)
movq 0x18(%rsp), %rdi
addq $0xa, %rdi
callq 0xcd11
movw %ax, 0x21c(%rbx)
movzwl 0x21a(%rbx), %ecx
movl %ecx, %edx
shll $0x10, %edx
movzwl %ax, %eax
orl %edx, %eax
movl %eax, 0x220(%rbx)
cmpw $0x3, %cx
jb 0x4fbb
cmpl $0x40001, %eax          # imm = 0x40001
jae 0x4fbb
cmpq $0x0, 0x30(%rsp)
je 0x4ceb
movb $0x1, 0x218(%rbx)
jmp 0x4f6c
incl %eax
cmpl $0x2, %eax
jae 0x4f7d
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb0f6(%rip), %rdi      # 0x1007a
leaq 0xb082(%rip), %rsi      # 0x1000d
leaq 0xb0aa(%rip), %rcx      # 0x1003c
movl $0x3c0, %edx            # imm = 0x3C0
callq 0x4210
leaq 0xb0e0(%rip), %rdi      # 0x10083
leaq 0xb063(%rip), %rsi      # 0x1000d
leaq 0xb08b(%rip), %rcx      # 0x1003c
movl $0x3c4, %edx            # imm = 0x3C4
callq 0x4210
movq 0x1202e(%rip), %rdi     # 0x16ff0
leaq 0xb17d(%rip), %rsi      # 0x10146
callq 0x4370
movzwl 0x21a(%rbx), %esi
movq %rax, %rdi
callq 0x4670
leaq 0xbb89(%rip), %rsi      # 0x10b6d
movq %rax, %rdi
callq 0x4370
movzwl 0x21c(%rbx), %esi
movq %rax, %rdi
callq 0x4670
leaq 0xb15c(%rip), %rsi      # 0x1015e
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4430
leaq 0xb10b(%rip), %rdi      # 0x1012e
leaq 0xafe3(%rip), %rsi      # 0x1000d
leaq 0xb00b(%rip), %rcx      # 0x1003c
movl $0x3d0, %edx            # imm = 0x3D0
callq 0x4210
leaq 0xafc2(%rip), %rdi      # 0x10004
leaq 0xafc4(%rip), %rsi      # 0x1000d
leaq 0xafec(%rip), %rcx      # 0x1003c
movl $0x3b1, %edx            # imm = 0x3B1
callq 0x4210