// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/tabix.c
// Function: reheader_file
int reheader_file(const char *fname, const char *header, int ftype, tbx_conf_t *conf, int threads)
{
    hts_tpool *tpool = NULL;
    if (threads >= 1) {
        if (!(tpool = hts_tpool_init(threads))) {
            hts_log_info("Could not initialize thread pool!");
        }
    }
    if ( ftype & IS_TXT || !ftype )
    {
        BGZF *fp = bgzf_open(fname,"r");
        if (!fp) {
            RELEASE_TPOOL(tpool);
            return -1;
        }
        if (bgzf_thread_pool(fp, tpool, 0) < 0) {
            hts_log_info("Could not set thread pool!");
        }
        if (bgzf_read_block(fp) != 0 || !fp->block_length ) {
            RELEASE_TPOOL(tpool);
            return -1;
        }

        char *buffer = fp->uncompressed_block;
        int skip_until = 0;

        // Skip the header: find out the position of the data block
        if ( buffer[0]==conf->meta_char )
        {
            skip_until = 1;
            while (1)
            {
                if ( buffer[skip_until]=='\n' )
                {
                    skip_until++;
                    if ( skip_until>=fp->block_length )
                    {
                        if ( bgzf_read_block(fp) != 0 || !fp->block_length ) {
                            RELEASE_TPOOL(tpool);
                            error("FIXME: No body in the file: %s\n", fname);
                        }
                        skip_until = 0;
                    }
                    // The header has finished
                    if ( buffer[skip_until]!=conf->meta_char ) break;
                }
                skip_until++;
                if ( skip_until>=fp->block_length )
                {
                    if (bgzf_read_block(fp) != 0 || !fp->block_length) {
                        RELEASE_TPOOL(tpool);
                        error("FIXME: No body in the file: %s\n", fname);
                    }
                    skip_until = 0;
                }
            }
        }

        // Output the new header
        FILE *hdr  = fopen(header,"r");
        if ( !hdr ) {
            RELEASE_TPOOL(tpool);
            error("%s: %s", header,strerror(errno));
        }
        const size_t page_size = 32768;
        char *buf = malloc(page_size);
        BGZF *bgzf_out = bgzf_open("-", "w");
        ssize_t nread;

        if (!buf) {
            RELEASE_TPOOL(tpool);
            error("%s\n", strerror(errno));
        }
        if (!bgzf_out) {
            RELEASE_TPOOL(tpool);
            error_errno("Couldn't open output stream");
        }
        if (bgzf_thread_pool(bgzf_out, tpool, 0) < 0) {
            hts_log_info("Could not set thread pool to output file!");
        }
        while ( (nread=fread(buf,1,page_size-1,hdr))>0 )
        {
            if ( nread<page_size-1 && buf[nread-1]!='\n' ) buf[nread++] = '\n';
            if (bgzf_write(bgzf_out, buf, nread) < 0) {
                RELEASE_TPOOL(tpool);
                error_errno("Write error %d", bgzf_out->errcode);
            }
        }
        if ( ferror(hdr) ) {
            RELEASE_TPOOL(tpool);
            error_errno("Failed to read \"%s\"", header);
        }
        if ( fclose(hdr) ) {
            RELEASE_TPOOL(tpool);
            error_errno("Closing \"%s\" failed", header);
        }

        // Output all remaining data read with the header block
        if ( fp->block_length - skip_until > 0 )
        {
            if (bgzf_write(bgzf_out, buffer+skip_until, fp->block_length-skip_until) < 0) {
                RELEASE_TPOOL(tpool);
                error_errno("Write error %d",fp->errcode);
            }
        }
        if (bgzf_flush(bgzf_out) < 0) {
            RELEASE_TPOOL(tpool);
            error_errno("Write error %d", bgzf_out->errcode);
        }

        while (1)
        {
            nread = bgzf_raw_read(fp, buf, page_size);
            if ( nread<=0 ) break;

            int count = bgzf_raw_write(bgzf_out, buf, nread);
            if (count != nread) {
                RELEASE_TPOOL(tpool);
                error_errno("Write failed, wrote %d instead of %d bytes", count,(int)nread);
            }
        }
        if (nread < 0) {
            RELEASE_TPOOL(tpool);
            error_errno("Error reading \"%s\"", fname);
        }
        if (bgzf_close(bgzf_out) < 0) {
            RELEASE_TPOOL(tpool);
            error_errno("Error %d closing output", bgzf_out->errcode);
        }
        if (bgzf_close(fp) < 0) {
            RELEASE_TPOOL(tpool);
            error_errno("Error %d closing \"%s\"", bgzf_out->errcode, fname);
        }
        free(buf);
    }
    else {
        RELEASE_TPOOL(tpool);
        error("todo: reheader BCF, BAM\n");  // BCF is difficult, records contain pointers to the header.
    }
    RELEASE_TPOOL(tpool);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
cmpl $0x1, -0x2c(%rbp)
jl 0x5fea
movl -0x2c(%rbp), %edi
callq 0x4da30
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x5fe8
movl $0x4, %edi
leaq 0x103265(%rip), %rsi    # 0x10923f
leaq 0x10326c(%rip), %rdx    # 0x10924d
movb $0x0, %al
callq 0x18fa0
jmp 0x5fea
movl -0x1c(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x5fff
cmpl $0x0, -0x1c(%rbp)
jne 0x66b3
movq -0x10(%rbp), %rdi
leaq 0x1080c6(%rip), %rsi    # 0x10e0d0
callq 0xa000
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x603e
movq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x6032
movq -0x48(%rbp), %rdi
callq 0x4e900
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x670a
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0xcb90
cmpl $0x0, %eax
jge 0x606c
movl $0x4, %edi
leaq 0x1031e1(%rip), %rsi    # 0x10923f
leaq 0x10320a(%rip), %rdx    # 0x10926f
movb $0x0, %al
callq 0x18fa0
movq -0x40(%rbp), %rdi
callq 0xae20
cmpl $0x0, %eax
jne 0x6084
movq -0x40(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x60a8
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x609c
movq -0x50(%rbp), %rdi
callq 0x4e900
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x670a
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x61c3
movl $0x1, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x615e
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jl 0x6145
movq -0x40(%rbp), %rdi
callq 0xae20
cmpl $0x0, %eax
jne 0x6114
movq -0x40(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x613e
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x612c
movq -0x68(%rbp), %rdi
callq 0x4e900
movq -0x10(%rbp), %rsi
leaq 0x103153(%rip), %rdi    # 0x10928a
movb $0x0, %al
callq 0x5d70
movl $0x0, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x28(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x615c
jmp 0x61c1
jmp 0x615e
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jl 0x61bc
movq -0x40(%rbp), %rdi
callq 0xae20
cmpl $0x0, %eax
jne 0x618b
movq -0x40(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x61b5
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x61a3
movq -0x70(%rbp), %rdi
callq 0x4e900
movq -0x10(%rbp), %rsi
leaq 0x1030dc(%rip), %rdi    # 0x10928a
movb $0x0, %al
callq 0x5d70
movl $0x0, -0x5c(%rbp)
jmp 0x60d6
jmp 0x61c3
movq -0x18(%rbp), %rdi
leaq 0x107f02(%rip), %rsi    # 0x10e0d0
callq 0x45a0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x6225
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x61f6
movq -0x80(%rbp), %rdi
callq 0x4e900
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0x118(%rbp), %rsi
movq %rax, %rdx
leaq 0x10f71c(%rip), %rdi    # 0x11593a
movb $0x0, %al
callq 0x5d70
movq $0x8000, -0x88(%rbp) # imm = 0x8000
movl $0x8000, %edi           # imm = 0x8000
callq 0x4680
movq %rax, -0x90(%rbp)
leaq 0x107790(%rip), %rdi    # 0x10d9d8
leaq 0x10709d(%rip), %rsi    # 0x10d2ec
callq 0xa000
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x62a3
movq -0x38(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x6286
movq -0xa8(%rbp), %rdi
callq 0x4e900
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq %rax, %rsi
leaq 0x1033a1(%rip), %rdi    # 0x10963d
movb $0x0, %al
callq 0x5d70
cmpq $0x0, -0x98(%rbp)
jne 0x62dc
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x62ce
movq -0xb0(%rbp), %rdi
callq 0x4e900
leaq 0x102fd5(%rip), %rdi    # 0x1092aa
movb $0x0, %al
callq 0x5e40
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %edx, %edx
callq 0xcb90
cmpl $0x0, %eax
jge 0x630d
movl $0x4, %edi
leaq 0x102f40(%rip), %rsi    # 0x10923f
leaq 0x102fc0(%rip), %rdx    # 0x1092c6
movb $0x0, %al
callq 0x18fa0
jmp 0x630f
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rcx
movl $0x1, %esi
movl $0x7fff, %edx           # imm = 0x7FFF
callq 0x47a0
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
jle 0x63e5
cmpq $0x7fff, -0xa0(%rbp) # imm = 0x7FFF
jae 0x6382
movq -0x90(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x6382
movq -0x90(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0xa0(%rbp)
movb $0xa, (%rax,%rcx)
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0xdf20
cmpq $0x0, %rax
jge 0x63e0
movq -0x38(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x63c3
movq -0xb8(%rbp), %rdi
callq 0x4e900
movq -0x98(%rbp), %rax
movl (%rax), %esi
andl $0xffff, %esi           # imm = 0xFFFF
leaq 0x102f17(%rip), %rdi    # 0x1092f0
movb $0x0, %al
callq 0x5e40
jmp 0x630f
movq -0x78(%rbp), %rdi
callq 0x49a0
cmpl $0x0, %eax
je 0x6426
movq -0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x6414
movq -0xc0(%rbp), %rdi
callq 0x4e900
movq -0x18(%rbp), %rsi
leaq 0x102ee0(%rip), %rdi    # 0x1092ff
movb $0x0, %al
callq 0x5e40
movq -0x78(%rbp), %rdi
callq 0x44b0
cmpl $0x0, %eax
je 0x6467
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0x6455
movq -0xc8(%rbp), %rdi
callq 0x4e900
movq -0x18(%rbp), %rsi
leaq 0x102eb3(%rip), %rdi    # 0x109313
movb $0x0, %al
callq 0x5e40
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
subl -0x5c(%rbp), %eax
cmpl $0x0, %eax
jle 0x64dd
movq -0x98(%rbp), %rdi
movq -0x58(%rbp), %rsi
movslq -0x5c(%rbp), %rax
addq %rax, %rsi
movq -0x40(%rbp), %rax
movl 0x8(%rax), %eax
subl -0x5c(%rbp), %eax
movslq %eax, %rdx
callq 0xdf20
cmpq $0x0, %rax
jge 0x64db
movq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x64c1
movq -0xd0(%rbp), %rdi
callq 0x4e900
movq -0x40(%rbp), %rax
movl (%rax), %esi
andl $0xffff, %esi           # imm = 0xFFFF
leaq 0x102e1c(%rip), %rdi    # 0x1092f0
movb $0x0, %al
callq 0x5e40
jmp 0x64dd
movq -0x98(%rbp), %rdi
callq 0xd790
cmpl $0x0, %eax
jge 0x652c
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
je 0x650f
movq -0xd8(%rbp), %rdi
callq 0x4e900
movq -0x98(%rbp), %rax
movl (%rax), %esi
andl $0xffff, %esi           # imm = 0xFFFF
leaq 0x102dcb(%rip), %rdi    # 0x1092f0
movb $0x0, %al
callq 0x5e40
jmp 0x652e
movq -0x40(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0xc8f0
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jg 0x6556
jmp 0x65c9
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0xe2e0
movl %eax, -0xdc(%rbp)
movslq -0xdc(%rbp), %rax
cmpq -0xa0(%rbp), %rax
je 0x65c4
movq -0x38(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, -0xe8(%rbp)
je 0x65a7
movq -0xe8(%rbp), %rdi
callq 0x4e900
movl -0xdc(%rbp), %esi
movq -0xa0(%rbp), %rax
movl %eax, %edx
leaq 0x102d6a(%rip), %rdi    # 0x109327
movb $0x0, %al
callq 0x5e40
jmp 0x652e
cmpq $0x0, -0xa0(%rbp)
jge 0x6606
movq -0x38(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0x65f4
movq -0xf0(%rbp), %rdi
callq 0x4e900
movq -0x10(%rbp), %rsi
leaq 0x102d53(%rip), %rdi    # 0x109352
movb $0x0, %al
callq 0x5e40
movq -0x98(%rbp), %rdi
callq 0xe340
cmpl $0x0, %eax
jge 0x6655
movq -0x38(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xf8(%rbp)
je 0x6638
movq -0xf8(%rbp), %rdi
callq 0x4e900
movq -0x98(%rbp), %rax
movl (%rax), %esi
andl $0xffff, %esi           # imm = 0xFFFF
leaq 0x102d17(%rip), %rdi    # 0x109365
movb $0x0, %al
callq 0x5e40
movq -0x40(%rbp), %rdi
callq 0xe340
cmpl $0x0, %eax
jge 0x66a5
movq -0x38(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x6684
movq -0x100(%rbp), %rdi
callq 0x4e900
movq -0x98(%rbp), %rax
movl (%rax), %esi
andl $0xffff, %esi           # imm = 0xFFFF
movq -0x10(%rbp), %rdx
leaq 0x102cdf(%rip), %rdi    # 0x10937d
movb $0x0, %al
callq 0x5e40
movq -0x90(%rbp), %rdi
callq 0x45b0
jmp 0x66e2
movq -0x38(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, -0x108(%rbp)
je 0x66d4
movq -0x108(%rbp), %rdi
callq 0x4e900
leaq 0x102cb8(%rip), %rdi    # 0x109393
movb $0x0, %al
callq 0x5d70
movq -0x38(%rbp), %rax
movq %rax, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
je 0x6703
movq -0x110(%rbp), %rdi
callq 0x4e900
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)