// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/atomic.c
// Function: bcf_atomize
void bcf_atomize(const bcf_hdr_t *h, bcf1_t *b, bcf_atom_v *a)
{
	int i, cid, l_ref, l_cigar = 0;
	kstring_t cigar = {0,0,0};
	char *p_cigar = 0;

	for (i = 0; i < a->n; ++i)
		a->a[i].from_new = 0;

	cid = bcf_id2int(h, BCF_DT_ID, "CIGAR");
	if (cid >= 0) {
		bcf_unpack(b, BCF_UN_INFO);
		for (i = 0; i < b->n_info; ++i)
			if (b->d.info[i].key == cid) break;
		if (i < b->n_info && b->d.info[i].type == BCF_BT_CHAR) { // CIGAR in INFO
			p_cigar = (char*)b->d.info[i].vptr;
			l_cigar = b->d.info[i].len;
		}
	} else bcf_unpack(b, BCF_UN_STR);
	l_ref = strlen(b->d.allele[0]);

	for (i = 1; i < b->n_allele; ++i) { // traverse each allele
		char *p;
		int x, y, j, l, l_alt;
		l_alt = strlen(b->d.allele[i]);
		if (b->rlen != l_ref || (b->d.allele[i][0] == '<' && b->d.allele[i][l_alt-1] == '>')) { // symbolic allele
			bcf_add_atom(a, b->rid, b->pos, b->rlen, i, -1, b->d.allele[0], -1, b->d.allele[i]);
			continue;
		}
		// extract or generate CIGAR between REF and the current ALT
		cigar.l = 0;
		if (p_cigar) {
			for (p = p_cigar; p < p_cigar + l_cigar && *p != ','; ++p);
			assert(p - p_cigar > 0); // otherwise, incomplete CIGAR
			kputsn(p_cigar, p - p_cigar, &cigar);
			l_cigar -= p + 1 - p_cigar;
			p_cigar = p + 1;
		} else if (strlen(b->d.allele[i]) == b->rlen) {
			kputw(b->rlen, &cigar);
			kputc('M', &cigar);
		} else {
			int rest;
			l = l_alt - b->rlen;
			kputsn("1M", 2, &cigar);
			if (l > 0) {
				kputw(l, &cigar);
				kputc('I', &cigar);
				rest = b->rlen - 1;
			} else {
				kputw(-l, &cigar);
				kputc('D', &cigar);
				rest = l_alt - 1;
			}
			if (rest) {
				kputw(rest, &cigar);
				kputc('M', &cigar);
			}
		}
		// extract difference
		x = y = 0;
		for (p = cigar.s; *p; ++p) {
			l = strtol(p, &p, 10);
			if (*p == 'M' || *p == '=' || *p == 'X') {
				for (j = 0; j < l; ++j)
					if (b->d.allele[0][x+j] != b->d.allele[i][y+j])
						bcf_add_atom(a, b->rid, b->pos + x + j, 1, i, 1, &b->d.allele[0][x+j], 1, &b->d.allele[i][y+j]);
				x += l, y += l;
			} else if (*p == 'I') {
				if (x == 0 || y == 0)
					fprintf(stderr, "[W::%s] invalid insertion (%d,%d) at %s:%d\n", __func__, x, y, h->id[BCF_DT_CTG][b->rid].key, b->pos + 1);
				else bcf_add_atom(a, b->rid, b->pos + x - 1, 1, i, 1, &b->d.allele[0][x-1], l+1, &b->d.allele[i][y-1]);
				y += l;
			} else if (*p == 'D') {
				assert(x > 0 && y > 0);
				bcf_add_atom(a, b->rid, b->pos + x - 1, l+1, i, l+1, &b->d.allele[0][x-1], 1, &b->d.allele[i][y-1]);
				x += l;
			}
		}
	}
	free(cigar.s);
	a->n = bcf_atom_gen_at(h, b, a->n, a->a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%rsp)
pushq $0x30
popq %rax
xorl %ecx, %ecx
movslq (%rbx), %rdx
cmpq %rdx, %rcx
jge 0xe098
movq 0x8(%rbx), %rdx
andl $0xbfffffff, (%rdx,%rax) # imm = 0xBFFFFFFF
incq %rcx
addq $0x40, %rax
jmp 0xe07c
leaq 0xb383(%rip), %rdx      # 0x19422
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0xcee5
testl %eax, %eax
movq %rbx, 0x28(%rsp)
js 0xe0e0
movl %eax, %ebp
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0xc692
movzwl 0x10(%r14), %eax
imulq $0x18, %rax, %rcx
xorl %eax, %eax
cmpq %rax, %rcx
je 0xe10d
movq 0x78(%r14), %rdx
cmpl %ebp, (%rdx,%rax)
je 0xe0f6
addq $0x18, %rax
jmp 0xe0cc
movq $0x0, 0x10(%rsp)
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xc692
jmp 0xe116
cmpl $0x7, 0x4(%rdx,%rax)
jne 0xe10d
movq 0x10(%rdx,%rax), %rbx
movl 0x8(%rdx,%rax), %eax
movq %rax, 0x10(%rsp)
jmp 0xe118
movq $0x0, 0x10(%rsp)
xorl %ebx, %ebx
movq 0x68(%r14), %rax
movq (%rax), %rdi
callq 0x2170
movq %rax, 0x38(%rsp)
cltq
movq %rax, 0x50(%rsp)
pushq $0x1
popq %r13
movq $0x0, 0x48(%rsp)
movzwl 0x12(%r14), %eax
cmpq %rax, %r13
jae 0xe4a5
movq %rbx, 0x18(%rsp)
movq 0x68(%r14), %rbx
movq (%rbx,%r13,8), %rbp
movq %rbp, %rdi
callq 0x2170
movl 0x8(%r14), %ecx
cmpl 0x38(%rsp), %ecx
jne 0xe180
movq %rax, %r15
cmpb $0x3c, (%rbp)
jne 0xe1af
leal -0x1(%r15), %eax
cltq
cmpb $0x3e, (%rbp,%rax)
jne 0xe1af
movl (%r14), %esi
movl 0x4(%r14), %edx
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movl %r13d, %r8d
pushq $-0x1
popq %r9
pushq %rbp
pushq $-0x1
pushq (%rbx)
callq 0xe516
addq $0x20, %rsp
incq %r13
movq 0x18(%rsp), %rbx
jmp 0xe13d
andq $0x0, 0x60(%rsp)
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
leaq 0x60(%rsp), %r12
je 0xe214
movslq 0x10(%rsp), %rax
addq %rdi, %rax
xorl %r15d, %r15d
movq %rdi, %rbx
movq %rbx, 0x40(%rsp)
cmpq %rax, %rbx
jae 0xe1e9
cmpb $0x2c, (%rbx)
je 0xe1e9
incq %rbx
incq %r15
jmp 0xe1d2
testq %r15, %r15
jle 0xe4f7
movl %r15d, %esi
movq %r12, %rdx
callq 0xe64b
incq %rbx
notl %r15d
addl 0x10(%rsp), %r15d
movq %r15, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
jmp 0xe28f
cmpq 0x50(%rsp), %r15
jne 0xe222
movq 0x38(%rsp), %rdi
jmp 0xe273
leaq 0xb254(%rip), %rdi      # 0x1947d
pushq $0x2
popq %rsi
movq %r12, %rdx
callq 0xe64b
movl %r15d, %edi
subl 0x38(%rsp), %edi
jle 0xe256
movq %r12, %rsi
callq 0xe6df
pushq $0x49
popq %rdi
movq %r12, %rsi
callq 0xe7ba
movl 0x8(%r14), %r15d
jmp 0xe26b
negl %edi
movq %r12, %rsi
callq 0xe6df
pushq $0x44
popq %rdi
movq %r12, %rsi
callq 0xe7ba
decl %r15d
je 0xe286
movl %r15d, %edi
movq %r12, %rsi
callq 0xe6df
pushq $0x4d
popq %rdi
movq %r12, %rsi
callq 0xe7ba
movq $0x0, 0x18(%rsp)
movq 0x70(%rsp), %rbp
xorl %r15d, %r15d
movq %rbp, 0x48(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x40(%rsp)
cmpb $0x0, (%rbp)
je 0xe1a5
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x2270
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rbp
movzbl (%rbp), %eax
movl %ebx, 0xc(%rsp)
movslq %ebx, %rbx
movslq %r15d, %r12
cmpl $0x3d, %eax
je 0xe2fa
cmpl $0x44, %eax
je 0xe37b
cmpl $0x49, %eax
je 0xe3e4
cmpl $0x58, %eax
je 0xe2fa
cmpl $0x4d, %eax
jne 0xe446
movq %r15, 0x58(%rsp)
movq 0x20(%rsp), %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
subq $0x1, %r15
jb 0xe363
movq 0x68(%r14), %rcx
movq (%rcx), %rax
movb (%rax,%r12), %dl
movq (%rcx,%r13,8), %r10
cmpb (%r10,%rbx), %dl
je 0xe35b
addq %r12, %rax
addq %rbx, %r10
movl (%r14), %esi
movl 0x4(%r14), %edx
addl %r12d, %edx
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movl %r13d, %r8d
movl %ecx, %r9d
pushq %r10
pushq $0x1
pushq %rax
callq 0xe516
addq $0x20, %rsp
incq %rbx
incq %r12
jmp 0xe310
movq 0x58(%rsp), %r15
movq 0x20(%rsp), %rax
addl %eax, %r15d
movl 0xc(%rsp), %ebx
addl %eax, %ebx
jmp 0xe44a
testl %r15d, %r15d
movl 0xc(%rsp), %ebx
jle 0xe4d8
testl %ebx, %ebx
jle 0xe4d8
movl (%r14), %esi
movl 0x4(%r14), %eax
leal (%r15,%rax), %edx
decl %edx
movq 0x20(%rsp), %r12
leal 0x1(%r12), %ecx
movq 0x68(%r14), %rax
movq (%rax), %rdi
movl %r15d, %r8d
leaq -0x1(%rdi,%r8), %r10
movq (%rax,%r13,8), %rax
movl %ebx, %edi
addq %rdi, %rax
decq %rax
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movl %r13d, %r8d
movl %ecx, %r9d
pushq %rax
pushq $0x1
pushq %r10
callq 0xe516
addq $0x20, %rsp
addl %r12d, %r15d
jmp 0xe44a
testl %r15d, %r15d
je 0xe452
cmpl $0x0, 0xc(%rsp)
je 0xe452
movl (%r14), %esi
movl 0x4(%r14), %eax
leal (%r15,%rax), %edx
decl %edx
movq 0x68(%r14), %rax
movq (%rax), %rcx
leaq -0x1(%rcx,%r12), %r10
movq 0x20(%rsp), %r12
leal 0x1(%r12), %r11d
movq (%rax,%r13,8), %rax
addq %rbx, %rax
decq %rax
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
pushq $0x1
popq %rcx
movl %r13d, %r8d
movl %ecx, %r9d
pushq %rax
pushq %r11
pushq %r10
callq 0xe516
addq $0x20, %rsp
movl 0xc(%rsp), %ebx
addl %r12d, %ebx
jmp 0xe44a
movl 0xc(%rsp), %ebx
incq %rbp
jmp 0xe29e
movq 0x12b9f(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
movslq (%r14), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %r9
movl 0x4(%r14), %r10d
incl %r10d
subq $0x8, %rsp
leaq 0xaffe(%rip), %rsi      # 0x19480
leaq 0xb023(%rip), %rdx      # 0x194ac
movl %r15d, %ecx
movl 0x14(%rsp), %ebx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r10
callq 0x2250
popq %rcx
popq %rdx
movq 0x20(%rsp), %r12
jmp 0xe441
movq 0x48(%rsp), %rdi
callq 0x2050
movq 0x28(%rsp), %rbx
movl (%rbx), %edx
movq 0x8(%rbx), %rcx
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0xe83d
movl %eax, (%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xafd9(%rip), %rdi      # 0x194b8
leaq 0xaf52(%rip), %rsi      # 0x19438
leaq 0xaf54(%rip), %rcx      # 0x19441
movl $0xab, %edx
callq 0x21b0
leaq 0xaf2a(%rip), %rdi      # 0x19428
leaq 0xaf33(%rip), %rsi      # 0x19438
leaq 0xaf35(%rip), %rcx      # 0x19441
movl $0x83, %edx
callq 0x21b0