// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %r13
movq 0x70(%rsp), %rsi
imulq $0x58, %rcx, %r8
movq %rdx, 0x28(%rsp)
addq 0x48(%rdx), %r8
testq %rsi, %rsi
jns 0x9a50
movl 0x18(%r8), %esi
testq %r13, %r13
movq %r8, 0x8(%rsp)
je 0x9a89
movq %rsi, 0x8(%r13)
movq %rcx, 0x18(%r13)
movq (%r13), %rax
cmpq 0x10(%r13), %rsi
jle 0x9a8b
movq %rsi, 0x10(%r13)
movq %rax, %rdi
movq %rsi, %r15
callq 0x2370
movq %r15, %rsi
movq 0x8(%rsp), %r8
movq %rax, (%r13)
jmp 0x9a8b
xorl %eax, %eax
addq %rbx, %rsi
testq %r14, %r14
cmovneq %r14, %rax
testb %bpl, %bpl
movq %rax, 0x10(%rsp)
je 0x9d14
movl 0x18(%r8), %r13d
movq %r13, %r14
subq %rbx, %r14
subq %rsi, %r13
movq 0x20(%r8), %rbp
xorl %ecx, %ecx
xorl %r10d, %r10d
imulq $0x24, %rbp, %rdi
testq %rbp, %rbp
jle 0x9ccd
movq 0x30(%r8), %r9
movl -0x1c(%r9,%rdi), %esi
cmpq %rsi, %r13
jge 0x9ccd
decq %rbp
movl -0x20(%r9,%rdi), %edx
addq $-0x24, %rdi
cmpq %rdx, %r14
jle 0x9abd
movl 0xc(%r9,%rdi), %r11d
cmpq %rdx, %r13
movq %rdx, %rbx
cmovgq %r13, %rbx
cmpq %rsi, %r14
movq %rsi, %r15
cmovbq %r14, %r15
movq %r15, %r12
subq %rdx, %r12
movq %r15, 0x20(%rsp)
subq %rbx, %r15
movb 0x20(%r9,%rdi), %dl
testb $0x40, %dl
jne 0x9b4d
movl (%r9,%rdi), %r9d
testb $0x20, %dl
jne 0x9bc4
leaq (%rax,%r10), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rsi
addq %r11, %r12
subq %r12, %rsi
movq %r15, %rdx
pushq $0x1
popq %rcx
jmp 0x9bdc
movq %rbx, 0x30(%rsp)
addq 0x10(%r8), %r11
movl %r12d, %edx
andl $0x3, %edx
cmpq %r15, %rdx
movq %r15, %r9
cmovlq %rdx, %r9
decq %r12
testq %r9, %r9
movq %r9, 0x18(%rsp)
movq %r11, (%rsp)
jle 0x9bfb
leaq (%rax,%r10), %rdi
movq %r12, %rax
sarq $0x2, %rax
movzbl (%r11,%rax), %eax
leaq 0x24a29e(%rip), %rsi    # 0x253e30
leaq (%rsi,%rax,4), %rsi
subq %rdx, %rsi
addq $0x4, %rsi
movq %r9, %rdx
movq %r10, %rbx
callq 0x2160
movq 0x18(%rsp), %r9
movq %rbx, %r10
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %r8
addq %r9, %rbx
subq %r9, %r12
jmp 0x9bfe
subq %r12, %rsi
addq %r11, %rsi
leaq (%rax,%r10), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq %r15, %rdx
xorl %ecx, %ecx
movq %r10, %rbx
callq 0x6c9e
addq %r15, %rbx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
movq %rbx, %r10
jmp 0x9ab9
movq %r10, %rbx
subq %r9, %r15
movl %r15d, %edx
andl $0x3, %edx
sarq $0x2, %r15
testq %r15, %r15
cmovleq %rcx, %r15
negq %rbx
movq %r9, %rdi
movq %r10, %r9
addq %rax, %r9
testq %rdi, %rdi
movl $0x0, %esi
cmovgq %rdi, %rsi
addq %r9, %rsi
xorl %r11d, %r11d
cmpq %r11, %r15
je 0x9c61
movq %r12, %rdi
sarq $0x2, %rdi
movq (%rsp), %r10
movzbl (%r10,%rdi), %edi
leaq 0x24a1e4(%rip), %r10    # 0x253e30
movl (%r10,%rdi,4), %edi
movl %edi, (%rsi,%r11,4)
addq $-0x4, %r12
incq %r11
addq $-0x4, %rbx
jmp 0x9c30
movq %rbx, %r10
negq %r10
testq %rdx, %rdx
je 0x9ab9
movq 0x18(%rsp), %r15
testq %r15, %r15
movl $0x0, %esi
cmovgq %r15, %rsi
addq %rsi, %r9
leaq (%r9,%r11,4), %rdi
sarq $0x2, %r12
movq (%rsp), %rax
movzbl (%rax,%r12), %eax
leaq 0x24a194(%rip), %rsi    # 0x253e30
leaq (%rsi,%rax,4), %rsi
callq 0x2160
xorl %ecx, %ecx
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %rdx
subb %r15b, %dl
subb 0x30(%rsp), %dl
movzbl %dl, %r10d
andl $0x3, %r10d
subq %rbx, %r10
jmp 0x9ab9
movl 0x18(%r8), %r9d
movq %r9, %rcx
subq %r13, %rcx
subq %r14, %r9
xorl %edx, %edx
cmpq 0x40(%r8), %rdx
jge 0x9f1a
movq 0x38(%r8), %rsi
movl (%rsi,%rdx,4), %esi
notl %esi
addl 0x18(%r8), %esi
movq %rsi, %rdi
subq %r9, %rdi
jl 0x9d06
cmpq %rsi, %rcx
jle 0x9d06
movb $0x4e, (%rax,%rdi)
jmp 0x9d0f
cmpq %r9, %rsi
jl 0x9f1a
incq %rdx
jmp 0x9cdc
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rsi, 0x20(%rsp)
movq %rbx, (%rsp)
cmpq 0x20(%r8), %r14
jge 0x9eee
movq 0x30(%r8), %r9
imulq $0x24, %r14, %rdx
movl 0x8(%r9,%rdx), %ecx
cmpq %rbx, %rcx
jle 0x9ee6
addq %rdx, %r9
movl 0x4(%r9), %edx
cmpq %rdx, %rsi
jle 0x9eee
movl 0xc(%r9), %r12d
cmpq %rbx, %rdx
movq %rbx, %rdi
cmovgq %rdx, %rdi
movq %rdi, %rbp
subq %rdx, %rbp
cmpq %rcx, %rsi
movq %rcx, %rbx
cmovbq %rsi, %rbx
movq %rbx, %r13
subq %rdi, %r13
movb 0x20(%r9), %dl
testb $0x40, %dl
jne 0x9daa
shrb $0x5, %dl
andb $0x1, %dl
leaq (%rax,%r15), %rdi
testb %dl, %dl
jne 0x9e0a
addq %r12, %rbp
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movl (%r9), %r9d
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
jmp 0x9e2d
movq %rdi, 0x30(%rsp)
movq %r14, 0x18(%rsp)
addq 0x10(%r8), %r12
movl %ebp, %edx
andl $0x3, %edx
pushq $0x4
popq %r14
subq %rdx, %r14
cmpq %r13, %r14
cmovgeq %r13, %r14
testq %r13, %r13
jle 0x9e4d
leaq (%rax,%r15), %rdi
movq %rbp, %rcx
sarq $0x2, %rcx
movzbl (%r12,%rcx), %ecx
leaq 0x249c49(%rip), %rax    # 0x253a30
leaq (%rax,%rcx,4), %rsi
addq %rdx, %rsi
movq %r14, %rdx
callq 0x2160
leaq 0x249c33(%rip), %r8     # 0x253a30
movq 0x10(%rsp), %rax
addq %r14, %r15
addq %r14, %rbp
jmp 0x9e54
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq 0x18(%r8), %rsi
movl (%r9), %r9d
addq %r12, %rcx
subq %rcx, %rbp
addq (%rsi,%r9,8), %rbp
movzbl %dl, %ecx
movq %rbp, %rsi
movq %r13, %rdx
callq 0x6c9e
movq (%rsp), %rbx
addq %r13, %r15
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rsi
jmp 0x9ee6
leaq 0x249bdc(%rip), %r8     # 0x253a30
subq %r14, %r13
movl %r13d, %edx
andl $0x3, %edx
sarq $0x2, %r13
testq %r13, %r13
movl $0x0, %ecx
cmovleq %rcx, %r13
leaq (%rax,%r15), %rdi
negq %r15
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x9e9b
movq %rbp, %rsi
sarq $0x2, %rsi
movzbl (%r12,%rsi), %esi
movl (%r8,%rsi,4), %esi
movl %esi, (%rdi,%rcx,4)
addq $0x4, %rbp
incq %rcx
addq $-0x4, %r15
jmp 0x9e76
testq %rdx, %rdx
je 0x9ed0
leaq (%rdi,%rcx,4), %rdi
sarq $0x2, %rbp
movzbl (%r12,%rbp), %eax
leaq (%r8,%rax,4), %rsi
callq 0x2160
movq 0x10(%rsp), %rax
subb %r14b, %bl
subb 0x30(%rsp), %bl
movzbl %bl, %ecx
andl $0x3, %ecx
subq %r15, %rcx
movq %rcx, %r15
jmp 0x9ed3
negq %r15
movq 0x8(%rsp), %r8
movq (%rsp), %rbx
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r14
incq %r14
jmp 0x9d23
xorl %edi, %edi
cmpq 0x40(%r8), %rdi
jge 0x9f1a
movq 0x38(%r8), %rcx
movl (%rcx,%rdi,4), %ecx
movq %rcx, %rdx
subq %rbx, %rdx
jl 0x9f10
cmpq %rcx, %rsi
jle 0x9f10
movb $0x4e, (%rax,%rdx)
jmp 0x9f15
cmpq %rcx, %rsi
jle 0x9f1a
incq %rdi
jmp 0x9ef0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq