// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xec76d(%rip), %rsi     # 0xf2af1
callq 0x1b150
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x98(%rax)
jne 0x63b7
movq -0x10(%rbp), %rsi
leaq 0xe7122(%rip), %rdi     # 0xed4d2
movb $0x0, %al
callq 0x5c70
movl $0x0, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x6405
xorl %eax, %eax
subl -0x14(%rbp), %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x42f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x6403
xorl %eax, %eax
subl -0x14(%rbp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
leaq 0xe6fdd(%rip), %rdi     # 0xed3d9
movb $0x0, %al
callq 0x5c70
jmp 0x6405
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rdi
movq -0x8(%rbp), %rdx
addq $0x80, %rdx
movl $0x2, %esi
callq 0x1dfd0
cmpl $0x0, %eax
jle 0x6526
cmpl $0x0, -0x18(%rbp)
je 0x6466
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movsbl (%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x23, %edx
cmovel %ecx, %eax
movl %eax, -0x14(%rbp)
jmp 0x6526
cmpl $0x0, -0x14(%rbp)
jne 0x6486
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x6481
jmp 0x6414
jmp 0x6526
cmpl $0x0, -0x14(%rbp)
jle 0x64a4
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpl -0x14(%rbp), %eax
jge 0x649f
jmp 0x6414
jmp 0x6526
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x64b9
jmp 0x6526
movl -0x1c(%rbp), %eax
xorl %ecx, %ecx
subl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jne 0x64f8
movq -0x28(%rbp), %rax
movq (%rax), %rdi
callq 0x45a0
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0x4810
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rdi
callq 0x4920
movq %rax, %rdx
movq -0x28(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x1c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x6414
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x657e
movl -0x1c(%rbp), %eax
xorl %ecx, %ecx
subl -0x14(%rbp), %ecx
cmpl %ecx, %eax
je 0x6559
movl -0x1c(%rbp), %esi
movq -0x10(%rbp), %rdx
xorl %ecx, %ecx
subl -0x14(%rbp), %ecx
leaq 0xe6f94(%rip), %rdi     # 0xed4e6
movb $0x0, %al
callq 0x5c70
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %esi
movsbl 0x78(%rax), %edx
callq 0x5ac0
movq %rax, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0x659e
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rdi
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %esi
movsbl 0x78(%rax), %edx
callq 0x5ac0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jne 0x65be
movq -0x10(%rbp), %rsi
leaq 0xe6f6c(%rip), %rdi     # 0xed523
movb $0x0, %al
callq 0x5c70
cmpq $0x0, -0x38(%rbp)
je 0x65d0
movq -0x38(%rbp), %rax
cmpl $0x0, (%rax)
je 0x65d0
jmp 0x65ef
leaq 0xe6f60(%rip), %rdi     # 0xed537
leaq 0xe6f69(%rip), %rsi     # 0xed547
movl $0x18d, %edx            # imm = 0x18D
leaq 0xe6f69(%rip), %rcx     # 0xed553
callq 0x42e0
cmpl $0x0, -0x14(%rbp)
jne 0x668f
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movq -0x38(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x60(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x6652
cmpl $0x0, -0x5c(%rbp)
jle 0x6638
movq -0x8(%rbp), %rax
movsbl 0x78(%rax), %edi
leaq -0x58(%rbp), %rsi
callq 0x68f0
movl -0x5c(%rbp), %edi
addl $0x1, %edi
leaq -0x58(%rbp), %rsi
callq 0x6970
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6619
movq -0x38(%rbp), %rdi
callq 0x6040
movq -0x48(%rbp), %rdi
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %esi
movsbl 0x78(%rax), %edx
callq 0x5ac0
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rdi
callq 0x45a0
movq -0x8(%rbp), %rax
movl $0x1, 0x20(%rax)
movl $0x1, -0x3c(%rbp)
callq 0x69f0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x6888
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x79(%rbp)
je 0x6706
movq -0x70(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x23, %ecx
movb %al, -0x7a(%rbp)
je 0x6700
movq -0x70(%rbp), %rax
movsbl (%rax), %edi
callq 0x6a00
cmpl $0x0, %eax
setne %al
movb %al, -0x7a(%rbp)
movb -0x7a(%rbp), %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x670f
jmp 0x671d
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x66c9
movq -0x70(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x674f
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rdx
leaq 0xe6e38(%rip), %rdi     # 0xed580
movb $0x0, %al
callq 0x5c70
movq -0x70(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x67be
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7b(%rbp)
je 0x678d
movq -0x78(%rbp), %rax
movsbl (%rax), %edi
callq 0x6a30
cmpl $0x0, %eax
setne %al
movb %al, -0x7b(%rbp)
movb -0x7b(%rbp), %al
testb $0x1, %al
jne 0x6796
jmp 0x67a4
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6767
movq -0x78(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5d, %eax
jne 0x67bc
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x67be
jmp 0x67c0
movq -0x70(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x7c(%rbp)
je 0x67fd
movq -0x70(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x23, %ecx
movb %al, -0x7d(%rbp)
je 0x67f7
movq -0x70(%rbp), %rax
movsbl (%rax), %edi
callq 0x6a00
cmpl $0x0, %eax
setne %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
movb %al, -0x7c(%rbp)
movb -0x7c(%rbp), %al
testb $0x1, %al
jne 0x6806
jmp 0x6814
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x67c0
movq -0x70(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x6846
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x8(%rbp), %rax
movq 0x90(%rax), %rdx
leaq 0xe6d41(%rip), %rdi     # 0xed580
movb $0x0, %al
callq 0x5c70
movq -0x70(%rbp), %rdx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl -0x64(%rbp), %edx
callq 0x6a60
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x66a6
movq -0x38(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpl $0x0, -0x3c(%rbp)
jne 0x68a9
movq -0x8(%rbp), %rax
movq $0x0, 0x80(%rax)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x68d4
movq -0x28(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x45a0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x68b0
movq -0x28(%rbp), %rdi
callq 0x45a0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)