// File: /workspace/github_cpp_makefile_star2_O3/devinsmith[P]qvdreader/QvdFile.cpp
// Function: QvdFile::parseXmlHeader(char const*)
bool QvdFile::parseXmlHeader(const char *filename)
{
  const char *endQvdTableHeader = "</QvdTableHeader>";
  xmlParserCtxtPtr ctxt;

  ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, filename);

  // Read XML content of the file.
  int done;
  do {
    size_t len = readBytes();
    done = len < sizeof(_buf); // Did we reach end of file?

    // Unfortunately the Qvd file format includes an XML based
    // header followed by raw binary data.
    // We know that it occurs after </QvdTableHeader> so we need
    // to scan our read buffer each time to see if we have that tag.
    //
    // If we do, we alter the length read, set a pointer to the data just
    // after the tag and proceed to finish reading the XML portion of the
    // file.

    char *end;
    if ((end = strstr(_buf, endQvdTableHeader)) != NULL) {
      end += strlen(endQvdTableHeader);
      // For the XML portion, the dataPtrStart marks the end of the XML
      // and the start of raw data to be parsed later on.
      //
      // We treat this as an "End of File" situation for expat.
      _dataPtrStart = end;
      _bufLen = len;
      len = end - _buf;
      _bufLen -= len;
      done = 1;
    }

    if (xmlParseChunk(ctxt, _buf, len, done) != 0) {
      fprintf(stderr, "An error occurred parsing the XML\n");
      return false;
#if 0
      fprintf(stderr, "%s at line %lu\n",
        XML_ErrorString(XML_GetErrorCode(parser)),
        XML_GetCurrentLineNumber(parser));
#endif
    }
  } while (!done);

  if (ctxt->myDoc == NULL || !ctxt->wellFormed) {
    // XML header is not well formed.
    return false;
  }

  xmlDoc *doc = ctxt->myDoc; /* the resulting document tree */
  xmlNode *root_element = xmlDocGetRootElement(doc);

  for (xmlNode *node = root_element; node; node = node->next) {
    if (node->type == XML_ELEMENT_NODE &&
        strcmp((char *)node->name, "QvdTableHeader") == 0) {
      _hdr.ParseXml(node);
    }
  }

  xmlFreeDoc(doc);
  xmlFreeParserCtxt(ctxt);
  return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %r8
movq %rdi, %rbx
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4140
movq %rax, %r14
leaq 0x130(%rbx), %r15
leaq 0x4eae(%rip), %r12      # 0xa169
movq 0x128(%rbx), %rcx
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
movq %r15, %rdi
callq 0x44b0
movq %rax, %r13
cmpq $0x1fff, %rax           # imm = 0x1FFF
ja 0x52e6
movb $0x1, 0x2140(%rbx)
movq %r15, 0x2130(%rbx)
movq %r13, 0x2138(%rbx)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4070
testq %rax, %rax
jne 0x5331
xorl %ecx, %ecx
cmpq $0x2000, %r13           # imm = 0x2000
setb %cl
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x41b0
testl %eax, %eax
jne 0x53c8
cmpq $0x1fff, %r13           # imm = 0x1FFF
ja 0x52bb
jmp 0x535f
addq $0x11, %rax
movq %rax, 0x2130(%rbx)
subq %r15, %rax
subq %rax, %r13
movq %r13, 0x2138(%rbx)
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
movl $0x1, %ecx
callq 0x41b0
testl %eax, %eax
jne 0x53c8
movq 0x10(%r14), %r15
testq %r15, %r15
je 0x53cd
cmpl $0x0, 0x18(%r14)
je 0x53cd
movq %r15, %rdi
callq 0x44d0
testq %rax, %rax
je 0x53b4
movq %rax, %r12
leaq 0x4e18(%rip), %r13      # 0xa19e
cmpl $0x1, 0x8(%r12)
jne 0x53aa
movq 0x10(%r12), %rdi
movq %r13, %rsi
callq 0x4460
testl %eax, %eax
jne 0x53aa
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4160
movq 0x30(%r12), %r12
testq %r12, %r12
jne 0x5386
movq %r15, %rdi
callq 0x45e0
movq %r14, %rdi
callq 0x4170
movb $0x1, %al
jmp 0x53cf
callq 0x46d8
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop