// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq 0x10(%rdi), %rax
cmpq $0x2, %rax
setb %cl
jb 0x6fe85
movq 0x8(%rbx), %rcx
movq (%rcx), %rdx
cmpq 0x10(%rcx), %rdx
ja 0x6fe89
addq $0x20, %rcx
movl $0x2, %esi
movq %rsi, %rdx
cmpq %rsi, %rax
je 0x6fe7f
movq -0x10(%rcx), %rdi
leaq 0x1(%rdx), %rsi
leaq 0x10(%rcx), %r8
cmpq (%rcx), %rdi
movq %r8, %rcx
jbe 0x6fe63
cmpq %rax, %rdx
setae %cl
testb %cl, %cl
jne 0x6feaa
leaq 0xd2def(%rip), %rdi     # 0x142c7f
leaq 0xd261f(%rip), %rsi     # 0x1424b6
leaq 0xd2e02(%rip), %rcx     # 0x142ca0
movl $0xb99, %edx            # imm = 0xB99
xorl %eax, %eax
callq 0xc825d
movq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0x709e6
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x6fedd
movq 0x8(%rbx), %rcx
xorl %r15d, %r15d
cmpq $0x1, (%rcx)
jne 0x6fee0
incq %r15
addq $0x10, %rcx
cmpq %r15, %rax
jne 0x6fec6
movq %rax, %r15
jmp 0x6fee0
xorl %r15d, %r15d
movq %r14, %rdi
movq %r15, %rsi
callq 0x709e6
movq %rbx, 0x8(%rsp)
movq 0x10(%rbx), %rbx
cmpq %rbx, %r15
jae 0x6ffed
movl $0x2, %eax
movq %rax, 0x10(%rsp)
movl $0x1, %ebp
movq 0x8(%rsp), %rax
movq %r15, %rcx
shlq $0x4, %rcx
movq 0x8(%rax), %r13
movq %rcx, 0x20(%rsp)
addq %rcx, %r13
xorl %r12d, %r12d
movq (%r13), %rdi
movl $0x40, %esi
callq 0x70a5c
cmpq 0x10(%rsp), %rax
jne 0x6ff4f
leaq (%r15,%r12), %rax
incq %rax
incq %r12
addq $0x10, %r13
cmpq %rbx, %rax
jb 0x6ff27
movq %r15, 0x18(%rsp)
testq %r12, %r12
je 0x6ffcb
movq 0x10(%rsp), %rax
leaq -0x1(%rax), %rbx
xorl %r13d, %r13d
xorl %esi, %esi
btq %r13, %rbx
setb %sil
movq %r14, %rdi
callq 0x6fc25
incq %r13
cmpq $0x6, %r13
jne 0x6ff65
movq %r14, %rdi
movq %r12, %rsi
callq 0x709e6
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq 0x8(%rax), %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 0x6fc25
incq %r15
cmpq %r15, %rbp
jne 0x6ffa9
incq %rbx
cmpq %r12, %rbx
jne 0x6ff8d
movq 0x18(%rsp), %r15
addq %r12, %r15
incq 0x10(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
incq %rbp
cmpq %rbx, %r15
jb 0x6ff11
testq %rbx, %rbx
je 0x70028
movq 0x8(%rsp), %rbp
movq 0x8(%rbp), %r12
addq $0x8, %r12
xorl %r13d, %r13d
movl (%r12), %edi
movl $0x20, %esi
callq 0x70a5c
movq %rax, %r15
cmpq %rax, %r13
cmovgq %r13, %r15
addq $0x10, %r12
movq %r15, %r13
decq %rbx
jne 0x70002
jmp 0x70030
xorl %r15d, %r15d
movq 0x8(%rsp), %rbp
xorl %ebx, %ebx
xorl %esi, %esi
btq %rbx, %r15
setb %sil
movq %r14, %rdi
callq 0x6fc25
incq %rbx
cmpq $0x6, %rbx
jne 0x70032
cmpq $0x0, 0x10(%rbp)
je 0x7009b
xorl %ebx, %ebx
testq %r15, %r15
jle 0x70092
movq %rbx, %rax
shlq $0x4, %rax
movq 0x8(%rbp), %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 0x6fc25
incq %r12
cmpq %r12, %r15
jne 0x7006e
incq %rbx
cmpq 0x10(%rbp), %rbx
jb 0x70056
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq