// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: get_ul_subregion(all_ul_t*, char*, unsigned int, unsigned int, long, long, unsigned int)
void get_ul_subregion(all_ul_t *x, char *des, uint32_t id, uint32_t rev, int64_t ssp, int64_t sep, uint32_t reset_Ns)
{
    ul_vec_t *z = &(x->a[id]); 
    int64_t sl, slr, offset, begLen, tailLen, a_n, src_i, des_i, i;
    sl = sep - ssp;
	offset = ssp&3; begLen = 4-offset;
	if(begLen > sl) begLen = sl;
	tailLen = (sl-begLen)&3;
	a_n = (sl - begLen - tailLen)>>2;

    // fprintf(stderr, "[M::%s] z->r_base.n::%u, begLen::%ld, tailLen::%ld, a_n::%ld\n", 
    // __func__, (uint32_t)z->r_base.n, begLen, tailLen, a_n);
    src_i = ssp; i = 0; des_i = 0;
	if(begLen > 0) {
        // fprintf(stderr, "[M::%s] des_i::%ld, src_i>>2::%ld\n", __func__, des_i, src_i>>2);
		memcpy(des+des_i, bit_t_seq_table[z->r_base.a[src_i>>2]]+offset, begLen);
		des_i += begLen; src_i += begLen;
	}

	for (i = 0; i < a_n; i++) {
        // fprintf(stderr, "[M::%s] des_i::%ld, src_i>>2::%ld\n", __func__, des_i, src_i>>2);
		memcpy(des+des_i, bit_t_seq_table[z->r_base.a[src_i>>2]], 4);
		des_i += 4; src_i += 4;
	}

	if(tailLen > 0) {
        // fprintf(stderr, "[M::%s] des_i::%ld, src_i>>2::%ld\n", __func__, des_i, src_i>>2);
		memcpy(des+des_i, bit_t_seq_table[z->r_base.a[src_i>>2]], tailLen);
		des_i += tailLen; src_i += tailLen;
	}

    uint64_t k, sk = ssp, ek = sep;
    for (k = 0; k < z->N_site.n; k++) {
        if(z->N_site.a[k] >= sk && z->N_site.a[k] < ek){
            des[z->N_site.a[k]-sk] = 'N';
        } else if(z->N_site.a[k] >= ek) {
            break;
        }
    }
    
    if(reset_Ns) {
        for (i = 0; i < sl; i++) {
            if (seq_nt4_table[(uint8_t)des[i]] >= 4) des[i] = 'A';
        }
    }

    if(rev) {
        char t; slr = sl>>1;
        for (i = 0; i < slr; i++) {
            t = des[sl-i-1];
            des[sl-i-1] = RC_CHAR(des[i]);
            des[i] = RC_CHAR(t);
        }
        if(sl&1) des[i] = RC_CHAR(des[i]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r14
movl %ecx, 0x4(%rsp)
movl %edx, %eax
imulq $0x58, %rax, %r13
addq 0x48(%rdi), %r13
movq %rsi, %rbx
movq %r9, %r15
subq %r8, %r15
movl %r8d, %eax
andl $0x3, %eax
movl $0x4, %r12d
subq %rax, %r12
cmpq %r15, %r12
cmovgeq %r15, %r12
movq %r15, %rbp
subq %r12, %rbp
movl %ebp, %ecx
andl $0x3, %ecx
movq %rcx, 0x10(%rsp)
sarq $0x2, %rbp
testq %r15, %r15
jle 0x22134a
movq 0x10(%r13), %rcx
movq %r8, %rdx
sarq $0x2, %rdx
movzbl (%rcx,%rdx), %esi
shlq $0x2, %rsi
addq 0x73c9c(%rip), %rsi     # 0x294fc8
addq %rax, %rsi
movq %rbx, %rdi
movq %r12, %rdx
movq %r8, 0x8(%rsp)
callq 0x2160
movq 0x8(%rsp), %r8
leaq (%r12,%r8), %rax
jmp 0x221350
xorl %r12d, %r12d
movq %r8, %rax
testq %rbp, %rbp
jle 0x22137f
leaq 0x746d4(%rip), %rcx     # 0x295a30
movq 0x10(%r13), %rdx
movq %rax, %rsi
sarq $0x2, %rsi
movzbl (%rdx,%rsi), %edx
movl (%rcx,%rdx,4), %edx
movl %edx, (%rbx,%r12)
addq $0x4, %r12
addq $0x4, %rax
decq %rbp
jne 0x22135c
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x2213b0
addq %rbx, %r12
movq 0x10(%r13), %rcx
sarq $0x2, %rax
movzbl (%rcx,%rax), %esi
shll $0x2, %esi
addq 0x73c26(%rip), %rsi     # 0x294fc8
movq %r12, %rdi
movq %r8, %r12
callq 0x2160
movq %r12, %r8
movl 0x50(%rsp), %eax
movq 0x40(%r13), %rcx
testq %rcx, %rcx
je 0x2213f3
xorl %edx, %edx
movq 0x38(%r13), %rsi
movl (%rsi,%rdx,4), %esi
movq %rsi, %rdi
subq %r8, %rdi
setb %r10b
cmpq %r14, %rsi
setae %r9b
orb %r10b, %r9b
jne 0x2213e6
movb $0x4e, (%rbx,%rdi)
movq 0x40(%r13), %rcx
jmp 0x2213eb
cmpq %r14, %rsi
jae 0x2213f3
incq %rdx
cmpq %rcx, %rdx
jb 0x2213bf
testq %r15, %r15
setle %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x221421
xorl %eax, %eax
leaq 0x18815(%rip), %rcx     # 0x239c20
movzbl (%rbx,%rax), %edx
cmpb $0x4, (%rcx,%rdx)
jb 0x221419
movb $0x41, (%rbx,%rax)
incq %rax
cmpq %rax, %r15
jne 0x22140b
cmpl $0x0, 0x4(%rsp)
je 0x2214ac
movq %r15, %rax
sarq %rax
testq %rax, %rax
jle 0x221488
movq %rax, %rcx
negq %rcx
notq %r8
addq %rbx, %r14
addq %r8, %r14
xorl %edx, %edx
leaq 0x741e1(%rip), %rsi     # 0x295630
leaq 0x742df(%rip), %rdi     # 0x295735
movq %rbx, %r8
movzbl (%r14,%rdx), %r9d
movzbl (%r8), %r10d
movzbl (%rsi,%r10), %r10d
movb (%rdi,%r10), %r10b
movb %r10b, (%r14,%rdx)
movzbl (%rsi,%r9), %r9d
movb (%rdi,%r9), %r9b
movb %r9b, (%r8)
decq %rdx
incq %r8
cmpq %rdx, %rcx
jne 0x221459
jmp 0x22148a
xorl %eax, %eax
testb $0x1, %r15b
je 0x2214ac
movzbl (%rbx,%rax), %ecx
leaq 0x74195(%rip), %rdx     # 0x295630
movzbl (%rdx,%rcx), %ecx
leaq 0x7428f(%rip), %rdx     # 0x295735
movb (%rdx,%rcx), %cl
movb %cl, (%rbx,%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq