// File: /workspace/github_cpp_makefile_star2_O3/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 0x4540
testl %eax, %eax
jne 0x4fcc
leaq 0x1d0(%rbx), %r12
leaq 0x1e8(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x18(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4590
cmpl $0x1, %eax
jne 0x4ef6
movq %rbp, %rdi
callq 0xc15e
movq 0x30(%rsp), %rcx
testb %al, %al
je 0x4d37
testq %rcx, %rcx
jne 0x4f2d
cmpl $0x40000, 0x220(%rbx)   # imm = 0x40000
jne 0x4db4
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4feb
testb %al, %al
jne 0x4e87
movl $0x16, %edx
movq 0x12316(%rip), %r14     # 0x16ff0
movq %r14, %rdi
leaq 0xb415(%rip), %rsi      # 0x100f9
callq 0x4410
movq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x4340
movq %rax, %r13
movl $0xb, %edx
movq %rax, %rdi
leaq 0xb408(%rip), %rsi      # 0x10110
callq 0x4410
movq (%rsp), %rdi
callq 0x42d0
movq %r13, %rdi
movl %eax, %esi
callq 0x4690
movq %rax, %r13
movl $0x11, %edx
movq %rax, %rdi
leaq 0xb3ea(%rip), %rsi      # 0x1011c
jmp 0x4e5a
testq %rcx, %rcx
setne %cl
movq 0x20(%rsp), %rax
testq %rax, %rax
sete %dl
testb %dl, %cl
jne 0x4d85
cmpq $0x9, %rax
jl 0x4d75
movq 0x18(%rsp), %rax
movabsq $0x64616568736966, %rcx # imm = 0x64616568736966
cmpq %rcx, (%rax)
je 0x4d85
movabsq $0x656e6f62736966, %rcx # imm = 0x656E6F62736966
cmpq %rcx, (%rax)
je 0x4d85
movq %rbp, %rdi
callq 0xc15e
testb %al, %al
je 0x4e87
movq %rbp, %rdi
callq 0xc15e
testb %al, %al
jne 0x4fad
movq %rbp, %rdi
callq 0xc101
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x75e2
jmp 0x4e87
movl $0x30, %edx
movq 0x12230(%rip), %r14     # 0x16ff0
movq %r14, %rdi
leaq 0xb2c7(%rip), %rsi      # 0x10091
callq 0x4410
movzwl 0x21a(%rbx), %esi
movq %r14, %rdi
callq 0x4240
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xbd7d(%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 0xb2a9(%rip), %rsi      # 0x100c2
callq 0x4410
movq %r13, %rdi
movl $0x4, %esi
callq 0x4690
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x4410
movq %r13, %rdi
xorl %esi, %esi
callq 0x4690
movq %rax, %r13
movl $0x1b, %edx
movq %rax, %rdi
leaq 0xb283(%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 0xc7e0
testb %al, %al
je 0x4ee1
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
callq 0xca7f
movw %ax, 0x21a(%rbx)
movq 0x18(%rsp), %rdi
addq $0xa, %rdi
callq 0xca7f
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 0x4f4c
cmpl $0x40001, %eax          # imm = 0x40001
jae 0x4f4c
cmpq $0x0, 0x30(%rsp)
je 0x4c77
movb $0x1, 0x218(%rbx)
jmp 0x4efd
incl %eax
cmpl $0x2, %eax
jae 0x4f0e
movb $0x1, %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb165(%rip), %rdi      # 0x1007a
leaq 0xb0f1(%rip), %rsi      # 0x1000d
leaq 0xb119(%rip), %rcx      # 0x1003c
movl $0x3c0, %edx            # imm = 0x3C0
callq 0x4210
leaq 0xb14f(%rip), %rdi      # 0x10083
leaq 0xb0d2(%rip), %rsi      # 0x1000d
leaq 0xb0fa(%rip), %rcx      # 0x1003c
movl $0x3c4, %edx            # imm = 0x3C4
callq 0x4210
movq 0x1209d(%rip), %rdi     # 0x16ff0
leaq 0xb1ec(%rip), %rsi      # 0x10146
callq 0x4370
movzwl 0x21a(%rbx), %esi
movq %rax, %rdi
callq 0x4660
leaq 0xbbf8(%rip), %rsi      # 0x10b6d
movq %rax, %rdi
callq 0x4370
movzwl 0x21c(%rbx), %esi
movq %rax, %rdi
callq 0x4660
leaq 0xb1cb(%rip), %rsi      # 0x1015e
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41c0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4430
leaq 0xb17a(%rip), %rdi      # 0x1012e
leaq 0xb052(%rip), %rsi      # 0x1000d
leaq 0xb07a(%rip), %rcx      # 0x1003c
movl $0x3d0, %edx            # imm = 0x3D0
callq 0x4210
leaq 0xb031(%rip), %rdi      # 0x10004
leaq 0xb033(%rip), %rsi      # 0x1000d
leaq 0xb05b(%rip), %rcx      # 0x1003c
movl $0x3b1, %edx            # imm = 0x3B1
callq 0x4210