// File: /workspace/github_c_makefile_star2_O2/johsteffens[P]beth/lib/bcore/bcore.xo.c
// Function: bcore_huffman_tree_s_build
BCORE_DEFINE_OBJECT_INST_P_NASC_END( bcore_huffman_tree_s )

bcore_huffman_tree_s* bcore_huffman_tree_s_build( bcore_huffman_tree_s* o, const bcore_huffman_count_map_s* count_map, bcore_huffman_index_s* leaf_index )
{
    // bcore_huffman.x:530:5
    
    x_array_set_size(((x_array*)(o)),1 );
    for(sz_t i = 0; i < count_map->size; i++ )
    {
        bcore_huffman_node_s* node = ((bcore_huffman_node_s*)(x_array_push(((x_array*)(o)))));
        node->c = count_map->data[ i ].c;
        node->v = count_map->data[ i ].v;
        if( leaf_index ) bcore_huffman_index_s_set(leaf_index,node->v, o->size - 1 );
    }
    
    sz_t idx_a = 1;
    sz_t end_a = o->size;
    sz_t idx_b = o->size;
    while( end_a - idx_a + o->size - idx_b >= 2 )
    {
        u3_t b1 = ( idx_a < end_a && ( ( idx_b == o->size ) || ( o->data[ idx_a ].c <= o->data[ idx_b ].c ) ) ) ? idx_a++ : idx_b++;
        u3_t b0 = ( idx_a < end_a && ( ( idx_b == o->size ) || ( o->data[ idx_a ].c <= o->data[ idx_b ].c ) ) ) ? idx_a++ : idx_b++;
        o->data[ b1 ].p = o->data[ b0 ].p = o->size;
        bcore_huffman_node_s* node = ((bcore_huffman_node_s*)(x_array_push(((x_array*)(o)))));
        node->b1 = b1;
        node->b0 = b0;
        node->c = o->data[ b1 ].c + o->data[ b0 ].c;
    }
    
    return  o;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
pushq $0x1
popq %r14
movq %r14, %rsi
callq 0x30a50
leaq 0x8(%r15), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpq 0x10(%r12), %r13
jae 0x33b09
movq %rbx, %rdi
callq 0x34305
movq 0x8(%r12), %rcx
movq (%rcx,%rbp), %rcx
movq %rcx, (%rax)
movq 0x8(%r12), %rcx
movl 0x8(%rcx,%rbp), %esi
movl %esi, 0x20(%rax)
testq %r15, %r15
je 0x33b00
movq 0x10(%rbx), %rdx
decq %rdx
incq %rsi
movq 0x8(%rsp), %rdi
callq 0x67989
incq %r13
addq $0x10, %rbp
jmp 0x33ac0
movq 0x10(%rbx), %rdi
movq %rdi, %rax
movq %rdi, %r12
movq %rdi, 0x10(%rsp)
movq %rdi, %rdx
subq %r12, %rdx
subq %r14, %rdx
addq %rax, %rdx
cmpq $0x2, %rdx
jb 0x33be8
cmpq %rdi, %r14
jge 0x33b4e
cmpq %rax, %r12
je 0x33b5b
movq 0x8(%rbx), %rcx
imulq $0x28, %r14, %rdx
movq (%rcx,%rdx), %rdx
imulq $0x28, %r12, %rsi
cmpq (%rcx,%rsi), %rdx
jbe 0x33b5b
leaq 0x1(%r12), %rcx
movq %r14, %r13
movq %r12, %r14
jmp 0x33b62
leaq 0x1(%r14), %r13
movq %r12, %rcx
cmpq %rdi, %r13
jge 0x33b82
cmpq %rax, %rcx
je 0x33b90
movq 0x8(%rbx), %rdx
imulq $0x28, %r13, %rsi
movq (%rdx,%rsi), %rsi
imulq $0x28, %rcx, %rdi
cmpq (%rdx,%rdi), %rsi
jbe 0x33b90
leaq 0x1(%rcx), %r12
movq %r13, 0x8(%rsp)
movq %rcx, %r13
jmp 0x33b9c
leaq 0x1(%r13), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, %r12
movq 0x8(%rbx), %rcx
imulq $0x28, %r13, %r15
movq %rax, 0x8(%rcx,%r15)
movq 0x8(%rbx), %rcx
imulq $0x28, %r14, %rbp
movq %rax, 0x8(%rcx,%rbp)
movq %rbx, %rdi
callq 0x34305
movq %r14, 0x18(%rax)
movq %r13, 0x10(%rax)
movq 0x8(%rbx), %rcx
movq (%rcx,%r15), %rdx
addq (%rcx,%rbp), %rdx
movq %rdx, (%rax)
movq 0x10(%rbx), %rax
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rdi
jmp 0x33b18
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq