// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: rs_sort_ec16(unsigned short*, unsigned short*, int, int)
KRADIX_SORT_INIT(ec16, uint16_t, generic_key, 2)
KRADIX_SORT_INIT(ec32, uint32_t, generic_key, 4)
KRADIX_SORT_INIT(ec64, uint64_t, generic_key, 8)

#define kdq_clear(q) ((q)->count = (q)->front = 0)

typedef struct {size_t n, m; asg16_v *a; uint8_t *f; } cc_v;
cc_v scc = {0, 0, NULL, NULL};
cc_v scb = {0, 0, NULL, NULL};
cc_v sca = {0, 0, NULL, NULL};

typedef struct {size_t n, m; char *a; UC_Read z; asg8_v q;} sl_v;


void h_ec_lchain(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, 
								 int max_n_chain, int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t mcopy_num, double mcopy_rate, uint32_t chain_cutoff, uint32_t mcopy_khit_cut, uint64_t ocv_w);
void h_ec_lchain_amz(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, 
								 int max_n_chain, int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t chain_cutoff, uint32_t mcopy_khit_cut, uint64_t ocv_w);
void h_ec_lchain_re_gen(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, ha_pt_t *ha_idx, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, 
								 int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, 
								 int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, UC_Read *tu, asg64_v *oidx, asg16_v *scc);
void h_ec_lchain_re_gen3(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, uint64_t mz_w, uint64_t mz_k, ha_pt_t *ha_idx, All_reads *rref, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, 
								 int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, 
								 int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, UC_Read *tu, asg64_v *oidx, asg16_v *scc);
uint64_t get_mz1(const char *str, int len, int w, int k, uint32_t rid, int is_hpc, ha_abuf_t *ab, const void *hf, ha_pt_t *ha_idx, int sample_dist, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, ha_pt_t *pt, int min_freq, int32_t dp_min_len, float dp_e, st_mt_t *mt, int32_t ws, int32_t is_unique, void *km, uint64_t beg_i);
void get_pi_ec_chain(ha_abuf_t *ab, uint64_t rid, uint64_t rl, uint32_t tid, char* ts, uint64_t tl, uint64_t mz_w, uint64_t mz_k, overlap_region_alloc *overlap_list, Candidates_list *cl, double bw_thres, 
								int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, /**uint32_t is_accurate,**/ uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, 
								int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip);
void set_lchain_dp_op(uint32_t is_accurate, uint32_t mz_k, int64_t *max_skip, int64_t *max_iter, int64_t *max_dis, double *chn_pen_gap, double *chn_pen_skip, int64_t *quick_check);
void h_ec_lchain_re_gen_srt(ha_abuf_t *ab, ha_pt_t *ha_idx, overlap_region_alloc *olst, Candidates_list *cl);
uint64_t h_ec_lchain_re_gen_qry(ha_abuf_t *ab, uint64_t *k, uint64_t *l, uint64_t *i, uint64_t *idx_a, uint64_t idx_n, uint64_t *tid, uint64_t *trev);
uint64_t h_ec_lchain_re_chn(ha_abuf_t *ab, uint64_t si, uint64_t ei, uint32_t rid, char* rs, uint64_t rl, uint64_t tid, char* ts, uint64_t tl, uint64_t trev, uint64_t mz_w, uint64_t mz_k, overlap_region_alloc *olst, Candidates_list *cl, double bw_thres, 
								 int apend_be, uint64_t max_cnt, uint64_t min_cnt, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t quick_check, double chn_pen_gap, double chn_pen_skip, tiny_queue_t *tq, asg16_v *scc, int64_t *n, int64_t *zn);
overlap_region* h_ec_lchain_fast(ha_abuf_t *ab, uint32_t rid, UC_Read *qu, UC_Read *tu, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *ol, Candidates_list *cl, bit_extz_t *exz, asg16_v *buf, asg64_v *srt_i, double bw_thres, 
								 int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, ma_hit_t_alloc *in0, ma_hit_t_alloc *in1, double sh);
void h_ec_lchain_fast_new(ha_abuf_t *ab, uint32_t rid, UC_Read *qu, UC_Read *tu, All_reads *rref, overlap_region_alloc *ol, Candidates_list *cl, bit_extz_t *exz, asg16_v *buf, asg64_v *srt_i, ma_hit_t_alloc *in0, ma_hit_t_alloc *in1, double sh);

ec_ovec_buf_t* gen_ec_ovec_buf_t(uint32_t n)
{
    uint32_t k; ec_ovec_buf_t0 *z = NULL;
    ec_ovec_buf_t *p = NULL; CALLOC(p, 1);
    p->n = n; CALLOC(p->a, p->n);
    for (k = 0; k < p->n; k++) {
        z = &(p->a[k]);
        init_UC_Read(&z->self_read);
	    init_UC_Read(&z->ovlp_read);
	    init_Candidates_list(&z->clist);
	    init_overlap_region_alloc(&z->olist);

        // init_fake_cigar(&(z->tmp.f_cigar));
        // memset(&(z->tmp.w_list), 0, sizeof(z->tmp.w_list));
        // CALLOC(z->tmp.w_list.a, 1); z->tmp.w_list.n = z->tmp.w_list.m = 1;

        // kv_init(z->b_buf.a);
        // kv_init(z->r_buf.a);
        kv_init(z->k_flag.a);
        kv_init(z->sp);
        kv_init(z->pidx);
	    kv_init(z->v64);
        kv_init(z->v32);
        kv_init(z->v16);
        kv_init(z->v8q);
        kv_init(z->v8t);
        init_bit_extz_t(&(z->exz), 31);

        z->ab = ha_abuf_init();
        
        InitHaplotypeEvdience(&z->hap);
        z->cns.q = kdq_init(uint32_t);
    }
    
    return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %ecx, %ebx
movl %edx, %r14d
movl $0x1, %eax
movl %r14d, %ecx
shll %cl, %eax
leal -0x1(%rax), %edx
movq %rax, %rcx
shlq $0x4, %rcx
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
cltq
shlq $0x4, %rax
leaq (%r15,%rax), %r12
xorl %ecx, %ecx
movq %rdi, 0x8(%r15,%rcx)
movq %rdi, (%r15,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x17f631
cmpq %rsi, %rdi
je 0x17f66a
movq %rdi, %r8
movzwl (%r8), %r9d
movl %ebx, %ecx
shrl %cl, %r9d
andl %edx, %r9d
shll $0x4, %r9d
addq $0x2, 0x8(%r15,%r9)
addq $0x2, %r8
cmpq %rsi, %r8
jne 0x17f64b
testl %r14d, %r14d
je 0x17f691
movl $0x10, %ecx
movq -0x8(%r15,%rcx), %rsi
movq %rsi, %r8
subq %rdi, %r8
addq %r8, 0x8(%r15,%rcx)
movq %rsi, (%r15,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x17f674
movq %r15, %rsi
movq (%rsi), %r10
cmpq 0x8(%rsi), %r10
je 0x17f6f4
movzwl (%r10), %r8d
movl %r8d, %r9d
movl %ebx, %ecx
shrl %cl, %r9d
andl %edx, %r9d
shll $0x4, %r9d
addq %r15, %r9
cmpq %rsi, %r9
je 0x17f6fa
movl %r8d, %ecx
movq (%r9), %r10
movzwl (%r10), %r8d
leaq 0x2(%r10), %r11
movq %r11, (%r9)
movw %cx, (%r10)
movl %r8d, %r9d
movl %ebx, %ecx
shrl %cl, %r9d
andl %edx, %r9d
shll $0x4, %r9d
addq %r15, %r9
cmpq %rsi, %r9
jne 0x17f6b8
movq (%rsi), %rcx
leaq 0x2(%rcx), %r9
movq %r9, (%rsi)
movw %r8w, (%rcx)
jmp 0x17f701
addq $0x10, %rsi
jmp 0x17f701
addq $0x2, %r10
movq %r10, (%rsi)
cmpq %r12, %rsi
jne 0x17f694
movq %rdi, (%r15)
testl %r14d, %r14d
je 0x17f725
movl $0x10, %ecx
movq -0x8(%r15,%rcx), %rdx
movq %rdx, (%r15,%rcx)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x17f713
testl %ebx, %ebx
je 0x17f7b1
xorl %eax, %eax
subl %r14d, %ebx
cmovlel %eax, %ebx
movq (%r15), %rdi
movq 0x8(%r15), %rsi
movq %rsi, %rax
subq %rdi, %rax
sarq %rax
cmpq $0x41, %rax
jl 0x17f757
movl %r14d, %edx
movl %ebx, %ecx
callq 0x17f5f8
jmp 0x17f7a8
cmpq $0x2, %rax
jl 0x17f7a8
leaq 0x2(%rdi), %rdx
cmpq %rsi, %rdx
jae 0x17f7a8
movq %rdi, %rax
movq %rax, %r8
movzwl 0x2(%rax), %ecx
movq %rdx, %rax
cmpw (%r8), %cx
jae 0x17f79f
movq %rax, %rdx
cmpq %rdi, %rax
jbe 0x17f79c
movq %rax, %rdx
movzwl -0x2(%rdx), %r8d
cmpw %r8w, %cx
jae 0x17f79c
movw %r8w, (%rdx)
addq $-0x2, %rdx
cmpq %rdi, %rdx
ja 0x17f784
movw %cx, (%rdx)
leaq 0x2(%rax), %rdx
cmpq %rsi, %rdx
jb 0x17f769
addq $0x10, %r15
cmpq %r12, %r15
jne 0x17f735
leaq -0x20(%rbp), %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq