// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %r8, %r13
leaq 0x24cbe0(%rip), %rax    # 0x254230
movq 0x18(%rax), %r8
xorl %r11d, %r11d
testq %rcx, %rcx
cmovgq %rcx, %r11
movq (%r8,%rdx,8), %r14
testq %r13, %r13
cmovsq %r14, %r13
movq %r13, %rcx
subq %r11, %rcx
movq %rcx, (%rsp)
setle %cl
movq %r14, %rbp
subq %r13, %rbp
setl %r8b
orb %cl, %r8b
je 0x7691
pushq $-0x1
popq %rax
movq %rax, (%rsp)
jmp 0x7932
movl %r9d, 0x1c(%rsp)
movq 0x60(%rsp), %r15
movq 0x30(%rax), %rax
pushq $-0x1
popq %rbx
movl %r15d, %ecx
shll %cl, %ebx
movq (%rax,%rdx,8), %r12
movb $0x8, %r10b
pushq $0x8
popq %rax
xorl %edx, %edx
idivq %r15
movq %rax, 0x8(%rsp)
testq %rdi, %rdi
je 0x773b
movq (%rsp), %rax
cmpq %rax, 0x8(%rdi)
jae 0x7737
movq (%rsp), %rax
decq %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 %rsi, 0x8(%rdi)
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r11, 0x20(%rsp)
callq 0x2370
movq 0x20(%rsp), %r11
movb $0x8, %r10b
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rax, 0x10(%rdi)
jmp 0x773b
movq 0x10(%rdi), %rsi
notb %bl
subb %r15b, %r10b
cmpb $0x0, 0x1c(%rsp)
je 0x77a3
movq %rdi, %r13
movq %rbp, %rax
cqto
idivq 0x8(%rsp)
subq %r11, %r14
movl %r15d, %r8d
imull %edx, %r8d
testb %r8b, %r8b
je 0x77ff
movzbl %r10b, %edi
movq (%rsp), %r9
cmpb $0x7, %r8b
ja 0x7807
cmpq %r14, %rbp
jge 0x7807
movzbl (%r12,%rax), %edx
movl %r8d, %ecx
shll %cl, %edx
movl %r10d, %ecx
shrl %cl, %edx
andb %bl, %dl
movb %dl, -0x1(%rsi,%r9)
decq %r9
addb %r15b, %r8b
incq %rbp
jmp 0x776f
movq %rdi, 0x10(%rsp)
movq %r11, %rax
cqto
idivq 0x8(%rsp)
movl %r15d, %r9d
imull %edx, %r9d
testb %r9b, %r9b
je 0x78a3
movl %r10d, %ebp
movzbl %r10b, %edi
xorl %r10d, %r10d
leaq (%r11,%r10), %r8
cmpb $0x7, %r9b
ja 0x78ad
cmpq %r13, %r8
jge 0x78ad
movzbl (%r12,%rax), %edx
movl %r9d, %ecx
shll %cl, %edx
movl %ebp, %ecx
shrl %cl, %edx
andb %bl, %dl
movb %dl, (%rsi,%r10)
incq %r10
addb %r15b, %r9b
jmp 0x77cc
movzbl %r10b, %edi
movq (%rsp), %r9
movq %rbp, %rax
cqto
idivq 0x8(%rsp)
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp), %r8
cmpq %r14, %r8
jg 0x784d
xorl %edx, %edx
cmpb $0x7, %dl
ja 0x7845
movzbl (%r12,%rax), %r10d
movl %edx, %ecx
shll %cl, %r10d
movl %edi, %ecx
shrl %cl, %r10d
andb %bl, %r10b
movb %r10b, -0x1(%rsi,%r9)
decq %r9
addb %r15b, %dl
jmp 0x7821
incq %rax
movq %r8, %rbp
jmp 0x7811
cmpq %r14, %rbp
jge 0x787b
xorl %edx, %edx
cmpq %r14, %rbp
jge 0x787b
movzbl (%r12,%rax), %r8d
movl %edx, %ecx
shll %cl, %r8d
movl %edi, %ecx
sarl %cl, %r8d
andb %bl, %r8b
movb %r8b, -0x1(%rsi,%r9)
decq %r9
addb %r15b, %dl
incq %rbp
jmp 0x7854
testq %r9, %r9
je 0x792a
leaq 0x1daed2(%rip), %rdi    # 0x1e275d
leaq 0x1dae61(%rip), %rsi    # 0x1e26f3
leaq 0x1dae6b(%rip), %rcx    # 0x1e2704
movl $0x3ca, %edx            # imm = 0x3CA
callq 0x2110
movzbl %r10b, %edi
xorl %r10d, %r10d
movq %r11, %r8
movq %r8, %rax
cqto
idivq 0x8(%rsp)
movq 0x8(%rsp), %rcx
leaq (%r8,%rcx), %r9
cmpq %r13, %r9
jg 0x78f2
xorl %edx, %edx
cmpb $0x7, %dl
ja 0x78ea
movzbl (%r12,%rax), %r8d
movl %edx, %ecx
shll %cl, %r8d
movl %edi, %ecx
shrl %cl, %r8d
andb %bl, %r8b
movb %r8b, (%rsi,%r10)
incq %r10
addb %r15b, %dl
jmp 0x78c7
incq %rax
movq %r9, %r8
jmp 0x78b7
cmpq %r13, %r8
jge 0x791f
xorl %edx, %edx
cmpq %r13, %r8
jge 0x791f
movzbl (%r12,%rax), %r9d
movl %edx, %ecx
shll %cl, %r9d
movl %edi, %ecx
sarl %cl, %r9d
andb %bl, %r9b
movb %r9b, (%rsi,%r10)
incq %r10
addb %r15b, %dl
incq %r8
jmp 0x78f9
cmpq (%rsp), %r10
movq 0x10(%rsp), %r13
jne 0x7945
movq (%rsp), %rax
movq %rax, (%r13)
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1dad9f(%rip), %rdi    # 0x1e26eb
leaq 0x1dada0(%rip), %rsi    # 0x1e26f3
leaq 0x1dadaa(%rip), %rcx    # 0x1e2704
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x2110