// File: /workspace/github_c_makefile_star2_O1/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
xorl %eax, %eax
xorl %r13d, %r13d
btl %eax, %ebp
adcl $0x0, %r13d
incl %eax
cmpl $0x4, %eax
jne 0x3c05d
cmpl %r13d, 0x58(%rdi)
jle 0x3c12b
cmpl $0x2, %r13d
movq %rdi, 0x8(%rsp)
jb 0x3c13a
leal -0x1(%r13), %edx
movq 0x28(%rdi), %r14
movl $0x1, %r9d
xorl %r8d, %r8d
movq %r14, %rbx
movq %r14, %rcx
movq %r14, %r12
movl %edx, 0x1c(%rsp)
movzbl (%r12), %eax
cmpl $0x2c, %eax
je 0x3c0b5
testl %eax, %eax
jne 0x3c120
jmp 0x3c143
btl %r9d, %ebp
jae 0x3c113
movq %r12, %r15
cmpq %rcx, %rbx
je 0x3c101
movq %r12, %r15
subq %rbx, %r15
movq %r14, %rdx
subq %rbx, %rdx
movq %rcx, %rdi
movq %rbx, %rsi
movq %rcx, 0x20(%rsp)
movl %r8d, 0x4(%rsp)
movl %r9d, 0x10(%rsp)
callq 0x34e0
movl 0x10(%rsp), %r9d
movl 0x4(%rsp), %r8d
movl 0x1c(%rsp), %edx
movq 0x8(%rsp), %rdi
addq 0x20(%rsp), %r15
incl %r8d
cmpl %edx, %r8d
jge 0x3c110
movb $0x2c, (%r15)
incq %r15
movq %r15, %rcx
cmpl %edx, %r8d
jge 0x3c140
incl %r9d
leaq 0x1(%r12), %rbx
incq %r12
incq %r14
jmp 0x3c0a2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rdi), %rcx
jmp 0x3c167
movb $0x0, (%rcx)
cmpl %edx, %r8d
jge 0x3c16a
subq %rbx, %r14
movq %rcx, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rcx, %rbx
callq 0x34e0
movq %rbx, %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, %rbx
callq 0x34e0
movq 0x8(%rsp), %rdi
subl 0x30(%rdi), %ebx
addl %ebx, 0x8(%rdi)
movslq 0x48(%rdi), %rax
testq %rax, %rax
jle 0x3c1c5
movq 0x50(%rdi), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %esi, %esi
cmpl $0x4754, (%rdx)         # imm = 0x4754
cmovel %esi, %ecx
incq %rsi
addq $0x10, %rdx
cmpq %rsi, %rax
jne 0x3c1a6
movslq %ecx, %rax
movq %rax, 0x10(%rsp)
jmp 0x3c1ce
movq $-0x1, 0x10(%rsp)
leal 0x1(%r13), %r15d
imull %r13d, %r15d
movl %r15d, %eax
shrl %eax
movl 0x58(%rdi), %ebx
cmpl %ebx, %eax
movl %ebx, %edi
movl %eax, 0x4(%rsp)
cmovgl %eax, %edi
shlq $0x2, %rdi
callq 0x3240
testl %ebx, %ebx
jle 0x3c257
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, %r14d
cmovael %esi, %r14d
testl %r11d, %r11d
cmovel %r10d, %r14d
testl %r14d, %r14d
jne 0x3c244
leal (%r9,%r11), %r14d
movslq %r8d, %r12
incl %r8d
movl %r14d, (%rax,%r12,4)
incl %r11d
cmpl %r11d, %edi
jne 0x3c21f
incl %edx
incl %edi
addl %r11d, %r9d
cmpl %ebx, %edx
jne 0x3c20c
movq 0x8(%rsp), %rbx
movl 0x5c(%rbx), %ecx
cmpl $0x0, 0x48(%rbx)
jle 0x3c312
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 0x3c2fd
addq %r8, %rdi
movl 0x4(%rsp), %r8d
movl %r8d, 0x4(%rdi)
testl %ecx, %ecx
jle 0x3c2fd
movq 0x8(%rdi), %rdi
movq 0x8(%rsp), %r8
movl 0x58(%r8), %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 0x3c2f5
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 0x3c2de
addl %r14d, %r10d
incq %r9
cmpq %rcx, %r9
jne 0x3c2c5
incq %rsi
movq 0x8(%rsp), %rbx
movslq 0x48(%rbx), %rdi
cmpq %rdi, %rsi
jl 0x3c275
movl $0x0, (%rax)
movslq 0x58(%rbx), %rdx
cmpq $0x2, %rdx
jl 0x3c34a
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 0x3c32f
testl %ecx, %ecx
movq 0x10(%rsp), %r10
jle 0x3c3a2
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 0x3c3c4
movl %edi, %r9d
andl $0x7, %r9d
movl (%rax,%r9,4), %r9d
testl %r9d, %r9d
js 0x3c3c4
andl $-0x40, %edi
shll $0x3, %r8d
orl %r9d, %edi
orl %r8d, %edi
movb %dil, (%rsi,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x3c359
movq %rax, %rdi
callq 0x3350
movl %r13d, 0x58(%rbx)
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3a436
leaq 0x8945(%rip), %rdi      # 0x44d10
leaq 0x8957(%rip), %rsi      # 0x44d29
leaq 0x895b(%rip), %rcx      # 0x44d34
movl $0xa8, %edx
callq 0x31b0