// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq 0x8(%rdi), %r14
movq 0x10(%rdi), %rax
leaq 0x1(%rax), %rcx
cmpq %r14, %rcx
jbe 0x3e2ba
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x20, %rax
orq %rcx, %rax
incq %rax
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
imulq $0xa8, %rax, %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), %rcx
testq %rcx, %rcx
je 0x3e308
movq (%rbx), %rdx
imulq $0xa8, %rcx, %rax
movl -0x98(%rdx,%rax), %esi
cmpl 0x10(%r13), %esi
jne 0x3e308
addq %rax, %rdx
movl 0x24(%r13), %esi
xorl %eax, %eax
cmpl %esi, -0x84(%rdx)
jg 0x3e5f5
jne 0x3e301
movl -0x88(%rdx), %edx
cmpl 0x20(%r13), %edx
jbe 0x3e5f5
decq %rcx
movq %rcx, 0x10(%rbx)
leaq 0x4(%r13), %r14
movl 0x4(%r13), %eax
movl 0x14(%r13), %edx
leaq 0x14(%r13), %rcx
movl %eax, %esi
subl %edx, %esi
movq %r12, 0x20(%rsp)
jbe 0x3e328
movl %esi, (%r14)
jmp 0x3e32f
subl %eax, %edx
movl %edx, (%rcx)
movq %r14, %rcx
xorl %eax, %eax
movl %eax, (%rcx)
movl 0x8(%r13), %esi
movl 0x18(%r13), %ecx
movq %rsi, %rdi
notq %rdi
addq %r15, %rdi
movq %rcx, %rdx
notq %rdx
addq %rbp, %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %rdx, %rdi
setle %r8b
setg %r9b
cmovlq %rdi, %rdx
cmovgl %esi, %ecx
movl %r15d, %esi
cmovgl %ebp, %esi
shll $0x4, %r9d
shll $0x4, %r8d
decl %esi
movl %esi, 0x8(%r13,%r9)
addl %ecx, %edx
movl %edx, 0x8(%r13,%r8)
movq (%rbx), %r12
movq 0x10(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbx)
imulq $0xa8, %rcx, %rbx
leaq (%r12,%rbx), %rdx
movl 0x24(%r13), %ecx
movl %ecx, 0x24(%r12,%rbx)
movl %eax, 0x28(%r12,%rbx)
movb %al, 0x2c(%r12,%rbx)
movl %eax, 0x30(%r12,%rbx)
movb %al, 0x2e(%r12,%rbx)
movl (%r13), %ecx
movl %ecx, (%r12,%rbx)
movl 0x10(%r13), %ecx
movl %ecx, 0x10(%r12,%rbx)
movl %eax, 0xc(%r12,%rbx)
movl 0xc(%r13), %eax
movl %eax, 0x1c(%r12,%rbx)
movl 0x40(%r13), %eax
addl $0x2, %eax
cmpl %eax, 0x44(%r12,%rbx)
movq %rdx, (%rsp)
jae 0x3e403
movl %eax, %esi
movl %eax, 0x44(%rdx)
movq 0x38(%rdx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq (%rsp), %rdx
movq %rax, 0x38(%rdx)
leaq (%r12,%rbx), %rax
addq $0x38, %rax
movq %rax, 0x8(%rsp)
movl $0x0, 0x40(%rdx)
cmpq $0x1, 0x10(%rsp)
jne 0x3e447
cmpl $0x0, 0xc(%r13)
je 0x3e432
movl 0x8(%r13), %eax
notl %eax
leal (%r15,%rax), %esi
jmp 0x3e435
movl (%r14), %esi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3dd21
movq (%rsp), %rdx
leaq 0x38(%r13), %r14
cmpl $0x1, 0xc(%r13)
jne 0x3e4ed
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), %ebx
testq %rbx, %rbx
je 0x3e58c
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movabsq $0x100000000, %r13      # imm = 0x100000000
movq (%r14), %rcx
movq %r12, %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 0x3e4e0
shrq $0x20, %rsi
notl %esi
addl %r15d, %esi
movq 0x8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3dd21
movq %rbp, %rax
addq %r13, %r12
decq %rbx
jne 0x3e4a3
jmp 0x3e58c
movl 0x8(%r13), %eax
movl %eax, 0x8(%rdx)
movl 0x4(%r13), %eax
movl %eax, 0x4(%rdx)
movl 0x18(%r13), %eax
movl %eax, 0x18(%rdx)
movl 0x14(%r13), %eax
movl %eax, 0x14(%rdx)
movl 0x40(%r13), %r15d
testq %r15, %r15
je 0x3e58c
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
movl 0x4(%r13), %eax
movl 0x8(%r13), %ebx
addl 0x18(%r13), %eax
movl %eax, 0x1c(%rsp)
addl 0x14(%r13), %ebx
leaq 0x1(%r15), %r13
shlq $0x20, %r15
addq %r12, %r15
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq (%r14), %rcx
movq %r15, %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, %rbp
cmpq %rbp, %rax
je 0x3e580
shrq $0x20, %rsi
addl %ebx, %ecx
movl 0x1c(%rsp), %edx
subl %ecx, %edx
movq 0x8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3dd21
movq %rbp, %rax
decq %r13
addq %r12, %r15
cmpq $0x1, %r13
jg 0x3e540
movq 0x60(%rsp), %r15
cmpq $0x1, 0x10(%rsp)
movq (%rsp), %rdx
jne 0x3e5d8
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 0x3e5d8
movl %eax, %ecx
shrl %ecx
movl %ecx, %edx
negl %edx
testb $0x1, %al
cmovel %ecx, %edx
movq 0x8(%rsp), %rdi
xorl %ecx, %ecx
callq 0x3dd21
movq (%rsp), %rdx
movq %r14, %rdi
movq %rdx, %rsi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r8
callq 0x3ddb3
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq