// File: /workspace/github_cpp_makefile_star2_O2/cpearce[P]OggIndex/src/OggIndex.cpp
// Function: main
int main(int argc, char** argv) 
{
  if (!gOptions.Parse(argc, argv)) {
    return -1;
  }

  string filename = gOptions.GetInputFilename();
  ifstream input(filename.c_str(), ios::in | ios::binary);
  ogg_sync_state state;
  ogg_int32_t ret = ogg_sync_init(&state);
  assert(ret==0);
 
  DecoderMap decoders;
  
  ogg_page page;
  memset(&page, 0, sizeof(ogg_page));

  ogg_uint64_t bytesRead = 0;
  ogg_int64_t offset = 0;
  ogg_uint32_t pageNumber = 0;
  bool gotAllHeaders = false;
  ogg_uint64_t endOfHeaders = 0;
  ogg_uint64_t oldSkeletonLength = 0;

  
  // We store all non-skeleton header pages in the order in which we read them,
  // so that we can rewrite them easily.
  vector<ogg_page*> headerPages;

  while (ReadPage(&state, &page, input, bytesRead)) {
    assert(IsPageAtOffset(filename, offset, &page));
    pageNumber++;
    ogg_uint32_t serial = ogg_page_serialno(&page);
    Decoder* decoder = 0;
    if (ogg_page_bos(&page)) {
      decoder = Decoder::Create(&page);
      decoders[serial] = decoder;
    } else {
      decoder = decoders[serial];
    }
    if (!decoder) {
      cerr << "FAIL: Unhandled stream type, serialno="
           << serial << " aborting indexing!" << endl;
      return -1;
    }

    ogg_uint32_t length = page.body_len + page.header_len;

    if (gOptions.GetDumpPages()) {
      ogg_int64_t granulepos = ogg_page_granulepos(&page);
      cout << "[" << decoder->TypeStr() << "] page @" << offset
           << " length=" << length << " granulepos=" << granulepos 
           << " end_time=" << decoder->GranuleposToTime(granulepos) << "ms"
           << " s=" << serial << " packet_starts=" << CountPacketStarts(&page)
           << " packet_ends=" << ogg_page_packets(&page)
           << " checksum=" << GetChecksum(&page) << endl;
    }

    decoder->Decode(&page, offset);
    
    if (!gotAllHeaders) {
      gotAllHeaders = true;
      DecoderMap::iterator itr = decoders.begin();
      while (itr != decoders.end()) {
        Decoder* d = itr->second;
        if (!d->GotAllHeaders()) {
          gotAllHeaders = false;
          break;
        }
        itr++;
      }
      if (decoder->Type() == TYPE_SKELETON) {
        oldSkeletonLength += length;
      } else {
        // Record all header pages except skeleton. We'll just rewrite the
        // skeleton, and the skeleton decoder will remember its packets.
        headerPages.push_back(Clone(&page));
      }
      if (gotAllHeaders) {
        endOfHeaders = offset + length;
      }
    }

    offset += length;
    memset(&page, 0, sizeof(ogg_page));
  }
  if (!gotAllHeaders) {
    // We've not completely read all the headers, assume the end of the last
    // page we've read is the end of headers. This will turn into the content
    // offset. The input file is probably corrupt.
    cout << "WARNING: Couldn't successfully read all header packets!" << endl;
    endOfHeaders = offset;
  }

  const ogg_int64_t fileLength = bytesRead;
  assert(input.eof());
  if (offset != fileLength) {
    cerr << "WARNING: Ogg page lengths don't sum to file length!" << endl;
  }  
  
  assert(fileLength == InputFileLength());
  
  ogg_sync_clear(&state);
  
  SkeletonEncoder encoder(decoders, fileLength, oldSkeletonLength, endOfHeaders);

  // Reopen the file so we can write it out with the index.
  input.close();
  input.clear();
  input.open(filename.c_str(), ios::in | ios::binary);
  assert(input.good());
  
  // Open output file.
  ofstream output(gOptions.GetOutputFilename().c_str(), ios::out | ios::binary);
  
  // Encode the new skeleton track.
  encoder.Encode();
  
  // Write out the new skeleton BOS page.
  encoder.WriteBosPage(output);
  if (!output.good()) {
    cerr << "ERROR: Failed to write new skeleton BOS page." << endl;
    return -1;
  }
  
  // Write out all the other tracks' header pages.
  for (ogg_uint32_t i=0; i<headerPages.size(); i++) {
    WritePage(output, *headerPages[i]);
    if (!output.good()) {
      cerr << "ERROR: Failed to write other tracks' header pages." << endl;
      return -1;
    }
    FreeClone(headerPages[i]);
  }

  // Write out remaining skeleton pages.
  encoder.WritePages(output);
  if (!output.good()) {
    cerr << "ERROR: Failed to write new skeleton index pages." << endl;
    return -1;
  }

  assert(encoder.ContentOffset() == output.tellp());

  // Copy content pages.
  input.seekg((std::streamoff)endOfHeaders);
  CopyFileData(input, output, fileLength - endOfHeaders);
  if (!output.good()) {
    cerr << "ERROR: Failed to write remaining content pages." << endl;
    return -1;
  }
  
  output.close();
  input.close();

  ogg_int64_t trackLength = encoder.GetTrackLength();
  cout << "Skeleton " << SKELETON_VERSION_MAJOR << "." << SKELETON_VERSION_MINOR 
       << " track with keyframe indexes uses " << trackLength << " bytes, "
       << ((float)trackLength / (float)(fileLength + trackLength)) * 100.0
       << "% overhead" << endl;

  int retval = 0;
  if (gOptions.GetVerifyIndex()) {
    cout << "Validating keyframe indexes..." << endl;
    if (!ValidateIndexedOgg(gOptions.GetOutputFilename())) {
      cerr << "FAIL: Verification of the index failed!" << endl;
      retval = -1;
    } else {
      cout << "SUCCESS: index is valid." << endl;
    }
  }

  return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x568, %rsp            # imm = 0x568
movq %rsi, %rdx
movl %edi, %esi
leaq 0xe02f(%rip), %rdi      # 0x15400
callq 0x84a0
testb %al, %al
je 0x775d
leaq 0xe01b(%rip), %rsi      # 0x15400
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x82ae
movq (%rbx), %rsi
leaq 0x360(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x46d0
leaq 0x140(%rsp), %rdi
callq 0x4160
testl %eax, %eax
jne 0x7c4e
leaq 0x68(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %r14
movapd %xmm0, 0x10(%r14)
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, (%r14)
movapd %xmm0, (%rax)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
xorl %ebx, %ebx
leaq 0x140(%rsp), %rdi
movq %r14, %rsi
leaq 0x360(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0xa9bc
testb %al, %al
je 0x7765
leaq 0xb0(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xac11
testb %al, %al
je 0x7c31
movq %r14, %rdi
callq 0x42c0
movl %eax, 0xd0(%rsp)
movq %r14, %rdi
callq 0x4690
testl %eax, %eax
je 0x74fe
movq %r14, %rdi
callq 0x5048
movq %rax, %rbp
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x7d9e
movq %rbp, (%rax)
jmp 0x7513
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x7d9e
movq (%rax), %rbp
testq %rbp, %rbp
je 0x792f
movq 0x98(%rsp), %r15
addq 0xa8(%rsp), %r15
leaq 0xdecd(%rip), %rdi      # 0x15400
callq 0x82da
testb %al, %al
je 0x768f
movq %r14, %rdi
callq 0x45c0
movq %rax, %r13
movq 0xda7e(%rip), %rdi      # 0x14fd0
leaq 0x8752(%rip), %rsi      # 0xfcab
callq 0x4340
movq %rax, %r12
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x30(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x4340
movq %rax, %rdi
leaq 0x73c2(%rip), %rsi      # 0xe942
callq 0x4340
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x73b1(%rip), %rsi      # 0xe94b
callq 0x4340
movl %r15d, %esi
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
leaq 0x7020(%rip), %rsi      # 0xe5d4
callq 0x4340
movq %rax, %rdi
movq %r13, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x7386(%rip), %rsi      # 0xe954
callq 0x4340
movq %rax, %r12
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x38(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x7af6(%rip), %rsi      # 0xf0ee
callq 0x4340
movq %rax, %rdi
leaq 0x84bd(%rip), %rsi      # 0xfac4
callq 0x4340
movl 0xd0(%rsp), %esi
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
leaq 0x733a(%rip), %rsi      # 0xe95f
callq 0x4340
movq %rax, %r12
movq %r14, %rdi
callq 0xaf95
movq %r12, %rdi
movl %eax, %esi
callq 0x4650
movq %rax, %rdi
leaq 0x7326(%rip), %rsi      # 0xe96f
callq 0x4340
movq %rax, %r12
movq %r14, %rdi
callq 0x46b0
movq %r12, %rdi
movl %eax, %esi
callq 0x4650
movq %rax, %rdi
leaq 0x7310(%rip), %rsi      # 0xe97d
callq 0x4340
movq %rax, %r12
movq %r14, %rdi
callq 0xaeae
movl %eax, %esi
movq %r12, %rdi
callq 0x4240
movq %rax, %rdi
callq 0x41b0
movq (%rbp), %rax
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
testb $0x1, 0x4(%rsp)
je 0x76b1
movl %r15d, %eax
addq %rbx, %rax
jmp 0x773f
movq 0x78(%rsp), %r13
leaq 0x68(%rsp), %rax
cmpq %rax, %r13
je 0x76db
movq 0x28(%r13), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
testb %al, %al
je 0x76e3
movq %r13, %rdi
callq 0x4250
movq %rax, %r13
jmp 0x76b6
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x76eb
movl $0x0, 0x4(%rsp)
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x28(%rax)
cmpl $0x4, %eax
jne 0x7704
movl %r15d, %eax
addq %rax, 0x10(%rsp)
jmp 0x7729
movq %r14, %rdi
callq 0xa73c
movq %rax, 0x160(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x80e6
movl %r15d, %eax
addq %rbx, %rax
cmpb $0x0, 0x4(%rsp)
movq 0x8(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movq %rax, %rbx
jmp 0x7481
pushq $-0x1
popq %rbx
jmp 0x7999
testb $0x1, 0x4(%rsp)
jne 0x778c
movq 0xd85d(%rip), %rdi      # 0x14fd0
leaq 0x720e(%rip), %rsi      # 0xe988
callq 0x4340
movq %rax, %rdi
callq 0x41b0
movq %rbx, 0x8(%rsp)
movq 0x360(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x380(%rsp,%rax)
je 0x7c67
movq 0x18(%rsp), %r15
cmpq %r15, %rbx
je 0x77cb
movq 0xd839(%rip), %rdi      # 0x14ff0
leaq 0x720e(%rip), %rsi      # 0xe9cc
callq 0x4340
movq %rax, %rdi
callq 0x41b0
leaq 0xdc2e(%rip), %rsi      # 0x15400
leaq 0x160(%rsp), %rdi
callq 0x82ae
movq 0x160(%rsp), %rdi
callq 0xa8c4
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x4170
cmpq %rbx, %r15
jne 0x7c86
leaq 0x140(%rsp), %rdi
callq 0x4720
leaq 0xd0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
callq 0x8804
leaq 0x360(%rsp), %rbx
movq %rbx, %rdi
callq 0x4090
movq 0x360(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4640
movq 0xb0(%rsp), %rsi
leaq 0x360(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x4200
movq 0x360(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x380(%rsp,%rax)
jne 0x7ca5
leaq 0xdb70(%rip), %rsi      # 0x15400
leaq 0x40(%rsp), %rdi
callq 0x82c0
movq 0x40(%rsp), %rsi
leaq 0x160(%rsp), %rdi
pushq $0x14
popq %rdx
callq 0x43b0
leaq 0x40(%rsp), %rdi
callq 0x4170
leaq 0xd0(%rsp), %rdi
callq 0x8b30
leaq 0xd0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x9a24
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x180(%rsp,%rax)
je 0x79ad
leaq 0x7131(%rip), %rsi      # 0xea2d
movq 0xd6ed(%rip), %rdi      # 0x14ff0
callq 0x4340
movq %rax, %rdi
callq 0x41b0
pushq $-0x1
popq %rbx
leaq 0x160(%rsp), %rdi
callq 0x44e0
leaq 0xd0(%rsp), %rdi
callq 0x893a
jmp 0x796b
movq 0xd6ba(%rip), %rdi      # 0x14ff0
leaq 0x6fca(%rip), %rsi      # 0xe907
callq 0x4340
movl 0xd0(%rsp), %esi
movq %rax, %rdi
callq 0x4240
leaq 0x6fd6(%rip), %rsi      # 0xe92e
movq %rax, %rdi
callq 0x4340
movq %rax, %rdi
callq 0x41b0
pushq $-0x1
popq %rbx
leaq 0x20(%rsp), %rdi
callq 0x7e44
leaq 0x60(%rsp), %rdi
callq 0x7dec
leaq 0x360(%rsp), %rdi
callq 0x40c0
leaq 0xb0(%rsp), %rdi
callq 0x4170
movl %ebx, %eax
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
leaq 0x160(%rsp), %rbx
movl %r14d, %r14d
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x7a06
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0xa897
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x180(%rsp,%rax)
jne 0x7a3d
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0xa87a
incl %r14d
jmp 0x79b8
leaq 0xd0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x9a5c
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x180(%rsp,%rax)
je 0x7a5d
leaq 0x7056(%rip), %rsi      # 0xea8e
jmp 0x78fc
movq 0xd5ac(%rip), %rdi      # 0x14ff0
leaq 0x7010(%rip), %rsi      # 0xea5b
callq 0x4340
movq %rax, %rdi
callq 0x41b0
jmp 0x7910
movq 0x138(%rsp), %rbx
leaq 0x160(%rsp), %rdi
callq 0x42d0
cmpq %rax, %rbx
jne 0x7cc4
leaq 0x360(%rsp), %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x4430
movq %r15, %rdx
subq 0x8(%rsp), %rdx
leaq 0x360(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0xad85
movq 0x160(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x180(%rsp,%rax)
je 0x7ace
leaq 0x7020(%rip), %rsi      # 0xeae9
jmp 0x78fc
leaq 0x160(%rsp), %rdi
callq 0x44b0
leaq 0x360(%rsp), %rdi
callq 0x4090
leaq 0xd0(%rsp), %rdi
callq 0x99e0
movq %rax, %rbx
movq 0xd4d1(%rip), %rdi      # 0x14fd0
leaq 0x70c0(%rip), %rsi      # 0xebc6
callq 0x4340
pushq $0x4
popq %rsi
movq %rax, %rdi
callq 0x4650
leaq 0x7050(%rip), %rsi      # 0xeb6d
movq %rax, %rdi
callq 0x4340
movq %rax, %rdi
xorl %esi, %esi
callq 0x4650
leaq 0x6fe3(%rip), %rsi      # 0xeb19
movq %rax, %rdi
callq 0x4340
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4320
leaq 0x6fec(%rip), %rsi      # 0xeb3c
movq %rax, %rdi
callq 0x4340
cvtsi2ss %rbx, %xmm0
addq %rbx, %r15
cvtsi2ss %r15, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x6d33(%rip), %xmm0     # 0xe8a8
movq %rax, %rdi
callq 0x45b0
leaq 0x6fc1(%rip), %rsi      # 0xeb45
movq %rax, %rdi
callq 0x4340
movq %rax, %rdi
callq 0x41b0
leaq 0xd865(%rip), %rdi      # 0x15400
callq 0x82d2
xorl %ebx, %ebx
testb %al, %al
je 0x7913
movq 0xd41f(%rip), %rdi      # 0x14fd0
leaq 0x6f98(%rip), %rsi      # 0xeb50
callq 0x4340
movq %rax, %rdi
callq 0x41b0
leaq 0xd834(%rip), %rsi      # 0x15400
leaq 0x40(%rsp), %rdi
callq 0x82c0
leaq 0x40(%rsp), %rdi
callq 0xb430
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x4170
testb %bpl, %bpl
je 0x7c11
movq 0xd3d8(%rip), %rdi      # 0x14fd0
leaq 0x6f98(%rip), %rsi      # 0xeb97
callq 0x4340
movq %rax, %rdi
callq 0x41b0
jmp 0x7913
movq 0xd3d8(%rip), %rdi      # 0x14ff0
leaq 0x6f50(%rip), %rsi      # 0xeb6f
callq 0x4340
movq %rax, %rdi
callq 0x41b0
jmp 0x7910
leaq 0x6ca7(%rip), %rdi      # 0xe8df
leaq 0x6c78(%rip), %rsi      # 0xe8b7
leaq 0x6c82(%rip), %rcx      # 0xe8c8
pushq $0x5a
popq %rdx
callq 0x4210
leaq 0x6c5b(%rip), %rdi      # 0xe8b0
leaq 0x6c5b(%rip), %rsi      # 0xe8b7
leaq 0x6c65(%rip), %rcx      # 0xe8c8
pushq $0x46
jmp 0x7c48
leaq 0x6d52(%rip), %rdi      # 0xe9c0
leaq 0x6c42(%rip), %rsi      # 0xe8b7
leaq 0x6c4c(%rip), %rcx      # 0xe8c8
movl $0x9b, %edx
callq 0x4210
leaq 0x6d73(%rip), %rdi      # 0xea00
leaq 0x6c23(%rip), %rsi      # 0xe8b7
leaq 0x6c2d(%rip), %rcx      # 0xe8c8
movl $0xa0, %edx
callq 0x4210
leaq 0x6d74(%rip), %rdi      # 0xea20
leaq 0x6c04(%rip), %rsi      # 0xe8b7
leaq 0x6c0e(%rip), %rcx      # 0xe8c8
movl $0xaa, %edx
callq 0x4210
leaq 0x6df4(%rip), %rdi      # 0xeabf
leaq 0x6be5(%rip), %rsi      # 0xe8b7
leaq 0x6bef(%rip), %rcx      # 0xe8c8
movl $0xca, %edx
callq 0x4210
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4170
jmp 0x7d3b
jmp 0x7d38
jmp 0x7d38
jmp 0x7d38
jmp 0x7d38
jmp 0x7d38
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4170
jmp 0x7d48
jmp 0x7d2d
jmp 0x7d65
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x4170
jmp 0x7d68
jmp 0x7d65
movq %rax, %rbx
jmp 0x7d7c
movq %rax, %rbx
jmp 0x7d89
movq %rax, %rbx
jmp 0x7d48
jmp 0x7d65
jmp 0x7d65
jmp 0x7d38
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x44e0
leaq 0xd0(%rsp), %rdi
callq 0x893a
jmp 0x7d68
jmp 0x7d65
jmp 0x7d65
jmp 0x7d65
jmp 0x7d65
jmp 0x7d65
jmp 0x7d65
jmp 0x7d65
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x7e44
leaq 0x60(%rsp), %rdi
callq 0x7dec
leaq 0x360(%rsp), %rdi
callq 0x40c0
leaq 0xb0(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x4680