// File: /workspace/github_c_makefile_star2_O3/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), %rax
movb 0x4(%rdi), %cl
movq %rdx, %r14
shrq %cl, %r14
movzbl 0x1(%rdi), %r11d
imulq %r11, %r14
movq %rdi, -0x8(%rsp)
movq 0x18(%rdi), %rcx
movq (%rax,%r14,8), %r10
movq %r10, %rbx
shrq $0x17, %rbx
leaq (%rcx,%rbx,8), %rcx
movq %rcx, 0x28(%rsi)
andl $0x7fffff, %r10d        # imm = 0x7FFFFF
shll $0x3, %r10d
addq (%rcx), %r10
movq %r10, 0x10(%rsi)
movq $0x0, (%r8)
cmpq $0x2, %r11
jb 0x9d4e
leaq (%rax,%r14,8), %rdi
addq $0x8, %rdi
decq %r11
xorl %ebx, %ebx
movq (%rdi,%rbx,8), %rax
movq %rax, (%r9,%rbx,8)
addq (%r8), %rax
movq %rax, (%r8)
incq %rbx
cmpq %rbx, %r11
jne 0x9d36
jmp 0x9d50
xorl %eax, %eax
movabsq $0x3fffffffffffffff, %r11 # imm = 0x3FFFFFFFFFFFFFFF
movq -0x8(%rsp), %rdi
movslq 0x8(%rdi), %rbx
leaq 0x1(%r11), %r15
movapd 0xda11(%rip), %xmm0     # 0x17780
pxor %xmm1, %xmm1
movq %r10, %r13
leaq (%r10,%rbx,8), %rbp
movq %rbp, %rdi
subq (%rcx), %rdi
cmpq $0x4000000, %rdi        # imm = 0x4000000
jne 0x9d9a
leaq 0x8(%rcx), %rdi
movq %rdi, 0x28(%rsi)
movq 0x8(%rcx), %r10
movq %rdi, %rcx
jmp 0x9d9d
movq %rbp, %r10
movq (%r10), %rdi
movq %rdi, %r14
shrq $0x3e, %r14
cmpq $0x1, %r14
movl $0xffff, %r12d          # imm = 0xFFFF
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmoveq %r11, %r12
cmpq $0x2, %r14
movabsq $0x3fffffffffffffff, %r11 # imm = 0x3FFFFFFFFFFFFFFF
cmoveq %r11, %r12
andq %rdi, %r12
addq %r12, %rax
cmpq %rdx, %rax
ja 0x9e95
cmpq %r15, %rdi
jae 0x9e3c
movq %rdi, %xmm2
pshufd $0x44, %xmm2, %xmm3     # xmm3 = xmm2[0,1,0,1]
psrlq $0x10, %xmm2
psrlq $0x20, %xmm3
movsd %xmm2, %xmm3            # xmm3 = xmm2[0],xmm3[1]
andpd %xmm0, %xmm3
shrq $0x30, %rdi
movdqu (%r9), %xmm2
paddq %xmm3, %xmm2
movdqu 0x18(%r9), %xmm3
movdqu %xmm2, (%r9)
addq %rdi, 0x10(%r9)
movd 0x8(%r10), %xmm2
punpcklwd %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3]
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddq %xmm3, %xmm2
movdqu %xmm2, 0x18(%r9)
movzwl 0xc(%r10), %eax
addq %rax, 0x28(%r9)
jmp 0x9e83
movq -0x8(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x1, %r14d
jne 0x9e6b
testq %rax, %rax
je 0x9e83
xorl %edi, %edi
movl 0x4(%r10,%rdi,4), %r14d
andl $0x3fffffff, %r14d      # imm = 0x3FFFFFFF
addq %r14, (%r9,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x9e51
jmp 0x9e83
testq %rax, %rax
je 0x9e83
xorl %edi, %edi
movq 0x8(%r10,%rdi,8), %r14
addq %r14, (%r9,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x9e72
addq (%r8), %r12
movq %r12, (%r8)
movq %r10, 0x10(%rsi)
movq %r12, %rax
jmp 0x9d73
movq %r13, 0x18(%rsi)
movq %rbp, %rdx
subq (%rcx), %rdx
xorl %ecx, %ecx
cmpq $0x4000000, %rdx        # imm = 0x4000000
setne %cl
shll $0x3, %ecx
orq $-0x10, %rcx
addq %rbp, %rcx
movq %rcx, 0x20(%rsi)
movq (%r13), %rcx
shrq $0x3e, %rcx
movq -0x8(%rsp), %rdx
movsbq 0x5(%rdx,%rcx), %rcx
leaq (,%rcx,8), %rcx
addq %r13, %rcx
movq %rcx, 0x10(%rsi)
movq %rcx, 0x30(%rsi)
movl $0x40, (%rsi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq