// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %rdx
leaq 0x1(%rdx), %rax
cmpq %r13, %rax
jbe 0x3d3dd
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x20, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x8(%r12)
movq (%r12), %rdi
imulq $0xa8, %rcx, %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), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x3d428
movq (%r12), %rsi
imulq $0xa8, %rdx, %rax
movl -0x98(%rsi,%rax), %edi
cmpl 0x10(%r14), %edi
jne 0x3d42a
addq %rax, %rsi
movl 0x24(%r14), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rsi)
jg 0x3d510
jne 0x3d423
movl -0x88(%rsi), %esi
cmpl 0x20(%r14), %esi
jbe 0x3d510
decq %rdx
jmp 0x3d42a
xorl %edx, %edx
movl 0x14(%r14), %eax
movl 0x4(%r14), %edi
movl %edi, %esi
subl %eax, %esi
cmovbl %ecx, %esi
subl %edi, %eax
cmovbl %ecx, %eax
movl 0x8(%r14), %edi
movl %esi, 0x4(%r14)
movl %eax, 0x14(%r14)
movl 0x18(%r14), %r8d
movq %r8, %r9
notq %r9
addq %rbx, %r9
leal (%r9,%rdi), %r10d
movq %rdi, %r11
notq %r11
addq %r15, %r11
leal -0x1(%rbx), %ebp
leal (%r11,%r8), %edi
cmpq %r9, %r11
cmovgl %ebp, %edi
leal -0x1(%r15), %r8d
cmovgl %r10d, %r8d
movl %edi, 0x18(%r14)
movl %r8d, 0x8(%r14)
movq (%r12), %r9
leaq 0x1(%rdx), %r10
movq %r10, 0x10(%r12)
imulq $0xa8, %rdx, %r10
leaq (%r9,%r10), %rdx
movl 0x24(%r14), %r11d
movl %r11d, 0x24(%r9,%r10)
movl %ecx, 0x28(%r9,%r10)
movb %cl, 0x2c(%r9,%r10)
movl %ecx, 0x30(%r9,%r10)
movb %cl, 0x2e(%r9,%r10)
movl (%r14), %r11d
movl %r11d, (%r9,%r10)
movl 0x10(%r14), %r11d
movl %r11d, 0x10(%r9,%r10)
movl %ecx, 0xc(%r9,%r10)
movl 0xc(%r14), %ecx
movl %ecx, 0x1c(%r9,%r10)
cmpl $0x1, %ecx
jne 0x3d4fe
notl %esi
addl %r15d, %esi
movl %esi, 0x8(%rdx)
movl 0x8(%r14), %ecx
notl %ecx
addl %r15d, %ecx
movl %ecx, 0x4(%rdx)
notl %eax
addl %ebx, %eax
movl %eax, 0x18(%rdx)
movl 0x18(%r14), %eax
notl %eax
addl %ebx, %eax
jmp 0x3d508
movl %r8d, 0x8(%rdx)
movl %esi, 0x4(%rdx)
movl %edi, 0x18(%rdx)
movl %eax, 0x14(%rdx)
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq