// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Process_Read.cpp
// Function: recover_UC_Read_sub_region(char*, long, long, unsigned char, All_reads*, long)
void recover_UC_Read_sub_region(char* r, int64_t start_pos, int64_t length, uint8_t strand, All_reads* R_INF, int64_t ID)
{
	int64_t readLen = Get_READ_LENGTH((*R_INF), ID);
	int64_t end_pos = start_pos + length - 1, begLen, tailLen, offset, mn, src_i, des_i, i;
	uint8_t* src = Get_READ((*R_INF), ID);

	if(strand == 0) {
		offset = start_pos&3;
		begLen = 4-offset;
		if(begLen > length) begLen = length;
		tailLen = (length-begLen)&3;
		mn = (length - begLen - tailLen)>>2;
		src_i = start_pos; des_i = 0; 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 < mn; 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;
		}

		if (R_INF->N_site[ID]) {
			for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) {
				if ((long long)R_INF->N_site[ID][i] >= start_pos && (long long)R_INF->N_site[ID][i] <= end_pos) {
					r[R_INF->N_site[ID][i] - start_pos] = 'N';
				}
				else if((long long)R_INF->N_site[ID][i] > end_pos) {
					break;
				}
			}
		}
	}
	else {
		start_pos = readLen - start_pos - 1;
		end_pos = readLen - end_pos - 1;

		begLen = (start_pos+1)&3;
		offset = 4 - begLen;
		if(begLen > length) begLen = length;
		tailLen = (length-begLen)&3;
		mn = (length - begLen - tailLen)>>2;
		src_i = start_pos; des_i = 0; 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 < mn; 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;
		}

		/**
		if (R_INF->N_site[ID]) {
			offset = readLen - start_pos - 1;
			for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) {
				if ((long long)R_INF->N_site[ID][i] >= end_pos && (long long)R_INF->N_site[ID][i] <= start_pos) {
					r[readLen - R_INF->N_site[ID][i] - 1 - offset] = 'N';
				}
				else if((long long)R_INF->N_site[ID][i] > start_pos) {
					break;
				}
			}
		}
		**/
		if (R_INF->N_site[ID]) {
			start_pos = readLen - start_pos - 1; end_pos = readLen - end_pos - 1;
			for (i = 1; i <= (long long)R_INF->N_site[ID][0]; i++) {
				offset = readLen - R_INF->N_site[ID][i] - 1;
				if(offset >= start_pos && offset <= end_pos) {
					r[offset - start_pos] = 'N';
				} else if(offset < start_pos) {
					break;
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbp
movq %rsi, %r15
movq 0x10(%r8), %rax
addq %rsi, %rdx
movq %rdx, 0x10(%rsp)
movq (%rax,%r9,8), %rbx
testl %ecx, %ecx
movq %rdi, 0x8(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
je 0x6d44
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rax
movq %rax, 0x30(%rsp)
subq %r15, %rax
leaq -0x1(%rax), %r12
andl $0x3, %eax
cmpq %rbp, %rax
movq %rbp, %r13
cmovlq %rax, %r13
subq %r13, %rbp
movl %ebp, %edx
andl $0x3, %edx
sarq $0x2, %rbp
leaq 0x24d127(%rip), %r14    # 0x253e30
testq %r13, %r13
jle 0x6da7
movq %r12, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq (%r14,%rcx,4), %rsi
subq %rax, %rsi
addq $0x4, %rsi
movq %rdx, 0x28(%rsp)
movq %r13, %rdx
callq 0x2160
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %rdi
subq %r13, %r12
jmp 0x6daa
movl %r15d, %eax
andl $0x3, %eax
pushq $0x4
popq %r13
subq %rax, %r13
cmpq %rbp, %r13
cmovgeq %rbp, %r13
movq %rbp, %r12
subq %r13, %r12
movl %r12d, %r8d
andl $0x3, %r8d
sarq $0x2, %r12
leaq 0x24ccc0(%rip), %r14    # 0x253a30
testq %rbp, %rbp
jle 0x6e6e
movq %r15, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq (%r14,%rcx,4), %rsi
addq %rax, %rsi
movq %r13, %rdx
movq %r8, %rbp
callq 0x2160
movq %rbp, %r8
movq 0x8(%rsp), %rdi
leaq (%r15,%r13), %rax
jmp 0x6e74
xorl %r13d, %r13d
xorl %eax, %eax
testq %rbp, %rbp
cmovleq %rax, %rbp
addq %rdi, %r13
cmpq %rax, %rbp
je 0x6dd8
movq %r12, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%r14,%rcx,4), %ecx
movl %ecx, (%r13,%rax,4)
addq $-0x4, %r12
incq %rax
jmp 0x6db6
testq %rdx, %rdx
je 0x6dff
leaq (,%rax,4), %rdi
addq %r13, %rdi
sarq $0x2, %r12
movzbl (%rbx,%r12), %eax
leaq (%r14,%rax,4), %rsi
callq 0x2160
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
movq (%r10), %rax
movq 0x20(%rsp), %r9
movq (%rax,%r9,8), %rax
testq %rax, %rax
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r14
je 0x6f20
pushq $0x1
popq %rcx
cmpq (%rax), %rcx
jg 0x6f20
movq (%rax,%rcx,8), %rdx
notq %rdx
addq %r14, %rdx
movq %rdx, %rsi
subq %r15, %rsi
setge %r11b
cmpq %rbx, %rdx
setl %r8b
andb %r11b, %r8b
cmpb $0x1, %r8b
jne 0x6e60
movb $0x4e, (%rdi,%rsi)
movq (%r10), %rax
movq (%rax,%r9,8), %rax
jmp 0x6e69
cmpq %r15, %rdx
jl 0x6f20
incq %rcx
jmp 0x6e26
xorl %r13d, %r13d
movq %r15, %rax
xorl %ecx, %ecx
testq %r12, %r12
cmovleq %rcx, %r12
addq %rdi, %r13
shlq $0x2, %r12
leaq (%rax,%rcx), %rdx
cmpq %rcx, %r12
je 0x6ea4
sarq $0x2, %rdx
movzbl (%rbx,%rdx), %edx
movl (%r14,%rdx,4), %edx
movl %edx, (%r13,%rcx)
addq $0x4, %rcx
jmp 0x6e84
testq %r8, %r8
je 0x6ec8
addq %rcx, %r13
sarq $0x2, %rdx
movzbl (%rbx,%rdx), %eax
leaq (%r14,%rax,4), %rsi
movq %r13, %rdi
movq %r8, %rdx
callq 0x2160
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r10
movq (%r10), %rax
movq 0x20(%rsp), %r9
movq (%rax,%r9,8), %rax
testq %rax, %rax
movq 0x10(%rsp), %rbx
je 0x6f20
pushq $0x1
popq %rcx
cmpq (%rax), %rcx
jg 0x6f20
movq (%rax,%rcx,8), %rdx
movq %rdx, %rsi
subq %r15, %rsi
setge %r11b
cmpq %rbx, %rdx
setl %r8b
andb %r11b, %r8b
cmpb $0x1, %r8b
jne 0x6f16
movb $0x4e, (%rdi,%rsi)
movq (%r10), %rax
movq (%rax,%r9,8), %rax
jmp 0x6f1b
cmpq %rbx, %rdx
jge 0x6f20
incq %rcx
jmp 0x6ee6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq