// File: chhylp123[P]hifiasm/Hash_Table.cpp
// Function: ha_chain_check(k_mer_hit*, int, Chain_Data*, int, double)
int32_t ha_chain_check(k_mer_hit *a, int32_t n_a, Chain_Data *dp, int32_t min_sc, double bw_thres)
{
	int32_t i, tot_indel = 0, tot_len = 0;
	double bw_pen;
	if (n_a == 0) return -1;
	for (i = 1; i < n_a; ++i)
		if (a[i-1].self_offset >= a[i].self_offset)
			break;
	if (i < n_a) return -1;
	bw_pen = 1.0 / bw_thres;
	// dp->score[0] = a[0].good? min_sc : min_sc>>1;
    dp->score[0] = normal_w(min_sc, (int64_t)a[0].cnt);
	dp->pre[0] = -1, dp->indels[0] = 0, dp->self_length[0] = 0, dp->occ[0] = 1;
	for (i = 1; i < n_a; ++i) {
		int32_t score, dg;
		int32_t dx = (int32_t)a[i].offset - (int32_t)a[i-1].offset;
		int32_t dy = (int32_t)a[i].self_offset - (int32_t)a[i-1].self_offset;
		int32_t dd = dx > dy? dx - dy : dy - dx;
		double gap_rate;
		tot_indel += dd;
		tot_len += dy;
		if (tot_indel > tot_len * bw_thres) break;
		dg = dx < dy? dx : dy;
		if (dd > THRESHOLD_MAX_SIZE && dd > dg * bw_thres) break;
		score = dg < min_sc? dg : min_sc;
        score = normal_w(score, (int64_t)a[i].cnt);
		gap_rate = (double)tot_indel / tot_len;
		score -= (int)(gap_rate * score * bw_pen);
		dp->score[i] = dp->score[i-1] + score;
		dp->pre[i] = i - 1;
		dp->indels[i] = tot_indel;
		dp->self_length[i] = tot_len;
        dp->occ[i] = i + 1;
	}
	if (i < n_a) return -1;
	return n_a;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x21978
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x21c95
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x219c1
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jb 0x219b4
jmp 0x219c1
jmp 0x219b6
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2197f
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x219d5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x21c95
movsd 0x360623(%rip), %xmm0   # 0x382000
divsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movslq -0x24(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jl 0x21a0d
movslq -0x24(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
cqto
idivq %rcx
movq %rax, -0x70(%rbp)
jmp 0x21a18
movl $0x1, %eax
movq %rax, -0x70(%rbp)
jmp 0x21a18
movq -0x70(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq $-0x1, (%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl $0x1, (%rax)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x21c7e
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x34(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x34(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x21ade
movl -0x54(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x21ae7
movl -0x58(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x58(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
cvtsi2sdl -0x3c(%rbp), %xmm1
mulsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x21b19
jmp 0x21c7e
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x21b29
movl -0x54(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x21b2f
movl -0x58(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x50(%rbp)
cmpl $0x1f, -0x5c(%rbp)
jle 0x21b55
cvtsi2sdl -0x5c(%rbp), %xmm0
cvtsi2sdl -0x50(%rbp), %xmm1
mulsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x21b55
jmp 0x21c7e
movl -0x50(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x21b65
movl -0x50(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x21b6b
movl -0x24(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jl 0x21bb0
movslq -0x4c(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cqto
idivq %rcx
movq %rax, -0x88(%rbp)
jmp 0x21bbe
movl $0x1, %eax
movq %rax, -0x88(%rbp)
jmp 0x21bbe
movq -0x88(%rbp), %rax
movl %eax, -0x4c(%rbp)
cvtsi2sdl -0x38(%rbp), %xmm0
cvtsi2sdl -0x3c(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
cvtsi2sdl -0x4c(%rbp), %xmm1
mulsd %xmm1, %xmm0
mulsd -0x48(%rbp), %xmm0
cvttsd2si %xmm0, %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl -0x4c(%rbp), %edx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x38(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %edx
addl $0x1, %edx
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x21a67
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x21c8f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x21c95
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x8, %rsp
popq %rbp
retq
nop