// File: /workspace/github_c_makefile_star2_O1/lh3[P]fermi-lite/rld0.c
// Function: rld_locate_blk
static inline uint64_t rld_locate_blk(const rld_t *e, rlditr_t *itr, uint64_t k, uint64_t *cnt, uint64_t *sum)
{
	int j;
	uint64_t c = 0, *q, *z = e->frame + (k>>e->ibits) * e->asize1;
	itr->i = e->z + (*z>>RLD_LBITS);
	q = itr->p = *itr->i + (*z&RLD_LMASK);
	for (j = 1, *sum = 0; j < e->asize1; ++j) *sum += (cnt[j-1] = z[j]);
	while (1) { // seek to the small block
		int type;
		q += e->ssize;
		if (q - *itr->i == RLD_LSIZE) q = *++itr->i;
		type = rld_block_type(*q);
		c = type == 2? *q&0x3fffffffffffffffULL : type == 1? *(uint32_t*)q : *(uint16_t*)q;
		if (*sum + c > k) break;
		if (type == 0) {
			uint16_t *p = (uint16_t*)q + 1;
#ifdef _DNA_ONLY
			cnt[0] += p[0]; cnt[1] += p[1]; cnt[2] += p[2]; cnt[3] += p[3]; cnt[4] += p[4]; cnt[5] += p[5];
#else
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j];
#endif
		} else if (type == 1) {
			uint32_t *p = (uint32_t*)q + 1;
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j] & 0x3fffffff;
		} else {
			uint64_t *p = (uint64_t*)q + 1;
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j];
		}
		*sum += c;
		itr->p = q;
	}
	itr->shead = itr->p;
	itr->stail = rld_get_stail(e, itr);
	itr->p += e->offset0[rld_block_type(*itr->shead)];
	itr->q = (uint8_t*)itr->p;
	itr->r = 64;
	return c + *sum;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r9
movq 0x38(%rdi), %r10
movb 0x4(%rdi), %cl
movq %rdx, %r11
shrq %cl, %r11
movzbl 0x1(%rdi), %eax
imulq %rax, %r11
movq 0x18(%rdi), %rbx
movq (%r10,%r11,8), %rcx
movq %rcx, %r14
shrq $0x17, %r14
leaq (%rbx,%r14,8), %rbx
movq %rbx, 0x28(%rsi)
andl $0x7fffff, %ecx         # imm = 0x7FFFFF
shll $0x3, %ecx
addq (%rbx), %rcx
movq %rcx, 0x10(%rsi)
movq $0x0, (%r8)
cmpq $0x2, %rax
jb 0x98f9
leaq (%r10,%r11,8), %r10
addq $0x8, %r10
decq %rax
xorl %r11d, %r11d
movq (%r10,%r11,8), %rbx
movq %rbx, (%r9,%r11,8)
addq %rbx, (%r8)
incq %r11
cmpq %r11, %rax
jne 0x98e6
movabsq $0x3fffffffffffffff, %r11 # imm = 0x3FFFFFFFFFFFFFFF
movslq 0x8(%rdi), %r10
movq 0x28(%rsi), %r14
leaq 0x1(%r11), %rbx
leaq (%rcx,%r10,8), %rcx
movq %rcx, %rax
subq (%r14), %rax
cmpq $0x4000000, %rax        # imm = 0x4000000
jne 0x9930
leaq 0x8(%r14), %rax
movq %rax, 0x28(%rsi)
movq 0x8(%r14), %rcx
movq %rax, %r14
movq (%rcx), %r12
movq %r12, %r13
shrq $0x3e, %r13
cmpq $0x1, %r13
je 0x994e
cmpl $0x2, %r13d
jne 0x9952
movq %r12, %rax
andq %r11, %rax
jmp 0x9957
movl (%rcx), %eax
jmp 0x9955
movzwl (%rcx), %eax
movl %eax, %eax
movq (%r8), %r15
addq %rax, %r15
cmpq %rdx, %r15
ja 0x99f6
cmpq %rbx, %r12
jae 0x99a2
movzwl 0x2(%rcx), %r12d
addq %r12, (%r9)
movzwl 0x4(%rcx), %r12d
addq %r12, 0x8(%r9)
movzwl 0x6(%rcx), %r12d
addq %r12, 0x10(%r9)
movzwl 0x8(%rcx), %r12d
addq %r12, 0x18(%r9)
movzwl 0xa(%rcx), %r12d
addq %r12, 0x20(%r9)
movzwl 0xc(%rcx), %r12d
addq %r12, 0x28(%r9)
jmp 0x99e6
movzbl (%rdi), %r12d
cmpl $0x1, %r13d
jne 0x99cd
testq %r12, %r12
je 0x99e6
xorl %r13d, %r13d
movl 0x4(%rcx,%r13,4), %ebp
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
addq %rbp, (%r9,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x99b4
jmp 0x99e6
testq %r12, %r12
je 0x99e6
xorl %r13d, %r13d
movq 0x8(%rcx,%r13,8), %rbp
addq %rbp, (%r9,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x99d5
addq %rax, (%r8)
movq %rcx, 0x10(%rsi)
cmpq %rdx, %r15
jbe 0x990f
movq 0x10(%rsi), %rcx
movq 0x28(%rsi), %rdx
movq %rcx, 0x18(%rsi)
leaq (%rcx,%r10,8), %r9
movq %r9, %r10
subq (%rdx), %r10
xorl %edx, %edx
cmpq $0x4000000, %r10        # imm = 0x4000000
setne %dl
shll $0x3, %edx
orq $-0x10, %rdx
addq %r9, %rdx
movq %rdx, 0x20(%rsi)
movq (%rcx), %rdx
shrq $0x3e, %rdx
movsbq 0x5(%rdi,%rdx), %rdx
leaq (%rcx,%rdx,8), %rcx
movq %rcx, 0x10(%rsi)
movq %rcx, 0x30(%rsi)
movl $0x40, (%rsi)
addq (%r8), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq