// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movl %ecx, %r13d
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x95268(%rip), %rsi     # 0x9b5c4
movq %r14, %rdi
callq 0x1447b
movq %rax, 0x98(%rbx)
testq %rax, %rax
je 0x675f
movq %rbx, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
testl %r15d, %r15d
movq %r14, 0x28(%rsp)
js 0x6390
xorl %eax, %eax
jmp 0x63ac
movl %r15d, %r12d
negl %r12d
movl $0x8, %esi
movq %r12, %rdi
callq 0x4300
testq %rax, %rax
je 0x6798
movq 0x10(%rsp), %r12
leaq 0x80(%r12), %rcx
movq %rcx, 0x48(%rsp)
movl %r15d, %r14d
negl %r14d
movq %rax, 0x18(%rsp)
leaq 0x8(%rax), %rbp
xorl %ebx, %ebx
movq 0x98(%r12), %rdi
movl $0x2, %esi
movq 0x48(%rsp), %rdx
callq 0x15cc5
testl %eax, %eax
jle 0x649c
testl %r13d, %r13d
jne 0x647c
testl %r15d, %r15d
jne 0x640e
movq 0x90(%r12), %rax
cmpb $0x23, (%rax)
je 0x63cf
jmp 0x6499
jle 0x641c
incl %ebx
cmpl %r15d, %ebx
jl 0x63cf
jmp 0x649c
movq 0x90(%r12), %rax
cmpb $0x23, (%rax)
jne 0x649c
movq 0x8(%rsp), %rbx
cmpl %r14d, %ebx
jne 0x6457
movq 0x18(%rsp), %rax
movq (%rax), %rdi
callq 0x45c0
decl %ebx
leaq (,%rbx,8), %rdx
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x4830
movq 0x90(%r12), %rdi
callq 0x4930
movslq %ebx, %rcx
incl %ebx
movq %rbx, 0x8(%rsp)
movq 0x18(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
jmp 0x63cd
movq 0x90(%r12), %rax
xorl %r15d, %r15d
cmpb $0x23, (%rax)
sete %r15b
movq $0x0, 0x8(%rsp)
jmp 0x649c
xorl %r15d, %r15d
testl %r15d, %r15d
movq 0x18(%rsp), %rax
js 0x64b2
leaq 0x90(%r12), %rax
xorl %ebx, %ebx
jmp 0x64c6
movl %r15d, %ecx
negl %ecx
movl $0x1, %ebx
cmpl %ecx, 0x8(%rsp)
jne 0x67ad
movq (%rax), %rdi
movsbl 0x78(%r12), %edx
xorl %esi, %esi
callq 0x5ddc
cmpq $0x0, 0x80(%r12)
je 0x676b
movq %rax, %r13
movl (%rax), %r14d
testl %r14d, %r14d
je 0x6779
testl %r15d, %r15d
je 0x6502
movl %ebx, 0x24(%rsp)
jmp 0x65f0
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
testl %r14d, %r14d
jle 0x65b2
negl %r14d
movl $0x1, %ebp
cmpl $0x1, %ebp
je 0x6591
movq 0x10(%rsp), %rax
movb 0x78(%rax), %bl
movq 0x30(%rsp), %r15
leaq 0x2(%r15), %rax
cmpq %rax, 0x38(%rsp)
jae 0x6579
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %r12
shrq %r12
testq %rcx, %rcx
movl $0x0, %ecx
cmovneq %rcx, %r12
addq %rax, %r12
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x4840
testq %rax, %rax
je 0x6591
movq %rax, 0x40(%rsp)
movq %r12, 0x38(%rsp)
movq 0x40(%rsp), %rax
leaq 0x1(%r15), %rcx
movq %rcx, 0x30(%rsp)
movb %bl, (%rax,%r15)
movb $0x0, 0x1(%rax,%r15)
movl %ebp, %edi
leaq 0x30(%rsp), %rsi
callq 0x67c5
leal (%r14,%rbp), %eax
incl %eax
movl %ebp, %ecx
incl %ecx
movl %ecx, %ebp
cmpl $0x1, %eax
jne 0x6524
movq %r13, %rdi
callq 0x6159
movq 0x40(%rsp), %r15
movq 0x10(%rsp), %r12
movsbl 0x78(%r12), %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x5ddc
movq %rax, %r13
movq %r15, %rdi
callq 0x45c0
movl $0x1, 0x20(%r12)
movl $0x1, 0x24(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x8(%r12)
cmpl $0x0, (%r13)
je 0x66ee
xorl %r15d, %r15d
movq %r13, 0x28(%rsp)
movq 0x8(%r13), %r12
movq (%r12,%r15,8), %rbp
leaq 0x2(%rbp), %rbx
movq %rbp, 0x50(%rsp)
movzbl (%rbp), %r13d
cmpl $0x23, %r13d
je 0x664c
testl %r13d, %r13d
je 0x6740
callq 0x4510
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r13,2)
je 0x6654
incq %rbp
incq %rbx
jmp 0x6628
cmpq $0x5b, %r13
je 0x6664
testl %r13d, %r13d
jne 0x668d
jmp 0x6740
movq %rbx, %r13
movzbl -0x1(%rbx), %r14d
testq %r14, %r14
je 0x6685
callq 0x4510
movq (%rax), %rax
leaq 0x1(%r13), %rbx
testb $0x8, 0x1(%rax,%r14,2)
jne 0x6664
cmpl $0x5d, %r14d
cmoveq %r13, %rbp
movq 0x28(%rsp), %r13
movzbl (%rbp), %ebx
cmpl $0x23, %ebx
je 0x66b2
testl %ebx, %ebx
je 0x6740
callq 0x4510
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbx,2)
je 0x66b7
incq %rbp
jmp 0x6692
testq %rbx, %rbx
je 0x6740
movq %rbp, (%r12,%r15,8)
movq 0x10(%rsp), %r12
movq 0x8(%r12), %rdi
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rsi
movl %r15d, %edx
callq 0x697f
incq %r15
movl (%r13), %eax
cmpq %rax, %r15
jb 0x6617
movq %r13, 0x10(%r12)
cmpl $0x0, 0x24(%rsp)
movq 0x8(%rsp), %rax
jne 0x670b
movq 0x48(%rsp), %rcx
movq $0x0, (%rcx)
testl %eax, %eax
movq 0x18(%rsp), %r15
jle 0x672a
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x45c0
incq %r14
cmpq %r14, %rbx
jne 0x6719
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45c0
movq 0x10(%rsp), %rax
movq 0x90(%rax), %rdx
leaq 0x8fc04(%rip), %rdi     # 0x96357
movq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x5ed4
leaq 0x8fb43(%rip), %rdi     # 0x962a9
movq %r14, %rsi
jmp 0x67a6
leaq 0x8fb88(%rip), %rdi     # 0x962fa
movq 0x28(%rsp), %rsi
jmp 0x67a6
leaq 0x8fb8e(%rip), %rdi     # 0x9630e
leaq 0x8fb97(%rip), %rsi     # 0x9631e
leaq 0x8fb9c(%rip), %rcx     # 0x9632a
movl $0x18d, %edx            # imm = 0x18D
callq 0x42f0
shlq $0x3, %r12
leaq 0x8fa0d(%rip), %rdi     # 0x961b0
movq %r12, %rsi
xorl %eax, %eax
callq 0x5ed4
leaq 0x8fb09(%rip), %rdi     # 0x962bd
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x5ed4