// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: init_mc_gg_status(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, R_to_U*, unsigned long, unsigned short)
kv_gg_status *init_mc_gg_status(ma_ug_t *ug, asg_t *read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, R_to_U* ruIndex, uint64_t t_cov, uint16_t hapN)
{
	fprintf(stderr, "t_cov-%lu\n", t_cov);
	uint64_t *covs = NULL, i, k, k_i, c = t_cov/hapN, c_min, c_max; 
	uint32_t len[2];
	uint8_t *vis = NULL; CALLOC(vis, read_g->n_seq);
	kv_gg_status *p = NULL; 
	CALLOC(covs, hapN);
	for (i = 0; i < hapN; i++)
	{
		c_min = ((i+1)*c) - (0.6*c);
		c_max = ((i+1)*c) + (0.6*c);
		if(i == 0) c_min = 0;
		if(i == hapN) c_max = (uint32_t)-1;
		covs[i] = (c_max<<32)|c_min; 
	}
	CALLOC(p, 1);
	p->n = p->m = ug->u.n;
	CALLOC(p->a, p->n);
	for (i = 0; i < p->n; i++)
	{
		c = get_utg_cov(ug, i, read_g, coverage_cut, sources, ruIndex, vis);
		p->a[i].h[0] = p->a[i].h[1] = (uint16_t)-1; p->a[i].s = 0; k_i = 0;
		p->a[i].hw[0] = 1; p->a[i].hw[1] = 0; p->a[i].hc = 0;
		for (k = 0; k < hapN; k++)
		{
			c_min = (uint32_t)covs[k]; c_max = covs[k]>>32;
			if(c < c_min || c >= c_max) continue;
			p->a[i].h[k_i++] = k + 1;
		}
		if(p->a[i].h[1] == (uint16_t)-1) continue;
		
		len[0] = (c >= (p->a[i].h[0]*(t_cov/hapN))? 
							c - (p->a[i].h[0]*(t_cov/hapN)) : (p->a[i].h[0]*(t_cov/hapN)) - c);
		len[1] = (c >= (p->a[i].h[1]*(t_cov/hapN))? 
							c - (p->a[i].h[1]*(t_cov/hapN)) : (p->a[i].h[1]*(t_cov/hapN)) - c);
		if(len[0] > len[1])
		{
			k_i = p->a[i].h[0];
			p->a[i].h[0] = p->a[i].h[1];
			p->a[i].h[1] = k_i;

			k_i = len[0];
			len[0] = len[1];
			len[1] = k_i;
		}
		
		p->a[i].hw[0] = (double)len[1]/(double)(len[0]+len[1]);
		p->a[i].hw[1] = (double)len[0]/(double)(len[0]+len[1]);
	}
	free(covs); free(vis);
	return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movzwl 0x80(%rsp), %ebp
movq 0x276bf3(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x21efbc(%rip), %rsi    # 0x23d3b3
xorl %r14d, %r14d
movq %r9, %rdx
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rax
xorl %edx, %edx
divq %rbp
movq %rax, %rbx
movq %r15, 0x28(%rsp)
movl 0x14(%r15), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x10(%rsp)
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x2120
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq %rbx, %xmm0
pshufd $0x44, %xmm0, %xmm1     # xmm1 = xmm0[0,1,0,1]
punpckldq 0x212e30(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq %rax, %r15
subpd 0x212e35(%rip), %xmm0   # 0x231290
leaq -0x1(%rbp), %rax
movq %rax, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
movapd %xmm0, %xmm3
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addpd %xmm3, %xmm0
leal 0x8(,%rbp,8), %eax
andl $0xffff0, %eax          # imm = 0xFFFF0
movdqa 0x212e43(%rip), %xmm3   # 0x2312d0
pxor 0x212e4b(%rip), %xmm2   # 0x2312e0
pcmpeqd %xmm5, %xmm5
movdqa %xmm1, %xmm6
psrlq $0x20, %xmm6
movdqa 0x2177d5(%rip), %xmm8   # 0x235c80
movdqa 0x2177ec(%rip), %xmm9   # 0x235ca0
movapd 0x2177f3(%rip), %xmm10  # 0x235cb0
movapd 0x21e7db(%rip), %xmm4   # 0x23cca0
mulpd %xmm0, %xmm4
mulpd 0x21e7df(%rip), %xmm0   # 0x23ccb0
movsd 0x212e2e(%rip), %xmm12  # 0x231308
movdqa 0x21301d(%rip), %xmm13  # 0x231500
movdqa %xmm3, %xmm11
pxor 0x212def(%rip), %xmm11  # 0x2312e0
movdqa %xmm11, %xmm14
pcmpgtd %xmm2, %xmm14
pcmpeqd %xmm2, %xmm11
pshufd $0xf5, %xmm11, %xmm11   # xmm11 = xmm11[1,1,3,3]
pand %xmm14, %xmm11
pshufd $0xf5, %xmm14, %xmm15   # xmm15 = xmm14[1,1,3,3]
por %xmm11, %xmm15
movdqa %xmm3, %xmm11
psubq %xmm5, %xmm11
movdqa %xmm11, %xmm7
pmuludq %xmm6, %xmm7
movdqa %xmm11, %xmm14
psrlq $0x20, %xmm14
pmuludq %xmm1, %xmm14
paddq %xmm7, %xmm14
psllq $0x20, %xmm14
pmuludq %xmm1, %xmm11
paddq %xmm14, %xmm11
movdqa %xmm11, %xmm7
pand 0x217735(%rip), %xmm7   # 0x235c90
por %xmm8, %xmm7
psrlq $0x20, %xmm11
por %xmm9, %xmm11
subpd %xmm10, %xmm11
addpd %xmm7, %xmm11
movapd %xmm4, %xmm14
addpd %xmm11, %xmm14
addpd %xmm0, %xmm11
movd %xmm15, %ecx
notl %ecx
testb $0x1, %cl
je 0x1e5e2
movapd %xmm14, %xmm7
subsd %xmm12, %xmm7
cvttsd2si %xmm7, %rcx
cvttsd2si %xmm14, %rdx
movq %rdx, %rsi
sarq $0x3f, %rsi
andq %rcx, %rsi
movapd %xmm11, %xmm7
subsd %xmm12, %xmm7
cvttsd2si %xmm7, %rcx
cvttsd2si %xmm11, %rdi
orq %rdx, %rsi
movq %rdi, %rdx
sarq $0x3f, %rdx
andl %ecx, %edx
orl %edx, %edi
testq %r14, %r14
cmoveq %r14, %rsi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, (%r15,%r14)
pxor %xmm5, %xmm15
pextrw $0x4, %xmm15, %ecx
testb $0x1, %cl
je 0x1e63e
unpckhpd %xmm14, %xmm14          # xmm14 = xmm14[1,1]
cvttsd2si %xmm14, %rcx
subsd %xmm12, %xmm14
cvttsd2si %xmm14, %rdx
movq %rcx, %rsi
sarq $0x3f, %rsi
unpckhpd %xmm11, %xmm11          # xmm11 = xmm11[1,1]
cvttsd2si %xmm11, %rdi
andq %rdx, %rsi
subsd %xmm12, %xmm11
cvttsd2si %xmm11, %rdx
orq %rcx, %rsi
movq %rdi, %rcx
sarq $0x3f, %rcx
andl %edx, %ecx
orl %ecx, %edi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, 0x8(%r15,%r14)
paddq %xmm13, %xmm3
addq $0x10, %r14
cmpq %r14, %rax
jne 0x1e4e3
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x2120
movq %rax, %r14
movq 0x18(%rsp), %rax
movq (%rax), %r12
movq %r12, 0x10(%r14)
movq %r12, 0x8(%r14)
pushq $0x20
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %r14, 0x20(%rsp)
movq %rax, (%r14)
testq %r12, %r12
je 0x1e7a6
movq %rax, %r13
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rdi
movl %r14d, %esi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0xcb734
movq %r14, %rdx
shlq $0x5, %rdx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, (%r13,%rdx)
movsd 0x212c38(%rip), %xmm0   # 0x231310
movupd %xmm0, 0x10(%r13,%rdx)
andw $0x0, 0x8(%r13,%rdx)
leaq (%rdx,%r13), %rcx
addq $0x6, %rcx
addq %r13, %rdx
xorl %esi, %esi
xorl %edi, %edi
movq (%r15,%rdi,8), %r8
movl %r8d, %r9d
cmpq %r9, %rax
jb 0x1e717
shrq $0x20, %r8
cmpq %r8, %rax
jae 0x1e717
leal 0x1(%rdi), %r8d
movw %r8w, 0x4(%rdx,%rsi,2)
incq %rsi
incq %rdi
cmpq %rdi, %rbp
jne 0x1e6f5
movzwl (%rcx), %edx
cmpq $0xffff, %rdx           # imm = 0xFFFF
je 0x1e79a
movzwl -0x2(%rcx), %edi
movq %rbx, %r8
imulq %rdi, %r8
movl %r8d, %r9d
subl %eax, %r9d
movq %rax, %rsi
subq %r8, %rsi
cmovbl %r9d, %esi
movq %rbx, %r8
imulq %rdx, %r8
movl %r8d, %r9d
subl %eax, %r9d
subq %r8, %rax
cmovbl %r9d, %eax
movl %eax, %r9d
movl %esi, %r8d
cmpl %eax, %esi
jbe 0x1e771
movw %dx, -0x2(%rcx)
movw %di, (%rcx)
movl %esi, %r9d
movl %eax, %r8d
movl %r9d, %edx
cvtsi2sd %rdx, %xmm0
addl %esi, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa(%rcx)
movl %r8d, %eax
cvtsi2sd %rax, %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x12(%rcx)
incq %r14
cmpq %r12, %r14
jne 0x1e690
movq %r15, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq