// File: /workspace/github_cpp_makefile_star2_O1/ctSkennerton[P]fxtract/util/sheap_t.c
// Function: main
int
main(void)
{
    plan_tests(1);
    setvbuf(stdout, 0, _IOLBF, 0);

    SHEAP *shp = sheap_create(10);
    ok(shp, "created");
    
    NAMEVAL some[22] = {
        { "alpha",    31 },
        { "beta",     41 },
        { "gamma",    59 },
        { "delta",    26 },
        { "epsilon",  53 },
        { "zeta",     58 },
        { "eta",      97 },
        { "theta",    93 },
        { "iota",     23 },
        { "kappa",    84 },
        { "lambda",   26 },
        { "mu",       43 },
        { "nu",       38 },
        { "xi",       32 },
        { "omicron",  79 },
        { "pi",       59 },
        { "rho",      28 },
        { "sigma",    84 },
        { "tau",      19 },
        { "phi",      71 },
        { "chi",      69 },
        { "psi",      19 }
    };

    int i;
    printf("push x 3\n");
    for (i = 0; i < 3; sheap_push(shp, &some[i++].val));
    sheap_dump(shp, stderr);

    for (i = 0; i < 3; i++)
        printf("pull %d\n", *sheap_pull(shp)), sheap_dump(shp, stderr);
    
    // Select the top (10) entries from a stream of 10+12, with dups.
    for (i = 0; i < 22; ++i) {
        if (i < sheap_size(shp)) {
            printf("push %d %s\n", some[i].val, some[i].name);
            sheap_push(shp, &some[i].val);
        } else if (some[i].val <= sheap_head(shp)) {
            printf("skip %d %s < %d \n",
                    some[i].val, some[i].name, sheap_head(shp));
        } else {
            // Only the first (10) elements of input are "saved" in "RAM".
            NAMEVAL *nvp = val_TO_NAMEVAL(sheap_pull(shp));   // extract lowest element
            printf("pull %d %s\n", nvp->val, nvp->name);
            *nvp = some[i]; 
            printf("push %d %s\n", nvp->val, nvp->name);
            sheap_push(shp, &nvp->val);
        }
    } 

    puts("# sheap_dump:");
    sheap_dump(shp, stderr);
    puts("# NAMEVAL[0..9]:");
    for (i = 0; i < 10; ++i)
        printf( " [%d]\t%d %s\n", i, some[i].val, some[i].name);

    puts("# pull x 10:");
    while (sheap_count(shp)) {
        NAMEVAL *nvp = val_TO_NAMEVAL(sheap_pull(shp));
        printf("\t%d %s\n", nvp->val, nvp->name);
    }

    sheap_dump(shp, stderr);
    sheap_destroy(shp);

    return exit_status();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl $0x1, %edi
callq 0x215a
movq 0x3c42(%rip), %rax      # 0x4fc0
movq (%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x1030
movl $0xa, %edi
callq 0x15e8
movq %rax, %rbx
xorl %esi, %esi
testq %rax, %rax
setne %sil
subq $0x8, %rsp
leaq 0x1c63(%rip), %r10      # 0x3013
leaq 0x1c52(%rip), %r11      # 0x3009
leaq 0x1c46(%rip), %r9       # 0x3004
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r10
pushq $0x24
pushq %r11
callq 0x1884
addq $0x20, %rsp
leaq 0x3881(%rip), %rsi      # 0x4c60
movq %rsp, %rdi
movl $0x160, %edx            # imm = 0x160
callq 0x10c0
leaq 0x1cfe(%rip), %rdi      # 0x30f1
callq 0x11b0
movl $0x8, %r14d
leaq (%rsp,%r14), %rsi
movq %rbx, %rdi
callq 0x1750
addq $0x10, %r14
cmpq $0x38, %r14
jne 0x13fe
movq 0x3bbd(%rip), %r15      # 0x4fd8
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x17d9
movl $0x3, %ebp
leaq 0x1c53(%rip), %r14      # 0x3085
movq %rbx, %rdi
callq 0x167c
movl (%rax), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x17d9
decl %ebp
jne 0x1432
leaq 0x8(%rsp), %r14
leaq 0x1c2d(%rip), %r15      # 0x308e
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x163b
cltq
movl (%r14), %ebp
cmpq %rax, %r13
jge 0x148e
movq -0x8(%r14), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x14e0
movq %rbx, %rdi
callq 0x164c
cmpl %eax, %ebp
jle 0x14e7
leaq -0x8(%r14), %rbp
movq %rbx, %rdi
callq 0x167c
movq %rax, %r12
movl (%rax), %esi
movq -0x8(%rax), %rdx
leaq 0x1bf6(%rip), %rdi      # 0x30ac
xorl %eax, %eax
callq 0x1040
movups (%rbp), %xmm0
movups %xmm0, -0x8(%r12)
movl (%r12), %esi
movq -0x8(%r12), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1750
jmp 0x150b
movl (%r14), %ebp
movq -0x8(%r14), %r12
movq %rbx, %rdi
callq 0x164c
leaq 0x1b9d(%rip), %rdi      # 0x309a
movl %ebp, %esi
movq %r12, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1040
incq %r13
addq $0x10, %r14
cmpq $0x16, %r13
jne 0x1464
leaq 0x1b95(%rip), %rdi      # 0x30b8
callq 0x11b0
movq 0x3aa9(%rip), %r13      # 0x4fd8
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x17d9
leaq 0x1b84(%rip), %rdi      # 0x30c6
callq 0x11b0
leaq 0x8(%rsp), %r12
leaq 0x1b84(%rip), %r14      # 0x30d7
xorl %r15d, %r15d
movl (%r12), %edx
movq -0x8(%r12), %rcx
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
incq %r15
addq $0x10, %r12
cmpq $0xa, %r15
jne 0x1556
leaq 0x1b64(%rip), %rdi      # 0x30e4
callq 0x11b0
movq %rbx, %rdi
callq 0x163e
testl %eax, %eax
je 0x15bc
leaq 0x1b44(%rip), %r14      # 0x30dc
movq %rbx, %rdi
callq 0x167c
movl (%rax), %esi
movq -0x8(%rax), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x163e
testl %eax, %eax
jne 0x1598
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x17d9
movq %rbx, %rdi
callq 0x161f
callq 0x23fe
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop