// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %rbx
testl %r8d, %r8d
jle 0x62c3
movl %r8d, %edi
callq 0x377ba
movq %rax, %r14
testq %rax, %rax
jne 0x62c6
leaq 0xa6e80(%rip), %rsi     # 0xad12b
leaq 0xa6e87(%rip), %rdx     # 0xad139
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x12edc
jmp 0x62c6
xorl %r14d, %r14d
testb $0xf, %bpl
sete %al
testl %ebp, %ebp
setne %cl
testb %al, %cl
jne 0x65b8
leaq 0xab9de(%rip), %rsi     # 0xb1cbf
movq %rbx, %rdi
callq 0x9009
testq %rax, %rax
je 0x6328
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xae9c
testl %eax, %eax
jns 0x631c
leaq 0xa6e22(%rip), %rsi     # 0xad12b
leaq 0xa6e4b(%rip), %rdx     # 0xad15b
movl $0x4, %edi
xorl %eax, %eax
callq 0x12edc
movq %r12, %rdi
callq 0x99c4
testl %eax, %eax
je 0x634b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x633a
movq %r14, %rdi
callq 0x38228
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x8(%r12)
je 0x6328
movq %rbx, 0x8(%rsp)
movq 0x28(%r12), %rbp
movsbl (%rbp), %eax
xorl %ebx, %ebx
cmpl %eax, 0x10(%r15)
jne 0x63e4
movl $0x1, %ebx
movslq %ebx, %rax
cmpb $0xa, (%rbp,%rax)
jne 0x63a5
incl %ebx
cmpl 0x8(%r12), %ebx
jl 0x6397
movq %r12, %rdi
callq 0x99c4
testl %eax, %eax
jne 0x63c4
xorl %ebx, %ebx
cmpl $0x0, 0x8(%r12)
je 0x63c4
movslq %ebx, %rax
movsbl (%rbp,%rax), %eax
cmpl %eax, 0x10(%r15)
jne 0x63e4
incl %ebx
cmpl 0x8(%r12), %ebx
jl 0x636e
movq %r12, %rdi
callq 0x99c4
testl %eax, %eax
jne 0x63c4
xorl %ebx, %ebx
cmpl $0x0, 0x8(%r12)
jne 0x636e
testq %r14, %r14
je 0x63d1
movq %r14, %rdi
callq 0x38228
leaq 0xa6d9e(%rip), %rdi     # 0xad176
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x60aa
movq %r14, (%rsp)
leaq 0xab8d0(%rip), %rsi     # 0xb1cbf
movq %r13, 0x10(%rsp)
movq %r13, %rdi
callq 0x45c0
testq %rax, %rax
je 0x65d3
movq %rax, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x46b0
movq %rax, %r15
leaq 0xab1ab(%rip), %rdi     # 0xb15c7
leaq 0xaaabe(%rip), %rsi     # 0xb0ee1
callq 0x9009
testq %r15, %r15
je 0x6605
movq %rax, %r13
testq %rax, %rax
je 0x6630
movq %r13, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0xae9c
testl %eax, %eax
jns 0x6469
leaq 0xa6cd5(%rip), %rsi     # 0xad12b
leaq 0xa6d55(%rip), %rdx     # 0xad1b2
movl $0x4, %edi
xorl %eax, %eax
callq 0x12edc
movl $0x1, %esi
movl $0x7fff, %edx           # imm = 0x7FFF
movq %r15, %rdi
movq %r14, %rcx
callq 0x47c0
testq %rax, %rax
jle 0x64d1
cmpq $0x7ffe, %rax           # imm = 0x7FFE
ja 0x649b
cmpb $0xa, -0x1(%r15,%rax)
je 0x649b
movb $0xa, (%r15,%rax)
incq %rax
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xbc1f
testq %rax, %rax
jns 0x6469
cmpq $0x0, (%rsp)
je 0x64be
movq (%rsp), %rdi
callq 0x38228
movzwl (%r13), %esi
leaq 0xa6d12(%rip), %rdi     # 0xad1dc
xorl %eax, %eax
callq 0x615e
movq %r14, %rdi
callq 0x49b0
testl %eax, %eax
jne 0x664e
movq %r14, %rdi
callq 0x44c0
testl %eax, %eax
jne 0x6667
movl 0x8(%r12), %eax
subl %ebx, %eax
jle 0x6516
movslq %ebx, %rcx
addq %rcx, %rbp
movl %eax, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0xbc1f
testq %rax, %rax
js 0x66f1
movq %r13, %rdi
callq 0xb78d
testl %eax, %eax
js 0x64ae
movl $0x8000, %edx           # imm = 0x8000
movq %r12, %rdi
movq %r15, %rsi
callq 0xacb9
testq %rax, %rax
jle 0x6576
movq %rax, %r14
movq %r13, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xbf4b
movq %rax, %rbp
movslq %ebp, %rax
cmpq %r14, %rax
je 0x6522
cmpq $0x0, (%rsp)
je 0x6563
movq (%rsp), %rdi
callq 0x38228
leaq 0xa6ca9(%rip), %rdi     # 0xad213
movl %ebp, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x615e
js 0x6685
movq %r13, %rdi
callq 0xc008
testl %eax, %eax
js 0x66a8
movq %r12, %rdi
callq 0xc008
testl %eax, %eax
js 0x66c9
movq %r15, %rdi
callq 0x45d0
xorl %ebp, %ebp
movq (%rsp), %rdi
testq %rdi, %rdi
jne 0x6335
jmp 0x633a
testq %r14, %r14
je 0x65c5
movq %r14, %rdi
callq 0x38228
leaq 0xa6cb3(%rip), %rdi     # 0xad27f
xorl %eax, %eax
callq 0x60aa
cmpq $0x0, (%rsp)
je 0x65e3
movq (%rsp), %rdi
callq 0x38228
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0xb6c88(%rip), %rdi     # 0xbd27e
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x60aa
cmpq $0x0, (%rsp)
je 0x6615
movq (%rsp), %rdi
callq 0x38228
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0xa6f01(%rip), %rdi     # 0xad529
movq %rax, %rsi
jmp 0x63dd
cmpq $0x0, (%rsp)
je 0x6640
movq (%rsp), %rdi
callq 0x38228
leaq 0xa6b4f(%rip), %rdi     # 0xad196
xorl %eax, %eax
callq 0x615e
cmpq $0x0, (%rsp)
je 0x665e
movq (%rsp), %rdi
callq 0x38228
leaq 0xa6b86(%rip), %rdi     # 0xad1eb
jmp 0x667e
cmpq $0x0, (%rsp)
je 0x6677
movq (%rsp), %rdi
callq 0x38228
leaq 0xa6b81(%rip), %rdi     # 0xad1ff
movq 0x10(%rsp), %rsi
jmp 0x66a1
cmpq $0x0, (%rsp)
je 0x6695
movq (%rsp), %rdi
callq 0x38228
leaq 0xa6ba2(%rip), %rdi     # 0xad23e
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x615e
cmpq $0x0, (%rsp)
je 0x66b8
movq (%rsp), %rdi
callq 0x38228
movzwl (%r13), %esi
leaq 0xa6b8d(%rip), %rdi     # 0xad251
jmp 0x64ca
cmpq $0x0, (%rsp)
je 0x66d9
movq (%rsp), %rdi
callq 0x38228
movzwl (%r13), %esi
leaq 0xa6b84(%rip), %rdi     # 0xad269
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x615e
cmpq $0x0, (%rsp)
je 0x6701
movq (%rsp), %rdi
callq 0x38228
movzwl (%r12), %esi
jmp 0x64c3