// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r12
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %rax
leaq 0x1(%rax), %rcx
cmpq %r13, %rcx
jbe 0x3def3
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x20, %rax
orq %rcx, %rax
incq %rax
movq %rax, 0x8(%r12)
movq (%r12), %rdi
imulq $0xa8, %rax, %rsi
callq 0x2370
movq %rax, (%r12)
imulq $0xa8, %r13, %rdi
addq %rax, %rdi
movq 0x8(%r12), %rax
subq %r13, %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%r12), %rcx
testq %rcx, %rcx
je 0x3df41
movq (%r12), %rdx
imulq $0xa8, %rcx, %rax
movl -0x98(%rdx,%rax), %esi
cmpl 0x10(%rbx), %esi
jne 0x3df41
addq %rax, %rdx
movl 0x24(%rbx), %esi
xorl %eax, %eax
cmpl %esi, -0x84(%rdx)
jg 0x3e049
jne 0x3df39
movl -0x88(%rdx), %edx
cmpl 0x20(%rbx), %edx
jbe 0x3e049
decq %rcx
movq %rcx, 0x10(%r12)
leaq 0x4(%rbx), %rcx
movl 0x4(%rbx), %edx
movl 0x14(%rbx), %esi
leaq 0x14(%rbx), %rax
movl %edx, %edi
subl %esi, %edi
jbe 0x3df5c
movl %edi, (%rcx)
movq %rax, %rcx
jmp 0x3df60
subl %edx, %esi
movl %esi, (%rax)
xorl %eax, %eax
movl %eax, (%rcx)
movl 0x8(%rbx), %esi
movl 0x18(%rbx), %ecx
movq %rsi, %rdi
notq %rdi
addq %r15, %rdi
movq %rcx, %rdx
notq %rdx
addq %r14, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %rdx, %rdi
setle %r8b
cmovlq %rdi, %rdx
setg %r9b
cmovgl %esi, %ecx
movl %r15d, %esi
cmovgl %r14d, %esi
shll $0x4, %r9d
shll $0x4, %r8d
decl %esi
movl %esi, 0x8(%rbx,%r9)
addl %ecx, %edx
movl %edx, 0x8(%rbx,%r8)
movq (%r12), %rdx
movq 0x10(%r12), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%r12)
imulq $0xa8, %rcx, %rsi
leaq (%rdx,%rsi), %rcx
movl 0x24(%rbx), %edi
movl %edi, 0x24(%rdx,%rsi)
movl %eax, 0x28(%rdx,%rsi)
movb %al, 0x2c(%rdx,%rsi)
movl %eax, 0x30(%rdx,%rsi)
movb %al, 0x2e(%rdx,%rsi)
movl (%rbx), %edi
movl %edi, (%rdx,%rsi)
movl 0x10(%rbx), %edi
movl %edi, 0x10(%rdx,%rsi)
movl %eax, 0xc(%rdx,%rsi)
movl 0xc(%rbx), %eax
movl %eax, 0x1c(%rdx,%rsi)
cmpl $0x1, %eax
jne 0x3e02c
movl 0x4(%rbx), %eax
notl %eax
addl %r15d, %eax
movl %eax, 0x8(%rcx)
movl 0x8(%rbx), %eax
notl %eax
addl %r15d, %eax
movl %eax, 0x4(%rcx)
movl 0x14(%rbx), %eax
notl %eax
addl %r14d, %eax
movl %eax, 0x18(%rcx)
movl 0x18(%rbx), %eax
notl %eax
addl %r14d, %eax
jmp 0x3e041
movl 0x8(%rbx), %eax
movl %eax, 0x8(%rcx)
movl 0x4(%rbx), %eax
movl %eax, 0x4(%rcx)
movl 0x18(%rbx), %eax
movl %eax, 0x18(%rcx)
movl 0x14(%rbx), %eax
movl %eax, 0x14(%rcx)
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq