// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %r8, 0x18(%rsp)
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq 0x8(%rdi), %r14
movq 0x10(%rdi), %rdx
leaq 0x1(%rdx), %rax
cmpq %r14, %rax
jbe 0x3d76d
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(%rbx)
movq (%rbx), %rdi
imulq $0xa8, %rcx, %rsi
callq 0x2370
movq %rax, (%rbx)
imulq $0xa8, %r14, %rdi
addq %rax, %rdi
movq 0x8(%rbx), %rax
subq %r14, %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%rbx), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x3d7b7
movq (%rbx), %rsi
imulq $0xa8, %rdx, %rax
movl -0x98(%rsi,%rax), %edi
cmpl 0x10(%r13), %edi
jne 0x3d7b9
addq %rax, %rsi
movl 0x24(%r13), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rsi)
jg 0x3da86
jne 0x3d7b2
movl -0x88(%rsi), %esi
cmpl 0x20(%r13), %esi
jbe 0x3da86
decq %rdx
jmp 0x3d7b9
xorl %edx, %edx
movl 0x14(%r13), %esi
movl 0x4(%r13), %edi
movl 0x8(%r13), %eax
movl %edi, %r8d
subl %esi, %r8d
cmovbl %ecx, %r8d
subl %edi, %esi
cmovbl %ecx, %esi
movl %r8d, 0x4(%r13)
movl %esi, 0x14(%r13)
movl 0x18(%r13), %esi
movq %rsi, %rdi
notq %rdi
addq %rbp, %rdi
leal (%rdi,%rax), %r8d
notq %rax
addq %r15, %rax
leal -0x1(%rbp), %r9d
leal -0x1(%r15), %r10d
addl %eax, %esi
cmpq %rdi, %rax
cmovgl %r9d, %esi
cmovgl %r8d, %r10d
movl %esi, 0x18(%r13)
movl %r10d, 0x8(%r13)
movq (%rbx), %r14
leaq 0x1(%rdx), %rax
movq %rax, 0x10(%rbx)
imulq $0xa8, %rdx, %rbx
leaq (%r14,%rbx), %rdx
movl 0x24(%r13), %eax
movl %eax, 0x24(%r14,%rbx)
movl %ecx, 0x28(%r14,%rbx)
movb %cl, 0x2c(%r14,%rbx)
movl %ecx, 0x30(%r14,%rbx)
movb %cl, 0x2e(%r14,%rbx)
movl (%r13), %eax
movl %eax, (%r14,%rbx)
movl 0x10(%r13), %eax
movl %eax, 0x10(%r14,%rbx)
movl %ecx, 0xc(%r14,%rbx)
movl 0xc(%r13), %eax
movl %eax, 0x1c(%r14,%rbx)
movl 0x40(%r13), %ecx
addl $0x2, %ecx
cmpl %ecx, 0x44(%r14,%rbx)
movq %rdx, 0x8(%rsp)
jae 0x3d894
movl %ecx, %esi
movl %ecx, 0x44(%rdx)
movq 0x38(%rdx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, 0x38(%rdx)
movl 0xc(%r13), %eax
leaq (%r14,%rbx), %rcx
addq $0x38, %rcx
movq %rcx, 0x10(%rsp)
movl $0x0, 0x40(%rdx)
cmpq $0x1, 0x18(%rsp)
jne 0x3d8db
testl %eax, %eax
je 0x3d8c0
movl 0x8(%r13), %eax
notl %eax
leal (%r15,%rax), %esi
jmp 0x3d8c4
movl 0x4(%r13), %esi
movq 0x10(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d202
movl 0xc(%r13), %eax
movq 0x8(%rsp), %rdx
movq %r12, 0x20(%rsp)
leaq 0x38(%r13), %r14
cmpl $0x1, %eax
jne 0x3d983
movl 0x4(%r13), %eax
notl %eax
addl %r15d, %eax
movl %eax, 0x8(%rdx)
movl 0x8(%r13), %eax
notl %eax
addl %r15d, %eax
movl %eax, 0x4(%rdx)
movl 0x14(%r13), %eax
notl %eax
addl %ebp, %eax
movl %eax, 0x18(%rdx)
movl 0x18(%r13), %eax
notl %eax
addl %ebp, %eax
movl %eax, 0x14(%rdx)
movl 0x40(%r13), %r12d
testq %r12, %r12
je 0x3da1b
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movabsq $0x100000000, %r13      # imm = 0x100000000
movq (%r14), %rcx
movq %rbx, %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, %rbp
cmpq %rbp, %rax
je 0x3d976
shrq $0x20, %rsi
notl %esi
addl %r15d, %esi
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3d202
movq %rbp, %rax
addq %r13, %rbx
decq %r12
jne 0x3d939
jmp 0x3da1b
movl 0x8(%r13), %eax
movl %eax, 0x4(%rsp)
movl %eax, 0x8(%rdx)
movl 0x4(%r13), %r15d
movl %r15d, 0x4(%rdx)
movl 0x18(%r13), %eax
movl %eax, 0x18(%rdx)
movl 0x14(%r13), %ecx
movl %ecx, 0x14(%rdx)
movl 0x40(%r13), %r13d
testq %r13, %r13
je 0x3da1b
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
addl %eax, %r15d
addl %ecx, 0x4(%rsp)
leaq 0x1(%r13), %rbx
shlq $0x20, %r13
addq %rbp, %r13
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq (%r14), %rcx
movq %r13, %rdx
sarq $0x1d, %rdx
movq (%rcx,%rdx), %rsi
movl %esi, %edx
shrl %edx
movl %edx, %ecx
negl %ecx
testb $0x1, %sil
cmovel %edx, %ecx
movslq %ecx, %r12
cmpq %r12, %rax
je 0x3da0f
shrq $0x20, %rsi
addl 0x4(%rsp), %ecx
movl %r15d, %edx
subl %ecx, %edx
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3d202
movq %r12, %rax
decq %rbx
addq %rbp, %r13
cmpq $0x1, %rbx
ja 0x3d9ce
movq 0x60(%rsp), %r15
cmpq $0x1, 0x18(%rsp)
movq 0x8(%rsp), %rdx
jne 0x3da69
movl 0x40(%rdx), %eax
decl %eax
movq 0x38(%rdx), %rcx
cltq
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
sarq $0x20, %rcx
movl 0x8(%rdx), %esi
cmpq %rsi, %rcx
je 0x3da69
movl %eax, %ecx
shrl %ecx
movl %ecx, %edx
negl %edx
testb $0x1, %al
cmovel %ecx, %edx
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3d202
movq 0x8(%rsp), %rdx
movq %r14, %rdi
movq %rdx, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r8
callq 0x3d296
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq