// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrieve_u_seq(UC_Read*, char*, ma_utg_t*, unsigned char, long, long, void*)
void retrieve_u_seq(UC_Read* i_r, char* i_s, ma_utg_t *u, uint8_t strand, int64_t s, int64_t l, void *km)
{
	if(u->m == 0 || u->n == 0) return;
	if(l < 0) l = u->len;
	char *r = NULL, *a = NULL;
	int64_t e = s + l, ssp, sep, rs, re, des_i;
	uint64_t k, rId, ori, r_l;
	if(i_r) {
        i_r->length = l; i_r->RID = 0;
        if(i_r->length > i_r->size) {
            i_r->size = i_r->length;
			if(!km) REALLOC(i_r->seq, i_r->size);
			else KREALLOC(km, i_r->seq, i_r->size);
            // 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 == 1) {
		sep = u->len - s; 
        ssp = u->len - e;
        s = ssp; e = sep; 
	}
	for (k = l = des_i = 0; k < u->n; k++) {
		rId = u->a[k]>>33;
		ori = u->a[k]>>32&1;
		r_l = (uint32_t)u->a[k];
		if(r_l == 0) continue;
		ssp = l; sep = l + r_l;
		l += r_l;
		if(sep <= s) continue;
		if(ssp >= e) break;
		rs = MAX(ssp, s); re = MIN(sep, e); 
		a = r + des_i; des_i += re - rs;
		recover_UC_Read_sub_region(a, rs-ssp, re-rs, ori, &R_INF, rId);
	}
	if(strand == 1) {
		char t;
		re = (e - s);
		l = re>>1; 
		for (k = 0; k < (uint64_t)l; k++) {
			des_i = re - k - 1;
			t = r[des_i];
			r[des_i] = RC_CHAR(r[k]);
			r[k] = RC_CHAR(t);
		}
		if(re&1) r[l] = RC_CHAR(r[l]);
	}
}

// Assembly:
cmpl $0x0, 0xc(%rdx)
je 0xa104
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
cmpl $0x0, 0x10(%rdx)
je 0xa0f6
movq %r9, %r10
movq %r8, %r14
movq %rsi, %r13
movq %rdi, %rbp
testq %r9, %r9
jns 0x9f69
movl (%r15), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
testq %rbp, %rbp
movl %ecx, 0xc(%rsp)
je 0x9fa0
movq %r10, 0x8(%rbp)
andq $0x0, 0x18(%rbp)
cmpq 0x10(%rbp), %r10
jle 0x9fa4
movq 0x50(%rsp), %rdi
movq %r10, 0x10(%rbp)
movq (%rbp), %rsi
testq %rdi, %rdi
je 0x9faa
movq %r10, %rdx
movq %r10, %rbx
callq 0x1a3acc
jmp 0x9fb8
xorl %eax, %eax
jmp 0x9fc3
movq (%rbp), %rax
jmp 0x9fc3
movq %rsi, %rdi
movq %r10, %rsi
movq %r10, %rbx
callq 0x2370
movq %rbx, %r10
movq %rax, (%rbp)
movl 0xc(%rsp), %ecx
addq %r14, %r10
testq %r13, %r13
cmovneq %r13, %rax
cmpb $0x1, %cl
jne 0x9fea
movl (%r15), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, %rcx
subq %r14, %rcx
subq %r10, %rdx
movq %rcx, %r10
movq %rdx, %r14
xorl %r12d, %r12d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
xorl %r11d, %r11d
movq %r14, 0x10(%rsp)
movl 0x10(%r15), %ecx
cmpq %rcx, %r13
jae 0xa08a
movq 0x18(%r15), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, %rbx
andq %rdx, %rbx
je 0xa07c
addq %r11, %rbx
cmpq %r14, %rbx
jle 0xa07f
cmpq %r10, %r11
jge 0xa08a
movq %r11, %rsi
cmpq %r14, %r11
jg 0xa032
movq %r14, %rsi
movq %rbx, %rdx
cmpq %r10, %rbx
jl 0xa03d
movq %r10, %rdx
movq %rcx, %r9
shrq $0x21, %r9
shrq $0x20, %rcx
leaq (%rax,%r12), %rdi
subq %rsi, %rdx
addq %rdx, %r12
subq %r11, %rsi
andl $0x1, %ecx
leaq 0x24a1d1(%rip), %r8     # 0x254230
movq %r10, %r14
movq %rax, %rbp
callq 0x6c9e
movq %rbp, %rax
movq %r14, %r10
movq 0x10(%rsp), %r14
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
jmp 0xa07f
movq %r11, %rbx
incq %r13
movq %rbx, %r11
jmp 0x9ffd
cmpb $0x1, 0xc(%rsp)
jne 0xa0f6
subq %r14, %r10
movq %r10, %r11
sarq %r11
leaq (%r10,%rax), %rsi
decq %rsi
xorl %edi, %edi
leaq 0x249586(%rip), %rdx    # 0x253630
leaq 0x249684(%rip), %rcx    # 0x253735
cmpq %rdi, %r11
je 0xa0e0
movzbl (%rsi), %r8d
movzbl (%rax,%rdi), %r9d
movzbl (%r9,%rdx), %r9d
movb (%r9,%rcx), %r9b
movb %r9b, (%rsi)
movzbl (%r8,%rdx), %r8d
movb (%r8,%rcx), %r8b
movb %r8b, (%rax,%rdi)
incq %rdi
decq %rsi
jmp 0xa0b1
testb $0x1, %r10b
je 0xa0f6
movzbl (%rax,%r11), %esi
movzbl (%rsi,%rdx), %edx
movb (%rdx,%rcx), %cl
movb %cl, (%rax,%r11)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq