// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/vcf.c
// Function: bcf_translate
int bcf_translate(const bcf_hdr_t *dst_hdr, bcf_hdr_t *src_hdr, bcf1_t *line)
{
    int i;
    if ( line->errcode )
    {
        char errordescription[1024] = "";
        hts_log_error("Unchecked error (%d %s) at %s:%"PRIhts_pos", exiting", line->errcode, bcf_strerror(line->errcode, errordescription, sizeof(errordescription)),  bcf_seqname_safe(src_hdr,line), line->pos+1);
        exit(1);
    }
    if ( src_hdr->ntransl==-1 ) return 0;    // no need to translate, all tags have the same id
    if ( !src_hdr->ntransl )  // called for the first time, see what needs translating
    {
        int dict;
        for (dict=0; dict<2; dict++)    // BCF_DT_ID and BCF_DT_CTG
        {
            src_hdr->transl[dict] = (int*) malloc(src_hdr->n[dict]*sizeof(int));
            for (i=0; i<src_hdr->n[dict]; i++)
            {
                if ( !src_hdr->id[dict][i].key ) // gap left after removed BCF header lines
                {
                    src_hdr->transl[dict][i] = -1;
                    continue;
                }
                src_hdr->transl[dict][i] = bcf_hdr_id2int(dst_hdr,dict,src_hdr->id[dict][i].key);
                if ( src_hdr->transl[dict][i]!=-1 && i!=src_hdr->transl[dict][i] ) src_hdr->ntransl++;
            }
        }
        if ( !src_hdr->ntransl )
        {
            free(src_hdr->transl[0]); src_hdr->transl[0] = NULL;
            free(src_hdr->transl[1]); src_hdr->transl[1] = NULL;
            src_hdr->ntransl = -1;
        }
        if ( src_hdr->ntransl==-1 ) return 0;
    }
    bcf_unpack(line,BCF_UN_ALL);

    // CHROM
    if ( src_hdr->transl[BCF_DT_CTG][line->rid] >=0 ) line->rid = src_hdr->transl[BCF_DT_CTG][line->rid];

    // FILTER
    for (i=0; i<line->d.n_flt; i++)
    {
        int src_id = line->d.flt[i];
        if ( src_hdr->transl[BCF_DT_ID][src_id] >=0 )
            line->d.flt[i] = src_hdr->transl[BCF_DT_ID][src_id];
        line->d.shared_dirty |= BCF1_DIRTY_FLT;
    }

    // INFO
    for (i=0; i<line->n_info; i++)
    {
        int src_id = line->d.info[i].key;
        int dst_id = src_hdr->transl[BCF_DT_ID][src_id];
        if ( dst_id<0 ) continue;
        line->d.info[i].key = dst_id;
        if ( !line->d.info[i].vptr ) continue;  // skip deleted
        int src_size = src_id>>7 ? ( src_id>>15 ? BCF_BT_INT32 : BCF_BT_INT16) : BCF_BT_INT8;
        int dst_size = dst_id>>7 ? ( dst_id>>15 ? BCF_BT_INT32 : BCF_BT_INT16) : BCF_BT_INT8;
        if ( src_size==dst_size )   // can overwrite
        {
            uint8_t *vptr = line->d.info[i].vptr - line->d.info[i].vptr_off;
            if ( dst_size==BCF_BT_INT8 ) { vptr[1] = (uint8_t)dst_id; }
            else if ( dst_size==BCF_BT_INT16 ) { *(uint16_t*)vptr = (uint16_t)dst_id; }
            else { *(uint32_t*)vptr = (uint32_t)dst_id; }
        }
        else    // must realloc
        {
            bcf_info_t *info = &line->d.info[i];
            kstring_t str = {0,0,0};
            bcf_enc_int1(&str, dst_id);
            bcf_enc_size(&str, info->len,info->type);
            uint32_t vptr_off = str.l;
            kputsn((char*)info->vptr, info->vptr_len, &str);
            if( info->vptr_free ) free(info->vptr - info->vptr_off);
            info->vptr_off = vptr_off;
            info->vptr = (uint8_t*)str.s + info->vptr_off;
            info->vptr_free = 1;
            line->d.shared_dirty |= BCF1_DIRTY_INF;
        }
    }

    // FORMAT
    for (i=0; i<line->n_fmt; i++)
    {
        int src_id = line->d.fmt[i].id;
        int dst_id = src_hdr->transl[BCF_DT_ID][src_id];
        if ( dst_id<0 ) continue;
        line->d.fmt[i].id = dst_id;
        if( !line->d.fmt[i].p ) continue;  // skip deleted
        int src_size = src_id>>7 ? ( src_id>>15 ? BCF_BT_INT32 : BCF_BT_INT16) : BCF_BT_INT8;
        int dst_size = dst_id>>7 ? ( dst_id>>15 ? BCF_BT_INT32 : BCF_BT_INT16) : BCF_BT_INT8;
        if ( src_size==dst_size )   // can overwrite
        {
            uint8_t *p = line->d.fmt[i].p - line->d.fmt[i].p_off;    // pointer to the vector size (4bits) and BT type (4bits)
            if ( dst_size==BCF_BT_INT8 ) { p[1] = dst_id; }
            else if ( dst_size==BCF_BT_INT16 ) { i16_to_le(dst_id, p + 1); }
            else { i32_to_le(dst_id, p + 1); }
        }
        else    // must realloc
        {
            bcf_fmt_t *fmt = &line->d.fmt[i];
            kstring_t str = {0,0,0};
            bcf_enc_int1(&str, dst_id);
            bcf_enc_size(&str, fmt->n, fmt->type);
            uint32_t p_off = str.l;
            kputsn((char*)fmt->p, fmt->p_len, &str);
            if( fmt->p_free ) free(fmt->p - fmt->p_off);
            fmt->p_off = p_off;
            fmt->p = (uint8_t*)str.s + fmt->p_off;
            fmt->p_free = 1;
            line->d.indiv_dirty = 1;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rdx, %r14
movq %rsi, %rcx
movq %rdi, 0x18(%rsp)
movl 0xcc(%rdx), %ebp
testl %ebp, %ebp
jne 0x2d4d0
movl 0x58(%rcx), %eax
cmpl $-0x1, %eax
je 0x2d193
testl %eax, %eax
movq %rcx, 0x8(%rsp)
jne 0x2d1a7
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %r14, 0x10(%rsp)
cmpq $0x2, %r12
je 0x2d167
movslq (%rcx,%r12,4), %r14
movq %r14, %rdi
shlq $0x2, %rdi
callq 0x46a0
movq 0x8(%rsp), %rcx
movq %rax, %r13
movq %rax, 0x60(%rcx,%r12,8)
xorl %ebx, %ebx
xorl %r15d, %r15d
movslq %r14d, %rax
cmpq %rax, %r15
jge 0x2d15a
movq 0x10(%rcx,%r12,8), %rax
movq (%rax,%rbx), %rdx
testq %rdx, %rdx
je 0x2d13b
movq 0x18(%rsp), %rdi
movl %r12d, %esi
callq 0x2489c
movl %eax, (%r13,%r15,4)
cmpl $-0x1, %eax
sete %cl
movl %eax, %eax
cmpq %rax, %r15
sete %al
orb %cl, %al
je 0x2d143
movq 0x8(%rsp), %rcx
jmp 0x2d151
orl $-0x1, (%r13,%r15,4)
jmp 0x2d151
incl %ebp
movq 0x8(%rsp), %rcx
movl %ebp, 0x58(%rcx)
movl (%rcx,%r12,4), %r14d
incq %r15
addq $0x10, %rbx
jmp 0x2d0fa
incq %r12
movq 0x10(%rsp), %r14
jmp 0x2d0ce
cmpl $-0x1, %ebp
je 0x2d193
testl %ebp, %ebp
jne 0x2d1a7
movq 0x60(%rcx), %rdi
movq %rcx, %rbx
callq 0x45c0
andq $0x0, 0x60(%rbx)
movq 0x68(%rbx), %rdi
callq 0x45c0
andq $0x0, 0x68(%rbx)
orl $-0x1, 0x58(%rbx)
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0xf
popq %rsi
movq %r14, %rdi
callq 0x2b198
movq 0x8(%rsp), %r10
movq 0x68(%r10), %rax
movslq 0x10(%r14), %rcx
movl (%rax,%rcx,4), %eax
testl %eax, %eax
js 0x2d1ca
movl %eax, 0x10(%r14)
xorl %eax, %eax
movslq 0x68(%r14), %rcx
cmpq %rcx, %rax
jge 0x2d1f8
movq 0x70(%r14), %rcx
movslq (%rcx,%rax,4), %rdx
movq 0x60(%r10), %rsi
movl (%rsi,%rdx,4), %edx
testl %edx, %edx
js 0x2d1eb
movl %edx, (%rcx,%rax,4)
orb $0x4, 0xb0(%r14)
incq %rax
jmp 0x2d1cc
pushq $0x20
popq %r12
xorl %r13d, %r13d
movl $0x8000, %r8d           # imm = 0x8000
pushq $0x3
popq %rbp
pushq $0x1
popq %r9
movq 0x18(%r14), %rax
movzwl %ax, %ecx
cmpq %rcx, %r13
jae 0x2d367
movq 0x90(%r14), %r15
movslq -0x20(%r15,%r12), %rdx
movq 0x60(%r10), %rax
movl (%rax,%rdx,4), %esi
testl %esi, %esi
js 0x2d356
movl %esi, -0x20(%r15,%r12)
movq -0x10(%r15,%r12), %rax
testq %rax, %rax
je 0x2d356
cmpl %r8d, %edx
movl %ebp, %edi
sbbl $0x0, %edi
cmpl %r8d, %esi
movl %ebp, %ecx
sbbl $0x0, %ecx
xorl %r8d, %r8d
movl $0x80, %r11d
cmpl %r11d, %esi
movl %r9d, %ebx
cmovbl %r9d, %ecx
setb %r8b
xorl %r9d, %r9d
cmpl %ecx, %edi
sete %r9b
cmpl %r11d, %edx
cmovael %r9d, %r8d
cmpb $0x1, %r8b
jne 0x2d2ba
movl -0x4(%r15,%r12), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
negq %rdx
cmpl $0x2, %ecx
je 0x2d349
cmpl $0x1, %ecx
movl %ebx, %r9d
movl $0x8000, %r8d           # imm = 0x8000
jne 0x2d362
movb %sil, 0x1(%rax,%rdx)
jmp 0x2d356
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x28086
movl -0x1c(%r15,%r12), %edx
movl (%r15,%r12), %esi
movq %rbx, %rdi
callq 0x27feb
movq 0x20(%rsp), %rbx
movq -0x10(%r15,%r12), %rdi
movl -0x8(%r15,%r12), %esi
leaq 0x20(%rsp), %rdx
callq 0x2787b
movl -0x4(%r15,%r12), %eax
testl %eax, %eax
jns 0x2d31a
movq -0x10(%r15,%r12), %rdi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rdi
callq 0x45c0
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq 0x30(%rsp), %rax
movq %rax, -0x10(%r15,%r12)
orl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, -0x4(%r15,%r12)
orb $0x8, 0xb0(%r14)
movq 0x8(%rsp), %r10
pushq $0x1
popq %r9
jmp 0x2d350
movw %si, (%rax,%rdx)
movl %ebx, %r9d
movl $0x8000, %r8d           # imm = 0x8000
incq %r13
addq $0x28, %r12
jmp 0x2d20c
movl %esi, (%rax,%rdx)
jmp 0x2d356
pushq $0x1c
popq %r13
xorl %r12d, %r12d
movl $0x8000, %ebx           # imm = 0x8000
shrq $0x20, %rax
movzbl %al, %eax
cmpq %rax, %r12
jae 0x2d193
movq 0x98(%r14), %r15
movslq -0x1c(%r15,%r13), %rdx
movq 0x60(%r10), %rax
movl (%rax,%rdx,4), %esi
testl %esi, %esi
js 0x2d4c0
movl %esi, -0x1c(%r15,%r13)
movq -0xc(%r15,%r13), %rax
testq %rax, %rax
je 0x2d4c0
cmpl %ebx, %edx
movl %ebp, %edi
sbbl $0x0, %edi
cmpl %ebx, %esi
movl %ebp, %ecx
sbbl $0x0, %ecx
xorl %r8d, %r8d
movl $0x80, %ebp
cmpl %ebp, %esi
movl %r9d, %r11d
cmovbl %r9d, %ecx
setb %r8b
xorl %r9d, %r9d
cmpl %ecx, %edi
sete %r9b
cmpl %ebp, %edx
cmovael %r9d, %r8d
cmpb $0x1, %r8b
jne 0x2d418
movl (%r15,%r13), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
negq %rdx
cmpl $0x2, %ecx
je 0x2d4af
cmpl $0x1, %ecx
pushq $0x3
popq %rbp
movl %r11d, %r9d
jne 0x2d4bc
movb %sil, 0x1(%rax,%rdx)
jmp 0x2d4c0
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x28086
movl -0x18(%r15,%r13), %esi
movl -0x10(%r15,%r13), %edx
movq %rbp, %rdi
callq 0x27feb
movq 0x20(%rsp), %rbx
movq -0xc(%r15,%r13), %rdi
movl -0x4(%r15,%r13), %esi
movq %rbp, %rdx
callq 0x2787b
movl (%r15,%r13), %eax
testl %eax, %eax
jns 0x2d476
movq -0xc(%r15,%r13), %rdi
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rdi
callq 0x45c0
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq 0x30(%rsp), %rax
movq %rax, -0xc(%r15,%r13)
orl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, (%r15,%r13)
movl $0x1, 0xb4(%r14)
movq 0x8(%rsp), %r10
pushq $0x1
popq %r9
movl $0x8000, %ebx           # imm = 0x8000
pushq $0x3
popq %rbp
jmp 0x2d4c0
movw %si, 0x1(%rax,%rdx)
pushq $0x3
popq %rbp
movl %r11d, %r9d
jmp 0x2d4c0
movl %esi, 0x1(%rax,%rdx)
incq %r12
movq 0x18(%r14), %rax
addq $0x20, %r13
jmp 0x2d373
leaq 0x20(%rsp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
movq %rcx, %rbx
callq 0x4250
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
movq %r15, %rsi
callq 0x277a1
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27650
movq (%r14), %rcx
incq %rcx
movq %rcx, (%rsp)
leaq 0x59874(%rip), %rsi     # 0x86d89
leaq 0x5987b(%rip), %rdx     # 0x86d97
pushq $0x1
popq %rbx
movl %ebx, %edi
movl %ebp, %ecx
movq %r15, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x8132
movl %ebx, %edi
callq 0x45f0