// File: /workspace/github_c_makefile_star2_O3/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 %rdx, %rbp
movb (%rcx), %dl
testb %dl, %dl
je 0x154c2
movq %rcx, %r14
incq %rcx
xorl %eax, %eax
leaq 0xa14e(%rip), %r15      # 0x1f5e0
testb $0x1, %dl
je 0x154a1
movzbl %dl, %edx
andl $-0x2, %edx
addl (%r15,%rdx,2), %eax
movb (%rcx), %dl
incq %rcx
testb %dl, %dl
jne 0x15492
testl %eax, %eax
je 0x154c2
movslq %esi, %r13
subl %eax, %edi
jne 0x154dd
movq %r8, %rdi
movl $0x1, %esi
movq %r13, %rdx
jmp 0x154ca
movslq %esi, %rdx
movq %r8, %rdi
xorl %esi, %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
movl $0x0, 0x20(%rsp)
movl %edi, 0x24(%rsp)
movq $0x0, 0x8(%rsp)
leaq (,%r13,8), %rdi
movq %r8, %rbx
callq 0x22e0
movq %rax, %r12
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21e0
testl %r13d, %r13d
movq %r12, 0x30(%rsp)
je 0x15674
leaq (,%r13,8), %r13
addq %rbp, %r13
movq %r12, %r8
movq %rbp, %rdi
movzbl (%r14), %r12d
testq %r12, %r12
je 0x1567a
movl %r12d, %eax
andl $-0x2, %eax
movl (%r15,%rax,2), %esi
andl $0x1, %r12d
movl 0xc(%rsp), %eax
addl 0x8(%rsp), %eax
movl (%rbp), %ecx
cmpl %ecx, %eax
ja 0x155a5
leal (%rax,%rsi), %edx
cmpl %edx, %ecx
jae 0x155a5
movq %rsi, 0x18(%rsp)
movl %r12d, %r9d
shll $0x2, %r9d
movl 0x20(%rsp,%r9), %esi
subl %eax, %esi
addl 0x8(%rsp,%r9), %esi
leaq 0x8(%rbp), %rbx
movq %rbx, %r15
addl %esi, %ecx
movl %ecx, -0x8(%rbx)
cmpq %r13, %rbx
je 0x155c3
movl (%r15), %ecx
leaq 0x8(%r15), %rbx
cmpl %ecx, %eax
ja 0x1559f
cmpl %edx, %ecx
jb 0x15583
addq $-0x8, %rbx
jmp 0x155c6
movq %rbp, %rbx
movl %r12d, %eax
addl %esi, 0x8(%rsp,%rax,4)
incq %r14
movq %rbx, %rbp
cmpq %r13, %rbx
jne 0x15536
jmp 0x1567a
movq %r13, %rbx
testl %r12d, %r12d
je 0x15625
movq %rdi, (%rsp)
movq %rbx, %rdx
subq %rbp, %rdx
movq %r8, %rdi
movq %rbp, %rsi
movq %r8, 0x10(%rsp)
callq 0x2280
movq 0x10(%rsp), %r8
movq %rbx, %rax
subq %rbp, %rax
leaq (%r8,%rax), %r8
jbe 0x1565f
addq $-0x8, %r15
addq $-0x8, %rbp
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rsi
movl 0xc(%rbp), %eax
movb $0x1, (%rcx,%rax)
addq $0x8, %rbp
cmpq %r15, %rbp
jb 0x15608
leaq 0x9fc1(%rip), %r15      # 0x1f5e0
movq (%rsp), %rdi
jmp 0x155a8
movq %rbx, %rax
subq %rbp, %rax
cmpq %rbp, %rdi
leaq 0x9fab(%rip), %r15      # 0x1f5e0
je 0x1565a
movq %rbp, %rsi
movq %rax, %rbp
movq %rax, %rdx
movq %r8, 0x10(%rsp)
movq %rdi, (%rsp)
callq 0x2360
movq %rbp, %rax
movq (%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rax, %rdi
jmp 0x1566a
leaq 0x9f7a(%rip), %r15      # 0x1f5e0
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
jmp 0x155a8
movq %rbp, %rdi
movq %r12, %r8
movq 0x30(%rsp), %rbx
subq %rbx, %r8
movq %rbx, %rsi
movq %r8, %rdx
callq 0x2280
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2050