// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rsi, %r12
movq %rdi, %r15
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %r14
testq %rdx, %rdx
movq %rdi, 0x8(%rsp)
je 0x34d8e
movq %rcx, %r13
movq %rdx, %rbp
movq %r12, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x34d43
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %r12
movq 0x10(%r15), %rbx
leaq 0x1(%r12), %rax
movq %rax, (%r15)
shlq $0x4, %r12
movl %r13d, 0x8(%rbx,%r12)
leal 0x1(%r13), %edi
callq 0x2280
movq %rax, (%rbx,%r12)
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2160
movq (%rbx,%r12), %rax
movb $0x0, (%rax,%r13)
movq 0x20(%rsp), %rbx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r12
testq %rbx, %rbx
je 0x350fa
testq %r12, %r12
je 0x34e49
movq (%r12), %rax
movq 0x58(%r15), %rcx
cmpq %rcx, %rax
setb %dl
leaq 0x1(%rax), %rsi
cmpq %rsi, %rcx
setae %cl
orb %dl, %cl
jne 0x34e0a
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 0x8(%rsp), %rdi
movq %rax, 0x48(%r15)
movq (%r12), %rax
movq 0x50(%r15), %rcx
subq %rcx, %rax
jb 0x34e3e
imulq $0x58, %rcx, %rdi
addq 0x48(%r15), %rdi
imulq $0x58, %rax, %rdx
addq $0x58, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rdi
movq (%r12), %rax
incq %rax
movq %rax, 0x50(%r15)
imulq $0x58, (%r12), %r12
addq 0x48(%r15), %r12
jmp 0x34eb9
movq 0x58(%r15), %rax
cmpq %rax, 0x50(%r15)
jne 0x34e7d
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x58(%r15)
movq 0x48(%r15), %rdi
imulq $0x58, %rax, %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x48(%r15)
movq 0x48(%r15), %rax
movq 0x50(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x50(%r15)
imulq $0x58, %rcx, %rcx
leaq (%rax,%rcx), %r12
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 %r13d, %r13d
movq %r13, (%r12)
movq %r13, 0x40(%r12)
movq %r13, 0x20(%r12)
movq 0x10(%rsp), %rax
movl %eax, 0x18(%r12)
testq %r14, %r14
sete %al
testq %rdi, %rdi
setle %cl
orb %al, %cl
jne 0x34fd4
leaq 0x18(%r14), %r15
movl $0x1, %ebp
xorl %r13d, %r13d
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
testb $0x40, 0x3(%r15)
je 0x34fbb
movl -0x14(%r15), %r13d
movq 0x28(%r12), %rax
cmpq %rax, 0x20(%r12)
jne 0x34f4c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%r12)
movq 0x30(%r12), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rdi
movq %rax, 0x30(%r12)
movq 0x20(%r12), %rax
movq 0x30(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%r12)
leaq (%rax,%rax,8), %rax
leaq (%rcx,%rax,4), %rax
movl -0xc(%r15), %ecx
andl %r8d, %ecx
movl %ecx, (%rax)
movl (%r15), %ecx
movb 0x20(%rax), %dl
shrl $0x1a, %ecx
andb $-0x80, %dl
andb $0x20, %cl
orb %dl, %cl
movb %cl, 0x20(%rax)
movl -0x14(%r15), %ecx
subl %r13d, %ecx
movl %ecx, 0x4(%rax)
movl -0x10(%r15), %r13d
movl %r13d, 0x8(%rax)
cmpl $0x0, (%r15)
js 0x34faa
movl -0x4(%r15), %edx
subl %r13d, %ecx
addl %edx, %ecx
movl %ecx, 0xc(%rax)
jmp 0x34fb8
movl -0x8(%r15), %esi
movl %esi, 0xc(%rax)
movl %r13d, %edx
subl %ecx, %edx
addl %esi, %edx
movl %edx, 0x10(%rax)
testq %r14, %r14
je 0x34fd4
leaq 0x1(%rbp), %rax
addq $0x1c, %r15
cmpq %rdi, %rbp
movq %rax, %rbp
jl 0x34efa
cmpq 0x10(%rsp), %r13
jge 0x350fa
movq %rbx, %r14
movq 0x28(%r12), %rax
cmpq %rax, 0x20(%r12)
jne 0x3501a
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x28(%r12)
movq 0x30(%r12), %rdi
shlq $0x2, %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq %rax, 0x30(%r12)
leaq 0x38(%r12), %r15
movq 0x20(%r12), %rax
movq 0x30(%r12), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%r12)
leaq (%rax,%rax,8), %rax
leaq (%rcx,%rax,4), %rbx
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
movb 0x20(%rbx), %al
andb $-0x80, %al
orb $0x40, %al
movb %al, 0x20(%rbx)
movl %r13d, 0x4(%rbx)
movq 0x10(%rsp), %rdx
movl %edx, 0x8(%rbx)
movl (%r12), %ecx
movl %ecx, 0xc(%rbx)
subl %r13d, %edx
movl %edx, %eax
shrl $0x2, %eax
andl $0x3, %edx
cmpl $0x1, %edx
sbbl $-0x1, %eax
addl %ecx, %eax
movl %eax, 0x10(%rbx)
cmpq %rax, 0x8(%r12)
jae 0x350c5
decq %rax
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, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%r12)
movq 0x10(%r12), %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movl 0x10(%rbx), %eax
movq %rax, (%r12)
movl 0xc(%rbx), %edi
addq 0x10(%r12), %rdi
movl 0x4(%rbx), %r8d
movl 0x8(%rbx), %edx
movq %r14, %rsi
addq %r8, %rsi
subl %r8d, %edx
movq %r15, %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x348a1
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq