// File: /workspace/github_cpp_makefile_star2_O1/cloudinary[P]fuif/encoding/../maniac/symbol_enc.h
// Function: int estimate_writer<15, FinalCompoundSymbolBitCoder<SimpleBitChance, RacOutput24<BlobIO>, 15>>(FinalCompoundSymbolBitCoder<SimpleBitChance, RacOutput24<BlobIO>, 15>&, int, int, int)
int estimate_writer(SymbolCoder& coder, int min, int max, int value) {
    assert(min<=max);
    assert(value>=min);
    assert(value<=max);

    uint64_t total=0;

    // avoid doing anything if the value is already known
    if (min == max) return total;

    if (value == 0) { // value is zero
        coder.estimate(true, BIT_ZERO,0,total);
        return total;
    }

    assert(min <= 0 && max >= 0); // should always be the case, because guess should always be in valid range

    coder.estimate(false,BIT_ZERO,0,total);
    int sign = (value > 0 ? 1 : 0);
    if (max > 0 && min < 0) {
        // only output sign bit if value can be both pos and neg
        coder.estimate(sign,BIT_SIGN,0,total);
    }
    if (sign) min = 1;
    if (!sign) max = -1;
    const int a = abs(value);
    const int e = maniac::util::ilog2(a);
    int amin = sign ? abs(min) : abs(max);
    int amax = sign ? abs(max) : abs(min);

    int emax = maniac::util::ilog2(amax);
    int i = maniac::util::ilog2(amin);

    while (i < emax) {
        // if exponent >i is impossible, we are done
        if ((1 << (i+1)) > amax) break;
        // if exponent i is possible, output the exponent bit
        coder.estimate(i==e, BIT_EXP, i,total);
        if (i==e) break;
        i++;
    }

    int have = (1 << e);
    int left = have-1;
    for (int pos = e; pos>0;) {
        int bit = 1;
        left ^= (1 << (--pos));
        int minabs1 = have | (1<<pos);
        if (minabs1 > amax) { // 1-bit is impossible
            bit = 0;
        } else {
            bit = (a >> pos) & 1;
            coder.estimate(bit, BIT_MANT, pos,total);
        }
        have |= (bit << pos);
    }
//    printf("Total to write %i in %i..%i: %lu\n",value,min,max,total);
    return total;
}

// Assembly:
xorl %eax, %eax
cmpl %edx, %esi
je 0x1be8a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x10(%rdi), %r8
movzwl (%r8), %eax
testl %ecx, %ecx
je 0x1be75
movl $0x1000, %edi           # imm = 0x1000
movl $0x1000, %r10d          # imm = 0x1000
subq %rax, %r10
leaq 0x148cd(%rip), %r9      # 0x30640
movzwl (%r9,%r10,2), %eax
testl %edx, %edx
setle %r10b
testl %esi, %esi
setns %r11b
orb %r10b, %r11b
jne 0x1bd9f
movzwl 0x2(%r8), %r10d
subq %r10, %rdi
testl %ecx, %ecx
cmovgq %r10, %rdi
movzwl (%r9,%rdi,2), %edi
addq %rdi, %rax
movl %ecx, %r10d
negl %r10d
cmovsl %ecx, %r10d
bsrl %r10d, %edi
movl %edi, %r11d
xorl $0x1f, %r11d
xorl %ebx, %ebx
testl %ecx, %ecx
setg %bl
leal -0x1(%rbx,%rbx), %ecx
cmovgl %edx, %esi
movl %ecx, %ebp
negl %ebp
cmovsl %ecx, %ebp
movl %esi, %edx
negl %edx
cmovsl %esi, %edx
bsrl %edx, %ebx
bsrl %ebp, %ecx
leaq 0x1(%rdi), %r14
movl $0x2, %ebp
shll %cl, %ebp
testl %esi, %esi
je 0x1be18
cmpq %rbx, %rcx
jae 0x1be18
cmpl %edx, %ebp
jg 0x1be18
movzwl 0x4(%r8,%rcx,2), %r15d
movl $0x1000, %r12d          # imm = 0x1000
subq %r15, %r12
cmpq %rcx, %rdi
leaq 0x1(%rcx), %r13
cmoveq %r15, %r12
movzwl (%r9,%r12,2), %ecx
addq %rcx, %rax
movq %r13, %rcx
cmpq %r13, %r14
jne 0x1bdda
cmpl $0x1f, %r11d
je 0x1be80
movl $0x1, %esi
movl %edi, %ecx
shll %cl, %esi
leaq -0x1(%rdi), %r11
movl %esi, %ecx
btsl %r11d, %ecx
xorl %ebx, %ebx
cmpl %edx, %ecx
jg 0x1be5d
leal -0x1(%rdi), %ecx
movl %r10d, %ebx
shrl %cl, %ebx
movzwl 0x1e(%r8,%rdi,2), %ecx
movl $0x1000, %r14d          # imm = 0x1000
subq %rcx, %r14
andl $0x1, %ebx
cmovneq %rcx, %r14
movzwl (%r9,%r14,2), %ecx
addq %rcx, %rax
decb %dil
movl %edi, %ecx
shll %cl, %ebx
orl %ebx, %esi
leaq 0x1(%r11), %rcx
movq %r11, %rdi
cmpq $0x1, %rcx
jg 0x1be27
jmp 0x1be80
leaq 0x147c4(%rip), %rcx     # 0x30640
movzwl (%rcx,%rax,2), %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq