// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r14
movq %rdx, %r15
movb (%rcx), %r9b
testb %r9b, %r9b
je 0x14fe1
leaq 0x1(%r14), %rcx
xorl %eax, %eax
leaq 0xa5fe(%rip), %rdx      # 0x1f5c0
testb $0x1, %r9b
je 0x14fd4
movzbl %r9b, %r9d
andl $-0x2, %r9d
addl (%rdx,%r9,2), %eax
movb (%rcx), %r9b
incq %rcx
testb %r9b, %r9b
jne 0x14fc2
jmp 0x14fe3
xorl %eax, %eax
testl %eax, %eax
je 0x14ffb
movslq %esi, %r12
subl %eax, %edi
jne 0x15016
movq %r8, %rdi
movl $0x1, %esi
movq %r12, %rdx
jmp 0x15003
movslq %esi, %rdx
movq %r8, %rdi
xorl %esi, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21e0
movl $0x0, 0x28(%rsp)
movl %edi, 0x2c(%rsp)
movq $0x0, 0x8(%rsp)
leaq (,%r12,8), %rdi
movq %r8, %rbx
callq 0x22e0
movq %rax, %r13
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x21e0
testl %r12d, %r12d
movq %r13, 0x38(%rsp)
je 0x151a9
leaq (%r15,%r12,8), %r11
leaq 0xa557(%rip), %rsi      # 0x1f5c0
movq %r13, %r9
movq %r15, %rdi
movq %r11, 0x10(%rsp)
movzbl (%r14), %r13d
testq %r13, %r13
je 0x151af
movl %r13d, %eax
andl $-0x2, %eax
movl (%rsi,%rax,2), %r8d
andl $0x1, %r13d
movl 0xc(%rsp), %eax
addl 0x8(%rsp), %eax
movl (%r15), %edx
cmpl %edx, %eax
ja 0x1514f
leal (%rax,%r8), %ecx
cmpl %ecx, %edx
jae 0x1514f
movq %r8, 0x18(%rsp)
movl %r13d, %esi
shll $0x2, %esi
movl 0x28(%rsp,%rsi), %edx
subl %eax, %edx
addl 0x8(%rsp,%rsi), %edx
movl $0x8, %r10d
movq %r15, %rsi
movq %rsi, %rbp
addl %edx, (%rsi)
movq %r10, %r12
addq $0x8, %rsi
cmpq %r11, %rsi
je 0x150ef
movl (%rsi), %r8d
cmpl %r8d, %eax
ja 0x150ef
leaq 0x8(%r12), %r10
cmpl %ecx, %r8d
jb 0x150cc
leaq 0x8(%rbp), %rbx
testl %r13d, %r13d
je 0x1516b
movq %rdi, 0x20(%rsp)
movq %r9, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r9, 0x40(%rsp)
callq 0x2280
movq 0x40(%rsp), %r9
addq %r12, %r9
cmpq %rbx, %r15
jae 0x15191
addq $-0x8, %r15
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %r11
leaq 0xa48e(%rip), %rsi      # 0x1f5c0
movq 0x18(%rsp), %r8
movl 0xc(%r15), %eax
movb $0x1, (%rcx,%rax)
addq $0x8, %r15
cmpq %rbp, %r15
jb 0x15137
movq 0x20(%rsp), %rdi
jmp 0x15152
movq %r15, %rbx
movl %r13d, %eax
addl %r8d, 0x8(%rsp,%rax,4)
incq %r14
movq %rbx, %r15
cmpq %r11, %rbx
jne 0x15074
jmp 0x151af
cmpq %r15, %rdi
je 0x1518c
movq %r15, %rsi
movq %r12, %rdx
movq %r9, %r15
movq %rdi, %rbp
callq 0x2360
movq 0x10(%rsp), %r11
movq %rbp, %rdi
movq %r15, %r9
addq %r12, %rdi
jmp 0x1519b
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r11
leaq 0xa41e(%rip), %rsi      # 0x1f5c0
movq 0x18(%rsp), %r8
jmp 0x15152
movq %r15, %rdi
movq %r13, %r9
movq 0x38(%rsp), %rbx
subq %rbx, %r9
movq %rbx, %rsi
movq %r9, %rdx
callq 0x2280
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2050