// File: /workspace/github_cpp_makefile_star2_O2/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 0x12721c(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0xc34a5(%rip), %rsi     # 0x1ef273
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
movq %rax, %r15
movq %rbx, %xmm1
punpckldq 0xb7453(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xb745b(%rip), %xmm1    # 0x1e3280
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0xb7437(%rip), %xmm1    # 0x1e3270
movapd 0xb743f(%rip), %xmm2    # 0x1e3280
movsd 0xc2d1f(%rip), %xmm3    # 0x1eeb68
mulsd %xmm0, %xmm3
movsd 0xb7443(%rip), %xmm4    # 0x1e3298
mulsd 0xb767b(%rip), %xmm0    # 0x1e34d8
cmpq %r14, %rbp
je 0x12bedd
leaq 0x1(%r14), %rax
movq %rax, %rcx
imulq %rbx, %rcx
movq %rcx, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm2, %xmm5
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
addsd %xmm5, %xmm6
movapd %xmm3, %xmm5
addsd %xmm6, %xmm5
cvttsd2si %xmm5, %rcx
movq %rcx, %rdx
subsd %xmm4, %xmm5
cvttsd2si %xmm5, %rsi
sarq $0x3f, %rdx
andq %rdx, %rsi
orq %rcx, %rsi
addsd %xmm0, %xmm6
cvttsd2si %xmm6, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd %xmm4, %xmm6
cvttsd2si %xmm6, %rdi
andl %edx, %edi
orl %edi, %ecx
testq %r14, %r14
cmoveq %r14, %rsi
shlq $0x20, %rcx
orq %rsi, %rcx
movq %rcx, (%r15,%r14,8)
movq %rax, %r14
jmp 0x12be5d
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 %rax, %r13
movq %r14, 0x20(%rsp)
movq %rax, (%r14)
xorl %r14d, %r14d
cmpq %r12, %r14
je 0x12c032
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 0x8fc3f
movq %r14, %rdx
shlq $0x5, %rdx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, (%r13,%rdx)
movsd 0xb733b(%rip), %xmm0    # 0x1e32a0
movupd %xmm0, 0x10(%r13,%rdx)
andw $0x0, 0x8(%r13,%rdx)
leaq (%rdx,%r13), %rcx
addq $0x6, %rcx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rbp
je 0x12bfaf
movq (%r15,%rsi,8), %r8
movl %r8d, %r9d
cmpq %r9, %rax
jb 0x12bfaa
shrq $0x20, %r8
cmpq %r8, %rax
jae 0x12bfaa
leaq (%rdx,%r13), %r8
leal 0x1(%rsi), %r9d
movw %r9w, 0x4(%r8,%rdi,2)
incq %rdi
incq %rsi
jmp 0x12bf7f
movzwl (%rcx), %edx
cmpq $0xffff, %rdx           # imm = 0xFFFF
je 0x12c02a
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 0x12c001
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
jmp 0x12bf14
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