// File: /workspace/github_cpp_makefile_star2_O2/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), %rcx
leaq 0x1(%rcx), %rax
cmpq %r13, %rax
jbe 0x11d5e
movq %rcx, %rax
shrq %rax
orq %rcx, %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), %rcx
xorl %edx, %edx
testq %rcx, %rcx
je 0x11da9
movq (%r12), %rsi
imulq $0xa8, %rcx, %rax
movl -0x98(%rsi,%rax), %edi
cmpl 0x10(%r14), %edi
jne 0x11dab
addq %rax, %rsi
movl 0x24(%r14), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rsi)
jg 0x11e94
jne 0x11da4
movl -0x88(%rsi), %esi
cmpl 0x20(%r14), %esi
jbe 0x11e94
decq %rcx
jmp 0x11dab
xorl %ecx, %ecx
movl 0x14(%r14), %eax
movl 0x4(%r14), %r8d
movl 0x8(%r14), %edi
movl %r8d, %esi
subl %eax, %esi
cmovbl %edx, %esi
subl %r8d, %eax
cmovbl %edx, %eax
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 -0x1(%r15), %edi
addl %r11d, %r8d
cmpq %r9, %r11
cmovgl %ebp, %r8d
cmovgl %r10d, %edi
movl %r8d, 0x18(%r14)
movl %edi, 0x8(%r14)
movq (%r12), %r9
leaq 0x1(%rcx), %r10
movq %r10, 0x10(%r12)
imulq $0xa8, %rcx, %r10
movl 0x24(%r14), %ecx
movl %ecx, 0x24(%r9,%r10)
andl $0x0, 0x28(%r9,%r10)
movb %dl, 0x2c(%r9,%r10)
andl $0x0, 0x30(%r9,%r10)
movb %dl, 0x2e(%r9,%r10)
movl (%r14), %ecx
movl %ecx, (%r9,%r10)
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%r9,%r10)
andl $0x0, 0xc(%r9,%r10)
leaq (%r9,%r10), %rcx
movl 0xc(%r14), %edx
movl %edx, 0x1c(%r9,%r10)
cmpl $0x1, %edx
jne 0x11e84
notl %esi
addl %r15d, %esi
movl %esi, 0x8(%rcx)
movl 0x8(%r14), %edx
notl %edx
addl %r15d, %edx
movl %edx, 0x4(%rcx)
notl %eax
addl %ebx, %eax
movl %eax, 0x18(%rcx)
movl 0x18(%r14), %eax
notl %eax
addl %ebx, %eax
jmp 0x11e8e
movl %edi, 0x8(%rcx)
movl %esi, 0x4(%rcx)
movl %r8d, 0x18(%rcx)
movl %eax, 0x14(%rcx)
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq