// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movq %r8, 0x18(%rsp)
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
movq 0x8(%rdi), %r12
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rax
cmpq %r12, %rax
jbe 0x120f2
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(%r14)
movq (%r14), %rdi
imulq $0xa8, %rcx, %rsi
callq 0x2370
movq %rax, (%r14)
imulq $0xa8, %r12, %rdi
addq %rax, %rdi
movq 0x8(%r14), %rax
subq %r12, %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%r14), %rcx
xorl %edx, %edx
testq %rcx, %rcx
je 0x1213e
movq (%r14), %rsi
imulq $0xa8, %rcx, %rax
movl -0x98(%rsi,%rax), %edi
cmpl 0x10(%rbp), %edi
jne 0x12147
addq %rax, %rsi
movl 0x24(%rbp), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rsi)
jg 0x123d2
jne 0x12134
movl -0x88(%rsi), %esi
cmpl 0x20(%rbp), %esi
jbe 0x123d2
movq %r15, 0x10(%rsp)
decq %rcx
jmp 0x1214c
movq %r15, 0x10(%rsp)
xorl %ecx, %ecx
jmp 0x1214c
movq %r15, 0x10(%rsp)
movl 0x14(%rbp), %esi
movl 0x4(%rbp), %edi
movl 0x8(%rbp), %eax
movl %edi, %r8d
subl %esi, %r8d
cmovbl %edx, %r8d
subl %edi, %esi
cmovbl %edx, %esi
movl %r8d, 0x4(%rbp)
movl %esi, 0x14(%rbp)
movl 0x18(%rbp), %esi
movq %rsi, %rdi
notq %rdi
addq %rbx, %rdi
leal (%rdi,%rax), %r8d
notq %rax
movq 0x8(%rsp), %r10
addq %r10, %rax
leal -0x1(%rbx), %r9d
decl %r10d
addl %eax, %esi
cmpq %rdi, %rax
cmovgl %r9d, %esi
cmovgl %r8d, %r10d
movl %esi, 0x18(%rbp)
movl %r10d, 0x8(%rbp)
movq (%r14), %r12
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%r14)
imulq $0xa8, %rcx, %r14
movl 0x24(%rbp), %eax
leaq (%r12,%r14), %r13
addq $0x38, %r13
movl %eax, -0x14(%r13)
andl $0x0, -0x10(%r13)
movb %dl, -0xc(%r13)
andl $0x0, -0x8(%r13)
movb %dl, -0xa(%r13)
movl (%rbp), %eax
movl %eax, -0x38(%r13)
movl 0x10(%rbp), %eax
movl %eax, -0x28(%r13)
andl $0x0, -0x2c(%r13)
movl 0xc(%rbp), %eax
movl %eax, -0x1c(%r13)
movl 0x40(%rbp), %esi
addl $0x2, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x123e1
cmpq $0x1, 0x18(%rsp)
jne 0x1222a
cmpl $0x0, 0xc(%rbp)
je 0x1221b
movl 0x8(%rbp), %eax
notl %eax
movq 0x8(%rsp), %rcx
leal (%rcx,%rax), %esi
jmp 0x1221e
movl 0x4(%rbp), %esi
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x11af6
addq %r14, %r12
leaq 0x38(%rbp), %r14
cmpl $0x1, 0xc(%rbp)
movq %r12, 0x20(%rsp)
jne 0x122d4
movl 0x4(%rbp), %eax
notl %eax
movq 0x8(%rsp), %rcx
addl %ecx, %eax
movl %eax, 0x8(%r12)
movl 0x8(%rbp), %eax
notl %eax
addl %ecx, %eax
movl %eax, 0x4(%r12)
movl 0x14(%rbp), %eax
notl %eax
addl %ebx, %eax
movl %eax, 0x18(%r12)
movl 0x18(%rbp), %eax
notl %eax
addl %ebx, %eax
movl %eax, 0x14(%r12)
movl 0x40(%rbp), %ebx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movabsq $0x100000000, %r12      # imm = 0x100000000
subq $0x1, %rbx
jb 0x1236e
movq (%r14), %rcx
movq %rbp, %rdx
sarq $0x1d, %rdx
movq (%rcx,%rdx), %rsi
movl %esi, %ecx
shrl %ecx
movl %ecx, %edx
negl %edx
testb $0x1, %sil
cmovel %ecx, %edx
movslq %edx, %r15
cmpq %r15, %rax
je 0x122cf
shrq $0x20, %rsi
notl %esi
addl 0x8(%rsp), %esi
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x11af6
movq %r15, %rax
addq %r12, %rbp
jmp 0x12289
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
movl 0x8(%rbp), %r8d
movl %r8d, 0x8(%r12)
movl 0x4(%rbp), %r15d
movl %r15d, 0x4(%r12)
movl 0x18(%rbp), %eax
movl %eax, 0x18(%r12)
movl 0x14(%rbp), %ecx
movl %ecx, 0x14(%r12)
movl 0x40(%rbp), %ebp
addl %eax, %r15d
addl %ecx, %r8d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbp, %rcx
shlq $0x20, %rcx
addq %rbx, %rcx
testq %rbp, %rbp
jle 0x1236e
decq %rbp
leaq (%rcx,%rbx), %rdx
movq (%r14), %rsi
sarq $0x1d, %rcx
movq (%rsi,%rcx), %rsi
movl %esi, %ecx
shrl %ecx
movl %ecx, %edi
negl %edi
testb $0x1, %sil
cmovel %ecx, %edi
movslq %edi, %r12
movq %rdx, %rcx
cmpq %r12, %rax
je 0x12318
shrq $0x20, %rsi
addl %r8d, %edi
movl %r15d, %edx
subl %edi, %edx
movq %r13, %rdi
xorl %ecx, %ecx
movl %r8d, 0x8(%rsp)
callq 0x11af6
movl 0x8(%rsp), %r8d
movq %r12, %rax
jmp 0x1230e
movq 0x60(%rsp), %rbx
cmpq $0x1, 0x18(%rsp)
movq 0x20(%rsp), %r15
jne 0x123b7
movl 0x40(%r15), %eax
decl %eax
movq 0x38(%r15), %rcx
cltq
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
sarq $0x20, %rcx
movl 0x8(%r15), %esi
cmpq %rsi, %rcx
je 0x123b7
movl %eax, %ecx
shrl %ecx
movl %ecx, %edx
negl %edx
testb $0x1, %al
cmovel %ecx, %edx
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x11af6
movq %r14, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
callq 0x11b88
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq