// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x50690
testb $0x1, %al
jne 0x4efae
leaq 0xf0843(%rip), %rdi     # 0x13f7d7
leaq 0xf0073(%rip), %rsi     # 0x13f00e
movl $0xb4a, %edx            # imm = 0xB4A
leaq 0xf0851(%rip), %rcx     # 0x13f7f8
movb $0x0, %al
callq 0x9d0f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x4f6c0
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x51(%rbp)
jae 0x4efff
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x1, (%rax)
sete %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x4f008
jmp 0x4f022
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x4efcf
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4f6c0
movq $0x2, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x4f14d
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rdx
xorl %eax, %eax
cmpq 0x10(%rdx), %rcx
movb %al, -0x52(%rbp)
jae 0x4f096
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movl $0x40, %esi
callq 0x4f750
cmpq -0x28(%rbp), %rax
sete %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0x4f09f
jmp 0x4f0b9
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x4f059
cmpq $0x0, -0x20(%rbp)
jbe 0x4f130
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
subq $0x1, %rsi
movl $0x6, %edx
callq 0x4f650
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4f6c0
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x4f12e
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
addq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rdx
subq $0x1, %rdx
callq 0x4f650
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4f0eb
jmp 0x4f130
movq -0x20(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4f037
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x4f1b3
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x48(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %edi
movl $0x20, %esi
callq 0x4f750
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x4f720
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x4f15d
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x6, %edx
callq 0x4f650
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x4f20e
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %esi
movq -0x40(%rbp), %rdx
callq 0x4f650
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x4f1cd
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)