// File: /workspace/github_cpp_makefile_star2_O1/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 0x215f5a
movq 0x10(%r13), %rcx
movq %r8, %rdx
sarq $0x2, %rdx
movzbl (%rcx,%rdx), %esi
shlq $0x2, %rsi
addq 0x7408c(%rip), %rsi     # 0x289fc8
addq %rax, %rsi
movq %rbx, %rdi
movq %r12, %rdx
movq %r8, 0x8(%rsp)
callq 0x2160
movq 0x8(%rsp), %r8
leaq (%r12,%r8), %rax
jmp 0x215f60
xorl %r12d, %r12d
movq %r8, %rax
testq %rbp, %rbp
jle 0x215f8f
leaq 0x74ac4(%rip), %rcx     # 0x28aa30
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 0x215f6c
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x215fc0
addq %rbx, %r12
movq 0x10(%r13), %rcx
sarq $0x2, %rax
movzbl (%rcx,%rax), %esi
shll $0x2, %esi
addq 0x74016(%rip), %rsi     # 0x289fc8
movq %r12, %rdi
movq %r8, %r12
callq 0x2160
movq %r12, %r8
movl 0x50(%rsp), %eax
cmpq $0x0, 0x40(%r13)
je 0x215ffe
xorl %ecx, %ecx
movq 0x38(%r13), %rdx
movl (%rdx,%rcx,4), %edx
movq %rdx, %rsi
subq %r8, %rsi
setb %dil
cmpq %r14, %rdx
setae %r9b
orb %dil, %r9b
jne 0x215ff0
movb $0x4e, (%rbx,%rsi)
jmp 0x215ff5
cmpq %r14, %rdx
jae 0x215ffe
incq %rcx
cmpq 0x40(%r13), %rcx
jb 0x215fcd
testl %eax, %eax
sete %al
testq %r15, %r15
setle %cl
orb %al, %cl
jne 0x21602c
xorl %eax, %eax
leaq 0x179fa(%rip), %rcx     # 0x22da10
movzbl (%rbx,%rax), %edx
cmpb $0x4, (%rcx,%rdx)
jb 0x216024
movb $0x41, (%rbx,%rax)
incq %rax
cmpq %rax, %r15
jne 0x216016
cmpl $0x0, 0x4(%rsp)
je 0x2160b7
movq %r15, %rax
sarq %rax
testq %rax, %rax
jle 0x216093
movq %rax, %rcx
negq %rcx
notq %r8
addq %rbx, %r14
addq %r8, %r14
xorl %edx, %edx
leaq 0x745d6(%rip), %rsi     # 0x28a630
leaq 0x746d4(%rip), %rdi     # 0x28a735
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 0x216064
jmp 0x216095
xorl %eax, %eax
testb $0x1, %r15b
je 0x2160b7
movzbl (%rbx,%rax), %ecx
leaq 0x7458a(%rip), %rdx     # 0x28a630
movzbl (%rdx,%rcx), %ecx
leaq 0x74684(%rip), %rdx     # 0x28a735
movb (%rdx,%rcx), %cl
movb %cl, (%rbx,%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq