// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
callq 0x2510
movsd %xmm0, -0x28(%rbp)
callq 0xcd150
movq %rax, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0xd3e84
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %esi
movq -0x18(%rbp), %rax
imulq $0xc, -0x30(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movq -0x18(%rbp), %rax
imulq $0xc, -0x30(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movq -0x18(%rbp), %rax
imulq $0xc, -0x30(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
shrl $0x1f, %ecx
callq 0xcd8f0
movq -0x18(%rbp), %rax
imulq $0xc, -0x30(%rbp), %rcx
addq %rcx, %rax
movb 0x8(%rax), %dl
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x30(%rbp), %rcx
movb %dl, 0x4(%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xd3e09
movq -0x40(%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 -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0xd4023
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
imulq $0x18, -0x30(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0xd4010
movq -0x8(%rbp), %rax
imulq $0x18, -0x30(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0xd3f15
jmp 0xd3fff
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), %eax
movq -0x18(%rbp), %rcx
movq -0x70(%rbp), %rdx
movq (%rdx), %rdx
shrq $0x20, %rdx
movl %edx, %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
movq -0x70(%rbp), %rdx
movl 0xc(%rdx), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x78(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq 0x2f085b(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl -0x20(%rbp), %r8d
leaq -0x60(%rbp), %r9
callq 0xd0300
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jl 0xd3fe5
movq -0x40(%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)
jmp 0xd3ffd
movq 0x2efff4(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x2ae5ef(%rip), %rsi    # 0x3825e5
movb $0x0, %al
callq 0x22a0
jmp 0xd3fff
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xd3ec8
jmp 0xd4012
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xd3eb2
movq -0x40(%rbp), %rdi
callq 0xcdd10
movq -0x40(%rbp), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x40(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)