// File: /workspace/github_c_makefile_star2_O0/johsteffens[P]beth/lib/bcore/bcore.xo.c
// Function: bcore_huffman_codec_s_encode_u2
const bcore_huffman_codec_s* bcore_huffman_codec_s_encode_u2( const bcore_huffman_codec_s* o, u2_t val, bcore_huffman_bit_buffer_s* bit_buffer )
{
    // bcore_huffman.x:99:1
    
    if( !o->tree ) ERR_fa( "No tree! Load a codec or run scanning first." );
    sz_t idx = bcore_huffman_index_s_get(o->leaf_index,val );
    if( idx == 0 ) ERR_fa( "An encoding for value #<u2_t> does not exist.", val );
    
    const bcore_huffman_node_s* node =&( o->tree->data[ idx ]);
    
    u3_t stack = 0;
    sz_t bits = 0;
    
    while( node->p )
    {
        bl_t bit = ( o->tree->data[ node->p ].b1 == idx );
        idx = node->p;
        node = (&(o->tree->data[ idx ]));
    
        stack = ( stack << 1 ) | ( ( bit ) ? 1 : 0 );
        bits++;
    }
    
    if( bits > 64 )  ERR_fa( "Encoding of value #<u2_t> consumes more than 64 bits.", val );
    
    for(sz_t i = 0; i < bits; i++ )
    {
        bcore_huffman_bit_buffer_s_push_bl(bit_buffer,( stack & 1 ) );
        stack = stack >> 1;
    }
    
    return  o;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x4e95f
leaq 0xf0ccc(%rip), %rdi     # 0x13f611
leaq 0xf06c2(%rip), %rsi     # 0x13f00e
movl $0x9eb, %edx            # imm = 0x9EB
leaq 0xf0cd9(%rip), %rcx     # 0x13f631
movb $0x0, %al
callq 0x9d0f0
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0xc(%rbp), %esi
callq 0x4eae0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x4e99f
movl -0xc(%rbp), %r8d
leaq 0xf0c8c(%rip), %rdi     # 0x13f611
leaq 0xf0682(%rip), %rsi     # 0x13f00e
movl $0x9ed, %edx            # imm = 0x9ED
leaq 0xf0cc6(%rip), %rcx     # 0x13f65e
movb $0x0, %al
callq 0x9d0f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
imulq $0x28, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x4ea56
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
imulq $0x28, 0x8(%rcx), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
sete %al
andb $0x1, %al
movb %al, -0x39(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
imulq $0x28, -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
shlq %rax
movb -0x39(%rbp), %sil
xorl %ecx, %ecx
movl $0x1, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x4e9c7
cmpq $0x40, -0x38(%rbp)
jle 0x4ea82
movl -0xc(%rbp), %r8d
leaq 0xf0ba9(%rip), %rdi     # 0x13f611
leaq 0xf059f(%rip), %rsi     # 0x13f00e
movl $0x9fe, %edx            # imm = 0x9FE
leaq 0xf0c11(%rip), %rcx     # 0x13f68c
movb $0x0, %al
callq 0x9d0f0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x4eacb
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
setne %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x4eb40
movq -0x30(%rbp), %rax
shrq %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x4ea8a
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)