// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r14
movq %rdi, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
cmpl $0x0, (%rdx)
jle 0x1209a
movl $0x30, %eax
xorl %ecx, %ecx
movq 0x8(%rdx), %rsi
andl $0xbfffffff, (%rsi,%rax) # imm = 0xBFFFFFFF
incq %rcx
movslq (%rdx), %rsi
addq $0x40, %rax
cmpq %rsi, %rcx
jl 0x12080
movq %rdx, 0x30(%rsp)
leaq 0xd3b6(%rip), %rdx      # 0x1f45c
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x106de
testl %eax, %eax
js 0x120e3
movl %eax, %ebp
movq %r14, %rdi
movl $0x4, %esi
callq 0xf0a4
movzwl 0x10(%r14), %eax
testq %rax, %rax
setne %sil
je 0x12157
movq 0x78(%r14), %rdx
cmpl %ebp, (%rdx)
jne 0x120fb
xorl %ecx, %ecx
jmp 0x12128
movq $0x0, 0x20(%rsp)
movq %r14, %rdi
movl $0x1, %esi
callq 0xf0a4
jmp 0x12160
addq $0x18, %rdx
leaq -0x1(%rax), %rsi
movq $0x0, 0x20(%rsp)
xorl %ecx, %ecx
cmpq %rcx, %rsi
je 0x12160
leaq 0x18(%rdx), %rdi
incq %rcx
cmpl %ebp, (%rdx)
movq %rdi, %rdx
jne 0x1210e
cmpq %rax, %rcx
setb %sil
testb %sil, %sil
je 0x126d5
movq 0x78(%r14), %rax
leaq (%rcx,%rcx,2), %rcx
cmpl $0x7, 0x4(%rax,%rcx,8)
jne 0x12157
leaq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x28(%rsp)
movl 0x8(%rax), %eax
movq %rax, 0x20(%rsp)
jmp 0x12169
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x68(%r14), %rax
movq (%rax), %rdi
callq 0x2170
movq %rax, 0x60(%rsp)
movzwl 0x12(%r14), %eax
testl $0xfffe, %eax           # imm = 0xFFFE
je 0x126a2
movl $0x1, %r13d
movq 0x68(%r14), %rbx
movq (%rbx,%r13,8), %rbp
movq %rbp, %rdi
callq 0x2170
movslq 0x8(%r14), %r12
cmpl 0x60(%rsp), %r12d
jne 0x121c1
movq %rax, %r15
cmpb $0x3c, (%rbp)
jne 0x121fd
leal -0x1(%r15), %eax
cltq
cmpb $0x3e, (%rbp,%rax)
jne 0x121fd
movl (%r14), %esi
movl 0x4(%r14), %edx
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movl %r12d, %ecx
movl %r13d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
pushq %rbp
pushq $-0x1
pushq (%rbx)
callq 0x1272a
addq $0x20, %rsp
incq %r13
movzwl 0x12(%r14), %eax
cmpq %rax, %r13
jb 0x12190
jmp 0x126a2
movq $0x0, (%rsp)
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x122da
movq %rsi, 0x38(%rsp)
movq %rsi, %rbx
movq 0x20(%rsp), %rax
testl %eax, %eax
jle 0x1223e
cltq
addq %rsi, %rax
movq %rsi, %rbx
cmpb $0x2c, (%rbx)
je 0x1223e
incq %rbx
movq %rbx, 0x38(%rsp)
cmpq %rax, %rbx
jb 0x1222c
movq %rbx, %rax
subq %rsi, %rax
testq %rax, %rax
jle 0x1270b
movslq %eax, %r15
leaq 0x1(%r15), %rax
cmpq 0x8(%rsp), %rax
jb 0x122a8
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2330
movq 0x28(%rsp), %rsi
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
movq %rsi, %rbp
callq 0x2280
movq %r15, (%rsp)
movb $0x0, (%r12,%r15)
incq %rbx
subl %ebx, %ebp
movq 0x20(%rsp), %rax
addl %ebp, %eax
movq %rax, 0x20(%rsp)
jmp 0x12488
cmpq %r12, %r15
jne 0x122e7
movl %r12d, %edi
jmp 0x12418
cmpq $0x3, 0x8(%rsp)
ja 0x1230c
movq $0x4, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x2330
movq %rax, 0x10(%rsp)
movl %r15d, %edi
subl %r12d, %edi
movq 0x10(%rsp), %rax
movw $0x4d31, (%rax)         # imm = 0x4D31
movq $0x2, (%rsp)
movb $0x0, 0x2(%rax)
testl %edi, %edi
jle 0x123a0
movq %rsp, %rsi
callq 0x1299e
movq (%rsp), %rbx
leaq 0x1(%rbx), %r15
cmpq 0x8(%rsp), %r15
jb 0x1238b
movq %r15, %rax
shrq %rax
orq %r15, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2330
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %r15, (%rsp)
movw $0x49, (%rax,%rbx)
movl 0x8(%r14), %r15d
jmp 0x12410
negl %edi
movq %rsp, %rsi
callq 0x1299e
movq (%rsp), %rbx
leaq 0x1(%rbx), %r12
cmpq 0x8(%rsp), %r12
jb 0x12401
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2330
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %r12, (%rsp)
movw $0x44, (%rax,%rbx)
decl %r15d
je 0x12486
movl %r15d, %edi
movq %rsp, %rsi
callq 0x1299e
movq (%rsp), %rbx
leaq 0x1(%rbx), %r15
cmpq 0x8(%rsp), %r15
jb 0x12477
movq %r15, %rax
shrq %rax
orq %r15, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2330
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %r15, (%rsp)
movw $0x4d, (%rax,%rbx)
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
movq %rdi, 0x38(%rsp)
cmpb $0x0, (%rdi)
movq %rbx, 0x28(%rsp)
je 0x121eb
xorl %ebp, %ebp
xorl %r15d, %r15d
leaq 0x38(%rsp), %rsi
movl $0xa, %edx
callq 0x2270
movq %rax, %r12
movq 0x38(%rsp), %rbx
movzbl (%rbx), %eax
cmpl $0x48, %eax
jle 0x124dc
cmpl $0x49, %eax
je 0x125df
cmpl $0x58, %eax
je 0x1254d
cmpl $0x4d, %eax
je 0x1254d
jmp 0x1263e
cmpl $0x3d, %eax
je 0x1254d
cmpl $0x44, %eax
jne 0x1263e
testl %r15d, %r15d
jle 0x126ec
testl %ebp, %ebp
jle 0x126ec
movl (%r14), %esi
movl 0x4(%r14), %eax
leal (%r15,%rax), %edx
decl %edx
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 %ebp, %edi
addq %rdi, %rax
decq %rax
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movl %r13d, %r8d
movl %ecx, %r9d
pushq %rax
pushq $0x1
pushq %r10
callq 0x1272a
addq $0x20, %rsp
addl %r12d, %r15d
jmp 0x1263e
movq %r15, 0x58(%rsp)
movl %ebp, 0x44(%rsp)
movq %r12, 0x50(%rsp)
testl %r12d, %r12d
jle 0x125ca
movslq 0x58(%rsp), %r12
movslq 0x44(%rsp), %r15
movq 0x50(%rsp), %rbp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x68(%r14), %rcx
movq (%rcx), %rax
movb (%rax,%r12), %dl
movq (%rcx,%r13,8), %r10
cmpb (%r10,%r15), %dl
je 0x125bf
addq %r12, %rax
addq %r15, %r10
movl (%r14), %esi
movl 0x4(%r14), %edx
addl %r12d, %edx
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movl $0x1, %ecx
movl %r13d, %r8d
movl $0x1, %r9d
pushq %r10
pushq $0x1
pushq %rax
callq 0x1272a
addq $0x20, %rsp
incq %r15
incq %r12
decq %rbp
jne 0x12575
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rax
addl %eax, %r15d
movl 0x44(%rsp), %ebp
addl %eax, %ebp
jmp 0x1263e
testl %r15d, %r15d
je 0x12656
testl %ebp, %ebp
je 0x12656
movl (%r14), %esi
movl 0x4(%r14), %eax
leal (%r15,%rax), %edx
decl %edx
movq 0x68(%r14), %rax
movq (%rax), %rcx
movslq %r15d, %rdi
leaq -0x1(%rcx,%rdi), %r10
leal 0x1(%r12), %r11d
movq (%rax,%r13,8), %rax
movslq %ebp, %rcx
addq %rcx, %rax
decq %rax
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movl $0x1, %ecx
movl %r13d, %r8d
movl $0x1, %r9d
pushq %rax
pushq %r11
pushq %r10
callq 0x1272a
addq $0x20, %rsp
addl %r12d, %ebp
leaq 0x1(%rbx), %rdi
movq %rdi, 0x38(%rsp)
cmpb $0x0, 0x1(%rbx)
jne 0x124a5
jmp 0x121eb
movq 0x1399b(%rip), %rax     # 0x25ff8
movq (%rax), %rdi
movq 0x48(%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 0xce31(%rip), %rsi      # 0x1f4b7
leaq 0xce56(%rip), %rdx      # 0x1f4e3
movl %r15d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
pushq %r10
callq 0x2250
addq $0x10, %rsp
jmp 0x1263b
movq 0x10(%rsp), %rdi
callq 0x2050
movq 0x30(%rsp), %rbx
movl (%rbx), %edx
movq 0x8(%rbx), %rcx
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x12af2
movl %eax, (%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
jmp 0x12169
leaq 0xcdfc(%rip), %rdi      # 0x1f4ef
leaq 0xcd78(%rip), %rsi      # 0x1f472
leaq 0xcd7a(%rip), %rcx      # 0x1f47b
movl $0xab, %edx
callq 0x21b0
leaq 0xcd50(%rip), %rdi      # 0x1f462
leaq 0xcd59(%rip), %rsi      # 0x1f472
leaq 0xcd5b(%rip), %rcx      # 0x1f47b
movl $0x83, %edx
callq 0x21b0