// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rcx, %r15
movl %edx, %ebx
movq %rsi, %rbp
movq %rdi, %r13
testl %r8d, %r8d
jle 0x5ec2
movl %r8d, %edi
callq 0x2c7f7
movq %rax, %r14
testq %rax, %rax
jne 0x5ec5
leaq 0x7f27f(%rip), %rsi     # 0x8512b
leaq 0x7f286(%rip), %rdx     # 0x85139
xorl %r14d, %r14d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x1035a
jmp 0x5ec5
xorl %r14d, %r14d
testb $0xf, %bl
sete %al
testl %ebx, %ebx
setne %cl
testb %al, %cl
jne 0x61b6
leaq 0x83d46(%rip), %rsi     # 0x89c25
movq %r13, %rdi
callq 0x882a
testq %rax, %rax
je 0x5f24
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xa0d7
testl %eax, %eax
jns 0x5f18
leaq 0x7f224(%rip), %rsi     # 0x8512b
leaq 0x7f24d(%rip), %rdx     # 0x8515b
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x1035a
movq %r12, %rdi
callq 0x9018
testl %eax, %eax
je 0x5f45
pushq $-0x1
popq %rbx
testq %r14, %r14
je 0x5f34
movq %r14, %rdi
callq 0x2d1b7
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%r12), %eax
testl %eax, %eax
je 0x5f24
movq %r13, 0x10(%rsp)
movq 0x28(%r12), %rbx
movsbl (%rbx), %ecx
xorl %r13d, %r13d
cmpl %ecx, 0x10(%r15)
jne 0x5fde
pushq $0x1
popq %r13
movslq %r13d, %rcx
cmpb $0xa, (%rbx,%rcx)
jne 0x5f9e
incl %r13d
cmpl %eax, %r13d
jl 0x5f91
movq %r12, %rdi
callq 0x9018
testl %eax, %eax
jne 0x5fbe
movl 0x8(%r12), %eax
xorl %r13d, %r13d
testl %eax, %eax
je 0x5fbe
movslq %r13d, %rcx
movsbl (%rbx,%rcx), %ecx
cmpl %ecx, 0x10(%r15)
jne 0x5fde
incl %r13d
cmpl %eax, %r13d
jl 0x5f68
movq %r12, %rdi
callq 0x9018
testl %eax, %eax
jne 0x5fbe
movl 0x8(%r12), %eax
xorl %r13d, %r13d
testl %eax, %eax
jne 0x5f68
testq %r14, %r14
je 0x5fcb
movq %r14, %rdi
callq 0x2d1b7
leaq 0x7f1a4(%rip), %rdi     # 0x85176
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x5cb1
movq %r14, 0x8(%rsp)
leaq 0x83c3b(%rip), %rsi     # 0x89c25
movq %rbp, 0x18(%rsp)
movq %rbp, %rdi
callq 0x45c0
testq %rax, %rax
je 0x61d1
movq %rax, %r14
movl $0x8000, %edi           # imm = 0x8000
callq 0x46b0
movq %rax, %r15
leaq 0x8351e(%rip), %rdi     # 0x89535
leaq 0x82e23(%rip), %rsi     # 0x88e41
callq 0x882a
testq %r15, %r15
je 0x6205
movq %rax, %rbp
testq %rax, %rax
je 0x6232
movq %rbx, 0x20(%rsp)
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0xa0d7
testl %eax, %eax
jns 0x6068
leaq 0x7f0d4(%rip), %rsi     # 0x8512b
leaq 0x7f154(%rip), %rdx     # 0x851b2
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x1035a
pushq $0x1
popq %rbx
movl $0x7fff, %edx           # imm = 0x7FFF
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq 0x47c0
testq %rax, %rax
jle 0x60d2
cmpq $0x7ffe, %rax           # imm = 0x7FFE
ja 0x609b
cmpb $0xa, -0x1(%r15,%rax)
je 0x609b
movb $0xa, (%r15,%rax)
incq %rax
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xacf8
testq %rax, %rax
jns 0x606b
cmpq $0x0, 0x8(%rsp)
je 0x60c0
movq 0x8(%rsp), %rdi
callq 0x2d1b7
movzwl (%rbp), %esi
leaq 0x7f111(%rip), %rdi     # 0x851dc
xorl %eax, %eax
callq 0x5d63
movq %r14, %rdi
callq 0x49b0
testl %eax, %eax
jne 0x6252
movq %r14, %rdi
callq 0x44c0
testl %eax, %eax
movq 0x20(%rsp), %rsi
jne 0x626d
movl 0x8(%r12), %eax
subl %r13d, %eax
jle 0x611a
movslq %r13d, %rcx
addq %rcx, %rsi
movl %eax, %edx
movq %rbp, %rdi
callq 0xacf8
testq %rax, %rax
js 0x62fd
movq %rbp, %rdi
callq 0xa89d
testl %eax, %eax
js 0x60ae
movl $0x8000, %edx           # imm = 0x8000
movq %r12, %rdi
movq %r15, %rsi
callq 0x9fa8
testq %rax, %rax
jle 0x617c
movq %rax, %rbx
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xaf13
movq %rax, %r14
movslq %r14d, %rax
cmpq %rbx, %rax
je 0x6126
cmpq $0x0, 0x8(%rsp)
je 0x6169
movq 0x8(%rsp), %rdi
callq 0x2d1b7
leaq 0x7f0a3(%rip), %rdi     # 0x85213
movl %r14d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x5d63
js 0x628d
movq %rbp, %rdi
callq 0xaf2a
testl %eax, %eax
js 0x62b2
movq %r12, %rdi
callq 0xaf2a
testl %eax, %eax
js 0x62d4
movq %r15, %rdi
callq 0x45d0
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
jmp 0x5f27
testq %r14, %r14
je 0x61c3
movq %r14, %rdi
callq 0x2d1b7
leaq 0x7f0b5(%rip), %rdi     # 0x8527f
xorl %eax, %eax
callq 0x5cb1
cmpq $0x0, 0x8(%rsp)
je 0x61e3
movq 0x8(%rsp), %rdi
callq 0x2d1b7
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x8eea8(%rip), %rdi     # 0x9509e
movq 0x18(%rsp), %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5cb1
cmpq $0x0, 0x8(%rsp)
je 0x6217
movq 0x8(%rsp), %rdi
callq 0x2d1b7
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x7f2ff(%rip), %rdi     # 0x85529
movq %rax, %rsi
jmp 0x5fd7
cmpq $0x0, 0x8(%rsp)
je 0x6244
movq 0x8(%rsp), %rdi
callq 0x2d1b7
leaq 0x7ef4b(%rip), %rdi     # 0x85196
xorl %eax, %eax
callq 0x5d63
cmpq $0x0, 0x8(%rsp)
je 0x6264
movq 0x8(%rsp), %rdi
callq 0x2d1b7
leaq 0x7ef80(%rip), %rdi     # 0x851eb
jmp 0x6286
cmpq $0x0, 0x8(%rsp)
je 0x627f
movq 0x8(%rsp), %rdi
callq 0x2d1b7
leaq 0x7ef79(%rip), %rdi     # 0x851ff
movq 0x18(%rsp), %rsi
jmp 0x62ab
cmpq $0x0, 0x8(%rsp)
je 0x629f
movq 0x8(%rsp), %rdi
callq 0x2d1b7
leaq 0x7ef98(%rip), %rdi     # 0x8523e
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x5d63
cmpq $0x0, 0x8(%rsp)
je 0x62c4
movq 0x8(%rsp), %rdi
callq 0x2d1b7
movzwl (%rbp), %esi
leaq 0x7ef82(%rip), %rdi     # 0x85251
jmp 0x60cb
cmpq $0x0, 0x8(%rsp)
je 0x62e6
movq 0x8(%rsp), %rdi
callq 0x2d1b7
movzwl (%rbp), %esi
leaq 0x7ef78(%rip), %rdi     # 0x85269
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x5d63
cmpq $0x0, 0x8(%rsp)
je 0x630f
movq 0x8(%rsp), %rdi
callq 0x2d1b7
movzwl (%r12), %esi
jmp 0x60c4