// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movl %r8d, %ebp
movq %rsi, %r13
movq %rdi, %r14
movq 0x70(%rsp), %rbx
imulq $0x58, %rcx, %r9
movq %rdx, 0x28(%rsp)
addq 0x48(%rdx), %r9
testq %rbx, %rbx
jns 0x35bfa
movl 0x18(%r9), %ebx
testq %r14, %r14
movq %r9, 0x20(%rsp)
je 0x35c2e
movq %rbx, 0x8(%r14)
movq %rcx, 0x18(%r14)
movq (%r14), %rax
cmpq 0x10(%r14), %rbx
jle 0x35c30
movq %rbx, 0x10(%r14)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2370
movq 0x20(%rsp), %r9
movq %rax, (%r14)
jmp 0x35c30
xorl %eax, %eax
movq 0x18(%rsp), %r14
addq %r14, %rbx
testq %r13, %r13
cmovneq %r13, %rax
testb %bpl, %bpl
movq %rax, 0x8(%rsp)
je 0x35e63
movl 0x18(%r9), %r10d
movq %r10, %r13
subq %r14, %r13
movq %r10, %r8
subq %rbx, %r8
movq 0x20(%r9), %r11
testq %r11, %r11
jle 0x35e10
xorl %r14d, %r14d
movq %r8, 0x10(%rsp)
movq %r13, 0x30(%rsp)
movq %r11, %rbp
decq %r11
movq 0x30(%r9), %rdx
leaq (%r11,%r11,8), %rcx
movl 0x8(%rdx,%rcx,4), %esi
cmpq %rsi, %r8
jge 0x35e0c
leaq (%rdx,%rcx,4), %rdi
movl 0x4(%rdi), %ecx
cmpq %rcx, %r13
jle 0x35e02
movl 0xc(%rdi), %ebx
cmpq %rcx, %r8
movq %rcx, %rdx
cmovgq %r8, %rdx
cmpq %rsi, %r13
movq %rsi, %r12
cmovbq %r13, %r12
movq %r12, %r15
subq %rcx, %r15
subq %rdx, %r12
movb 0x20(%rdi), %cl
testb $0x40, %cl
movq %r11, 0x18(%rsp)
jne 0x35d03
movl (%rdi), %r9d
testb $0x20, %cl
jne 0x35d8f
leaq (%rax,%r14), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rsi
addq %rbx, %r15
subq %r15, %rsi
movq %r12, %rdx
movl $0x1, %ecx
jmp 0x35da7
addq 0x10(%r9), %rbx
movl %r15d, %edx
andl $0x3, %edx
cmpq %r12, %rdx
movq %r12, %r13
cmovlq %rdx, %r13
decq %r15
testq %r13, %r13
jle 0x35d4e
leaq (%rax,%r14), %rdi
movq %r15, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq 0x2600fb(%rip), %rax    # 0x295e30
leaq (%rax,%rcx,4), %rsi
subq %rdx, %rsi
addq $0x4, %rsi
movq %r13, %rdx
callq 0x2160
addq %r13, %r14
subq %r13, %r15
subq %r13, %r12
movq %r12, %rdx
sarq $0x2, %rdx
testq %rdx, %rdx
jle 0x35dbb
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r13
leaq 0x2600c2(%rip), %rsi    # 0x295e30
movq %r15, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%rsi,%rcx,4), %ecx
movl %ecx, (%rax,%r14)
addq $0x4, %r14
addq $-0x4, %r15
decq %rdx
jne 0x35d6e
jmp 0x35dcc
subq %r15, %rsi
addq %rbx, %rsi
leaq (%rax,%r14), %rdi
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x32ef8
addq %r12, %r14
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %rax
jmp 0x35df8
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r13
leaq 0x260064(%rip), %rsi    # 0x295e30
andl $0x3, %r12d
je 0x35df3
leaq (%rax,%r14), %rdi
sarq $0x2, %r15
movzbl (%rbx,%r15), %eax
leaq (%rsi,%rax,4), %rsi
movq %r12, %rdx
callq 0x2160
movq 0x8(%rsp), %rax
addq %r12, %r14
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r11
cmpq $0x1, %rbp
jg 0x35c77
movl 0x18(%r9), %r10d
movq 0x40(%r9), %rcx
testq %rcx, %rcx
jle 0x3603a
movq %r10, %rdx
subq %r8, %rdx
subq %r13, %r10
xorl %esi, %esi
movq 0x38(%r9), %rdi
movl (%rdi,%rsi,4), %edi
notl %edi
addl 0x18(%r9), %edi
movq %rdi, %r8
subq %r10, %r8
jl 0x35e4d
cmpq %rdi, %rdx
jle 0x35e4d
movb $0x4e, (%rax,%r8)
movq 0x40(%r9), %rcx
jmp 0x35e56
cmpq %r10, %rdi
jl 0x3603a
incq %rsi
cmpq %rcx, %rsi
jl 0x35e28
jmp 0x3603a
cmpq $0x0, 0x20(%r9)
jle 0x36004
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbx, 0x10(%rsp)
movq 0x30(%r9), %rcx
leaq (%r12,%r12,8), %rdx
movl 0x8(%rcx,%rdx,4), %r10d
cmpq %r14, %r10
jle 0x35ff7
leaq (%rcx,%rdx,4), %rcx
movl 0x4(%rcx), %edx
cmpq %rdx, %rbx
jle 0x36004
cmpq %r14, %rdx
movq %r14, %rsi
cmovgq %rdx, %rsi
movq %rsi, %rbp
subq %rdx, %rbp
cmpq %r10, %rbx
movq %r10, %r13
cmovbq %rbx, %r13
movl 0xc(%rcx), %ebx
subq %rsi, %r13
movb 0x20(%rcx), %dl
testb $0x40, %dl
jne 0x35ef5
shrb $0x5, %dl
andb $0x1, %dl
leaq (%rax,%r15), %rdi
testb %dl, %dl
jne 0x35f7f
addq %rbx, %rbp
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movl (%rcx), %r9d
movq %rbp, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
jmp 0x35fa2
addq 0x10(%r9), %rbx
movl %ebp, %edx
andl $0x3, %edx
movl $0x4, %r14d
subq %rdx, %r14
cmpq %r13, %r14
cmovgeq %r13, %r14
testq %r13, %r13
jle 0x35f3e
leaq (%rax,%r15), %rdi
movq %rbp, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq 0x25fb07(%rip), %rax    # 0x295a30
leaq (%rax,%rcx,4), %rsi
addq %rdx, %rsi
movq %r14, %rdx
callq 0x2160
addq %r14, %r15
addq %r14, %rbp
subq %r14, %r13
movq %r13, %rdx
sarq $0x2, %rdx
testq %rdx, %rdx
jle 0x35fb6
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %r14
leaq 0x25fad2(%rip), %rsi    # 0x295a30
movq %rbp, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%rsi,%rcx,4), %ecx
movl %ecx, (%rax,%r15)
addq $0x4, %r15
addq $0x4, %rbp
decq %rdx
jne 0x35f5e
jmp 0x35fc7
movq 0x28(%rsp), %rax
movq 0x60(%rax), %r8
movq 0x18(%r8), %rsi
movl (%rcx), %r9d
addq %rbx, %r10
subq %r10, %rbp
addq (%rsi,%r9,8), %rbp
movzbl %dl, %ecx
movq %rbp, %rsi
movq %r13, %rdx
callq 0x32ef8
addq %r13, %r15
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %rax
jmp 0x35ff2
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %r14
leaq 0x25fa69(%rip), %rsi    # 0x295a30
andl $0x3, %r13d
je 0x35fed
leaq (%rax,%r15), %rdi
sarq $0x2, %rbp
movzbl (%rbx,%rbp), %eax
leaq (%rsi,%rax,4), %rsi
movq %r13, %rdx
callq 0x2160
movq 0x8(%rsp), %rax
addq %r13, %r15
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %rbx
incq %r12
cmpq 0x20(%r9), %r12
jl 0x35e79
movq 0x40(%r9), %rdi
testq %rdi, %rdi
jle 0x3603a
xorl %ecx, %ecx
movq 0x38(%r9), %rdx
movl (%rdx,%rcx,4), %edx
movq %rdx, %rsi
subq %r14, %rsi
jl 0x3602d
cmpq %rdx, %rbx
jle 0x3602d
movb $0x4e, (%rax,%rsi)
movq 0x40(%r9), %rdi
jmp 0x36032
cmpq %rdx, %rbx
jle 0x3603a
incq %rcx
cmpq %rdi, %rcx
jl 0x3600f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq