// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
subq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
andq $0x3, %rax
movq %rax, -0x48(%rbp)
movl $0x4, %eax
subq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x365621
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
subq -0x50(%rbp), %rax
andq $0x3, %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
subq -0x50(%rbp), %rax
subq -0x58(%rbp), %rax
sarq $0x2, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
cmpq $0x0, -0x50(%rbp)
jle 0x3656b3
movq -0x10(%rbp), %rdi
addq -0x70(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
sarq $0x2, %rcx
movzbl (%rax,%rcx), %eax
leaq 0x5f449(%rip), %rsi     # 0x3c4ad0
shlq $0x2, %rax
addq %rax, %rsi
addq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x2180
movq -0x50(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x365716
movq -0x10(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0x68(%rbp), %rsi
sarq $0x2, %rsi
movzbl (%rdx,%rsi), %edx
movl %edx, %esi
leaq 0x5f3e6(%rip), %rdx     # 0x3c4ad0
movl (%rdx,%rsi,4), %edx
movl %edx, (%rax,%rcx)
movq -0x70(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x3656bb
cmpq $0x0, -0x58(%rbp)
jle 0x365768
movq -0x10(%rbp), %rdi
addq -0x70(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
sarq $0x2, %rcx
movzbl (%rax,%rcx), %eax
leaq 0x5f390(%rip), %rsi     # 0x3c4ad0
shlq $0x2, %rax
addq %rax, %rsi
movq -0x58(%rbp), %rdx
callq 0x2180
movq -0x58(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jae 0x365813
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpq -0x88(%rbp), %rax
jb 0x3657e4
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpq -0x90(%rbp), %rax
jae 0x3657e4
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq -0x80(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subq -0x88(%rbp), %rcx
movb $0x4e, (%rax,%rcx)
jmp 0x365800
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpq -0x90(%rbp), %rax
jb 0x3657fe
jmp 0x365813
jmp 0x365800
jmp 0x365802
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x365786
cmpl $0x0, 0x10(%rbp)
je 0x365867
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x365865
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x24fa0(%rip), %rax     # 0x38a7e0
movzbl (%rax,%rcx), %eax
cmpl $0x4, %eax
jl 0x365855
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movb $0x41, (%rax,%rcx)
jmp 0x365857
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x365821
jmp 0x365867
cmpl $0x0, -0x18(%rbp)
je 0x36595f
movq -0x38(%rbp), %rax
sarq %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x36591f
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
subq -0x78(%rbp), %rcx
subq $0x1, %rcx
movb (%rax,%rcx), %al
movb %al, -0x91(%rbp)
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x5ed90(%rip), %rax     # 0x3c4650
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x5ee88(%rip), %rax     # 0x3c4755
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
subq -0x78(%rbp), %rcx
subq $0x1, %rcx
movb %dl, (%rax,%rcx)
movzbl -0x91(%rbp), %eax
movl %eax, %ecx
leaq 0x5ed5d(%rip), %rax     # 0x3c4650
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x5ee55(%rip), %rax     # 0x3c4755
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x365884
movq -0x38(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x36595d
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x5ed0e(%rip), %rax     # 0x3c4650
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x5ee06(%rip), %rax     # 0x3c4755
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x36595f
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)