// File: chhylp123[P]hifiasm/rcut.cpp
// Function: kr_drand_node_ref(unsigned long, mcgg_svaux_t*, unsigned short*, unsigned int, unsigned long)
static inline mcg_node_t kr_drand_node_ref(uint64_t id, mcgg_svaux_t *b, uint16_t *hc, mcg_node_t ref, uint64_t rev)
{
	uint64_t i, k, m, mm, mn, mi, cn, hn = b->s[id].h[0];
	mcg_node_t t;
	if(hc) (*hc) = 0;
	b->x = kr_splitmix64(b->x);
	if(b->s[id].h[1] != (uint16_t)-1)
	{
		union { uint64_t i; double d; } u;
		u.i = 0x3FFULL << 52 | (b->x) >> 12;
		if((u.d - 1.0) > b->s[id].hw[0])
		{
			hn = b->s[id].h[1];
			if(hc) (*hc) = 1;
		}
	} 
	if(rev) ref ^= (mcg_node_t)-1;
	ref &= b->mask;

	t = ref; cn = 0;
	while (t)
	{	
		cn += t&1;
		t >>= 1;
	} 
	if(cn == hn) return ref;

	if(cn>=hn) mm=cn, m=1, mn=cn-hn;///1->0
	else mm=b->hapN-cn, m=0, mn=hn-cn;///0->1

	for (i = 0; i < mn; i++)
	{
		mi = (b->x%mm);
		for (k = 0; k < b->hapN; k++)
		{
			if(((ref>>k)&1)!=m) continue;
			if(mi == 0)
			{
				ref ^= ((mcg_node_t)1<<k);
				break;
			}
			mi--;
		}
		mm--;
	}
    
	return ref;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x4(%rax), %eax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x229629
movq -0x20(%rbp), %rax
movw $0x0, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x21cf60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x2296d3
movq -0x18(%rbp), %rax
movq (%rax), %rax
shrq $0xc, %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
orq %rcx, %rax
movq %rax, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movsd 0x15897a(%rip), %xmm1   # 0x382000
subsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
ucomisd 0x10(%rax), %xmm0
jbe 0x2296d1
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x10(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2296cf
movq -0x20(%rbp), %rax
movw $0x1, (%rax)
jmp 0x2296d1
jmp 0x2296d3
cmpq $0x0, -0x30(%rbp)
je 0x2296e3
movl -0x24(%rbp), %eax
xorl $-0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x68(%rax), %eax
andl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x74(%rbp)
movq $0x0, -0x68(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x22971e
movl -0x74(%rbp), %eax
andl $0x1, %eax
movl %eax, %eax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x74(%rbp), %eax
shrl %eax
movl %eax, -0x74(%rbp)
jmp 0x2296fe
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x229733
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x22982e
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jb 0x22975b
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x1, -0x48(%rbp)
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x22977e
movq -0x18(%rbp), %rax
movl 0x6c(%rax), %eax
subq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x70(%rbp), %rax
subq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x229828
movq -0x18(%rbp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x50(%rbp)
movq %rdx, -0x60(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x6c(%rcx), %ecx
cmpq %rcx, %rax
jae 0x22980b
movl -0x24(%rbp), %eax
movq -0x40(%rbp), %rcx
shrl %cl, %eax
andl $0x1, %eax
movl %eax, %eax
cmpq -0x48(%rbp), %rax
je 0x2297d5
jmp 0x2297fd
cmpq $0x0, -0x60(%rbp)
jne 0x2297f1
movq -0x40(%rbp), %rax
movl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
xorl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x22980b
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2297af
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x229786
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)