// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, %r12
movq 0x10(%r8), %rax
addq %rsi, %rdx
movq %rdx, 0x18(%rsp)
movq (%rax,%r9,8), %rbx
testl %ecx, %ecx
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
je 0x33897
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rax
movq %rax, 0x8(%rsp)
subq %rsi, %rax
leaq -0x1(%rax), %r14
andl $0x3, %eax
cmpq %r13, %rax
movq %r13, %rbp
cmovlq %rax, %rbp
subq %rbp, %r13
movl %r13d, %ecx
andl $0x3, %ecx
movq %rcx, 0x10(%rsp)
sarq $0x2, %r13
leaq 0x2575c8(%rip), %r15    # 0x28ae30
testq %rbp, %rbp
jle 0x338fb
movq %r14, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
leaq (%r15,%rcx,4), %rsi
subq %rax, %rsi
addq $0x4, %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2160
subq %rbp, %r14
jmp 0x338fd
movl %esi, %eax
andl $0x3, %eax
movl $0x4, %ebp
subq %rax, %rbp
cmpq %r13, %rbp
cmovgeq %r13, %rbp
movq %r13, %r15
subq %rbp, %r15
movl %r15d, %ecx
andl $0x3, %ecx
movq %rcx, 0x8(%rsp)
sarq $0x2, %r15
leaq 0x257169(%rip), %r14    # 0x28aa30
testq %r13, %r13
jle 0x339ce
movq %rsi, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movq %rsi, %r13
leaq (%r14,%rcx,4), %rsi
addq %rax, %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2160
movq %rbp, %rax
addq %r13, %rax
jmp 0x339d3
xorl %ebp, %ebp
testq %r13, %r13
jle 0x33929
movq 0x10(%rsp), %rdx
movq %r14, %rax
sarq $0x2, %rax
movzbl (%rbx,%rax), %eax
movl (%r15,%rax,4), %eax
movl %eax, (%r12,%rbp)
addq $0x4, %rbp
addq $-0x4, %r14
decq %r13
jne 0x33907
jmp 0x3392e
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x3394b
addq %r12, %rbp
sarq $0x2, %r14
movzbl (%rbx,%r14), %eax
leaq (%r15,%rax,4), %rsi
movq %rbp, %rdi
callq 0x2160
movq 0x28(%rsp), %r9
movq (%r9), %rax
movq 0x30(%rsp), %r8
cmpq $0x0, (%rax,%r8,8)
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r11
movq 0x8(%rsp), %rbx
je 0x33a87
movq (%r9), %rax
movq (%rax,%r8,8), %rcx
cmpq $0x0, (%rcx)
jle 0x33a87
xorl %eax, %eax
movq 0x8(%rcx,%rax,8), %rcx
notq %rcx
addq %rbx, %rcx
movq %rcx, %rdx
subq %r10, %rdx
setge %sil
cmpq %r11, %rcx
setl %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x339b1
movb $0x4e, (%r12,%rdx)
jmp 0x339ba
cmpq %r10, %rcx
jl 0x33a87
movq (%r9), %rcx
movq (%rcx,%r8,8), %rcx
incq %rax
cmpq (%rcx), %rax
jl 0x33985
jmp 0x33a87
xorl %ebp, %ebp
movq %rsi, %rax
testq %r15, %r15
jle 0x339ff
movq 0x8(%rsp), %rdx
movq %rax, %rcx
sarq $0x2, %rcx
movzbl (%rbx,%rcx), %ecx
movl (%r14,%rcx,4), %ecx
movl %ecx, (%r12,%rbp)
addq $0x4, %rbp
addq $0x4, %rax
decq %r15
jne 0x339dd
jmp 0x33a04
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x33a20
addq %r12, %rbp
sarq $0x2, %rax
movzbl (%rbx,%rax), %eax
leaq (%r14,%rax,4), %rsi
movq %rbp, %rdi
callq 0x2160
movq 0x28(%rsp), %r9
movq (%r9), %rax
movq 0x30(%rsp), %r8
cmpq $0x0, (%rax,%r8,8)
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r10
je 0x33a87
movq (%r9), %rax
movq (%rax,%r8,8), %rcx
cmpq $0x0, (%rcx)
jle 0x33a87
xorl %eax, %eax
movq 0x8(%rcx,%rax,8), %rcx
movq %rcx, %rdx
subq %r13, %rdx
setge %sil
cmpq %r10, %rcx
setl %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x33a73
movb $0x4e, (%r12,%rdx)
jmp 0x33a78
cmpq %r10, %rcx
jge 0x33a87
movq (%r9), %rcx
movq (%rcx,%r8,8), %rcx
incq %rax
cmpq (%rcx), %rax
jl 0x33a4d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq