// File: chhylp123[P]hifiasm/Hash_Table.cpp
// Function: ovlp_chain_gen_fcigar(overlap_region_alloc*, overlap_region*, long, long, long, k_mer_hit*, long)
int ovlp_chain_gen_fcigar(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, dd, pdd, dq, dr, id, i, fn; 
    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;

    resize_fake_cigar(&(o->f_cigar), (t->f_cigar.length + 2), NULL);
    if(apend_be == 1) {
        add_fake_cigar(&(o->f_cigar), ((t->x_pos_strand)?(xl-t->x_pos_e-1):(t->x_pos_s)), 0, NULL);
    }

    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;
        
        pdd = INT32_MAX; fn = t->f_cigar.length;
        for (i = 0; i < fn; i++) {
            dd = get_fake_gap_shift(&(t->f_cigar), i);
            if(dd != pdd) {
                pdd = dd;
                add_fake_cigar(&(o->f_cigar), xl-get_fake_gap_pos(&(t->f_cigar), i)-1, pdd, NULL);
            }
        }
    } 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;
    
        dq = t->x_pos_e - t->x_pos_s;
        dr = t->y_pos_e - t->y_pos_s;
        id = dr - dq;///indel size from left

        pdd = INT32_MAX; fn = t->f_cigar.length;
        for (i = fn-1; i >= 0; i--) {
            dd = get_fake_gap_shift(&(t->f_cigar), i);
            if(dd != pdd) {
                pdd = dd;
                add_fake_cigar(&(o->f_cigar), get_fake_gap_pos(&(t->f_cigar), i), id - pdd, NULL);
            }
        }
    }

    if((apend_be == 1) && (get_fake_gap_pos(&(o->f_cigar), o->f_cigar.length-1) != ((int64_t)o->x_pos_e))) {
        add_fake_cigar(&(o->f_cigar), o->x_pos_e, get_fake_gap_shift(&(o->f_cigar), o->f_cigar.length-1), NULL);
    }

    ///debug
    debug_cigar(&(t->f_cigar), o, apend_be, hit, n_hit);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %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 0x1f1fe
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 0x1f2d3
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 0x1f2d3
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 0x1f2b5
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 0x1f2c1
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 0x1f2c1
movl $0x0, -0x4(%rbp)
jmp 0x1f802
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0x1f2d3
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x14(%rcx), %eax
ja 0x1f303
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 0x1f321
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 0x1f37b
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 0x1f39d
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, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl 0x24(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x90(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x90(%rbp), %rax
movb $0x0, 0x2c(%rax)
movq -0x90(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x90(%rbp), %rax
movb $0x0, 0x2e(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x90(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x90(%rbp), %rdi
addq $0x38, %rdi
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
addl $0x2, %eax
movl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x1f810
cmpq $0x1, -0x30(%rbp)
jne 0x1f4d3
movq -0x90(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x1f4aa
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x1f4b8
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl %eax, %esi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1e500
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x1f626
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x90(%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 -0x90(%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 -0x90(%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 -0x90(%rbp), %rax
movl %ecx, 0x14(%rax)
movq $0x7fffffff, -0x60(%rbp) # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movq %rax, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x1f621
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
movq -0x80(%rbp), %rax
movl %eax, %esi
callq 0x1e340
cltq
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x1f60e
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x90(%rbp), %rax
addq $0x38, %rax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
movq -0x80(%rbp), %rax
movl %eax, %esi
callq 0x1e320
movq -0xb0(%rbp), %rdi
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
subq %rcx, %rax
subq $0x1, %rax
movl %eax, %esi
movq -0x60(%rbp), %rax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1e500
jmp 0x1f610
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1f577
jmp 0x1f74b
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x14(%rcx), %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
subq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x7fffffff, -0x60(%rbp) # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x1f749
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
movq -0x80(%rbp), %rax
movl %eax, %esi
callq 0x1e340
cltq
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x1f736
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x90(%rbp), %rax
addq $0x38, %rax
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
movq -0x80(%rbp), %rax
movl %eax, %esi
callq 0x1e320
movq -0xb8(%rbp), %rdi
movl %eax, %esi
movq -0x78(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1e500
jmp 0x1f738
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1f6c3
jmp 0x1f74b
cmpq $0x1, -0x30(%rbp)
jne 0x1f7db
movq -0x90(%rbp), %rdi
addq $0x38, %rdi
movq -0x90(%rbp), %rax
movl 0x40(%rax), %esi
subl $0x1, %esi
callq 0x1e320
cltq
movq -0x90(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
je 0x1f7db
movq -0x90(%rbp), %rax
addq $0x38, %rax
movq %rax, -0xc8(%rbp)
movq -0x90(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0xbc(%rbp)
movq -0x90(%rbp), %rdi
addq $0x38, %rdi
movq -0x90(%rbp), %rax
movl 0x40(%rax), %esi
subl $0x1, %esi
callq 0x1e340
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %esi
movl %eax, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1e500
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
movq -0x90(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rbp), %r8
callq 0x1e6c0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nop