// File: enferex[P]fnplot/cs.cc
// Function: CS::initHeader(unsigned char const*, unsigned long)
void CS::initHeader(const uint8_t *data, size_t data_len)
{
    pos_t pos = {0};
    char buf[1024], *tok;

    pos.data = data;
    pos.data_len = data_len;
    getLine(&pos, buf, sizeof(buf));

    // After the header are the symbols
    this->_hdr.syms_start = pos.off;

    // Load in the header: <cscope>
    tok = strtok(buf, " ");
    if (strncmp(tok, "cscope", strlen("cscope"))) {
        ERR("This does not appear to be a cscope database");
        return;
    }

    // Version
    this->_hdr.version = atoi(strtok(NULL, " "));

    // Directory
    this->_hdr.dir = strndup(strtok(NULL, " "), 1024);

    // Optionals: [-c] [-T] [-q <syms>]
    while ((tok = strtok(NULL, " "))) {
        if (tok[0] == '-' && strlen(tok) == 2) {
            if (tok[1] == 'c')
              this->_hdr.compression = true;
            else if (tok[1] == 'T')
              this->_hdr.prefix_match = true;
            else if (tok[1] == 'q')
              this->_hdr.inverted_index = true; // TODO
            else {
                ERR("Unrecognized header option");
                return;
            }
        }
        else {
            this->_hdr.trailer = atol(tok);
            break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x440(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2150
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0x430(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x3760
movq -0x440(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq -0x430(%rbp), %rdi
leaq 0x787e(%rip), %rsi      # 0xa302
callq 0x2390
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rdi
leaq 0x7881(%rip), %rsi      # 0xa31f
movl $0x6, %edx
callq 0x2120
cmpl $0x0, %eax
je 0x2ae0
jmp 0x2aaf
movq 0xd522(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x7866(%rip), %rsi      # 0xa326
movb $0x0, %al
callq 0x2340
movq 0xd50a(%rip), %rax      # 0xffd8
movq (%rax), %rsi
movl $0xa, %edi
callq 0x2300
jmp 0x2c26
xorl %eax, %eax
movl %eax, %edi
leaq 0x7817(%rip), %rsi      # 0xa302
callq 0x2390
movq %rax, %rdi
callq 0x2380
movl %eax, %ecx
movq -0x440(%rbp), %rax
movl %ecx, (%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x77f4(%rip), %rsi      # 0xa302
callq 0x2390
movq %rax, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2060
movq %rax, %rcx
movq -0x440(%rbp), %rax
movq %rcx, 0x18(%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x77c9(%rip), %rsi      # 0xa302
callq 0x2390
movq %rax, -0x438(%rbp)
cmpq $0x0, %rax
je 0x2c26
movq -0x438(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x2c05
movq -0x438(%rbp), %rdi
callq 0x2100
cmpq $0x2, %rax
jne 0x2c05
movq -0x438(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x63, %eax
jne 0x2b95
movq -0x440(%rbp), %rax
movb $0x1, 0x4(%rax)
jmp 0x2c03
movq -0x438(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x54, %eax
jne 0x2bb2
movq -0x440(%rbp), %rax
movb $0x1, 0x6(%rax)
jmp 0x2c01
movq -0x438(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x71, %eax
jne 0x2bcf
movq -0x440(%rbp), %rax
movb $0x1, 0x5(%rax)
jmp 0x2bff
jmp 0x2bd1
movq 0xd400(%rip), %rax      # 0xffd8
movq (%rax), %rdi
leaq 0x7771(%rip), %rsi      # 0xa353
movb $0x0, %al
callq 0x2340
movq 0xd3e8(%rip), %rax      # 0xffd8
movq (%rax), %rsi
movl $0xa, %edi
callq 0x2300
jmp 0x2c26
jmp 0x2c01
jmp 0x2c03
jmp 0x2c21
movq -0x438(%rbp), %rdi
callq 0x22d0
movq %rax, %rcx
movq -0x440(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2c26
jmp 0x2b2e
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
nop