// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: append_utg_inexact_overlap_region_alloc(overlap_region_alloc*, overlap_region*, ma_utg_v*, int, void*)
int append_utg_inexact_overlap_region_alloc(overlap_region_alloc* list, overlap_region* tmp, 
                                        ma_utg_v *ua, 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 = ua->a[tmp->x_id].len - tmp->x_pos_e - 1;
    long long y_right_length = ua->a[tmp->y_id].len - tmp->y_pos_e - 1;

    if(x_right_length <= y_right_length)
    {
        tmp->x_pos_e = ua->a[tmp->x_id].len - 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 = ua->a[tmp->y_id].len - 1;
    }
    
    if (tmp->x_pos_strand == 1)
    {
        list->list[list->length].x_id = tmp->x_id;
        list->list[list->length].x_pos_e = ua->a[tmp->x_id].len - tmp->x_pos_s - 1;
        list->list[list->length].x_pos_s = ua->a[tmp->x_id].len - 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 = ua->a[tmp->y_id].len - tmp->y_pos_s - 1;
        list->list[list->length].y_pos_s = ua->a[tmp->y_id].len - 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), 
                ua->a[tmp->x_id].len - 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 %r8, %r14
movl %ecx, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
movq 0x8(%rdi), %r15
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rax
cmpq %r15, %rax
jbe 0x12919
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(%rbx)
imulq $0xa8, %rcx, %rdx
movq (%rbx), %rsi
testq %r14, %r14
je 0x128e8
movq %r14, %rdi
callq 0x1a3acc
jmp 0x128f3
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x2370
movq %rax, (%rbx)
imulq $0xa8, %r15, %rdi
addq %rax, %rdi
movq 0x8(%rbx), %rax
subq %r15, %rax
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%rbx), %rcx
xorl %edx, %edx
testq %rcx, %rcx
movq %rbx, 0x10(%rsp)
movq %r14, 0x18(%rsp)
je 0x12971
movq (%rbx), %rsi
imulq $0xa8, %rcx, %rax
movl -0x98(%rsi,%rax), %edi
cmpl 0x10(%r13), %edi
jne 0x12973
addq %rax, %rsi
movl 0x24(%r13), %edi
xorl %eax, %eax
cmpl %edi, -0x84(%rsi)
jg 0x12ce6
jne 0x12968
movl -0x88(%rsi), %esi
cmpl 0x20(%r13), %esi
jbe 0x12ce6
decq %rcx
movq %rcx, 0x10(%rbx)
jmp 0x12973
xorl %ecx, %ecx
movl 0x4(%r13), %r8d
movl 0x14(%r13), %eax
movl %r8d, %esi
subl %eax, %esi
cmovbl %edx, %esi
movl (%r13), %edi
subl %r8d, %eax
cmovbl %edx, %eax
movl %esi, 0x4(%r13)
movl %eax, 0x14(%r13)
movq 0x20(%rsp), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rdi, %r8
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl (%rdx,%r8), %r9d
andl %r11d, %r9d
movl 0x8(%r13), %ebx
movl %ebx, %ebp
notl %ebp
addl %r9d, %ebp
movl 0x10(%r13), %r8d
imulq $0x28, %r8, %r14
movl (%rdx,%r14), %r12d
andl %r11d, %r12d
movl 0x18(%r13), %r10d
movl %r10d, %r15d
notl %r15d
addl %r12d, %r15d
cmpl %r15d, %ebp
jbe 0x129f3
addq %rdx, %r14
addl %ebx, %r15d
movl %r15d, 0x8(%r13)
andl (%r14), %r11d
decl %r11d
movl %r15d, %r9d
movl %r11d, %r10d
jmp 0x129fd
decl %r9d
movl %r9d, 0x8(%r13)
addl %ebp, %r10d
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
movabsq $0xfffffffffffffff, %r14 # imm = 0xFFFFFFFFFFFFFFF
movl %r10d, 0x18(%r13)
movl 0xc(%r13), %ebx
imulq $0xa8, %rcx, %rcx
movq (%r15), %r11
movl %edi, (%r11,%rcx)
cmpl $0x1, %ebx
jne 0x12b66
movl (%r13), %edi
imulq $0x28, %rdi, %r9
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%rdx,%r9), %edi
andl %r10d, %edi
notl %esi
addl %edi, %esi
leaq (%r11,%rcx), %rdi
addq $0x38, %rdi
movl %esi, -0x30(%rdi)
movl (%rdx,%r9), %ecx
andl %r10d, %ecx
movl 0x8(%r13), %esi
notl %esi
addl %ecx, %esi
movl %esi, -0x34(%rdi)
andl $0x0, -0x2c(%rdi)
movl %r8d, -0x28(%rdi)
movl 0x10(%r13), %ecx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %esi
andl %r10d, %esi
notl %eax
addl %esi, %eax
movl %eax, -0x20(%rdi)
movl (%rdx,%rcx), %eax
andl %r10d, %eax
movl 0x18(%r13), %ecx
notl %ecx
addl %eax, %ecx
movl %ecx, -0x24(%rdi)
movl $0x1, -0x1c(%rdi)
movl 0x40(%r13), %esi
addl $0x2, %esi
movq %r12, %rdx
callq 0x123e1
cmpl $0x1, 0xc(%rsp)
jne 0x12ad1
movq (%r15), %rax
imulq $0xa8, 0x10(%r15), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
movl -0x34(%rdi), %esi
xorl %edx, %edx
movq %r12, %rcx
callq 0x11af6
xorl %r12d, %r12d
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %ebp, %ebp
movl 0x40(%r13), %eax
cmpq %rax, %rbp
jae 0x12c5c
movq 0x38(%r13), %rax
movq %r12, %rcx
sarq $0x1d, %rcx
movq (%rax,%rcx), %rsi
movl %esi, %eax
shrl %eax
movl %eax, %edx
negl %edx
testb $0x1, %sil
cmovel %eax, %edx
movslq %edx, %rbx
cmpq %rbx, %r14
je 0x12b5b
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
imulq $0xa8, 0x10(%rcx), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movl (%r13), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
shrq $0x20, %rsi
notl %esi
addl %eax, %esi
movq 0x18(%rsp), %rcx
callq 0x11af6
movq %rbx, %r14
incq %rbp
addq %r15, %r12
jmp 0x12ae0
leaq (%r11,%rcx), %rdi
addq $0x38, %rdi
movl %r9d, -0x30(%rdi)
movl %esi, -0x34(%rdi)
movl %ebx, -0x2c(%rdi)
movl %r8d, -0x28(%rdi)
movl %r10d, -0x20(%rdi)
movl %eax, -0x24(%rdi)
movl 0x1c(%r13), %eax
movl %eax, -0x1c(%rdi)
movl 0x40(%r13), %esi
addl $0x2, %esi
movq %r12, %rdx
callq 0x123e1
cmpl $0x1, 0xc(%rsp)
jne 0x12bc0
movq (%r15), %rax
imulq $0xa8, 0x10(%r15), %rcx
leaq (%rax,%rcx), %rdi
addq $0x38, %rdi
movl -0x34(%rdi), %esi
xorl %edx, %edx
movq %r12, %rcx
callq 0x11af6
movl 0x4(%r13), %eax
movl 0x8(%r13), %r12d
movl 0x40(%r13), %ebx
addl 0x18(%r13), %eax
movl %eax, 0x20(%rsp)
addl 0x14(%r13), %r12d
decl %ebx
movq %rbx, %r15
shlq $0x20, %r15
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
testq %rbx, %rbx
js 0x12c5c
movq %r13, %rbp
movq 0x38(%r13), %rax
movq %r15, %rcx
sarq $0x1d, %rcx
movq (%rax,%rcx), %rsi
movl %esi, %ecx
shrl %ecx
movl %ecx, %eax
negl %eax
testb $0x1, %sil
cmovel %ecx, %eax
movslq %eax, %r13
cmpq %r13, %r14
je 0x12c51
movq %rdx, %r14
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
imulq $0xa8, 0x10(%rdx), %rdx
leaq (%rcx,%rdx), %rdi
addq $0x38, %rdi
shrq $0x20, %rsi
addl %r12d, %eax
movl 0x20(%rsp), %edx
subl %eax, %edx
movq 0x18(%rsp), %rcx
callq 0x11af6
movq %r14, %rdx
movq %r13, %r14
decq %rbx
addq %rdx, %r15
movq %rbp, %r13
jmp 0x12beb
cmpl $0x1, 0xc(%rsp)
movq 0x10(%rsp), %rbx
jne 0x12cb2
movq (%rbx), %rdi
imulq $0xa8, 0x10(%rbx), %rcx
movl 0x40(%rdi,%rcx), %eax
decl %eax
movq 0x38(%rdi,%rcx), %rdx
cltq
movq (%rdx,%rax,8), %rax
movq %rax, %rdx
sarq $0x20, %rdx
movl 0x8(%rdi,%rcx), %esi
cmpq %rsi, %rdx
je 0x12cb2
addq %rcx, %rdi
addq $0x38, %rdi
movl %eax, %ecx
shrl %ecx
movl %ecx, %edx
negl %edx
testb $0x1, %al
cmovel %ecx, %edx
movq 0x18(%rsp), %rcx
callq 0x11af6
movl 0x24(%r13), %eax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
imulq $0xa8, %rdx, %rsi
movl %eax, 0x24(%rcx,%rsi)
andl $0x0, 0x28(%rcx,%rsi)
xorl %eax, %eax
movb %al, 0x2c(%rcx,%rsi)
andl $0x0, 0x30(%rcx,%rsi)
movb %al, 0x2e(%rcx,%rsi)
incq %rdx
movq %rdx, 0x10(%rbx)
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq