// File: /workspace/github_c_makefile_star2_O1/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 0x139537(%rip), %rax    # 0x1a8e20
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 0x6f943
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0x6f943
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0x6f943
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0x6faf5
movq (%rax,%rcx), %r13
testq %r13, %r13
jne 0x6f973
movabsq $-0x5073d588c3beb52e, %rdi # imm = 0xAF8C2A773C414AD2
movq %r12, %rsi
callq 0xf3f85
movq %rax, %r13
leaq 0x1394b8(%rip), %rdi    # 0x1a8e20
movq %r12, %rsi
movq %rax, %rdx
callq 0x12128c
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf69d8
cmpq $0x0, 0x10(%r15)
je 0x6f9d9
leaq 0x8(%r14), %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rbx), %rsi
movq %rbx, %rdi
callq 0x5b7f0
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 0x6f9cc
movq 0x10(%rbx), %rdx
decq %rdx
incq %rsi
movq %r12, %rdi
callq 0xd2be9
incq %rbp
addq $0x10, %r13
cmpq 0x10(%r15), %rbp
jb 0x6f993
movq 0x10(%rbx), %rdi
leaq -0x3(%rdi), %rax
cmpq $-0x3, %rax
ja 0x6fae3
movl $0x1, %r15d
movq %rdi, %rax
movq %rdi, %r8
movq %rdi, 0x10(%rsp)
cmpq %rdi, %r15
jge 0x6fa1c
cmpq %rax, %r8
je 0x6fa28
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 0x6fa28
leaq 0x1(%r8), %rcx
movq %r15, %r13
movq %r8, %r15
jmp 0x6fa2f
leaq 0x1(%r15), %r13
movq %r8, %rcx
cmpq %rdi, %r13
jge 0x6fa56
cmpq %rax, %rcx
je 0x6fa67
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 0x6fa67
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq %r13, %rbp
movq %rcx, %r13
jmp 0x6fa70
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 0x5b7f0
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 0x6f9fc
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
jmp 0x6f947