// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_sg_gen(ma_hit_t_alloc const*, long long, ma_sub_t const*, int, int)
asg_t *ma_sg_gen(const ma_hit_t_alloc* sources, long long n_read, const ma_sub_t *coverage_cut, 
int max_hang, int min_ovlp)
{
    double startTime = Get_T();
	size_t i, j;
	asg_t *g;
	///just calloc
	g = asg_init();

	///add seq to graph, seq just save the length of each read
	for (i = 0; i < (uint64_t)n_read; ++i) 
    {
        ///if a read has been deleted, should we still add them?
		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;
	}

    g->seq_vis = (uint8_t*)calloc(g->n_seq*2, sizeof(uint8_t));

    for (i = 0; i < (uint64_t)n_read; ++i) 
    {
        for (j = 0; j < sources[i].length; j++)
        {
            int r;
		    asg_arc_t t, *p;
		    const ma_hit_t *h = &(sources[i].buffer[j]);
            if(h->del) continue;

            //high coverage region [sub[qn].e, sub[qn].s) in query
            int ql = coverage_cut[Get_qn(*h)].e - coverage_cut[Get_qn(*h)].s;
            //high coverage region [sub[qn].e, sub[qn].s) in target
            int tl = coverage_cut[Get_tn(*h)].e - coverage_cut[Get_tn(*h)].s;
		    r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
            /**
            #define MA_HT_INT       (-1)
            #define MA_HT_QCONT      (-2)
            #define MA_HT_TCONT      (-3)
            #define MA_HT_SHORT_OVLP (-4)
            the short overlaps and the overlaps with contain reads have already been removed
            here we should have overhang
            so r should always >= 0
            **/
            if (r >= 0) 
            {
                ///push node?
                p = asg_arc_pushp(g);
                *p = t;
		    }
            else
            {
                fprintf(stderr, "error\n");
            }
        }
    }

    asg_cleanup(g);
    g->r_seq = g->n_seq;

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }

	return g;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0x4(%rsp)
movl %ecx, (%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
callq 0x300fc
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, %r12
testq %r15, %r15
je 0xa3119
leaq 0x8(%r14), %rbx
xorl %r13d, %r13d
movl -0x8(%rbx), %ecx
movl -0x4(%rbx), %edx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
shrl $0x1f, %ecx
movq %r12, %rdi
movl %r13d, %esi
callq 0xa0115
movb (%rbx), %al
movq 0x20(%r12), %rcx
movb %al, 0x4(%rcx,%r13,8)
incq %r13
addq $0xc, %rbx
cmpq %r13, %r15
jne 0xa30e4
movl 0x14(%r12), %edi
addl %edi, %edi
movl $0x1, %esi
callq 0x2120
movq %rax, 0x30(%r12)
testq %r15, %r15
je 0xa327d
xorl %eax, %eax
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %r15, 0x10(%rsp)
movq %rax, 0x18(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x8(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax,8)
je 0xa3267
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq (%r15), %rax
cmpq $0x0, 0x18(%rax,%rbx)
js 0xa324a
leaq (%rax,%rbx), %rdi
movl 0x4(%rax,%rbx), %ecx
movl 0xc(%rax,%rbx), %eax
leaq (%rcx,%rcx,2), %rcx
movl 0x4(%r14,%rcx,4), %esi
movl (%r14,%rcx,4), %ecx
andl %r13d, %ecx
subl %ecx, %esi
leaq (%rax,%rax,2), %rax
movl 0x4(%r14,%rax,4), %edx
movl (%r14,%rax,4), %eax
andl %r13d, %eax
subl %eax, %edx
leaq 0x1e75a7(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl (%rsp), %ecx
movl 0x4(%rsp), %r8d
leaq 0x20(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xa3260
movl 0x4(%r12), %ecx
andl %r13d, %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0xa320b
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%r12)
movq 0x8(%r12), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x8(%r12)
movq 0x8(%r12), %rax
movl 0x4(%r12), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r12)
leaq (%rdx,%rdx,2), %rcx
movq 0x30(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movups 0x20(%rsp), %xmm0
movups %xmm0, (%rax,%rcx,8)
incq %rbp
movl 0xc(%r15), %eax
addq $0x28, %rbx
cmpq %rax, %rbp
jb 0xa316b
jmp 0xa3267
callq 0xfc77
jmp 0xa324a
movq 0x18(%rsp), %rax
incq %rax
movq 0x10(%rsp), %r15
cmpq %r15, %rax
jne 0xa3145
movq %r12, %rdi
callq 0xa02fa
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%r12), %eax
movl %eax, 0x18(%r12)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq