// File: /workspace/github_c_makefile_star2_O3/johsteffens[P]beth/lib/bcore/bcore.xo.c
// Function: bcore_huffman_count_map_s_encode
bcore_huffman_count_map_s* bcore_huffman_count_map_s_encode( bcore_huffman_count_map_s* o, bcore_huffman_bit_buffer_s* out )
{
    // bcore_huffman.x:458:1
    
    if( !bcore_huffman_count_map_s_is_sorted(o) ) ERR_fa( "Map is not sorted." );
    
    bcore_huffman_bit_buffer_s_push_packed_u3(out,o->size );
    
    sz_t idx = 0;
    u3_t count = 0;
    
    {
        for(; idx < o->size && o->data[ idx ].c == 1 ; idx++ ) count++;
        bcore_huffman_bit_buffer_s_push_packed_u3(out,count );
    }
    
    sz_t bits = 2;
    while( idx < o->size )
    {
        count = 0;
        for(sz_t i = idx; i < o->size && bcore_huffman_min_bits(o->data[ i ].c, 64 ) == bits ; i++ ) count++;
        if( count > 0 )
        {
            bcore_huffman_bit_buffer_s_push_u3(out,bits - 1, 6 );
            bcore_huffman_bit_buffer_s_push_packed_u3(out,count );
            for(sz_t i = 0; i < count; i++ ) bcore_huffman_bit_buffer_s_push_u3(out,o->data[ idx + i ].c, bits - 1 );
        }
        idx += count;
        bits++;
    }
    
    sz_t max_bits = 0;
    for(sz_t i = 0; i < o->size; i++ ) max_bits = sz_max( max_bits, bcore_huffman_min_bits(o->data[ i ].v, 32 ) );
    bcore_huffman_bit_buffer_s_push_u3(out,max_bits, 6 );
    for(sz_t i = 0; i < o->size; i++ ) bcore_huffman_bit_buffer_s_push_u3(out,o->data[ i ].v, max_bits );
    
    return  o;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rdi), %rsi
cmpq $0x2, %rsi
jb 0x6997c
movq 0x8(%r15), %rax
movq (%rax), %rcx
cmpq 0x10(%rax), %rcx
ja 0x69957
addq $0x20, %rax
movl $0x2, %edx
movq %rdx, %rcx
cmpq %rdx, %rsi
je 0x69952
movq -0x10(%rax), %rdi
leaq 0x1(%rcx), %rdx
leaq 0x10(%rax), %r8
cmpq (%rax), %rdi
movq %r8, %rax
jbe 0x69936
cmpq %rsi, %rcx
jae 0x6997c
leaq 0xce199(%rip), %rdi     # 0x137af7
leaq 0xcd9c9(%rip), %rsi     # 0x13732e
leaq 0xce1ac(%rip), %rcx     # 0x137b18
movl $0xb99, %edx            # imm = 0xB99
xorl %eax, %eax
callq 0xbe6f9
movq 0x10(%r15), %rsi
movq %r14, %rdi
callq 0x6a47e
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x699ab
movq 0x8(%r15), %rcx
xorl %r12d, %r12d
cmpq $0x1, (%rcx)
jne 0x699ae
incq %r12
addq $0x10, %rcx
cmpq %r12, %rax
jne 0x69994
movq %rax, %r12
jmp 0x699ae
xorl %r12d, %r12d
movq %r14, %rdi
movq %r12, %rsi
callq 0x6a47e
movq 0x10(%r15), %rbx
cmpq %rbx, %r12
movq %r15, 0x8(%rsp)
jae 0x69ab5
movl $0x2, %eax
movq %rax, 0x10(%rsp)
movl $0x1, %ebp
movq %r12, 0x18(%rsp)
shlq $0x4, %r12
movq 0x8(%r15), %r13
movq %r12, 0x20(%rsp)
addq %r12, %r13
xorl %r12d, %r12d
movq (%r13), %rdi
movl $0x40, %esi
callq 0x6a4f4
cmpq 0x10(%rsp), %rax
jne 0x69a1e
movq 0x18(%rsp), %rax
addq %r12, %rax
incq %rax
incq %r12
addq $0x10, %r13
cmpq %rbx, %rax
jb 0x699f2
testq %r12, %r12
je 0x69a99
movq 0x10(%rsp), %rax
leaq -0x1(%rax), %rbx
xorl %r13d, %r13d
xorl %esi, %esi
btq %r13, %rbx
setb %sil
movq %r14, %rdi
callq 0x696de
incq %r13
cmpq $0x6, %r13
jne 0x69a2f
movq %r14, %rdi
movq %r12, %rsi
callq 0x6a47e
xorl %ebx, %ebx
movq 0x8(%r15), %rax
addq 0x20(%rsp), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
movq (%rcx,%rax), %r13
xorl %r15d, %r15d
xorl %esi, %esi
btq %r15, %r13
setb %sil
movq %r14, %rdi
callq 0x696de
incq %r15
cmpq %r15, %rbp
jne 0x69a6e
incq %rbx
cmpq %r12, %rbx
movq 0x8(%rsp), %r15
jne 0x69a57
movq 0x10(%r15), %rbx
movq 0x18(%rsp), %rax
addq %r12, %rax
movq %rax, %r12
incq 0x10(%rsp)
incq %rbp
cmpq %rbx, %rax
jb 0x699da
testq %rbx, %rbx
je 0x69aeb
movq 0x8(%r15), %r12
addq $0x8, %r12
xorl %r13d, %r13d
movl (%r12), %edi
movl $0x20, %esi
callq 0x6a4f4
movq %rax, %r15
cmpq %rax, %r13
cmovgq %r13, %r15
addq $0x10, %r12
movq %r15, %r13
decq %rbx
jne 0x69ac5
jmp 0x69aee
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %esi, %esi
btq %rbx, %r15
setb %sil
movq %r14, %rdi
callq 0x696de
incq %rbx
cmpq $0x6, %rbx
jne 0x69af0
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x69b6d
xorl %ebx, %ebx
testq %r15, %r15
jle 0x69b65
movq %rbx, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0x8(%rcx,%rax), %r13d
xorl %r12d, %r12d
movl $0x1, %eax
movl %r12d, %ecx
shlq %cl, %rax
xorl %esi, %esi
testl %r13d, %eax
setne %sil
movq %r14, %rdi
callq 0x696de
incq %r12
cmpq %r12, %r15
jne 0x69b38
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
incq %rbx
cmpq %rax, %rbx
jb 0x69b1b
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq