// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Process_Read.cpp
// Function: append_ul_t_compress_ovlp(all_ul_t*, unsigned long*, char*, long, char*, long, ul_ov_t*, long)
void append_ul_t_compress_ovlp(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) {
	int64_t i, mine, maxs, ovlp, end;
	ul_vec_t *p = NULL;
	nid_t *np = NULL;
	ul_ov_t *z = NULL, *zp = NULL;
	uc_block_t *b = NULL;

	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) {
		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)]);
		}
		

		p->bb.n = p->N_site.n = p->r_base.n = 0;
		p->rlen = str_l; 
		
		if(o == NULL || on == 0) on = 0;
		for (i = end = 0, zp = NULL; i < on; i++) {
			z = &(o[i]);
			if(!z->el) continue;
			if(zp) {
				mine = MIN(zp->qe, z->qe); maxs = MAX(zp->qs, z->qs);
				ovlp = mine - maxs; 
				if(zp->qe >= z->qe && zp->qs <= z->qs) continue;
			} else {
				ovlp = -z->qs; 
			}
			if(ovlp < 0) {///push original bases
				kv_pushp(uc_block_t, p->bb, &b);
				b->hid = (uint32_t)-1/**x->mm**/; b->rev = 0; b->base = 1; b->pchain = 0;
				b->qs = end; b->qe = b->qs - ovlp;
				b->ts = p->r_base.n; b->te = b->ts + B4L(-ovlp);
				kv_resize(uint8_t, p->r_base, b->te); p->r_base.n = b->te;
				ha_encode_base(p->r_base.a+b->ts, str+b->qs, b->qe-b->qs, &(p->N_site), b->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->pchain = 0;
			b->qs = z->qs + (ovlp>0?ovlp:0); b->qe = z->qe;
			if(z->rev) {
				b->ts = z->ts; b->te = z->ts + (b->qe - b->qs);
			} else {
				b->ts = z->te - (b->qe - b->qs); b->te = z->te;
			}

			end = MAX(zp?zp->qe:0, z->qe);
		}
		
		if(end < str_l) {///push original bases
			kv_pushp(uc_block_t, p->bb, &b);
			b->hid = (uint32_t)-1/**x->mm**/; b->rev = 0; b->base = 1; b->pchain = 0;
			b->qs = end; b->qe = str_l;
			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;
			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
		}
		// 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);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rsi, %rbx
movq %rdi, %r8
movq 0x68(%rsp), %r12
movq 0x60(%rsp), %rbp
testq %rdx, %rdx
movq %rdi, (%rsp)
je 0x8284
movq %rcx, %r13
movq %rdx, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq (%r8), %r15
cmpq 0x8(%r8), %r15
jne 0x8242
leaq (%r15,%r15), %rsi
testq %r15, %r15
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x4, %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, %rbx
movq %rax, 0x10(%r8)
movq (%r8), %r15
jmp 0x8246
movq 0x10(%r8), %rbx
leaq 0x1(%r15), %rax
movq %rax, (%r8)
shlq $0x4, %r15
movl %r13d, 0x8(%rbx,%r15)
leal 0x1(%r13), %edi
callq 0x2280
movq %rax, %r14
movq %rax, (%rbx,%r15)
movq %rax, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x2160
movq (%rsp), %r8
movb $0x0, (%r14,%r13)
movq 0x20(%rsp), %rbx
cmpq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r14
je 0x85a7
testq %rbx, %rbx
je 0x8347
movq (%rbx), %rax
movq 0x58(%r8), %rcx
cmpq %rcx, %rax
setb %dl
leaq 0x1(%rax), %rsi
cmpq %rsi, %rcx
setae %cl
orb %dl, %cl
jne 0x8309
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(%r8)
movq 0x48(%r8), %rdi
imulq $0x58, %rcx, %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x48(%r8)
movq (%rbx), %rax
movq 0x50(%r8), %rdx
movq %rax, %rcx
subq %rdx, %rcx
jb 0x833d
imulq $0x58, %rdx, %rdi
addq 0x48(%r8), %rdi
imulq $0x58, %rcx, %rdx
addq $0x58, %rdx
xorl %esi, %esi
callq 0x20e0
movq (%rsp), %r8
movq (%rbx), %rax
incq %rax
movq %rax, 0x50(%r8)
movq (%rbx), %rax
imulq $0x58, %rax, %rbx
addq 0x48(%r8), %rbx
jmp 0x83b3
movq 0x50(%r8), %rcx
cmpq 0x58(%r8), %rcx
jne 0x837e
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rax
movq %rax, 0x58(%r8)
movq 0x48(%r8), %rdi
imulq $0x58, %rax, %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, 0x48(%r8)
movq 0x50(%r8), %rcx
jmp 0x8382
movq 0x48(%r8), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0x50(%r8)
imulq $0x58, %rcx, %rcx
leaq (%rax,%rcx), %rbx
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)
andq $0x0, 0x50(%rax,%rcx)
andq $0x0, (%rbx)
andq $0x0, 0x40(%rbx)
andq $0x0, 0x20(%rbx)
movl %r14d, 0x18(%rbx)
leaq 0x18(%rbp), %r15
xorl %r13d, %r13d
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
testq %rbp, %rbp
je 0x84a6
cmpq %r12, %r13
jge 0x84a6
testb $0x40, 0x3(%r15)
je 0x849a
movl -0x14(%r15), %r14d
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0x8430
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x24, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x24, %rcx, %rcx
movl -0xc(%r15), %edx
andl %r8d, %edx
movl %edx, (%rax,%rcx)
movl (%r15), %edx
movb 0x20(%rax,%rcx), %sil
shrl $0x1a, %edx
andb $-0x80, %sil
andb $0x20, %dl
orb %sil, %dl
movb %dl, 0x20(%rax,%rcx)
movl -0x14(%r15), %edx
subl %r14d, %edx
movl %edx, 0x4(%rax,%rcx)
movl -0x10(%r15), %r14d
movl %r14d, 0x8(%rax,%rcx)
cmpl $0x0, (%r15)
js 0x8486
movl -0x4(%r15), %esi
subl %r14d, %edx
addl %esi, %edx
movl %edx, %edi
jmp 0x8491
movl -0x8(%r15), %edi
movl %r14d, %esi
subl %edx, %esi
addl %edi, %esi
addq %rcx, %rax
movl %edi, 0xc(%rax)
movl %esi, 0x10(%rax)
incq %r13
addq $0x1c, %r15
jmp 0x83d5
movq 0x10(%rsp), %r13
cmpq %r13, %r14
jge 0x85a7
movq 0x20(%rbx), %rax
movq 0x30(%rbx), %r12
cmpq 0x28(%rbx), %rax
jne 0x84eb
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rbx)
imulq $0x24, %rcx, %rsi
movq %r12, %rdi
callq 0x2370
movq %rax, %r12
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rax
leaq 0x38(%rbx), %r15
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rbx)
imulq $0x24, %rax, %rax
orl $-0x1, (%r12,%rax)
movb 0x20(%r12,%rax), %cl
andb $-0x80, %cl
orb $0x40, %cl
movb %cl, 0x20(%r12,%rax)
movl %r14d, 0x4(%r12,%rax)
movl %r13d, 0x8(%r12,%rax)
movl (%rbx), %edx
movl %edx, 0xc(%r12,%rax)
subl %r14d, %r13d
movl %r13d, %ecx
shrl $0x2, %ecx
movl %r13d, %esi
andl $0x3, %esi
cmpl $0x1, %esi
sbbl $-0x1, %ecx
addl %edx, %ecx
movl %ecx, 0x10(%r12,%rax)
cmpq %rcx, 0x8(%rbx)
jae 0x85b6
addq %rax, %r12
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(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movl 0x10(%r12), %ecx
movl 0xc(%r12), %edx
movl 0x4(%r12), %r14d
movl 0x8(%r12), %r13d
subl %r14d, %r13d
jmp 0x85ba
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rax
movq %rcx, (%rbx)
movl %edx, %edi
addq %rax, %rdi
movl %r14d, %r8d
movq 0x8(%rsp), %rsi
addq %r8, %rsi
movl %r13d, %edx
movq %r15, %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7dde