// File: 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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x5290
movl %eax, 0x24(%rsp)
cmpl $0x0, %eax
jg 0x1629b
cmpl $0x0, 0x24(%rsp)
jne 0x16260
movq 0x48(%rsp), %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x56c0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x16142
jmp 0x16161
leaq 0x83c1(%rip), %rdi      # 0x1e50a
leaq 0x832b(%rip), %rsi      # 0x1e47b
movl $0x9f, %edx
leaq 0x8468(%rip), %rcx      # 0x1e5c4
callq 0x5280
movq 0x38(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x100000, %edx         # imm = 0x100000
callq 0x5520
movq 0x38(%rsp), %rdi
callq 0x58d0
movl %eax, 0x28(%rsp)
movslq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
cmpl $0x0, 0x28(%rsp)
jne 0x16223
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x5850
testb $0x1, %al
jne 0x161b5
jmp 0x161b7
jmp 0x161d6
leaq 0x839a(%rip), %rdi      # 0x1e558
leaq 0x82b6(%rip), %rsi      # 0x1e47b
movl $0xa6, %edx
leaq 0x83f3(%rip), %rcx      # 0x1e5c4
callq 0x5280
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpq (%rcx), %rax
je 0x16216
movq 0x11e04(%rip), %rdi     # 0x27ff0
leaq 0x8372(%rip), %rsi      # 0x1e565
callq 0x53d0
movq %rax, %rdi
leaq 0x83a4(%rip), %rsi      # 0x1e5a6
callq 0x53d0
movq %rax, %rdi
movq 0x11daf(%rip), %rsi     # 0x27fc0
callq 0x5430
movl $0x0, 0x54(%rsp)
jmp 0x162d6
movq 0x48(%rsp), %rdi
movslq 0x28(%rsp), %rsi
callq 0x5500
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jne 0x1623f
jmp 0x1625e
leaq 0x6dbe(%rip), %rdi      # 0x1d004
leaq 0x822e(%rip), %rsi      # 0x1e47b
movl $0xaf, %edx
leaq 0x836b(%rip), %rcx      # 0x1e5c4
callq 0x5280
jmp 0x16296
cmpl $0x0, 0x24(%rsp)
jge 0x16269
jmp 0x16288
leaq 0x839a(%rip), %rdi      # 0x1e60a
leaq 0x8204(%rip), %rsi      # 0x1e47b
movl $0xb1, %edx
leaq 0x8341(%rip), %rcx      # 0x1e5c4
callq 0x5280
xorl %eax, %eax
subl 0x24(%rsp), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x160fd
cmpl $-0x1, 0x24(%rsp)
jne 0x162ce
movq 0x11d27(%rip), %rdi     # 0x27fd0
leaq 0x835e(%rip), %rsi      # 0x1e60e
callq 0x53d0
movq %rax, %rdi
movq 0x11d01(%rip), %rsi     # 0x27fc0
callq 0x5430
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x162d6
movl 0x2c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nop