// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/bcfutils.c
// Function: bcf_fit_alt
void bcf_fit_alt(bcf1_t *b, int mask)
{
    mask |= 1; // REF must be always present

    int i,j,nals=0;
    for (i=0; i<sizeof(int); i++)
        if ( mask&1<<i) nals++;
    if ( b->n_alleles <= nals ) return;

    // update ALT, in principle any of the alleles can be removed
    char *p;
    if ( nals>1 ) 
    {
        char *dst, *src;
        int n=0, nalts=nals-1;
        for (src=dst=p=b->alt, i=1; *p; p++)
        {
            if ( *p!=',' ) continue;

            if ( mask&1<<i )
            {
                n++;
                if ( src!=dst )
                {
                    memmove(dst,src,p-src);
                    dst += p-src;
                }
                else dst = p;
                if ( n<nalts ) { *dst=','; dst++; }
            }
            i++;

            if ( n>=nalts ) { *dst=0; break; }
            src = p+1;
        }
        if ( n<nalts )
        {
            memmove(dst,src,p-src);
            dst += p-src;
            *dst = 0;
        }
        p = dst;
    }
    else p = b->alt, *p = '\0';
    p++;
    memmove(p, b->flt, b->str + b->l_str - b->flt);
    b->l_str -= b->flt - p;

    // update PL and GT
    int ipl=-1, igt=-1;
    for (i = 0; i < b->n_gi; ++i) 
    {
        bcf_ginfo_t *g = b->gi + i;
        if (g->fmt == bcf_str2int("PL", 2)) ipl = i;
        if (g->fmt == bcf_str2int("GT", 2)) igt = i;
    }

    // .. create mapping between old and new indexes
    int npl = nals * (nals+1) / 2;
    int *map = malloc(sizeof(int)*(npl>b->n_alleles ? npl : b->n_alleles));
    int kori=0,knew=0;
    for (i=0; i<b->n_alleles; i++)
    {
        for (j=0; j<=i; j++)
        {
            int skip=0;
            if ( i && !(mask&1<<i) ) skip=1;
            if ( j && !(mask&1<<j) ) skip=1;
            if ( !skip ) { map[knew++] = kori; }
            kori++;
        }
    }
    // .. apply to all samples
    int n_smpl = b->n_smpl;
    for (i = 0; i < b->n_gi; ++i) 
    {
        bcf_ginfo_t *g = b->gi + i;
        if (g->fmt == bcf_str2int("PL", 2)) 
        {
            g->len = npl;
            uint8_t *d = (uint8_t*)g->data;
            int ismpl, npl_ori = b->n_alleles * (b->n_alleles + 1) / 2;
            for (knew=ismpl=0; ismpl<n_smpl; ismpl++)
            {
                uint8_t *dl = d + ismpl * npl_ori;
                for (j=0; j<npl; j++) d[knew++] = dl[map[j]];
            }
        } // FIXME: to add GL
    }
    // update GTs
    map[0] = 0;
    for (i=1, knew=0; i<b->n_alleles; i++)
        map[i] = mask&1<<i ? ++knew : -1;
    for (i=0; i<n_smpl; i++)
    {
        uint8_t gt = ((uint8_t*)b->gi[igt].data)[i];
        int a1 = (gt>>3)&7;
        int a2 = gt&7;
        assert( map[a1]>=0 && map[a2]>=0 );
        ((uint8_t*)b->gi[igt].data)[i] = ((1<<7|1<<6)&gt) | map[a1]<<3 | map[a2];
    }
    free(map);
    b->n_alleles = nals;
    bcf_sync(b);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebp
orl $0x1, %ebp
movd %ebp, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqa %xmm0, %xmm1
psrld $0x3, %xmm1
movdqa %xmm0, %xmm2
psrld $0x2, %xmm2
punpckhqdq %xmm1, %xmm2    # xmm2 = xmm2[1],xmm1[1]
movdqa %xmm0, %xmm1
psrld $0x1, %xmm1
punpcklqdq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0]
shufps $0xcc, %xmm2, %xmm0     # xmm0 = xmm0[0,3],xmm2[0,3]
andps 0x9563(%rip), %xmm0     # 0x45de0
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %ebx
cmpl %ebx, 0x58(%rdi)
jle 0x3c94b
cmpl $0x2, %ebx
movq %rdi, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
jb 0x3c95a
leal -0x1(%rbx), %edx
movq 0x28(%rdi), %r14
movl $0x1, %r13d
xorl %r8d, %r8d
movq %r14, %rsi
movq %r14, %rcx
movq %r14, %r12
movl %edx, 0x1c(%rsp)
movzbl (%r12), %eax
cmpl $0x2c, %eax
je 0x3c8df
testl %eax, %eax
jne 0x3c943
jmp 0x3c963
btl %r13d, %ebp
jae 0x3c936
movq %r12, %r15
cmpq %rcx, %rsi
je 0x3c924
movq %r12, %r15
subq %rsi, %r15
movq %r14, %rdx
subq %rsi, %rdx
movq %rcx, %rdi
movq %rsi, %rbx
movq %rcx, 0x10(%rsp)
movl %r8d, 0x4(%rsp)
callq 0x34e0
movl 0x4(%rsp), %r8d
movq %rbx, %rsi
movl 0x1c(%rsp), %edx
movq 0x8(%rsp), %rdi
addq 0x10(%rsp), %r15
incl %r8d
cmpl %edx, %r8d
jge 0x3c933
movb $0x2c, (%r15)
incq %r15
movq %r15, %rcx
cmpl %edx, %r8d
jge 0x3c960
incl %r13d
leaq 0x1(%r12), %rsi
incq %r12
incq %r14
jmp 0x3c8cc
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rdi), %rcx
jmp 0x3c989
movb $0x0, (%rcx)
cmpl %edx, %r8d
movq 0x20(%rsp), %rbx
jge 0x3c98c
subq %rsi, %r14
movq %rcx, %rdi
movq %r14, %rdx
movq %rcx, %r15
callq 0x34e0
movq %r15, %rcx
movq 0x8(%rsp), %rdi
addq %r14, %rcx
movb $0x0, (%rcx)
incq %rcx
movq 0x30(%rdi), %rsi
movslq 0x8(%rdi), %rdx
addq 0x18(%rdi), %rdx
subq %rsi, %rdx
movq %rcx, %rdi
movq %rcx, %r14
callq 0x34e0
movq 0x8(%rsp), %rsi
subl 0x30(%rsi), %r14d
addl %r14d, 0x8(%rsi)
movslq 0x48(%rsi), %r13
testq %r13, %r13
jle 0x3c9e8
movq 0x50(%rsi), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edx, %edx
cmpl $0x4754, (%rcx)         # imm = 0x4754
cmovel %edx, %eax
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %r13
jne 0x3c9ca
cltq
movq %rax, 0x10(%rsp)
jmp 0x3c9f1
movq $-0x1, 0x10(%rsp)
leal 0x1(%rbx), %r15d
imull %ebx, %r15d
movl %r15d, %eax
shrl %eax
movl 0x58(%rsi), %r14d
cmpl %r14d, %eax
movl %r14d, %edi
movl %eax, 0x4(%rsp)
cmovgl %eax, %edi
shlq $0x2, %rdi
callq 0x3240
testl %r14d, %r14d
jle 0x3ca7d
movl %ebp, %ecx
notl %ecx
xorl %edx, %edx
movl $0x1, %esi
movl $0x1, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
btl %edx, %ecx
setb %r10b
testl %edx, %edx
cmovel %edx, %r10d
xorl %r11d, %r11d
btl %r11d, %ebp
movl %r10d, %ebx
cmovael %esi, %ebx
testl %r11d, %r11d
cmovel %r10d, %ebx
testl %ebx, %ebx
jne 0x3ca69
leal (%r9,%r11), %ebx
movslq %r8d, %r12
incl %r8d
movl %ebx, (%rax,%r12,4)
incl %r11d
cmpl %r11d, %edi
jne 0x3ca46
incl %edx
incl %edi
addl %r11d, %r9d
cmpl %r14d, %edx
jne 0x3ca33
movq 0x8(%rsp), %rbx
movl 0x5c(%rbx), %ecx
testl %r13d, %r13d
jle 0x3cb39
movl 0x4(%rsp), %edx
cmpl $0x1, %edx
adcl $0x0, %edx
xorl %esi, %esi
movq 0x50(%rbx), %rdi
movq %rsi, %r8
shlq $0x4, %r8
cmpl $0x504c, (%rdi,%r8)     # imm = 0x504C
jne 0x3cb26
addq %r8, %rdi
movl 0x4(%rsp), %r8d
movl %r8d, 0x4(%rdi)
testl %ecx, %ecx
jle 0x3cb26
movq 0x8(%rdi), %rdi
movl 0x58(%rbx), %r8d
leal 0x1(%r8), %r9d
imull %r8d, %r9d
movl %r9d, %r8d
shrl $0x1f, %r8d
addl %r9d, %r8d
sarl %r8d
movslq %r8d, %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl $0x2, %r15d
jb 0x3cb15
movq %r9, %r11
imulq %r8, %r11
addq %rdi, %r11
movslq %r10d, %rbx
addq %rdi, %rbx
xorl %r14d, %r14d
movslq (%rax,%r14,4), %r12
movb (%r11,%r12), %r12b
movb %r12b, (%rbx,%r14)
incq %r14
cmpq %r14, %rdx
jne 0x3cafe
addl %r14d, %r10d
incq %r9
cmpq %rcx, %r9
jne 0x3cae5
movq 0x8(%rsp), %rbx
movl 0x48(%rbx), %r13d
incq %rsi
movslq %r13d, %rdi
cmpq %rdi, %rsi
jl 0x3ca9a
movl 0x58(%rbx), %r14d
movl $0x0, (%rax)
cmpl $0x2, %r14d
jl 0x3cb70
movl %r14d, %edx
xorl %esi, %esi
movl $0x1, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leal 0x1(%rsi), %r9d
btl %edi, %ebp
cmovbl %r9d, %esi
cmovael %r8d, %r9d
movl %r9d, (%rax,%rdi,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x3cb55
testl %ecx, %ecx
movq 0x10(%rsp), %r10
jle 0x3cbc8
xorl %edx, %edx
shlq $0x4, %r10
movq 0x50(%rbx), %rsi
movq 0x8(%rsi,%r10), %rsi
movzbl (%rsi,%rdx), %edi
movl %edi, %r8d
shrl %r8d
andl $0x1c, %r8d
movl (%rax,%r8), %r8d
testl %r8d, %r8d
js 0x3cbee
movl %edi, %r9d
andl $0x7, %r9d
movl (%rax,%r9,4), %r9d
testl %r9d, %r9d
js 0x3cbee
andl $-0x40, %edi
shll $0x3, %r8d
orl %r9d, %edi
orl %r8d, %edi
movb %dil, (%rsi,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x3cb7f
movq %rax, %rdi
callq 0x3350
movq 0x20(%rsp), %rax
movl %eax, 0x58(%rbx)
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3ab86
leaq 0x922b(%rip), %rdi      # 0x45e20
leaq 0x923d(%rip), %rsi      # 0x45e39
leaq 0x9241(%rip), %rcx      # 0x45e44
movl $0xa8, %edx
callq 0x31b0