// File: /workspace/github_c_makefile_star2_O3/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 0x62d2
movl %r8d, %edi
callq 0x36481
movq %rax, %r14
testq %rax, %rax
jne 0x62d5
leaq 0xa4e71(%rip), %rsi     # 0xab12b
leaq 0xa4e78(%rip), %rdx     # 0xab139
xorl %r14d, %r14d
movl $0x4, %edi
xorl %eax, %eax
callq 0x1295d
jmp 0x62d5
xorl %r14d, %r14d
testb $0xf, %bpl
sete %al
testl %ebp, %ebp
setne %cl
testb %al, %cl
jne 0x65c0
leaq 0xa99da(%rip), %rsi     # 0xafcca
movq %rbx, %rdi
callq 0x90c8
testq %rax, %rax
je 0x6337
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xade4
testl %eax, %eax
jns 0x632b
leaq 0xa4e13(%rip), %rsi     # 0xab12b
leaq 0xa4e3c(%rip), %rdx     # 0xab15b
movl $0x4, %edi
xorl %eax, %eax
callq 0x1295d
movq %r12, %rdi
callq 0x99d1
testl %eax, %eax
je 0x635a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x6349
movq %r14, %rdi
callq 0x36eb3
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%r12), %eax
testl %eax, %eax
je 0x6337
movq %r13, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x28(%r12), %r13
movsbl (%r13), %ecx
xorl %ebx, %ebx
cmpl %ecx, 0x10(%r15)
jne 0x63f8
movl $0x1, %ebx
movslq %ebx, %rcx
cmpb $0xa, (%r13,%rcx)
jne 0x63bb
incl %ebx
cmpl %eax, %ebx
jl 0x63ac
movq %r12, %rdi
callq 0x99d1
testl %eax, %eax
jne 0x63d8
movl 0x8(%r12), %eax
xorl %ebx, %ebx
testl %eax, %eax
je 0x63d8
movslq %ebx, %rcx
movsbl (%r13,%rcx), %ecx
cmpl %ecx, 0x10(%r15)
jne 0x63f8
incl %ebx
cmpl %eax, %ebx
jl 0x6384
movq %r12, %rdi
callq 0x99d1
testl %eax, %eax
jne 0x63d8
movl 0x8(%r12), %eax
xorl %ebx, %ebx
testl %eax, %eax
jne 0x6384
testq %r14, %r14
je 0x63e5
movq %r14, %rdi
callq 0x36eb3
leaq 0xa4d8a(%rip), %rdi     # 0xab176
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x60b9
movq %r14, (%rsp)
leaq 0xa98c7(%rip), %rsi     # 0xafcca
movq 0x10(%rsp), %rdi
callq 0x45c0
testq %rax, %rax
je 0x65db
movq %rax, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x46b0
movq %rax, %r15
leaq 0xa91ad(%rip), %rdi     # 0xaf5da
leaq 0xa8abd(%rip), %rsi     # 0xaeef1
callq 0x90c8
testq %r15, %r15
je 0x660d
movq %rax, %rbp
testq %rax, %rax
je 0x6638
movq %rbp, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0xade4
testl %eax, %eax
jns 0x647a
leaq 0xa4cc4(%rip), %rsi     # 0xab12b
leaq 0xa4d44(%rip), %rdx     # 0xab1b2
movl $0x4, %edi
xorl %eax, %eax
callq 0x1295d
movl $0x1, %esi
movl $0x7fff, %edx           # imm = 0x7FFF
movq %r15, %rdi
movq %r14, %rcx
callq 0x47c0
testq %rax, %rax
jle 0x64e1
cmpq $0x7ffe, %rax           # imm = 0x7FFE
ja 0x64ac
cmpb $0xa, -0x1(%r15,%rax)
je 0x64ac
movb $0xa, (%r15,%rax)
incq %rax
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xbb7d
testq %rax, %rax
jns 0x647a
cmpq $0x0, (%rsp)
je 0x64cf
movq (%rsp), %rdi
callq 0x36eb3
movzwl (%rbp), %esi
leaq 0xa4d02(%rip), %rdi     # 0xab1dc
xorl %eax, %eax
callq 0x616d
movq %r14, %rdi
callq 0x49b0
testl %eax, %eax
jne 0x6656
movq %r14, %rdi
callq 0x44c0
testl %eax, %eax
jne 0x666f
movl 0x8(%r12), %eax
subl %ebx, %eax
jle 0x6526
movslq %ebx, %rcx
addq %rcx, %r13
movl %eax, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0xbb7d
testq %rax, %rax
js 0x66f3
movq %rbp, %rdi
callq 0xb6fd
testl %eax, %eax
js 0x64bf
movl $0x8000, %edx           # imm = 0x8000
movq %r12, %rdi
movq %r15, %rsi
callq 0xac01
testq %rax, %rax
jle 0x6587
movq %rax, %r14
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xbe93
movq %rax, %r13
movslq %r13d, %rax
cmpq %r14, %rax
je 0x6532
cmpq $0x0, (%rsp)
je 0x6573
movq (%rsp), %rdi
callq 0x36eb3
leaq 0xa4c99(%rip), %rdi     # 0xab213
movl %r13d, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x616d
js 0x668d
movq %rbp, %rdi
callq 0xbf45
testl %eax, %eax
movq (%rsp), %r14
js 0x66b0
movq %r12, %rdi
callq 0xbf45
testl %eax, %eax
js 0x66ce
movq %r15, %rdi
callq 0x45d0
xorl %ebp, %ebp
jmp 0x633c
testq %r14, %r14
je 0x65cd
movq %r14, %rdi
callq 0x36eb3
leaq 0xa4cab(%rip), %rdi     # 0xab27f
xorl %eax, %eax
callq 0x60b9
cmpq $0x0, (%rsp)
je 0x65eb
movq (%rsp), %rdi
callq 0x36eb3
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0xb53f0(%rip), %rdi     # 0xbb9ee
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x60b9
cmpq $0x0, (%rsp)
je 0x661d
movq (%rsp), %rdi
callq 0x36eb3
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0xa4ef9(%rip), %rdi     # 0xab529
movq %rax, %rsi
jmp 0x63f1
cmpq $0x0, (%rsp)
je 0x6648
movq (%rsp), %rdi
callq 0x36eb3
leaq 0xa4b47(%rip), %rdi     # 0xab196
xorl %eax, %eax
callq 0x616d
cmpq $0x0, (%rsp)
je 0x6666
movq (%rsp), %rdi
callq 0x36eb3
leaq 0xa4b7e(%rip), %rdi     # 0xab1eb
jmp 0x6686
cmpq $0x0, (%rsp)
je 0x667f
movq (%rsp), %rdi
callq 0x36eb3
leaq 0xa4b79(%rip), %rdi     # 0xab1ff
movq 0x10(%rsp), %rsi
jmp 0x66a9
cmpq $0x0, (%rsp)
je 0x669d
movq (%rsp), %rdi
callq 0x36eb3
leaq 0xa4b9a(%rip), %rdi     # 0xab23e
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x616d
testq %r14, %r14
je 0x66be
movq (%rsp), %rdi
callq 0x36eb3
movzwl (%rbp), %esi
leaq 0xa4b88(%rip), %rdi     # 0xab251
jmp 0x64da
testq %r14, %r14
je 0x66dc
movq (%rsp), %rdi
callq 0x36eb3
movzwl (%rbp), %esi
leaq 0xa4b82(%rip), %rdi     # 0xab269
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x616d
cmpq $0x0, (%rsp)
je 0x6703
movq (%rsp), %rdi
callq 0x36eb3
movzwl (%r12), %esi
jmp 0x64d3