// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x74544(%rip), %rsi     # 0x7a526
movq %rbx, %rdi
callq 0x111c7
movq %rax, 0x98(%r15)
testq %rax, %rax
je 0x638c
movl %ebp, %eax
negl %eax
movq $0x0, 0x10(%rsp)
testl %ebp, %ebp
movq %rbx, 0x30(%rsp)
movl %eax, 0x28(%rsp)
js 0x6018
xorl %eax, %eax
jmp 0x602f
movl %eax, %r12d
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x4300
testq %rax, %rax
je 0x63c5
leaq 0x80(%r15), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x90(%r15), %rcx
movq %rcx, 0x8(%rsp)
movq %rax, 0x20(%rsp)
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movl %ebp, %ebx
notl %ebx
leaq (,%rbx,8), %rax
movq %rax, 0x40(%rsp)
pushq $0x2
popq %r12
xorl %r14d, %r14d
movq 0x98(%r15), %rdi
movl %r12d, %esi
movq 0x38(%rsp), %rdx
callq 0x12938
testl %eax, %eax
jle 0x611c
testl %r13d, %r13d
jne 0x612d
testl %ebp, %ebp
jne 0x60a8
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpb $0x23, (%rax)
je 0x606d
jmp 0x614c
jle 0x60b7
incl %r14d
cmpl %ebp, %r14d
jl 0x606d
jmp 0x6364
movq 0x8(%rsp), %rax
movq (%rax), %rdi
cmpb $0x23, (%rdi)
jne 0x6341
movq 0x10(%rsp), %r14
cmpl 0x28(%rsp), %r14d
jne 0x60fe
movq 0x20(%rsp), %r14
movq (%r14), %rdi
callq 0x45c0
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x4830
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl %ebx, %r14d
callq 0x4930
movslq %r14d, %rcx
incl %r14d
movq %r14, 0x10(%rsp)
movq 0x20(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
jmp 0x606a
testl %ebp, %ebp
js 0x6341
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
jmp 0x6155
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
xorl %ebp, %ebp
cmpb $0x23, (%rax)
sete %bpl
movq $0x0, 0x10(%rsp)
movq %rcx, %rax
jmp 0x6153
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
xorl %ebx, %ebx
movq (%rax), %rdi
movsbl 0x78(%r15), %edx
xorl %esi, %esi
callq 0x5abc
cmpq $0x0, 0x80(%r15)
je 0x6398
movq %rax, %r14
movl (%rax), %eax
testl %eax, %eax
je 0x63a6
testl %ebp, %ebp
je 0x618c
movl %ebx, 0x18(%rsp)
jmp 0x620e
leaq 0x50(%rsp), %r12
andq $0x0, 0x10(%r12)
xorps %xmm0, %xmm0
movaps %xmm0, (%r12)
xorl %ebx, %ebx
testl %eax, %eax
cmovgl %eax, %ebx
negl %ebx
pushq $0x1
popq %r13
leal (%rbx,%r13), %eax
cmpl $0x1, %eax
je 0x61d8
cmpl $0x1, %r13d
je 0x61c8
movsbl 0x78(%r15), %edi
movq %r12, %rsi
callq 0x63ed
movl %r13d, %edi
movq %r12, %rsi
callq 0x642c
incl %r13d
jmp 0x61ac
movq %r14, %rdi
callq 0x5df6
movq 0x60(%rsp), %r12
movsbl 0x78(%r15), %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x5abc
movq %rax, %r14
movq %r12, %rdi
callq 0x45c0
movl $0x1, 0x20(%r15)
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
callq 0x6545
movq %rax, 0x8(%r15)
xorl %r13d, %r13d
movq %r15, 0x48(%rsp)
movq %r14, 0x28(%rsp)
movl (%r14), %eax
cmpq %rax, %r13
jae 0x62f1
movq 0x8(%r14), %rbx
movq (%rbx,%r13,8), %r15
leaq 0x1(%r15), %r14
movq %r15, %r12
movzbl (%r12), %ebp
cmpl $0x23, %ebp
je 0x625e
testl %ebp, %ebp
je 0x6373
movsbl %bpl, %edi
callq 0x6550
testl %eax, %eax
je 0x6266
incq %r12
incq %r14
jmp 0x623f
cmpl $0x5b, %ebp
je 0x6274
testl %ebp, %ebp
jne 0x629f
jmp 0x6373
movb (%r14), %bpl
testb %bpl, %bpl
je 0x629f
callq 0x4510
movq (%rax), %rax
movzbl %bpl, %ecx
incq %r14
testb $0x8, 0x1(%rax,%rcx,2)
jne 0x6274
cmpb $0x5d, %bpl
cmoveq %r14, %r12
jmp 0x629f
incq %r12
movzbl (%r12), %ebp
cmpl $0x23, %ebp
je 0x629c
testl %ebp, %ebp
je 0x6373
movsbl %bpl, %edi
callq 0x6550
testl %eax, %eax
jne 0x629c
testb %bpl, %bpl
je 0x6373
movq %r12, (%rbx,%r13,8)
movq 0x48(%rsp), %r15
movq 0x8(%r15), %rdi
movq 0x28(%rsp), %r14
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rsi
movl %r13d, %edx
callq 0x6569
incq %r13
jmp 0x6224
movq %r14, 0x10(%r15)
cmpl $0x0, 0x18(%rsp)
jne 0x6305
movq 0x38(%rsp), %rax
andq $0x0, (%rax)
xorl %ebx, %ebx
movq 0x10(%rsp), %r15
testl %r15d, %r15d
cmovlel %ebx, %r15d
movq 0x20(%rsp), %r14
cmpq %rbx, %r15
je 0x632b
movq (%r14,%rbx,8), %rdi
callq 0x45c0
incq %rbx
jmp 0x6318
movq %r14, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45c0
movq 0x10(%rsp), %rsi
movl 0x28(%rsp), %ecx
cmpl %ecx, %esi
jne 0x63da
pushq $0x1
popq %rbx
movq 0x20(%rsp), %rax
movq %rcx, 0x10(%rsp)
jmp 0x6155
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
pushq $0x1
popq %rbp
jmp 0x6155
movq 0x8(%rsp), %rax
movq (%rax), %rdx
leaq 0x6efd5(%rip), %rdi     # 0x75357
movq %r15, %rsi
xorl %eax, %eax
callq 0x5b9e
leaq 0x6ef16(%rip), %rdi     # 0x752a9
movq %rbx, %rsi
jmp 0x63d3
leaq 0x6ef5b(%rip), %rdi     # 0x752fa
movq 0x30(%rsp), %rsi
jmp 0x63d3
leaq 0x6ef61(%rip), %rdi     # 0x7530e
leaq 0x6ef6a(%rip), %rsi     # 0x7531e
leaq 0x6ef6f(%rip), %rcx     # 0x7532a
movl $0x18d, %edx            # imm = 0x18D
callq 0x42f0
shlq $0x3, %r12
leaq 0x6ede0(%rip), %rdi     # 0x751b0
movq %r12, %rsi
xorl %eax, %eax
callq 0x5b9e
leaq 0x6eedc(%rip), %rdi     # 0x752bd
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x5b9e