// File: /workspace/github_cpp_makefile_star2_O1/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 0x26ae74(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x211fed(%rip), %rsi    # 0x231163
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 %rbx, %xmm1
punpckldq 0x20607e(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
movq %rax, %r15
subpd 0x206083(%rip), %xmm1   # 0x225250
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
cmpq $0x1, %rbp
adcq $0x0, %rbp
addsd %xmm1, %xmm0
movq 0x206057(%rip), %xmm1   # 0x225240
movapd 0x20605f(%rip), %xmm2   # 0x225250
movsd 0x211877(%rip), %xmm3   # 0x230a70
mulsd %xmm0, %xmm3
movsd 0x206063(%rip), %xmm4   # 0x225268
mulsd 0x20627b(%rip), %xmm0   # 0x225488
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
sarq $0x3f, %rdx
subsd %xmm4, %xmm5
cvttsd2si %xmm5, %rsi
andq %rdx, %rsi
addsd %xmm0, %xmm6
cvttsd2si %xmm6, %rdx
orq %rcx, %rsi
movq %rdx, %rcx
sarq $0x3f, %rcx
subsd %xmm4, %xmm6
cvttsd2si %xmm6, %rdi
andl %ecx, %edi
orl %edi, %edx
testq %r14, %r14
cmoveq %r14, %rsi
shlq $0x20, %rdx
orq %rsi, %rdx
movq %rdx, (%r15,%r14,8)
movq %rax, %r14
cmpq %rax, %rbp
jne 0x1f20d
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 0x1f3e5
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 0xca45c
movq %r14, %rdx
shlq $0x5, %rdx
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movq %rcx, (%r13,%rdx)
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, 0x10(%r13,%rdx)
andq $0x0, 0x18(%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 0x1f358
shrq $0x20, %r8
cmpq %r8, %rax
jae 0x1f358
leal 0x1(%rdi), %r8d
movw %r8w, 0x4(%rdx,%rsi,2)
incq %rsi
incq %rdi
cmpq %rdi, %rbp
jne 0x1f336
movzwl (%rcx), %edx
cmpw $-0x1, %dx
je 0x1f3d9
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
movzwl (%rcx), %r8d
imulq %rbx, %r8
movl %r8d, %r9d
subl %eax, %r9d
subq %r8, %rax
cmovbl %r9d, %eax
movl %eax, %r9d
movl %esi, %r8d
cmpl %eax, %esi
jbe 0x1f3b0
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 0x1f2cb
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