// File: chhylp123[P]hifiasm/Hash_Table.cpp
// Function: ovlp_chain_gen(overlap_region_alloc*, overlap_region*, long, long, long, k_mer_hit*, long)
int ovlp_chain_gen(overlap_region_alloc* ol, overlap_region* t, int64_t xl, int64_t yl, int64_t apend_be, k_mer_hit* hit, int64_t n_hit)
{
    if (ol->length + 1 > ol->size) {
        uint64_t sl = ol->size;
        ol->size = ol->length + 1;
        kroundup64(ol->size);
        REALLOC(ol->list, ol->size);
        /// need to set new space to be 0
        memset(ol->list + sl, 0, sizeof(overlap_region)*(ol->size - sl));
    }

    if ((ol->length!=0) && (ol->list[ol->length-1].y_id==t->y_id)) {    
        if((ol->list[ol->length-1].shared_seed > t->shared_seed) ||
           ((ol->list[ol->length-1].shared_seed == t->shared_seed) && 
           (ol->list[ol->length-1].overlapLen <= t->overlapLen))) {
            return 0;
        } else {
            ol->length--;
        }
    }

    int64_t xr, yr; 
    if(t->x_pos_s <= t->y_pos_s) {
        t->y_pos_s -= t->x_pos_s; t->x_pos_s = 0;
    } else {
        t->x_pos_s -= t->y_pos_s; t->y_pos_s = 0;
    }

    xr = xl-t->x_pos_e-1; yr = yl-t->y_pos_e-1;
    if(xr <= yr) {
        t->x_pos_e = xl-1; t->y_pos_e += xr;        
    } else {
        t->y_pos_e = yl-1; t->x_pos_e += yr; 
    }

    overlap_region *o = &(ol->list[ol->length++]);
    o->shared_seed = t->shared_seed;
    o->align_length = 0;
    o->is_match = 0;
    o->non_homopolymer_errors = 0;
    o->strong = 0;
    o->x_id = t->x_id;
    o->y_id = t->y_id;
    o->x_pos_strand = 0;///always 0
    o->y_pos_strand = t->x_pos_strand;

    if (t->x_pos_strand == 1) {
        o->x_pos_e = xl-t->x_pos_s-1; o->x_pos_s = xl-t->x_pos_e-1;
        o->y_pos_e = yl-t->y_pos_s-1; o->y_pos_s = yl-t->y_pos_e-1;
    } else {
        o->x_pos_e = t->x_pos_e; o->x_pos_s = t->x_pos_s;
        o->y_pos_e = t->y_pos_e; o->y_pos_s = t->y_pos_s;
    }
    ///debug
    // debug_cigar(&(t->f_cigar), o, apend_be, hit, n_hit);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x1e9cb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x20, %rcx
movq -0x10(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
imulq $0xa8, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x40(%rbp), %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x40(%rbp), %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1eaa0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0x1eaa0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jg 0x1ea82
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x24(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0x1ea8e
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x1ea8e
movl $0x0, -0x4(%rbp)
jmp 0x1ecb0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0x1eaa0
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x14(%rcx), %eax
ja 0x1ead0
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x4(%rax)
jmp 0x1eaee
movq -0x18(%rbp), %rax
movl 0x14(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jg 0x1eb48
movq -0x20(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rdx
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x18(%rax)
jmp 0x1eb6a
movq -0x28(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x50(%rbp), %rdx
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x10(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x10(%rdx)
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x2c(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x2e(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x58(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x1ec71
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x1eca9
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)