// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r8, %r15
movl %ecx, 0x4(%rsp)
movl %edx, %eax
imulq $0x58, %rax, %rbp
addq 0x48(%rdi), %rbp
movq %rsi, %rbx
movq %r9, %rcx
subq %r8, %rcx
movl %r15d, %eax
andl $0x3, %eax
pushq $0x4
popq %r12
subq %rax, %r12
cmpq %rcx, %r12
cmovgeq %rcx, %r12
movq %rcx, %r13
subq %r12, %r13
movl %r13d, %r8d
andl $0x3, %r8d
sarq $0x2, %r13
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
jle 0x1d234f
movq 0x10(%rbp), %rcx
movq %r15, %rdx
sarq $0x2, %rdx
movzbl (%rcx,%rdx), %esi
shlq $0x2, %rsi
addq 0x80c97(%rip), %rsi     # 0x252fc8
addq %rax, %rsi
movq %rbx, %rdi
movq %r12, %rdx
movq %r8, 0x8(%rsp)
callq 0x2160
movq 0x8(%rsp), %r8
leaq (%r12,%r15), %rax
jmp 0x1d2355
xorl %r12d, %r12d
movq %r15, %rax
xorl %ecx, %ecx
testq %r13, %r13
cmovleq %rcx, %r13
movl 0x50(%rsp), %r10d
shlq $0x2, %r13
addq %rbx, %r12
leaq 0x816bf(%rip), %rdx     # 0x253a30
leaq (%rax,%rcx), %rsi
cmpq %rcx, %r13
je 0x1d2393
movq 0x10(%rbp), %rdi
sarq $0x2, %rsi
movzbl (%rdi,%rsi), %esi
movl (%rdx,%rsi,4), %esi
movl %esi, (%r12,%rcx)
addq $0x4, %rcx
jmp 0x1d2371
testq %r8, %r8
je 0x1d23c2
addq %rcx, %r12
movq 0x10(%rbp), %rax
sarq $0x2, %rsi
movzbl (%rax,%rsi), %esi
shll $0x2, %esi
addq 0x80c17(%rip), %rsi     # 0x252fc8
movq %r12, %rdi
movq %r8, %rdx
movl %r10d, %r12d
callq 0x2160
movl %r12d, %r10d
xorl %eax, %eax
movq 0x10(%rsp), %r9
cmpq 0x40(%rbp), %rax
jae 0x1d23fc
movq 0x38(%rbp), %rcx
movl (%rcx,%rax,4), %ecx
movq %rcx, %rdx
subq %r15, %rdx
setb %sil
cmpq %r14, %rcx
setae %dil
orb %sil, %dil
jne 0x1d23f2
movb $0x4e, (%rbx,%rdx)
jmp 0x1d23f7
cmpq %r14, %rcx
jae 0x1d23fc
incq %rax
jmp 0x1d23c9
testl %r10d, %r10d
je 0x1d242e
xorl %eax, %eax
testq %r9, %r9
movl $0x0, %ecx
cmovgq %r9, %rcx
leaq 0x196ea(%rip), %rdx     # 0x1ebb00
cmpq %rax, %rcx
je 0x1d242e
movzbl (%rbx,%rax), %esi
cmpb $0x4, (%rdx,%rsi)
jb 0x1d2429
movb $0x41, (%rbx,%rax)
incq %rax
jmp 0x1d2416
cmpl $0x0, 0x4(%rsp)
je 0x1d249d
movq %r9, %rax
sarq %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
notq %r15
addq %rbx, %r14
addq %r15, %r14
leaq 0x811dc(%rip), %rsi     # 0x253630
leaq 0x812da(%rip), %rdx     # 0x253735
cmpq %rcx, %rax
je 0x1d2489
movzbl (%r14), %edi
movzbl (%rbx,%rcx), %r8d
movzbl (%rsi,%r8), %r8d
movb (%rdx,%r8), %r8b
movb %r8b, (%r14)
movzbl (%rsi,%rdi), %edi
movb (%rdx,%rdi), %dil
movb %dil, (%rbx,%rcx)
incq %rcx
decq %r14
jmp 0x1d245b
testb $0x1, %r9b
je 0x1d249d
movzbl (%rbx,%rax), %ecx
movzbl (%rsi,%rcx), %ecx
movb (%rdx,%rcx), %cl
movb %cl, (%rbx,%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq