// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrive_bqual(asg8_v*, unsigned char*, unsigned long, long, long, unsigned char, long)
int64_t retrive_bqual(asg8_v *dv, uint8_t *ds, uint64_t id, int64_t s, int64_t e, uint8_t rev, int64_t bitn)
{
	int64_t rl = Get_READ_LENGTH(R_INF, id), l; 
	if(s < 0) s = 0; if(e < 0) e = rl;
	if(s >= e || e > rl) return -1;

	uint8_t *da = NULL, *src = Get_QUAL(R_INF, id), mm = (((uint8_t)1)<<bitn)-1, mlf = 8 - bitn, mrf;
	int64_t bitr = 8/bitn, dk, sk, swk;
	l = e - s;
	if(dv) {
		kv_resize(uint8_t, *dv, ((uint64_t)l)); da = dv->a;
	} else {
		da = ds;
	}

	if(!rev) {
		dk = 0; sk = s;

		mrf = ((s%bitr)*bitn);
		// if(s == 21519 && e == 22332) {
		// 	fprintf(stderr, "+[M::%s] id::%lu, in::[%ld, %ld), rev::%u, bitn::%ld, bitr::%ld, mrf::%u\n", __func__, id, s, e, rev, bitn, bitr, mrf);
		// }
		if(mrf) {
			for (swk = sk/bitr; mrf < 8 && sk < e; mrf += bitn, sk++) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		for (swk = sk/bitr; (sk + bitr) <= e; sk += bitr, swk++) {
			for (mrf = 0; mrf < 8; mrf += bitn) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}

		if(sk < e) {
			for (mrf = 0; sk < e; mrf += bitn, sk++) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		// if(dk != l) {
		// 	fprintf(stderr, "+[M::%s] id::%lu, in::[%ld, %ld), rev::%u, bitn::%ld, bitr::%ld\n", __func__, id, s, e, rev, bitn, bitr);
		// }
		assert(dk == l);
	} else {
		sk = s; s = e; e = sk;
		s = rl - s; e = rl - e;
		dk = l; sk = s; 

		mrf = ((s%bitr)*bitn);
		if(mrf) {
			for (swk = sk/bitr; mrf < 8 && sk < e; mrf += bitn, sk++) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		for (swk = sk/bitr; (sk + bitr) <= e; sk += bitr, swk++) {
			for (mrf = 0; mrf < 8; mrf += bitn) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}

		if(sk < e) {
			for (mrf = 0; sk < e; mrf += bitn, sk++) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		assert(dk == 0);
	}
	dv->n = l;

	return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x8(%rsp)
leaq 0x2628e3(%rip), %rdi    # 0x296230
movq 0x18(%rdi), %rax
xorl %ebx, %ebx
testq %rcx, %rcx
cmovgq %rcx, %rbx
movq (%rax,%rdx,8), %r13
testq %r8, %r8
cmovsq %r13, %r8
movq %r8, %r10
subq %rbx, %r10
setle %al
movq %r13, 0x30(%rsp)
movq %r8, 0x18(%rsp)
subq %r8, %r13
setl %cl
orb %al, %cl
movq $-0x1, %rax
jne 0x33bef
movl %r9d, 0x14(%rsp)
movq 0x70(%rsp), %r14
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r14d, %ecx
shll %cl, %r15d
movq 0x30(%rdi), %rax
movq (%rax,%rdx,8), %r12
movb $0x8, %bpl
movl $0x8, %eax
xorl %edx, %edx
idivq %r14
movq %rax, %r11
cmpq $0x0, 0x8(%rsp)
je 0x33a47
movq 0x8(%rsp), %rax
cmpq %r10, 0x8(%rax)
jae 0x33a3e
leaq -0x1(%r10), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq 0x8(%rsp), %rax
movq %rsi, 0x8(%rax)
movq 0x10(%rax), %rdi
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
callq 0x2370
movq 0x20(%rsp), %r11
movq 0x28(%rsp), %r10
movq %rax, %rsi
movq 0x8(%rsp), %rax
movq %rsi, 0x10(%rax)
jmp 0x33a47
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
notb %r15b
subb %r14b, %bpl
cmpb $0x0, 0x14(%rsp)
je 0x33b32
movq %r13, %rax
cqto
idivq %r11
movq 0x30(%rsp), %rcx
subq %rbx, %rcx
movq %rcx, %rbx
movl %r14d, %edi
imull %edx, %edi
leal -0x1(%rdi), %ecx
movq %r10, %r8
cmpb $0x6, %cl
ja 0x33aa5
movzbl (%r12,%rax), %edx
movl %edi, %ecx
shll %cl, %edx
movl %ebp, %ecx
shrl %cl, %edx
andb %r15b, %dl
movb %dl, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dil
incq %r13
cmpb $0x7, %dil
ja 0x33aa5
cmpq %rbx, %r13
jl 0x33a7c
movq %r13, %rax
cqto
idivq %r11
leaq (%r11,%r13), %rcx
cmpq %rbx, %rcx
jg 0x33ae0
movq %rcx, %r13
xorl %edx, %edx
movzbl (%r12,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
shrl %cl, %edi
andb %r15b, %dil
movb %dil, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dl
cmpb $0x8, %dl
jb 0x33abb
incq %rax
jmp 0x33aad
cmpq %rbx, %r13
jge 0x33b0a
xorl %edx, %edx
movzbl (%r12,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
sarl %cl, %edi
andb %r15b, %dil
movb %dil, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dl
incq %r13
cmpq %rbx, %r13
jl 0x33ae7
testq %r8, %r8
je 0x33be4
leaq 0x1fcc43(%rip), %rdi    # 0x23075d
leaq 0x1fcbd2(%rip), %rsi    # 0x2306f3
leaq 0x1fcbdc(%rip), %rcx    # 0x230704
movl $0x3ca, %edx            # imm = 0x3CA
callq 0x2110
movq %rbx, %rax
cqto
idivq %r11
movl %r14d, %edi
imull %edx, %edi
leal -0x1(%rdi), %ecx
xorl %r8d, %r8d
cmpb $0x6, %cl
ja 0x33b7a
movq 0x18(%rsp), %r13
movzbl (%r12,%rax), %edx
movl %edi, %ecx
shll %cl, %edx
movl %ebp, %ecx
shrl %cl, %edx
andb %r15b, %dl
movb %dl, (%rsi,%r8)
incq %r8
addb %r14b, %dil
incq %rbx
cmpb $0x7, %dil
ja 0x33b7f
cmpq %r13, %rbx
jl 0x33b50
jmp 0x33b7f
movq 0x18(%rsp), %r13
movq %rbx, %rax
cqto
idivq %r11
leaq (%rbx,%r11), %rcx
cmpq %r13, %rcx
jg 0x33bb9
movq %rcx, %rbx
xorl %edx, %edx
movzbl (%r12,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
shrl %cl, %edi
andb %r15b, %dil
movb %dil, (%rsi,%r8)
incq %r8
addb %r14b, %dl
cmpb $0x8, %dl
jb 0x33b95
incq %rax
jmp 0x33b87
subq %rbx, %r13
jle 0x33bdf
xorl %edx, %edx
movzbl (%r12,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
sarl %cl, %edi
andb %r15b, %dil
movb %dil, (%rsi,%r8)
incq %r8
addb %r14b, %dl
decq %r13
jne 0x33bc0
cmpq %r10, %r8
jne 0x33bfe
movq 0x8(%rsp), %rax
movq %r10, (%rax)
movq %r10, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fcae6(%rip), %rdi    # 0x2306eb
leaq 0x1fcae7(%rip), %rsi    # 0x2306f3
leaq 0x1fcaf1(%rip), %rcx    # 0x230704
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x2110