// File: /workspace/github_c_makefile_star2_O2/lh3[P]bgt/pbwt.c
// Function: pbs_dec
void pbs_dec(int m, int r, pbs_dat_t *d, const uint8_t *u, uint8_t *a) // IMPORTANT: d MUST BE sorted by d[i].r
{
	const uint8_t *q;
	int n1;
	for (q = u, n1 = 0; *q; ++q) // count the number of 1 bits
		if (*q&1) n1 += pbr_tbl[*q>>1];
	if (n1 == 0) { // all zero
		memset(a, 0, r);
	} else if (n1 == m) { // all one
		memset(a, 1, r);
	} else {
		pbs_dat_t *p = d, *end = d + r, *d1, *x[2];
		int c[2], acc[2];
		acc[0] = 0, acc[1] = m - n1; // accumulative counts
		c[0] = c[1] = 0; // running marginal counts
		d1 = (pbs_dat_t*)malloc(r * sizeof(pbs_dat_t));
		x[0] = d, x[1] = d1;
		memset(a, 0, r);
		for (q = u; p != end && *q; ++q) {
			int l = pbr_tbl[*q>>1], b = *q&1, s = c[0] + c[1];
			if (s <= p->r && p->r < s + l) {
				int w = acc[b] + c[b] - s;
				pbs_dat_t *p0 = p;
				do {
					p->r += w; // this is a faster version of: "p->r = acc[b] + c[b] + (p->r - s);"
					++p;
				} while (p != end && s <= p->r && p->r < s + l);
				if (b == 0) {
					if (x[0] != p0) memmove(x[0], p0, (p - p0) * sizeof(pbs_dat_t));
					x[0] += p - p0;
				} else {
					memcpy(x[1], p0, (p - p0) * sizeof(pbs_dat_t));
					x[1] += p - p0;
					while (p0 < p) a[p0->i] = 1, ++p0;
				}
			}
			c[b] += l;
		}
		memcpy(x[0], d1, (x[1] - d1) * sizeof(pbs_dat_t));
		free(d1);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
xorl %eax, %eax
leaq 0x8e3d(%rip), %r10      # 0x19590
movq %rcx, %r9
movzbl (%r9), %r8d
testl %r8d, %r8d
je 0x10772
testb $0x1, %r8b
je 0x1076d
andl $-0x2, %r8d
addl (%r10,%r8,2), %eax
incq %r9
jmp 0x10756
testl %eax, %eax
je 0x10788
movslq %esi, %r12
subl %eax, %edi
jne 0x107a3
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r12, %rdx
jmp 0x10790
movslq %esi, %rdx
movq %rbx, %rdi
xorl %esi, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
leaq (%rdx,%r12,8), %r13
andl $0x0, 0x20(%rsp)
movl %edi, 0x24(%rsp)
andq $0x0, (%rsp)
leaq (,%r12,8), %rdi
movq %rdx, %r14
movq %rcx, %r15
callq 0x22e0
movq %rax, %rbp
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x21e0
leaq 0x8db1(%rip), %rsi      # 0x19590
movq %r15, %r9
movq %r14, %rdi
movq %rbp, 0x28(%rsp)
movq %rbp, %r10
movq %r14, %r12
cmpq %r13, %r12
je 0x10918
movzbl (%r9), %r14d
testq %r14, %r14
je 0x10918
movl %r14d, %eax
andl $-0x2, %eax
movl (%rsi,%rax,2), %r15d
andl $0x1, %r14d
movl 0x4(%rsp), %eax
addl (%rsp), %eax
movl (%r12), %ecx
cmpl %ecx, %eax
ja 0x10861
leal (%rax,%r15), %edx
cmpl %edx, %ecx
jae 0x10861
movl %r14d, %r8d
shll $0x2, %r8d
movl 0x20(%rsp,%r8), %esi
subl %eax, %esi
addl (%rsp,%r8), %esi
leaq 0x8(%r12), %rbp
addl %esi, %ecx
movl %ecx, -0x8(%rbp)
cmpq %r13, %rbp
je 0x10876
movl (%rbp), %ecx
addq $0x8, %rbp
cmpl %ecx, %eax
ja 0x1085b
cmpl %edx, %ecx
jb 0x10842
addq $-0x8, %rbp
jmp 0x10879
movq %r12, %rbp
movl %r14d, %eax
addl %r15d, (%rsp,%rax,4)
incq %r9
movq %rbp, %r12
jmp 0x107f0
movq %r13, %rbp
movq %r9, 0x18(%rsp)
testl %r14d, %r14d
je 0x108d4
movq %rdi, 0x10(%rsp)
movq %rbp, %rdx
subq %r12, %rdx
movq %r10, 0x8(%rsp)
movq %r10, %rdi
movq %r12, %rsi
movq %rdx, 0x30(%rsp)
callq 0x2280
movq 0x18(%rsp), %r9
leaq 0x8ce1(%rip), %rsi      # 0x19590
cmpq %rbp, %r12
jae 0x108c3
movl 0x4(%r12), %eax
movb $0x1, (%rbx,%rax)
addq $0x8, %r12
jmp 0x108af
movq 0x8(%rsp), %r10
addq 0x30(%rsp), %r10
movq 0x10(%rsp), %rdi
jmp 0x10864
movq %rbp, %rax
subq %r12, %rax
cmpq %r12, %rdi
je 0x10904
movq %r12, %rsi
movq %rax, %r12
movq %rax, %rdx
movq %rdi, 0x10(%rsp)
movq %r10, 0x8(%rsp)
callq 0x2370
movq %r12, %rax
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rdi
addq %rax, %rdi
movq 0x18(%rsp), %r9
leaq 0x8c7d(%rip), %rsi      # 0x19590
jmp 0x10864
movq 0x28(%rsp), %rbx
subq %rbx, %r10
movq %rbx, %rsi
movq %r10, %rdx
callq 0x2280
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2050