// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_sg_gen_ul(ma_hit_t_alloc*, long, ma_sub_t const*, R_to_U*, long, long, long)
asg_t *ma_sg_gen_ul(ma_hit_t_alloc* sources, int64_t n_read, const ma_sub_t *coverage_cut, 
R_to_U* ruIndex, int64_t max_hang, int64_t min_ovlp, int64_t ul_occ)
{
	int64_t i, j, r; asg_arc_t t, *p; const ma_hit_t *h; 
	asg_t *g = asg_init(); 
	for (i = 0; i < n_read; ++i) {
		asg_seq_set(g, i, coverage_cut[i].e - coverage_cut[i].s, coverage_cut[i].del);
        g->seq[i].c = coverage_cut[i].c;
	}
    CALLOC(g->seq_vis, (g->n_seq<<1));

    // prt_specific_overlap(sources, 22233, 22235, "+");
    // prt_specific_overlap(sources, 22235, 22233, "+");
    // fprintf(stderr, "[M::%s::] n_read::%ld\n", __func__, n_read);
    recover_contain_g(g, sources, ruIndex, max_hang, min_ovlp, ul_occ);

    for (i = 0; i < n_read; ++i) {
        if(g->seq[i].del) continue;
        for (j = 0; j < sources[i].length; j++) {
		    h = &(sources[i].buffer[j]);
            // if((Get_qn(*h) == 22233 && Get_tn(*h) == 22235)||
            //             (Get_qn(*h) == 22235 && Get_tn(*h) == 22233)) {
            //     fprintf(stderr, "[M::%s::] qn::%u, tn::%u, del::%u, bl::%u\n", __func__, Get_qn(*h), Get_tn(*h), 
            //         h->del, h->bl);
            // }
            if(h->del) continue;
		    r = ma_hit2arc(h, (coverage_cut[Get_qn(*h)].e-coverage_cut[Get_qn(*h)].s), 
                    (coverage_cut[Get_tn(*h)].e-coverage_cut[Get_tn(*h)].s), max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
            assert(r >= 0);
            p = asg_arc_pushp(g); *p = t; 
            p->ou = ((h->bl>OU_MASK)?OU_MASK:h->bl);
            // if(Get_qn(*h) == 10498 && Get_tn(*h) == 10505) {
            //     fprintf(stderr, "[M::%s::] qn::%u, tn::%u, p->ou::%u, h->bl::%u\n", __func__, 
            //     Get_qn(*h), Get_tn(*h), p->ou, h->bl);
            // }
        }
    }

    asg_cleanup(g);
    asg_symm(g);
    g->r_seq = g->n_seq;
	return g;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
callq 0xcd150
movq %rax, -0x78(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0xd4383
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
movq -0x18(%rbp), %rax
imulq $0xc, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movq -0x18(%rbp), %rax
imulq $0xc, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movq -0x18(%rbp), %rax
imulq $0xc, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
shrl $0x1f, %ecx
callq 0xcd8f0
movq -0x18(%rbp), %rax
imulq $0xc, -0x38(%rbp), %rcx
addq %rcx, %rax
movb 0x8(%rax), %dl
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rcx
movb %dl, 0x4(%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xd4308
movq -0x78(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2140
movq %rax, %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x78(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movq 0x10(%rbp), %r9
callq 0x312d50
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0xd45b3
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x38(%rbp), %rcx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xd43f8
jmp 0xd45a2
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0xd45a0
movq -0x8(%rbp), %rax
imulq $0x18, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0xd444d
jmp 0xd458f
movq -0x70(%rbp), %rdi
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %esi
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movq -0x28(%rbp), %rax
movl %eax, %ecx
leaq 0x2f032e(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movq -0x30(%rbp), %rax
movl %eax, %r8d
leaq -0x60(%rbp), %r9
callq 0xd0300
cltq
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0xd44f1
jmp 0xd4510
leaq 0x2b035a(%rip), %rdi    # 0x384852
leaq 0x2b23be(%rip), %rsi    # 0x3868bd
movl $0xbc2, %edx            # imm = 0xBC2
leaq 0x2b2655(%rip), %rcx    # 0x386b60
callq 0x2130
movq -0x78(%rbp), %rdi
callq 0xd4050
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, (%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
cmpl $0x3fff, %eax           # imm = 0x3FFF
jbe 0xd455b
movl $0x3fff, %eax           # imm = 0x3FFF
movl %eax, -0x7c(%rbp)
jmp 0xd4570
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x20, %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movw %ax, %dx
movq -0x68(%rbp), %rax
movw 0x10(%rax), %cx
andw $0x3fff, %dx            # imm = 0x3FFF
andw $0xc000, %cx            # imm = 0xC000
orw %dx, %cx
movw %cx, 0x10(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd4400
jmp 0xd45a2
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xd43ce
movq -0x78(%rbp), %rdi
callq 0xcdd10
movq -0x78(%rbp), %rdi
callq 0xce020
movq -0x78(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x78(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x78(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)