// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: append_inexact_overlap_region_alloc(overlap_region_alloc*, overlap_region*, long long, long long, int, void*)
int append_inexact_overlap_region_alloc(overlap_region_alloc* list, overlap_region* tmp, 
                                        long long xLen, long long yLen, int add_beg_end, void *km)
{
   
    if (list->length + 1 > list->size)
    {
        uint64_t sl = list->size;
        list->size = list->length + 1;
        kroundup64(list->size);
        if(!km) {
            REALLOC(list->list, list->size);
        } else {
            KREALLOC(km, list->list, list->size);
        }
        /// need to set new space to be 0
        memset(list->list + sl, 0, sizeof(overlap_region)*(list->size - sl));
    }

    if (list->length!=0 && list->list[list->length - 1].y_id==tmp->y_id)
    {    
        ///if(list->list[list->length - 1].shared_seed >= tmp->shared_seed)
        if((list->list[list->length - 1].shared_seed > tmp->shared_seed)
           ||
           ((list->list[list->length - 1].shared_seed == tmp->shared_seed) && 
           (list->list[list->length - 1].overlapLen <= tmp->overlapLen)))
        {
            return 0;
        }
        else
        {
            list->length--;
        }
    }

    if(tmp->x_pos_s <= tmp->y_pos_s)
    {
        tmp->y_pos_s = tmp->y_pos_s - tmp->x_pos_s;
        tmp->x_pos_s = 0;
    }
    else
    {
        tmp->x_pos_s = tmp->x_pos_s - tmp->y_pos_s;
        tmp->y_pos_s = 0;
    }


    long long x_right_length = xLen - tmp->x_pos_e - 1;
    long long y_right_length = yLen - tmp->y_pos_e - 1;

    if(x_right_length <= y_right_length)
    {
        tmp->x_pos_e = xLen - 1;
        tmp->y_pos_e = tmp->y_pos_e + x_right_length;        
    }
    else
    {
        tmp->x_pos_e = tmp->x_pos_e + y_right_length;
        tmp->y_pos_e = yLen - 1;
    }
    
    if (tmp->x_pos_strand == 1)
    {
        list->list[list->length].x_id = tmp->x_id;
        list->list[list->length].x_pos_e = xLen - tmp->x_pos_s - 1;
        list->list[list->length].x_pos_s = xLen - tmp->x_pos_e - 1;
        list->list[list->length].x_pos_strand = 0;

        list->list[list->length].y_id = tmp->y_id;
        list->list[list->length].y_pos_e = yLen - tmp->y_pos_s - 1;
        list->list[list->length].y_pos_s = yLen - tmp->y_pos_e - 1;
        list->list[list->length].y_pos_strand = 1;

        resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2), km);
        if(add_beg_end == 1)
        {
            add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0, km);
        }
        
        long long distance_gap;
        /****************************may have bugs********************************/
        ///long long pre_distance_gap = 0;
        long long pre_distance_gap = 0xfffffffffffffff;
        /****************************may have bugs********************************/
        long long i = 0;
        for (i = 0; i < (long long)tmp->f_cigar.length; i++)
        {
            distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i);
            if(distance_gap != pre_distance_gap)
            {
                pre_distance_gap = distance_gap;
                add_fake_cigar(&(list->list[list->length].f_cigar), xLen - get_fake_gap_pos(&(tmp->f_cigar), i) - 1, 
                pre_distance_gap, km);
            }
        }

        if(add_beg_end == 1 && get_fake_gap_pos(&(list->list[list->length].f_cigar), 
            list->list[list->length].f_cigar.length - 1) != (long long)list->list[list->length].x_pos_e)
        {
            add_fake_cigar(&(list->list[list->length].f_cigar), 
            list->list[list->length].x_pos_e, 
            get_fake_gap_shift(&(list->list[list->length].f_cigar), 
            list->list[list->length].f_cigar.length - 1), km);
        }
    }
    else
    {
        list->list[list->length].x_id = tmp->x_id;
        list->list[list->length].x_pos_e = tmp->x_pos_e;
        list->list[list->length].x_pos_s = tmp->x_pos_s;
        list->list[list->length].x_pos_strand = tmp->x_pos_strand;

        list->list[list->length].y_id = tmp->y_id;
        list->list[list->length].y_pos_e = tmp->y_pos_e;
        list->list[list->length].y_pos_s = tmp->y_pos_s;
        list->list[list->length].y_pos_strand = tmp->y_pos_strand;



        resize_fake_cigar(&(list->list[list->length].f_cigar), (tmp->f_cigar.length + 2), km);
        if(add_beg_end == 1)
        {
            add_fake_cigar(&(list->list[list->length].f_cigar), list->list[list->length].x_pos_s, 0, km);
        }
        
        long long distance_self_pos = tmp->x_pos_e - tmp->x_pos_s;
        long long distance_pos = tmp->y_pos_e - tmp->y_pos_s;
        long long init_distance_gap = distance_pos - distance_self_pos;
        /****************************may have bugs********************************/
        ///long long pre_distance_gap = init_distance_gap;
        long long pre_distance_gap = 0xfffffffffffffff;
        /****************************may have bugs********************************/
        long long distance_gap;
        long long i = 0;
        for (i = tmp->f_cigar.length - 1; i >= 0; i--)
        {
            distance_gap = get_fake_gap_shift(&(tmp->f_cigar), i);
            if(distance_gap != pre_distance_gap)
            {
                pre_distance_gap = distance_gap;

                add_fake_cigar(&(list->list[list->length].f_cigar), 
                get_fake_gap_pos(&(tmp->f_cigar), i), init_distance_gap - pre_distance_gap, km);
            }
        }

        if(add_beg_end == 1 && get_fake_gap_pos(&(list->list[list->length].f_cigar), 
        list->list[list->length].f_cigar.length - 1) != (long long)list->list[list->length].x_pos_e)
        {
            add_fake_cigar(&(list->list[list->length].f_cigar), 
            list->list[list->length].x_pos_e, 
            get_fake_gap_shift(&(list->list[list->length].f_cigar), 
            list->list[list->length].f_cigar.length - 1), km);
        } 
    }

    list->list[list->length].shared_seed = tmp->shared_seed;
    list->list[list->length].align_length = 0;
    list->list[list->length].is_match = 0;
    list->list[list->length].non_homopolymer_errors = 0;
    list->list[list->length].strong = 0;

    list->length++;

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movl %r8d, %r12d
movq %rcx, %r13
movq %rdx, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rbp
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rax
cmpq %rbp, %rax
movq %rsi, 0x10(%rsp)
jbe 0x3db9e
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)
imulq $0xa8, %rcx, %rdx
movq (%r14), %rsi
testq %r15, %r15
je 0x3db68
movq %r15, %rdi
callq 0x1ee21d
jmp 0x3db73
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2370
movq %rax, (%r14)
imulq $0xa8, %rbp, %rdi
addq %rax, %rdi
movq 0x8(%r14), %rax
subq %rbp, %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%r14), %rcx
movq 0x10(%rsp), %rsi
xorl %r8d, %r8d
testq %rcx, %rcx
je 0x3dbef
movq (%r14), %rdx
imulq $0xa8, %rcx, %rax
movl -0x98(%rdx,%rax), %edi
cmpl 0x10(%rsi), %edi
jne 0x3dbf8
addq %rax, %rdx
movl 0x24(%rsi), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rdx)
jg 0x3df19
jne 0x3dbe1
movl -0x88(%rdx), %edx
cmpl 0x20(%rsi), %edx
jbe 0x3df19
movq %r15, 0x8(%rsp)
decq %rcx
movq %rcx, 0x10(%r14)
jmp 0x3dbfd
movq %r15, 0x8(%rsp)
xorl %ecx, %ecx
jmp 0x3dbfd
movq %r15, 0x8(%rsp)
movl 0x4(%rsi), %edi
movl 0x14(%rsi), %eax
movl %edi, %edx
subl %eax, %edx
cmovbl %r8d, %edx
subl %edi, %eax
cmovbl %r8d, %eax
movl (%rsi), %ebp
movl %edx, 0x4(%rsi)
movl %eax, 0x14(%rsi)
movl 0x8(%rsi), %edi
movl 0x18(%rsi), %r8d
movq %r8, %r10
notq %r10
addq %r13, %r10
leal (%r10,%rdi), %r11d
notq %rdi
addq %rbx, %rdi
leal -0x1(%r13), %r15d
leal -0x1(%rbx), %r9d
addl %edi, %r8d
cmpq %r10, %rdi
cmovgl %r11d, %r9d
movq 0x10(%rsp), %rsi
cmovgl %r15d, %r8d
movl %r9d, 0x8(%rsi)
movl %r8d, 0x18(%rsi)
movl 0xc(%rsi), %r10d
imulq $0xa8, %rcx, %rcx
movq (%r14), %rdi
movl %ebp, (%rdi,%rcx)
cmpl $0x1, %r10d
movl %r12d, 0x1c(%rsp)
jne 0x3dd86
notl %edx
addl %ebx, %edx
addq %rcx, %rdi
addq $0x38, %rdi
movl %edx, -0x30(%rdi)
movl 0x8(%rsi), %ecx
notl %ecx
movq %rbx, %r15
addl %ebx, %ecx
movl %ecx, -0x34(%rdi)
movl $0x0, -0x2c(%rdi)
movl 0x10(%rsi), %ecx
movl %ecx, -0x28(%rdi)
notl %eax
addl %r13d, %eax
movl %eax, -0x20(%rdi)
movl 0x18(%rsi), %eax
notl %eax
addl %r13d, %eax
movl %eax, -0x24(%rdi)
movl $0x1, -0x1c(%rdi)
movl 0x40(%rsi), %esi
addl $0x2, %esi
movq 0x8(%rsp), %rdx
callq 0x3da95
cmpl $0x1, %r12d
jne 0x3dcf0
movq (%r14), %rax
imulq $0xa8, 0x10(%r14), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
movl -0x34(%rdi), %esi
xorl %edx, %edx
movq 0x8(%rsp), %rcx
callq 0x3d202
movq 0x10(%rsp), %r8
movl 0x40(%r8), %eax
testl %eax, %eax
je 0x3dd7c
movabsq $0xfffffffffffffff, %rcx # imm = 0xFFFFFFFFFFFFFFF
xorl %r13d, %r13d
movabsq $0x100000000, %rbp      # imm = 0x100000000
xorl %r12d, %r12d
movq 0x38(%r8), %rdx
movq %r13, %rsi
sarq $0x1d, %rsi
movq (%rdx,%rsi), %rsi
movl %esi, %edi
shrl %edi
movl %edi, %edx
negl %edx
testb $0x1, %sil
cmovel %edi, %edx
movslq %edx, %rbx
cmpq %rbx, %rcx
je 0x3dd6f
movq (%r14), %rax
imulq $0xa8, 0x10(%r14), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
shrq $0x20, %rsi
notl %esi
addl %r15d, %esi
movq 0x8(%rsp), %rcx
callq 0x3d202
movq 0x10(%rsp), %r8
movl 0x40(%r8), %eax
movq %rbx, %rcx
incq %r12
movl %eax, %edx
addq %rbp, %r13
cmpq %rdx, %r12
jb 0x3dd17
cmpl $0x1, 0x1c(%rsp)
jmp 0x3de8e
addq %rcx, %rdi
addq $0x38, %rdi
movl %r9d, -0x30(%rdi)
movl %edx, -0x34(%rdi)
movl %r10d, -0x2c(%rdi)
movl 0x10(%rsi), %ecx
movl %ecx, -0x28(%rdi)
movl %r8d, -0x20(%rdi)
movl %eax, -0x24(%rdi)
movl 0x1c(%rsi), %eax
movl %eax, -0x1c(%rdi)
movl 0x40(%rsi), %esi
addl $0x2, %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x3da95
cmpl $0x1, %r12d
jne 0x3dde4
movq (%r14), %rax
imulq $0xa8, 0x10(%r14), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
movl -0x34(%rdi), %esi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x3d202
movq %r14, 0x20(%rsp)
movabsq $0xfffffffffffffff, %rax # imm = 0xFFFFFFFFFFFFFFF
movq 0x10(%rsp), %r8
movl 0x4(%r8), %r12d
movl 0x8(%r8), %r13d
movl 0x40(%r8), %ebp
addl 0x18(%r8), %r12d
addl 0x14(%r8), %r13d
decl %ebp
leaq 0x1(%rbp), %r15
shlq $0x20, %rbp
movabsq $-0x100000000, %rbx     # imm = 0xFFFFFFFF00000000
movq 0x38(%r8), %rcx
movq %rbp, %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, %r14
cmpq %r14, %rax
je 0x3de7c
movq 0x20(%rsp), %rdx
movq (%rdx), %rax
imulq $0xa8, 0x10(%rdx), %rdx
leaq (%rax,%rdx), %rdi
addq $0x38, %rdi
shrq $0x20, %rsi
addl %r13d, %ecx
movl %r12d, %edx
subl %ecx, %edx
movq 0x8(%rsp), %rcx
callq 0x3d202
movq 0x10(%rsp), %r8
movq %r14, %rax
addq %rbx, %rbp
decq %r15
jg 0x3de20
cmpl $0x1, 0x1c(%rsp)
movq 0x20(%rsp), %r14
movq 0x8(%rsp), %rcx
jne 0x3dee5
movq (%r14), %rdi
imulq $0xa8, 0x10(%r14), %r9
movl 0x40(%rdi,%r9), %eax
decl %eax
movq 0x38(%rdi,%r9), %rdx
cltq
movq (%rdx,%rax,8), %rax
movq %rax, %rdx
sarq $0x20, %rdx
movl 0x8(%rdi,%r9), %esi
cmpq %rsi, %rdx
je 0x3dee5
addq %r9, %rdi
addq $0x38, %rdi
movl %eax, %r8d
shrl %r8d
movl %r8d, %edx
negl %edx
testb $0x1, %al
cmovel %r8d, %edx
callq 0x3d202
movq 0x10(%rsp), %r8
movl 0x24(%r8), %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdx
imulq $0xa8, %rdx, %rsi
movl %eax, 0x24(%rcx,%rsi)
xorl %eax, %eax
movl %eax, 0x28(%rcx,%rsi)
movb %al, 0x2c(%rcx,%rsi)
movl %eax, 0x30(%rcx,%rsi)
movb %al, 0x2e(%rcx,%rsi)
incq %rdx
movq %rdx, 0x10(%r14)
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq