// File: cloudinary[P]fuif/maniac/chance.cpp
// Function: build_table(unsigned short (*) [2], unsigned int, unsigned int)
void build_table(uint16_t newchances[4096][2], uint32_t factor, unsigned int max_p) {
    const int64_t one = 1LL << 32;
    const int size = 4096;
    int64_t p;
    unsigned int last_p8, p8;
    unsigned int i;

    memset(newchances,0,sizeof(uint16_t) * size * 2);

    last_p8 = 0;
    p = one / 2;
    for (i = 0; i < size / 2; i++) {
        p8 = (size * p + one / 2) >> 32; //FIXME try without the one
        if (p8 <= last_p8) p8 = last_p8 + 1;
        if (last_p8 && last_p8 < size && p8 <= max_p) newchances[last_p8][1] = p8;

        p += ((one - p) * factor + one / 2) >> 32;
        last_p8 = p8;
    }

    for (i = size - max_p; i <= max_p; i++) {
        if (newchances[i][1]) continue;

        p = (i * one + size / 2) / size;
        p += ((one - p) * factor + one / 2) >> 32;
        p8 = (size * p + one / 2) >> 32; //FIXME try without the one
        if (p8 <= i) p8 = i + 1;
        if (p8 > max_p) p8 = max_p;
        newchances[i][1] = p8;
    }

    for (i = 1; i < size; i++)
        newchances[i][0] = size - newchances[size - i][1];

}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movl %edx, 0x28(%rsp)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x20(%rsp)
movl $0x1000, 0x1c(%rsp)     # imm = 0x1000
movq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x4000, %edx           # imm = 0x4000
callq 0x31f0
movl $0x0, 0xc(%rsp)
movl $0x80000000, %eax       # imm = 0x80000000
movq %rax, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x800, 0x4(%rsp)       # imm = 0x800
jae 0x1ccee
movq 0x10(%rsp), %rax
shlq $0xc, %rax
movabsq $0x80000000, %rcx       # imm = 0x80000000
addq %rcx, %rax
sarq $0x20, %rax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0xc(%rsp), %eax
ja 0x1cc74
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0x1cca4
cmpl $0x1000, 0xc(%rsp)      # imm = 0x1000
jae 0x1cca4
movl 0x8(%rsp), %eax
cmpl 0x28(%rsp), %eax
ja 0x1cca4
movl 0x8(%rsp), %eax
movw %ax, %dx
movq 0x30(%rsp), %rax
movl 0xc(%rsp), %ecx
movw %dx, 0x2(%rax,%rcx,4)
movabsq $0x100000000, %rax      # imm = 0x100000000
subq 0x10(%rsp), %rax
movl 0x2c(%rsp), %ecx
imulq %rcx, %rax
movabsq $0x80000000, %rcx       # imm = 0x80000000
addq %rcx, %rax
sarq $0x20, %rax
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x1cc33
movl $0x1000, %eax           # imm = 0x1000
subl 0x28(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x28(%rsp), %eax
ja 0x1cdd8
movq 0x30(%rsp), %rax
movl 0x4(%rsp), %ecx
cmpw $0x0, 0x2(%rax,%rcx,4)
je 0x1cd1f
jmp 0x1cdc8
movl 0x4(%rsp), %eax
shlq $0x20, %rax
addq $0x800, %rax            # imm = 0x800
movl $0x1000, %ecx           # imm = 0x1000
cqto
idivq %rcx
movq %rax, 0x10(%rsp)
movabsq $0x100000000, %rax      # imm = 0x100000000
subq 0x10(%rsp), %rax
movl 0x2c(%rsp), %ecx
imulq %rcx, %rax
movabsq $0x80000000, %rcx       # imm = 0x80000000
addq %rcx, %rax
sarq $0x20, %rax
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
shlq $0xc, %rax
movabsq $0x80000000, %rcx       # imm = 0x80000000
addq %rcx, %rax
sarq $0x20, %rax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x4(%rsp), %eax
ja 0x1cda1
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x28(%rsp), %eax
jbe 0x1cdb3
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movw %ax, %dx
movq 0x30(%rsp), %rax
movl 0x4(%rsp), %ecx
movw %dx, 0x2(%rax,%rcx,4)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x1ccfb
movl $0x1, 0x4(%rsp)
cmpl $0x1000, 0x4(%rsp)      # imm = 0x1000
jae 0x1ce23
movq 0x30(%rsp), %rax
movl $0x1000, %ecx           # imm = 0x1000
subl 0x4(%rsp), %ecx
movl %ecx, %ecx
movzwl 0x2(%rax,%rcx,4), %ecx
movl $0x1000, %eax           # imm = 0x1000
subl %ecx, %eax
movw %ax, %dx
movq 0x30(%rsp), %rax
movl 0x4(%rsp), %ecx
movw %dx, (%rax,%rcx,4)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x1cde0
addq $0x38, %rsp
retq
nopl (%rax,%rax)