// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrieve_ul_t(UC_Read*, char*, all_ul_t*, unsigned long, unsigned char, long, long)
void retrieve_ul_t(UC_Read* i_r, char *i_s, all_ul_t *ref, uint64_t ID, uint8_t strand, int64_t s, int64_t l) {
	ul_vec_t *p = &(ref->a[ID]); 
	if(l < 0) l = p->rlen;
	uc_block_t *b = NULL;
	char *r = NULL;
	uint8_t *src = NULL;
	int64_t k, i, a_n, e = s + l, ssp, sep, sl, rts, rte;
	int64_t offset, begLen, tailLen, src_i, des_i;
	if(i_r) {
		i_r->length = l; i_r->RID = ID;
		if(i_r->length > i_r->size) {
			i_r->size = i_r->length;
			i_r->seq = (char*)realloc(i_r->seq,sizeof(char)*(i_r->size));
		}
		r = i_r->seq;
	}
	if(i_s) r = i_s;
	
	

	if(strand == 0) {
		for (k = 0, des_i = 0; k < (int64_t)p->bb.n; k++) {
			b = &(p->bb.a[k]); 
			if(b->qe <= s) continue;
			if(b->qs >= e) break;
			src = p->r_base.a + b->ts; 
			ssp = MAX(s, b->qs) - b->qs; 
			sep = MIN(e, b->qe) - b->qs;
			sl = sep - ssp;
			
			if(b->base/**b->hid&ref->mm**/){///original bases
				offset = ssp&3; 
				begLen = 4-offset;
				if(begLen > sl) begLen = sl;
				tailLen = (sl-begLen)&3;
				a_n = (sl - begLen - tailLen)>>2;

				src_i = ssp; i = 0;
				if(begLen > 0) {
					memcpy(r+des_i, bit_t_seq_table[src[src_i>>2]]+offset, begLen);
					des_i += begLen; src_i += begLen;
				}

				for (i = 0; i < a_n; i++) {
					memcpy(r+des_i, bit_t_seq_table[src[src_i>>2]], 4);
					des_i += 4; src_i += 4;
				}

				if(tailLen > 0) {
					memcpy(r+des_i, bit_t_seq_table[src[src_i>>2]], tailLen);
					des_i += tailLen; src_i += tailLen;
				}
			} else {///ovlps
				if(b->rev == 0) {
					recover_UC_Read_sub_region(r+des_i, b->ts + ssp, sep - ssp, b->rev, ref->hR, b->hid);
				} else{
					rts = b->ts + (b->qe - sep); rte = rts + sep - ssp;
					recover_UC_Read_sub_region(r+des_i, Get_READ_LENGTH((*ref->hR), b->hid) - rte, 
					sep - ssp, b->rev, ref->hR, b->hid);
				}
				des_i += sep - ssp;
			}
		}
		for (k = 0; k < (int64_t)p->N_site.n; k++) {
			if(p->N_site.a[k] >= s && p->N_site.a[k] < e){
				r[p->N_site.a[k]-s] = 'N';
			}
			else if(p->N_site.a[k] >= e) {
				break;
			}
		}
	} else {
		sep = p->rlen - s; 
		ssp = p->rlen - e;
		s = ssp; e = sep; 
		///[s, e)
		for (k = ((int64_t)p->bb.n)-1, des_i = 0; k >= 0; k--) {
			b = &(p->bb.a[k]); 
			if(b->qe <= s) break;
			if(b->qs >= e) continue;
            src = p->r_base.a + b->ts; 
            ssp = MAX(s, b->qs) - b->qs; 
            sep = MIN(e, b->qe) - b->qs;
            sl = sep - ssp;
			///[ssp, sep)
			
			if(b->base/**b->hid&ref->mm**/){///original bases
				begLen = sep&3;
        		offset = 4 - begLen;
				if(begLen > sl) begLen = sl;
				tailLen = (sl-begLen)&3;
				a_n = (sl - begLen - tailLen)>>2;
				src_i = sep-1; i = 0;

				if(begLen > 0) {
					memcpy(r+des_i, bit_t_seq_table_rc[src[src_i>>2]]+offset, begLen);
					des_i += begLen; src_i -= begLen;
				}

				for (i = 0; i < a_n; i++) {
					memcpy(r+des_i, bit_t_seq_table_rc[src[src_i>>2]], 4);
					des_i += 4; src_i -= 4;
				}

				if(tailLen > 0) {
					memcpy(r+des_i, bit_t_seq_table_rc[src[src_i>>2]], tailLen);
					des_i += tailLen; src_i -= tailLen;
				}
			} else {///ovlps
				if(b->rev == 0) {///b->rev != strand
					rts = b->ts + ssp; rte = rts + sep - ssp;
					recover_UC_Read_sub_region(r+des_i, Get_READ_LENGTH((*ref->hR), b->hid) - rte, sep - ssp, 1, ref->hR, b->hid);
				} else {///b->rev == strand
					rts = b->ts + (b->qe - sep); rte = rts + sep - ssp;
					recover_UC_Read_sub_region(r+des_i, rts, sep - ssp, 0, ref->hR, b->hid);
				}
				des_i += sep - ssp;
			}
		}

		sep = p->rlen - s; 
		ssp = p->rlen - e;
		s = ssp; e = sep; 
		for (k = 0; k < (int64_t)p->N_site.n; k++) {
			sl = p->rlen - p->N_site.a[k] - 1;
			if(sl >= s && sl < e) r[sl-s] = 'N';
			else if(sl < s) {
				break;
			}
		}		
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movl %r8d, %ebp
movq %rsi, %r12
movq %rdi, %r13
movq 0x70(%rsp), %rsi
imulq $0x58, %rcx, %r8
movq %rdx, 0x28(%rsp)
addq 0x48(%rdx), %r8
testq %rsi, %rsi
jns 0x366ca
movl 0x18(%r8), %esi
testq %r13, %r13
movq %r8, 0x20(%rsp)
je 0x36704
movq %rsi, 0x8(%r13)
movq %rcx, 0x18(%r13)
cmpq 0x10(%r13), %rsi
jle 0x366fe
movq %rsi, 0x10(%r13)
movq (%r13), %rdi
movq %rsi, %r14
callq 0x2370
movq %r14, %rsi
movq 0x20(%rsp), %r8
movq %rax, (%r13)
movq (%r13), %r10
jmp 0x36707
xorl %r10d, %r10d
addq %rbx, %rsi
testq %r12, %r12
cmovneq %r12, %r10
testb %bpl, %bpl
movq %r10, 0x8(%rsp)
je 0x3692d
movl 0x18(%r8), %edi
movq %rdi, %r13
subq %rbx, %r13
subq %rsi, %rdi
movq 0x20(%r8), %rdx
testq %rdx, %rdx
jle 0x368db
xorl %r12d, %r12d
movq %rdi, 0x10(%rsp)
movq %r13, 0x30(%rsp)
movq %rdx, %rbp
decq %rdx
movq 0x30(%r8), %rax
leaq (%rdx,%rdx,8), %rcx
movl 0x8(%rax,%rcx,4), %esi
cmpq %rsi, %rdi
jge 0x368db
leaq (%rax,%rcx,4), %rax
movl 0x4(%rax), %ecx
cmpq %rcx, %r13
jle 0x368d1
movq %rdx, 0x18(%rsp)
movl 0xc(%rax), %ebx
cmpq %rcx, %rdi
movq %rcx, %rdx
cmovgq %rdi, %rdx
cmpq %rsi, %r13
movq %rsi, %r15
cmovlq %r13, %r15
movq %r15, %r14
subq %rcx, %r14
subq %rdx, %r15
movb 0x20(%rax), %cl
testb $0x40, %cl
jne 0x367d2
movl (%rax), %r9d
testb $0x20, %cl
jne 0x3685e
leaq (%r10,%r12), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rsi
addq %rbx, %r14
subq %r14, %rsi
movq %r15, %rdx
movl $0x1, %ecx
jmp 0x36876
addq 0x10(%r8), %rbx
movl %r14d, %eax
andl $0x3, %eax
cmpq %r15, %rax
movq %r15, %r13
cmovlq %rax, %r13
decq %r14
testq %r13, %r13
jle 0x3681d
leaq (%r10,%r12), %rdi
movq %r14, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq 0x25462c(%rip), %rdx    # 0x28ae30
leaq (%rdx,%rcx,4), %rsi
subq %rax, %rsi
addq $0x4, %rsi
movq %r13, %rdx
callq 0x2160
addq %r13, %r12
subq %r13, %r14
subq %r13, %r15
movq %r15, %rax
sarq $0x2, %rax
testq %rax, %rax
jle 0x3688a
movq 0x8(%rsp), %r10
movq 0x30(%rsp), %r13
leaq 0x2545f3(%rip), %rdx    # 0x28ae30
movq %r14, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%rdx,%rcx,4), %ecx
movl %ecx, (%r10,%r12)
addq $0x4, %r12
addq $-0x4, %r14
decq %rax
jne 0x3683d
jmp 0x3689b
subq %r14, %rsi
addq %rbx, %rsi
leaq (%r10,%r12), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x337f7
addq %r15, %r12
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %r10
jmp 0x368c7
movq 0x8(%rsp), %r10
movq 0x30(%rsp), %r13
leaq 0x254595(%rip), %rdx    # 0x28ae30
andl $0x3, %r15d
je 0x368c2
leaq (%r10,%r12), %rdi
sarq $0x2, %r14
movzbl (%rbx,%r14), %eax
leaq (%rdx,%rax,4), %rsi
movq %r15, %rdx
callq 0x2160
movq 0x8(%rsp), %r10
addq %r15, %r12
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
cmpq $0x1, %rbp
jg 0x36746
cmpq $0x0, 0x40(%r8)
jle 0x36afe
movl 0x18(%r8), %eax
movq %rax, %rcx
subq %rdi, %rcx
subq %r13, %rax
xorl %edx, %edx
movq 0x38(%r8), %rsi
movl (%rsi,%rdx,4), %esi
notl %esi
addl 0x18(%r8), %esi
movq %rsi, %rdi
subq %rax, %rdi
jl 0x36916
cmpq %rsi, %rcx
jle 0x36916
movb $0x4e, (%r10,%rdi)
jmp 0x3691f
cmpq %rax, %rsi
jl 0x36afe
incq %rdx
cmpq 0x40(%r8), %rdx
jl 0x368f5
jmp 0x36afe
cmpq $0x0, 0x20(%r8)
jle 0x36acc
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rsi, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x30(%r8), %rcx
leaq (%r15,%r15,8), %rdx
movl 0x8(%rcx,%rdx,4), %eax
cmpq %rbx, %rax
jle 0x36abf
leaq (%rcx,%rdx,4), %rdx
movl 0x4(%rdx), %ecx
cmpq %rcx, %rsi
jle 0x36acc
cmpq %rbx, %rcx
movq %rbx, %rdi
cmovgq %rcx, %rdi
movq %rdi, %r13
subq %rcx, %r13
cmpq %rax, %rsi
movq %rax, %r12
cmovlq %rsi, %r12
movl 0xc(%rdx), %ebx
subq %rdi, %r12
movb 0x20(%rdx), %sil
testb $0x40, %sil
jne 0x369c4
shrb $0x5, %sil
andb $0x1, %sil
leaq (%r10,%r14), %rdi
movzbl %sil, %ecx
testb %sil, %sil
jne 0x36a49
addq %rbx, %r13
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movl (%rdx), %r9d
jmp 0x36a63
addq 0x10(%r8), %rbx
movl %r13d, %eax
andl $0x3, %eax
movl $0x4, %ebp
subq %rax, %rbp
cmpq %r12, %rbp
cmovgeq %r12, %rbp
testq %r12, %r12
jle 0x36a0d
leaq (%r10,%r14), %rdi
movq %r13, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq 0x254038(%rip), %rdx    # 0x28aa30
leaq (%rdx,%rcx,4), %rsi
addq %rax, %rsi
movq %rbp, %rdx
callq 0x2160
addq %rbp, %r14
addq %rbp, %r13
subq %rbp, %r12
movq %r12, %rax
sarq $0x2, %rax
testq %rax, %rax
jle 0x36a7d
movq 0x8(%rsp), %r10
leaq 0x254008(%rip), %rdx    # 0x28aa30
movq %r13, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%rdx,%rcx,4), %ecx
movl %ecx, (%r10,%r14)
addq $0x4, %r14
addq $0x4, %r13
decq %rax
jne 0x36a28
jmp 0x36a89
movq 0x28(%rsp), %rsi
movq 0x60(%rsi), %r8
movq 0x18(%r8), %rsi
movl (%rdx), %r9d
addq %rbx, %rax
subq %rax, %r13
addq (%rsi,%r9,8), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x337f7
addq %r12, %r14
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %r10
jmp 0x36ab5
movq 0x8(%rsp), %r10
leaq 0x253fa7(%rip), %rdx    # 0x28aa30
andl $0x3, %r12d
je 0x36ab0
leaq (%r10,%r14), %rdi
sarq $0x2, %r13
movzbl (%rbx,%r13), %eax
leaq (%rdx,%rax,4), %rsi
movq %r12, %rdx
callq 0x2160
movq 0x8(%rsp), %r10
addq %r12, %r14
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rsi
incq %r15
cmpq 0x20(%r8), %r15
jl 0x36948
cmpq $0x0, 0x40(%r8)
jle 0x36afe
xorl %eax, %eax
movq 0x38(%r8), %rcx
movl (%rcx,%rax,4), %ecx
movq %rcx, %rdx
subq %rbx, %rdx
jl 0x36af0
cmpq %rcx, %rsi
jle 0x36af0
movb $0x4e, (%r10,%rdx)
jmp 0x36af5
cmpq %rcx, %rsi
jle 0x36afe
incq %rax
cmpq 0x40(%r8), %rax
jl 0x36ad5
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq