// File: /workspace/github_cpp_makefile_star2_O1/cpearce[P]OggIndex/src/Utils.cpp
// Function: PageSeek(ogg_sync_state*, ogg_page*, std::istream&, unsigned long&)
int PageSeek(ogg_sync_state* state,
             ogg_page* page,
             istream& stream,
             ogg_uint64_t& bytesRead)
{
  int retval = 0;
  ogg_int32_t bytes = 0;
  ogg_int32_t r = 0;
  ogg_uint64_t intialBytesRead = bytesRead;
  while ((r = ogg_sync_pageseek(state, page)) <= 0) {
    if (r == 0) {
      // Need to read more data to get to next page.
      char* buffer = ogg_sync_buffer(state, FILE_BUFFER_SIZE);
      assert(buffer);

      stream.read(buffer, FILE_BUFFER_SIZE);
      bytes = stream.gcount();
      bytesRead += bytes;
      if (bytes == 0) {
        // End of file
        assert(stream.eof());
        if (intialBytesRead != bytesRead) {
          cerr << "WARNING: Reached end of file, when expecting to find more data! "
               << "Page header may be incorrect!" << endl;
        }
        return false;
      }

      ogg_int32_t ret = ogg_sync_wrote(state, bytes);
      assert(ret == 0);
    } else {
      assert(r<0);
      // We skipped -r bytes reading up to next ogg page capture.
      retval += (-r);
    }
  }
  if (r == -1) {
    cout << "ERROR: sync failure in ReadPage()" << endl;
    return -1;
  }
  return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq (%rcx), %r13
callq 0x4220
testl %eax, %eax
jle 0xc56b
xorl %ebp, %ebp
jmp 0xc663
movq %r13, (%rsp)
xorl %ebp, %ebp
testl %eax, %eax
je 0xc57c
subl %eax, %ebp
jmp 0xc650
movl $0x100000, %esi         # imm = 0x100000
movq %r12, %rdi
callq 0x45f0
testq %rax, %rax
je 0xc674
movl $0x100000, %edx         # imm = 0x100000
movq %r14, %rdi
movq %rax, %rsi
callq 0x4470
movl 0x8(%r14), %r13d
movslq %r13d, %rsi
movq (%rbx), %rax
addq %rsi, %rax
movq %rax, (%rbx)
testq %r13, %r13
je 0xc5cc
movq %r12, %rdi
callq 0x4460
testl %eax, %eax
je 0xc647
jmp 0xc693
movq (%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x20(%r14,%rcx)
je 0xc6b2
cmpq %rax, (%rsp)
je 0xc647
movl $0x40, %edx
movq 0xa9ff(%rip), %rdi      # 0x16ff0
leaq 0x4fa1(%rip), %rsi      # 0x11599
callq 0x4410
movl $0x1d, %edx
movq 0xa9e7(%rip), %rdi      # 0x16ff0
leaq 0x4fca(%rip), %rsi      # 0x115da
callq 0x4410
movq 0xa9d4(%rip), %rcx      # 0x16ff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x4360
movsbl %al, %esi
movq 0xa9b6(%rip), %rdi      # 0x16ff0
callq 0x4040
movq %rax, %rdi
callq 0x4250
testq %r13, %r13
je 0xc564
movq %r12, %rdi
movq %r15, %rsi
callq 0x4220
testl %eax, %eax
jle 0xc571
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4ec3(%rip), %rdi      # 0x1153e
leaq 0x4e2d(%rip), %rsi      # 0x114af
leaq 0x4f6f(%rip), %rcx      # 0x115f8
movl $0x9f, %edx
callq 0x4210
leaq 0x396a(%rip), %rdi      # 0x10004
leaq 0x4e0e(%rip), %rsi      # 0x114af
leaq 0x4f50(%rip), %rcx      # 0x115f8
movl $0xaf, %edx
callq 0x4210
leaq 0x4ed3(%rip), %rdi      # 0x1158c
leaq 0x4def(%rip), %rsi      # 0x114af
leaq 0x4f31(%rip), %rcx      # 0x115f8
movl $0xa6, %edx
callq 0x4210