// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Process_Read.cpp
// Function: append_ul_t_back(all_ul_t*, unsigned long*, char*, long, char*, long, ul_ov_t*, long, float)
void append_ul_t_back(all_ul_t *x, uint64_t *rid, char* id, int64_t id_l, char* str, int64_t str_l, ul_ov_t *o, int64_t on, float p_chain_rate) {
	int64_t i, mine, maxs, ovlp, st, et, bl = 0, pc = 0;
	uint32_t o_l, o_r;
	ul_vec_t *p = NULL;
	nid_t *np = NULL;
	ul_ov_t *z = NULL;
	uc_block_t *b = NULL, tt;

	if(id) {
		kv_pushp(nid_t, x->nid, &np);
		np->n = id_l; MALLOC(np->a, np->n+1); memcpy(np->a, id, id_l); np->a[id_l] = '\0';
	}

	if(str||str_l) {
		if(rid == NULL) {
			kv_pushp(ul_vec_t, *x, &p);
			memset(p, 0, sizeof(*p));
		} else {
			if((*rid) >= x->m) kv_resize(ul_vec_t, *x, (*rid) + 1);
			if((*rid) >= x->n) {
				memset(x->a+x->n, 0, sizeof(*p)*((*rid) + 1 - x->n));
				x->n = (*rid) + 1;
			}
			p = &(x->a[(*rid)]);
		}
		// if((*rid) == 23) fprintf(stderr, "#rid->%lu, on->%ld\n", *rid, on);

		p->bb.n = p->N_site.n = p->r_base.n = 0; p->dd = 0;
		p->rlen = str_l; 
		
		if(o == NULL || on == 0) on = 0;
		for (i = on-1, st = et = str_l; i >= 0; i--) {
			z = &(o[i]);
			if(z->el) {
				mine = MIN(et, ((int64_t)z->qe)); maxs = MAX(st, ((int64_t)z->qs));
				ovlp = mine - maxs; 
				
				if(ovlp < 0) {///push original bases
					kv_pushp(uc_block_t, p->bb, &b);
					b->hid = 0/**x->mm**/; b->rev = 0; b->base = 1; b->pchain = 0; b->el = 0;
					b->qe = maxs; b->qs = b->qe + ovlp; bl += (b->qe-b->qs);
					o_l = (b->qs >= UL_FLANK?UL_FLANK:b->qs);
					o_r = ((str_l-b->qe)>=UL_FLANK?UL_FLANK:(str_l-b->qe));
					b->hid |= (o_l<<15); b->hid |= o_r;
					b->qs -= o_l; b->qe += o_r;
					b->ts = p->r_base.n; b->te = b->ts + B4L(b->qe-b->qs);
					kv_resize(uint8_t, p->r_base, b->te); p->r_base.n = b->te;
					// if(!str) fprintf(stderr, "+rid->%lu\n", *rid);
					ha_encode_base(p->r_base.a+b->ts, str+b->qs, b->qe-b->qs, &(p->N_site), b->qs);
				} 

				st = MIN(st, z->qs);
			}

			///push ovlp bases
			kv_pushp(uc_block_t, p->bb, &b);
			b->hid = (z->tn<<1)>>1; b->rev = z->rev; b->base = 0; b->el = z->el;
			b->pchain = ((z->tn&((uint32_t)(0x80000000)))?1:0);
			b->qs = z->qs; b->qe = z->qe;
			b->ts = z->ts; b->te = z->te;
			if(b->pchain) pc++;
			
			// st = MIN(st, z->qs);
		}
		
		if(st > 0) {///push original bases
			kv_pushp(uc_block_t, p->bb, &b);
			b->hid = 0/**x->mm**/; b->rev = 0; b->base = 1; b->pchain = 0; b->el = 0;
			b->qe = st; b->qs = 0; bl += (b->qe-b->qs);
			o_l = (b->qs >= UL_FLANK?UL_FLANK:b->qs);
			o_r = ((str_l-b->qe)>=UL_FLANK?UL_FLANK:(str_l-b->qe));
			b->hid |= (o_l<<15); b->hid |= o_r;
			b->qs -= o_l; b->qe += o_r;
			b->ts = p->r_base.n; b->te = b->ts + B4L(b->qe-b->qs);
			kv_resize(uint8_t, p->r_base, b->te); p->r_base.n = b->te;
			// if(!str) fprintf(stderr, "-rid->%lu, st->%ld, str_l->%ld\n", *rid, st, str_l);
			ha_encode_base(p->r_base.a+b->ts, str+b->qs, b->qe-b->qs, &(p->N_site), b->qs);	
			// push_subblock_original_bases(str, x, p, end, str_l, 321);//for debug
		}

		if(pc > 0) p->dd = 3;
		if((pc == on) && ((str_l-bl) > (str_l*p_chain_rate))) p->dd = 2;
		if((pc == on) && (bl == 0)) p->dd = 1;
		// debug_append_ul_t(o, on, p);
		// char *sst = NULL; CALLOC(sst, str_l);//for debug
		// retrieve_ul_t(NULL, sst, x, rid?*rid:x->n-1, 0, 0, -1);
		// if(memcmp(sst, str, str_l)) {
		// 	fprintf(stderr, "ap-Wrong read, id: %ld, [%d, %ld)\n", (int64_t)(rid?*rid:x->n-1), 0, str_l);
		// 	for (i = 0; i < str_l; i++) {
		// 		if(sst[i] != str[i]) fprintf(stderr, "[%ld] input:%c, decompress:%c\n", i, str[i], sst[i]);
		// 	}
		// }
		// free(sst);
		ovlp = p->bb.n>>1;
		for (i = 0; i < ovlp; i++) {
			tt = p->bb.a[i]; 
			p->bb.a[i] = p->bb.a[p->bb.n-i-1];
			p->bb.a[p->bb.n-i-1] = tt;
		}
		
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movss %xmm0, 0x1c(%rsp)
movq %r9, (%rsp)
movq %r8, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
testq %rdx, %rdx
je 0x3492a
movq %rcx, %r13
movq %rdx, %rbp
movq (%r15), %r12
cmpq 0x8(%r15), %r12
jne 0x348f1
leaq (%r12,%r12), %rax
testq %r12, %r12
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, %r14
movq %rax, 0x10(%r15)
movq (%r15), %r12
jmp 0x348f5
movq 0x10(%r15), %r14
leaq 0x1(%r12), %rax
movq %rax, (%r15)
shlq $0x4, %r12
movl %r13d, 0x8(%r14,%r12)
leal 0x1(%r13), %edi
callq 0x2280
movq %rax, (%r14,%r12)
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2160
movq (%r14,%r12), %rax
movb $0x0, (%rax,%r13)
movq 0x10(%rsp), %rax
orq (%rsp), %rax
je 0x34f47
movq 0xa8(%rsp), %r13
movq 0xa0(%rsp), %r12
testq %rbx, %rbx
je 0x349f3
movq (%rbx), %rax
movq 0x58(%r15), %rcx
cmpq %rcx, %rax
setb %dl
leaq 0x1(%rax), %rsi
cmpq %rsi, %rcx
setae %cl
orb %dl, %cl
jne 0x349b9
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
incq %rcx
movq %rcx, 0x58(%r15)
movq 0x48(%r15), %rdi
imulq $0x58, %rcx, %rsi
callq 0x2370
movq %rax, 0x48(%r15)
movq (%rbx), %rax
movq 0x50(%r15), %rdx
movq %rax, %rcx
subq %rdx, %rcx
jb 0x349e9
imulq $0x58, %rdx, %rdi
addq 0x48(%r15), %rdi
imulq $0x58, %rcx, %rdx
addq $0x58, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%rbx), %rax
incq %rax
movq %rax, 0x50(%r15)
movq (%rbx), %rax
imulq $0x58, %rax, %r14
addq 0x48(%r15), %r14
jmp 0x34a60
movq 0x50(%r15), %rcx
cmpq 0x58(%r15), %rcx
jne 0x34a28
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x58(%r15)
movq 0x48(%r15), %rdi
imulq $0x58, %rcx, %rsi
callq 0x2370
movq %rax, 0x48(%r15)
movq 0x50(%r15), %rcx
jmp 0x34a2c
movq 0x48(%r15), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x50(%r15)
imulq $0x58, %rcx, %rcx
leaq (%rax,%rcx), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rax,%rcx)
movups %xmm0, 0x30(%rax,%rcx)
movups %xmm0, 0x20(%rax,%rcx)
movups %xmm0, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq $0x0, 0x50(%rax,%rcx)
xorl %eax, %eax
movq %rax, (%r14)
leaq 0x38(%r14), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x40(%r14)
movq $0x0, 0x8(%rsp)
movq %rax, 0x20(%r14)
movb $0x0, 0x50(%r14)
testq %r12, %r12
movq %r13, %rcx
cmoveq %r12, %rcx
movq (%rsp), %rax
movl %eax, 0x18(%r14)
movq %rax, %rbx
movl $0x0, %ebp
movq %rcx, 0x28(%rsp)
testq %rcx, %rcx
jle 0x34d30
leaq 0x1(%r13), %r15
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rax
leaq (%r12,%rax), %r13
addq $-0x4, %r13
xorl %ebp, %ebp
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq $0x0, 0x8(%rsp)
movq (%rsp), %rbx
testb $0x40, 0x3(%r13)
je 0x34c74
movl -0x14(%r13), %eax
movl -0x10(%r13), %r12d
movq (%rsp), %rcx
cmpq %rcx, %r12
cmovgeq %rcx, %r12
cmpq %rax, %rbx
movq %rbx, %rcx
movq %rax, %rbx
movq %rcx, 0x38(%rsp)
cmovgq %rcx, %rbx
cmpq %r12, %rbx
jle 0x34c62
movq 0x20(%r14), %rcx
movq 0x30(%r14), %rax
cmpq 0x28(%r14), %rcx
jne 0x34b52
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%r14)
shlq $0x2, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%r14)
movq 0x20(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r14)
leaq (%rcx,%rcx,8), %r9
movl $0x200, %edi            # imm = 0x200
cmpl %edi, %r12d
movl $0x200, %ecx            # imm = 0x200
cmovbl %r12d, %ecx
movl %ebx, %esi
movq (%rsp), %rdx
subq %rsi, %rdx
cmpq $0x200, %rdx            # imm = 0x200
cmovgel %edi, %edx
movb $0x40, 0x20(%rax,%r9,4)
movl %r12d, %esi
subl %ecx, %esi
shll $0xf, %ecx
orl %edx, %ecx
movl %ecx, (%rax,%r9,4)
movl %esi, 0x4(%rax,%r9,4)
addl %ebx, %edx
movl %edx, 0x8(%rax,%r9,4)
movl (%r14), %r8d
movl %r8d, 0xc(%rax,%r9,4)
subl %esi, %edx
movl %edx, %edi
shrl $0x2, %edi
movl %edx, %ecx
andl $0x3, %ecx
cmpl $0x1, %ecx
sbbl $-0x1, %edi
addl %r8d, %edi
movl %edi, 0x10(%rax,%r9,4)
movq 0x10(%r14), %rcx
cmpq %rdi, 0x8(%r14)
jae 0x34c35
leaq (%rax,%r9,4), %rax
movq %rax, 0x30(%rsp)
decq %rdi
movq %rdi, %rax
shrq %rax
orq %rdi, %rax
movq %rax, %rdx
shrq $0x2, %rdx
orq %rax, %rdx
movq %rdx, %rax
shrq $0x4, %rax
orq %rdx, %rax
movq %rax, %rdx
shrq $0x8, %rdx
orq %rax, %rdx
movq %rdx, %rsi
shrq $0x10, %rsi
orq %rdx, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq %rcx, %rdi
callq 0x2370
movq %rax, %rcx
movq %rax, 0x10(%r14)
movq 0x30(%rsp), %rax
movl 0x10(%rax), %edi
movl 0xc(%rax), %r8d
movl 0x4(%rax), %esi
movl 0x8(%rax), %edx
subl %esi, %edx
subl %r12d, %ebx
addq %rbx, 0x8(%rsp)
movq %rdi, (%r14)
movl %r8d, %edi
addq %rcx, %rdi
movl %esi, %r8d
movq 0x10(%rsp), %rax
leaq (%rax,%r8), %rsi
movl %edx, %edx
movq 0x20(%rsp), %rcx
callq 0x33eca
movl -0x14(%r13), %eax
movq 0x38(%rsp), %rbx
cmpq %rax, %rbx
cmovgeq %rax, %rbx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq 0x20(%r14), %rcx
movq 0x30(%r14), %rax
cmpq 0x28(%r14), %rcx
jne 0x34cae
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%r14)
shlq $0x2, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%r14)
movq 0x20(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r14)
leaq (%rcx,%rcx,8), %rcx
movl -0xc(%r13), %edx
andl %r12d, %edx
movl %edx, (%rax,%rcx,4)
movl (%r13), %edx
movb 0x20(%rax,%rcx,4), %sil
shrl $0x1a, %edx
andb $0x20, %dl
andb $-0x61, %sil
orb %dl, %sil
movb %sil, 0x20(%rax,%rcx,4)
movl (%r13), %edi
shrl $0x17, %edi
andb $-0x80, %dil
andb $0x3f, %sil
orb %dil, %sil
movb %sil, 0x20(%rax,%rcx,4)
movl -0xc(%r13), %esi
shrl $0x1f, %esi
addq %rsi, %rbp
orb %dl, %sil
orb %dil, %sil
movb %sil, 0x20(%rax,%rcx,4)
movsd -0x8(%r13), %xmm0
movsd -0x14(%r13), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movups %xmm1, 0x4(%rax,%rcx,4)
decq %r15
addq $-0x1c, %r13
cmpq $0x1, %r15
ja 0x34adf
testq %rbx, %rbx
jle 0x34d6e
movq 0x20(%r14), %rcx
cmpq 0x28(%r14), %rcx
jne 0x34d78
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x28(%r14)
movq 0x30(%r14), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x30(%r14)
movq 0x20(%r14), %rcx
jmp 0x34d7c
movq 0x8(%rsp), %r15
jmp 0x34e6c
movq 0x30(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r14)
leaq (%rcx,%rcx,8), %rdi
movb $0x40, 0x20(%rax,%rdi,4)
movl $0x0, 0x4(%rax,%rdi,4)
movq %rbx, %rsi
movl %esi, %ebx
movq (%rsp), %rcx
subq %rbx, %rcx
cmpq $0x200, %rcx            # imm = 0x200
movl $0x200, %edx            # imm = 0x200
cmovll %ecx, %edx
movl %edx, (%rax,%rdi,4)
addl %esi, %edx
movl %edx, 0x8(%rax,%rdi,4)
movl (%r14), %esi
movl %esi, 0xc(%rax,%rdi,4)
movl %edx, %ecx
shrl $0x2, %ecx
movl %edx, %r8d
andl $0x3, %r8d
cmpl $0x1, %r8d
sbbl $-0x1, %ecx
addl %esi, %ecx
movl %ecx, 0x10(%rax,%rdi,4)
cmpq %rcx, 0x8(%r14)
jae 0x34e3d
leaq (%rax,%rdi,4), %r15
decq %rcx
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, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movl 0x10(%r15), %ecx
movl 0xc(%r15), %esi
movl 0x4(%r15), %r9d
movl 0x8(%r15), %edx
jmp 0x34e44
movq 0x10(%r14), %rax
xorl %r9d, %r9d
movq 0x8(%rsp), %r15
addq %rbx, %r15
movq %rcx, (%r14)
movl %esi, %edi
addq %rax, %rdi
movl %r9d, %r8d
movq 0x10(%rsp), %rsi
addq %r8, %rsi
subl %r9d, %edx
movq 0x20(%rsp), %rcx
callq 0x33eca
testq %rbp, %rbp
jle 0x34e76
movb $0x3, 0x50(%r14)
cmpq 0x28(%rsp), %rbp
jne 0x34eb0
testq %r15, %r15
je 0x34ea4
movq (%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
subq %r15, %rax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x1c(%rsp), %xmm0
ucomiss %xmm0, %xmm1
jbe 0x34eb0
cmpq $0x1, %r15
movb $0x2, %al
sbbb $0x0, %al
movb %al, 0x50(%r14)
movq 0x20(%r14), %rax
cmpq $0x2, %rax
jb 0x34f47
shrq %rax
movq $-0x24, %rcx
xorl %edx, %edx
movq 0x30(%r14), %rsi
movl 0x20(%rsi,%rdx), %edi
movl %edi, 0x60(%rsp)
movups (%rsi,%rdx), %xmm0
movups 0x10(%rsi,%rdx), %xmm1
movaps %xmm1, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq 0x20(%r14), %rdi
leaq (%rdi,%rdi,8), %rdi
leaq (%rsi,%rdi,4), %rdi
movups (%rcx,%rdi), %xmm0
movups 0x10(%rcx,%rdi), %xmm1
movups %xmm0, (%rsi,%rdx)
movups %xmm1, 0x10(%rsi,%rdx)
movl 0x20(%rcx,%rdi), %edi
movl %edi, 0x20(%rsi,%rdx)
movq 0x20(%r14), %rsi
leaq (%rsi,%rsi,8), %rsi
shlq $0x2, %rsi
addq 0x30(%r14), %rsi
movl 0x60(%rsp), %edi
movl %edi, 0x20(%rcx,%rsi)
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movups %xmm1, 0x10(%rcx,%rsi)
movups %xmm0, (%rcx,%rsi)
addq $0x24, %rdx
addq $-0x24, %rcx
decq %rax
jne 0x34eca
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq