// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebx
movl %edx, %r14d
pushq $0x1
popq %rax
movl %r14d, %ecx
shll %cl, %eax
movq %rax, %rcx
shlq $0x4, %rcx
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
movslq %eax, %r12
movq %r12, %rcx
shlq $0x4, %rcx
leaq (%r15,%rcx), %rdx
movq %r15, %r8
testq %rcx, %rcx
je 0x13cb34
movq %rdi, 0x8(%r8)
movq %rdi, (%r8)
addq $0x10, %r8
addq $-0x10, %rcx
jmp 0x13cb1e
decl %eax
movq %rdi, %r8
cmpq %rsi, %r8
je 0x13cb5a
movzwl (%r8), %r9d
movl %ebx, %ecx
shrl %cl, %r9d
andl %eax, %r9d
shll $0x4, %r9d
addq $0x2, 0x8(%r15,%r9)
addq $0x2, %r8
jmp 0x13cb39
leaq 0x10(%r15), %rsi
shlq $0x4, %r12
leaq -0x10(%r12), %rcx
movq %rsi, %r8
testq %rcx, %rcx
je 0x13cb8a
movq -0x8(%r8), %r9
movq %r9, %r10
subq %rdi, %r10
addq %r10, 0x8(%r8)
movq %r9, (%r8)
addq $0x10, %r8
addq $-0x10, %rcx
jmp 0x13cb6a
movq %r15, %r8
cmpq %rdx, %r8
je 0x13cc01
movq (%r8), %r11
cmpq 0x8(%r8), %r11
je 0x13cbfb
movzwl (%r11), %r9d
movl %r9d, %r10d
movl %ebx, %ecx
shrl %cl, %r10d
andl %eax, %r10d
shll $0x4, %r10d
addq %r15, %r10
cmpq %r8, %r10
je 0x13cbf2
movl %r9d, %ecx
movq (%r10), %r11
movzwl (%r11), %r9d
leaq 0x2(%r11), %r13
movq %r13, (%r10)
movw %cx, (%r11)
movl %r9d, %r10d
movl %ebx, %ecx
shrl %cl, %r10d
andl %eax, %r10d
shll $0x4, %r10d
addq %r15, %r10
cmpq %r8, %r10
jne 0x13cbb6
movq (%r8), %rcx
leaq 0x2(%rcx), %r10
movq %r10, (%r8)
movw %r9w, (%rcx)
jmp 0x13cb8d
addq $0x2, %r11
movq %r11, (%r8)
jmp 0x13cb8d
addq $0x10, %r8
jmp 0x13cb8d
movq %rdi, (%r15)
leaq -0x10(%r12), %rax
testq %rax, %rax
je 0x13cc1f
movq -0x8(%rsi), %rcx
movq %rcx, (%rsi)
addq $0x10, %rsi
addq $-0x10, %rax
jmp 0x13cc09
testl %ebx, %ebx
je 0x13cc67
xorl %eax, %eax
subl %r14d, %ebx
cmovlel %eax, %ebx
testq %r12, %r12
je 0x13cc67
movq (%r15), %rdi
movq 0x8(%r15), %rsi
movq %rsi, %rax
subq %rdi, %rax
sarq %rax
cmpq $0x41, %rax
jl 0x13cc52
movl %r14d, %edx
movl %ebx, %ecx
callq 0x13cae2
jmp 0x13cc5d
cmpq $0x2, %rax
jl 0x13cc5d
callq 0x13caa4
addq $0x10, %r15
addq $-0x10, %r12
jmp 0x13cc2b
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq