// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x20bf
movq 0x3cd2(%rip), %rax      # 0x4fc0
movq (%rax), %rdi
xorl %esi, %esi
movl %ebx, %edx
xorl %ecx, %ecx
callq 0x1030
pushq $0xa
popq %rdi
callq 0x1550
movq %rax, %rbx
xorl %esi, %esi
testq %rax, %rax
setne %sil
subq $0x8, %rsp
leaq 0x1cf8(%rip), %r10      # 0x3013
leaq 0x1ce7(%rip), %r11      # 0x3009
leaq 0x1cdb(%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 0x17b0
addq $0x20, %rsp
leaq 0x3916(%rip), %rsi      # 0x4c60
movq %rsp, %rdi
movl $0x160, %edx            # imm = 0x160
callq 0x10c0
leaq 0x1d93(%rip), %rdi      # 0x30f1
callq 0x11b0
pushq $0x8
popq %r14
cmpq $0x38, %r14
je 0x137f
leaq (%rsp,%r14), %rsi
movq %rbx, %rdi
callq 0x1690
addq $0x10, %r14
jmp 0x1367
movq 0x3c52(%rip), %r15      # 0x4fd8
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x1707
pushq $0x3
popq %rbp
leaq 0x1cea(%rip), %r14      # 0x3085
subl $0x1, %ebp
jb 0x13c1
movq %rbx, %rdi
callq 0x15db
movl (%rax), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x1707
jmp 0x139b
leaq 0x8(%rsp), %r14
leaq 0x1cc1(%rip), %r15      # 0x308e
xorl %r13d, %r13d
cmpq $0x16, %r13
je 0x148d
movq %rbx, %rdi
callq 0x159c
cltq
movl (%r14), %ebp
cmpq %rax, %r13
jge 0x1404
movq -0x8(%r14), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x1456
movq %rbx, %rdi
callq 0x15ad
cmpl %eax, %ebp
jle 0x145d
leaq -0x8(%r14), %rbp
movq %rbx, %rdi
callq 0x15db
movq %rax, %r12
movl (%rax), %esi
movq -0x8(%rax), %rdx
leaq 0x1c80(%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 0x1690
jmp 0x1481
movl (%r14), %ebp
movq -0x8(%r14), %r12
movq %rbx, %rdi
callq 0x15ad
leaq 0x1c27(%rip), %rdi      # 0x309a
movl %ebp, %esi
movq %r12, %rdx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1040
incq %r13
addq $0x10, %r14
jmp 0x13d0
leaq 0x1c24(%rip), %rdi      # 0x30b8
callq 0x11b0
movq 0x3b38(%rip), %r13      # 0x4fd8
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x1707
leaq 0x1c13(%rip), %rdi      # 0x30c6
callq 0x11b0
leaq 0x8(%rsp), %r12
leaq 0x1c13(%rip), %r14      # 0x30d7
xorl %r15d, %r15d
cmpq $0xa, %r15
je 0x14ec
movl (%r12), %edx
movq -0x8(%r12), %rcx
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
callq 0x1040
incq %r15
addq $0x10, %r12
jmp 0x14c7
leaq 0x1bf1(%rip), %rdi      # 0x30e4
callq 0x11b0
leaq 0x1bdd(%rip), %r14      # 0x30dc
movq %rbx, %rdi
callq 0x159f
testl %eax, %eax
je 0x1525
movq %rbx, %rdi
callq 0x15db
movl (%rax), %esi
movq -0x8(%rax), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1040
jmp 0x14ff
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x1707
movq %rbx, %rdi
callq 0x1580
callq 0x235d
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq