// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/annot-tsv.c
// Function: parse_header
void parse_header(dat_t *dat, char *fname, int nth_row, int autodetect)
{
    dat->fp = hts_open(fname,"r");
    if ( !dat->fp ) error("Failed to open: %s\n", fname);

    // buffer comment lines when N<0
    int nbuf = 0;
    char **buf = NULL;
    if ( nth_row < 0 )
    {
        buf = calloc(-nth_row,sizeof(*buf));
        if ( !buf ) error("Out of memory, failed to allocate %zu bytes\n",(-nth_row)*sizeof(*buf));
    }

    int irow = 0;
    cols_t *cols = NULL;
    while ( hts_getline(dat->fp, KS_SEP_LINE, &dat->line) > 0 )
    {
        if ( autodetect )
        {
            // if the first line is comment line, use it as a header. Otherwise go
            // with numeric indices
            nth_row = dat->line.s[0]=='#' ? 1 : 0;
            break;
        }
        if ( nth_row==0 )
        {
            // N=0 .. comment lines to be ignored, read until we get to the first data line
            if ( dat->line.s[0]=='#' ) continue;
            break;
        }
        if ( nth_row>0 )
        {
            // N>1 .. regardless of this being a comment or data line, read until Nth line
            if ( ++irow < nth_row ) continue;
            break;
        }
        // N<0 .. keep abs(N) comment lines in a sliding buffer
        if ( dat->line.s[0]!='#' ) break;   // data line
        if ( nbuf == -nth_row )
        {
            // one more comment line and the buffer is full. We could use round buffer
            // for efficiency, but the assumption is abs(nth_row) is small
            free(buf[0]);
            memmove(buf, &buf[1], (nbuf-1)*sizeof(*buf));
            nbuf--;
        }
        buf[nbuf++] = strdup(dat->line.s);
    }

    int keep_line = 0;
    if ( nth_row < 0 )
    {
        if ( nbuf!=-nth_row )
            error("Found %d header lines in %s, cannot fetch N=%d from the end\n",nbuf,fname,-nth_row);
        cols = cols_split(buf[0], NULL, dat->delim);
        keep_line = 1;
    }
    else
        cols = cols_split(dat->line.s, NULL, dat->delim);

    if ( !dat->line.l ) error("Failed to read: %s\n", fname);
    assert(cols && cols->n);

    if ( nth_row == 0 ) // create numeric indices
    {
        // create a dummy header with numeric field names
        kstring_t str = {0,0,0};
        int i, n = cols->n;
        for (i=0; i<n; i++)
        {
            if ( i>0 ) kputc(dat->delim, &str);
            kputw(i+1, &str);
        }
        cols_destroy(cols);
        cols = cols_split(str.s, NULL, dat->delim);
        free(str.s);
        dat->hdr.dummy = 1;
        keep_line = 1;
    }

    dat->hdr.name2idx = khash_str2int_init();
    int i;
    for (i=0; i<cols->n; i++)
    {
        char *ss = cols->off[i];
        while ( *ss && (*ss=='#' || isspace_c(*ss)) ) ss++;
        if ( !*ss ) error("Could not parse the header field \"%s\": %s\n", cols->off[i],dat->line.s);
        if ( *ss=='[' )
        {
            char *se = ss+1;
            while ( *se && isdigit_c(*se) ) se++;
            if ( *se==']' ) ss = se + 1;
        }
        while ( *ss && (*ss=='#' || isspace_c(*ss)) ) ss++;
        if ( !*ss ) error("Could not parse the header field \"%s\": %s\n", cols->off[i],dat->line.s);
        cols->off[i] = ss;
        khash_str2int_set(dat->hdr.name2idx, cols->off[i], i);
    }
    dat->hdr.cols = cols;
    if ( !keep_line ) dat->line.l = 0;

    for (i=0; i<nbuf; i++) free(buf[i]);
    free(buf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x92255(%rip), %rsi     # 0x985cb
movq %r14, %rdi
callq 0x13ee8
movq %rax, 0x98(%rbx)
testq %rax, %rax
je 0x67c6
movl %r15d, %r13d
negl %r13d
testl %r15d, %r15d
movq %r14, 0x18(%rsp)
js 0x63a2
xorl %eax, %eax
jmp 0x63bb
movl %r13d, %r12d
movl $0x8, %esi
movq %r12, %rdi
callq 0x4300
testq %rax, %rax
je 0x67ff
movq $0x0, 0x10(%rsp)
leaq 0x80(%rbx), %rcx
movq %rcx, 0x60(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x90(%rbx), %r12
movq %rax, 0x28(%rsp)
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movl %r15d, %ebx
notl %ebx
leaq (,%rbx,8), %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rdi
movl $0x2, %esi
movq 0x60(%rsp), %rdx
callq 0x15732
testl %eax, %eax
jle 0x64a3
testl %ebp, %ebp
jne 0x64ca
testl %r15d, %r15d
jne 0x643d
movq (%r12), %rax
cmpb $0x23, (%rax)
je 0x63ff
jmp 0x64e6
jle 0x644c
incl %r14d
cmpl %r15d, %r14d
jl 0x63ff
jmp 0x679f
movq (%r12), %rdi
cmpb $0x23, (%rdi)
jne 0x64ab
movq 0x10(%rsp), %r14
cmpl %r13d, %r14d
jne 0x6485
movq 0x28(%rsp), %r14
movq (%r14), %rdi
callq 0x45c0
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x4830
movq (%r12), %rdi
movl %ebx, %r14d
callq 0x4930
movslq %r14d, %rcx
incl %r14d
movq %r14, 0x10(%rsp)
movq 0x28(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
jmp 0x63fc
movb $0x1, %bpl
testl %r15d, %r15d
jns 0x64ec
movq 0x10(%rsp), %rsi
cmpl %r13d, %esi
jne 0x6814
xorl %ebp, %ebp
movq 0x28(%rsp), %rax
movl %r13d, %ecx
movq %rcx, 0x10(%rsp)
jmp 0x64ef
movq (%r12), %rax
xorl %r15d, %r15d
cmpb $0x23, (%rax)
sete %r15b
movb $0x1, %bpl
movq $0x0, 0x10(%rsp)
jmp 0x64ec
movb $0x1, %bpl
xorl %r15d, %r15d
movq %r12, %rax
movq 0x8(%rsp), %r14
movq (%rax), %rdi
movsbl 0x78(%r14), %edx
xorl %esi, %esi
callq 0x5dec
cmpq $0x0, 0x80(%r14)
je 0x67d2
movq %rax, %r13
movl (%rax), %ebx
testl %ebx, %ebx
je 0x67e0
testl %r15d, %r15d
je 0x654e
movq %rbp, 0x18(%rsp)
movq %r12, 0x38(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
leaq 0x8(%r14), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x8(%r14)
jmp 0x667d
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testl %ebx, %ebx
jle 0x661a
movq %r13, 0x20(%rsp)
movq %r12, %r13
negl %ebx
movl $0x1, %ebp
cmpl $0x1, %ebp
je 0x65e6
movq 0x8(%rsp), %rax
movb 0x78(%rax), %r14b
movq 0x40(%rsp), %r15
leaq 0x2(%r15), %rax
cmpq %rax, 0x48(%rsp)
jae 0x65ce
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %r12
shrq %r12
testq %rcx, %rcx
movl $0x0, %ecx
cmovneq %rcx, %r12
addq %rax, %r12
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x4840
testq %rax, %rax
je 0x65e6
movq %rax, 0x50(%rsp)
movq %r12, 0x48(%rsp)
jmp 0x65d3
movq 0x50(%rsp), %rax
leaq 0x1(%r15), %rcx
movq %rcx, 0x40(%rsp)
movb %r14b, (%rax,%r15)
movb $0x0, 0x1(%rax,%r15)
movl %ebp, %edi
leaq 0x40(%rsp), %rsi
callq 0x682a
leal (%rbx,%rbp), %eax
incl %eax
movl %ebp, %ecx
incl %ecx
movl %ecx, %ebp
cmpl $0x1, %eax
jne 0x6576
movq 0x50(%rsp), %r15
movq 0x8(%rsp), %r14
movq %r13, %r12
movq 0x20(%rsp), %r13
jmp 0x661d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x6178
movsbl 0x78(%r14), %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x5dec
movq %rax, %r13
movq %r15, %rdi
callq 0x45c0
movl $0x1, 0x20(%r14)
movl (%r13), %ebx
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x8(%r14)
testl %ebx, %ebx
je 0x6761
movq $0x0, 0x18(%rsp)
movq %r12, 0x38(%rsp)
leaq 0x8(%r14), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
movq %r13, 0x20(%rsp)
movq 0x8(%r13), %rbx
movq (%rbx,%r15,8), %r14
leaq 0x1(%r14), %r12
movq %r14, %rbp
movzbl (%rbp), %r13d
cmpl $0x23, %r13d
je 0x66b8
testl %r13d, %r13d
je 0x67ad
callq 0x4510
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r13,2)
je 0x66c0
incq %rbp
incq %r12
jmp 0x6694
cmpq $0x5b, %r13
je 0x66d0
testl %r13d, %r13d
jne 0x66f1
jmp 0x67ad
movb (%r12), %cl
testb %cl, %cl
je 0x66f1
movzbl %cl, %edx
incq %r12
testb $0x8, 0x1(%rax,%rdx,2)
jne 0x66d0
cmpb $0x5d, %cl
cmoveq %r12, %rbp
jmp 0x66f1
incq %rbp
movzbl (%rbp), %ecx
cmpl $0x23, %ecx
je 0x66ee
testl %ecx, %ecx
je 0x67ad
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x66ee
testq %rcx, %rcx
je 0x67ad
movq %rbp, (%rbx,%r15,8)
movq 0x30(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rsp), %r13
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rsi
movl %r15d, %edx
callq 0x69f3
incq %r15
movl (%r13), %eax
cmpq %rax, %r15
jb 0x6685
movq 0x8(%rsp), %rax
movq %r13, 0x10(%rax)
cmpb $0x0, 0x18(%rsp)
je 0x6765
movq 0x60(%rsp), %rax
movq $0x0, (%rax)
jmp 0x6765
movq %r13, 0x10(%r14)
movq 0x10(%rsp), %rax
testl %eax, %eax
movq 0x28(%rsp), %r15
jle 0x6789
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x45c0
incq %r14
cmpq %r14, %rbx
jne 0x6778
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45c0
movb $0x1, %bpl
movl $0x1, %r15d
jmp 0x64ec
movq 0x38(%rsp), %rax
movq (%rax), %rdx
leaq 0x8cb9b(%rip), %rdi     # 0x93357
movq %r14, %rsi
xorl %eax, %eax
callq 0x5edc
leaq 0x8cadc(%rip), %rdi     # 0x932a9
movq %r14, %rsi
jmp 0x680d
leaq 0x8cb21(%rip), %rdi     # 0x932fa
movq 0x18(%rsp), %rsi
jmp 0x680d
leaq 0x8cb27(%rip), %rdi     # 0x9330e
leaq 0x8cb30(%rip), %rsi     # 0x9331e
leaq 0x8cb35(%rip), %rcx     # 0x9332a
movl $0x18d, %edx            # imm = 0x18D
callq 0x42f0
shlq $0x3, %r12
leaq 0x8c9a6(%rip), %rdi     # 0x931b0
movq %r12, %rsi
xorl %eax, %eax
callq 0x5edc
leaq 0x8caa2(%rip), %rdi     # 0x932bd
movq 0x18(%rsp), %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x5edc