// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdi), %r12
leaq 0x132a67(%rip), %rax    # 0x19be20
movl (%rax), %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x20(%rax,%rcx,8), %rdx
movq 0x18(%rax,%rcx,8), %rcx
andq %r12, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r12, %rdx
je 0x6940f
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0x6940f
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0x6940f
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0x69418
movq (%rax,%rcx), %r13
testq %r13, %r13
jne 0x6943f
movabsq $-0x5073d588c3beb52e, %rdi # imm = 0xAF8C2A773C414AD2
movq %r12, %rsi
callq 0xe9bc5
movq %rax, %r13
leaq 0x1329ec(%rip), %rdi    # 0x19be20
movq %r12, %rsi
movq %rax, %rdx
callq 0x115fac
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xec548
cmpq $0x0, 0x10(%r15)
je 0x694a5
leaq 0x8(%r14), %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rbx), %rsi
movq %rbx, %rdi
callq 0x55f9e
movq 0x8(%r15), %rcx
movq (%rcx,%r13), %rcx
movq %rcx, (%rax)
movq 0x8(%r15), %rcx
movl 0x8(%rcx,%r13), %esi
movl %esi, 0x20(%rax)
testq %r14, %r14
je 0x69498
movq 0x10(%rbx), %rdx
decq %rdx
incq %rsi
movq %r12, %rdi
callq 0xc8a20
incq %rbp
addq $0x10, %r13
cmpq 0x10(%r15), %rbp
jb 0x6945f
movq 0x10(%rbx), %rdi
leaq -0x3(%rdi), %rax
cmpq $-0x3, %rax
ja 0x695af
movl $0x1, %r15d
movq %rdi, %rax
movq %rdi, %r8
movq %rdi, 0x10(%rsp)
cmpq %rdi, %r15
jge 0x694e8
cmpq %rax, %r8
je 0x694f4
movq 0x8(%rbx), %rcx
leaq (%r15,%r15,4), %rdx
movq (%rcx,%rdx,8), %rdx
leaq (%r8,%r8,4), %rsi
cmpq (%rcx,%rsi,8), %rdx
jbe 0x694f4
leaq 0x1(%r8), %rcx
movq %r15, %r13
movq %r8, %r15
jmp 0x694fb
leaq 0x1(%r15), %r13
movq %r8, %rcx
cmpq %rdi, %r13
jge 0x69522
cmpq %rax, %rcx
je 0x69533
movq 0x8(%rbx), %rdx
leaq (,%r13,4), %rsi
addq %r13, %rsi
movq (%rdx,%rsi,8), %rsi
leaq (%rcx,%rcx,4), %rdi
cmpq (%rdx,%rdi,8), %rsi
jbe 0x69533
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq %r13, %rbp
movq %rcx, %r13
jmp 0x6953c
leaq 0x1(%r13), %rbp
movq %rcx, 0x8(%rsp)
movq 0x8(%rbx), %rcx
leaq (,%r13,8), %rdx
leaq (%rdx,%rdx,4), %r14
movq %rax, 0x8(%rcx,%r14)
movq 0x8(%rbx), %rcx
leaq (,%r15,8), %rdx
leaq (%rdx,%rdx,4), %r12
movq %rax, 0x8(%rcx,%r12)
movq (%rbx), %rsi
movq %rbx, %rdi
callq 0x55f9e
movq %r15, 0x18(%rax)
movq %r13, 0x10(%rax)
movq 0x8(%rbx), %rcx
movq (%rcx,%r14), %rdx
addq (%rcx,%r12), %rdx
movq %rdx, (%rax)
movq 0x10(%rbx), %rax
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rdi
movq %rdi, %rdx
subq %r8, %rdx
subq %rbp, %rdx
addq %rax, %rdx
movq %rbp, %r15
cmpq $0x1, %rdx
ja 0x694c8
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq