// File: 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
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movl $0x0, -0x4(%rbp)
movl $0x1, %edi
callq 0x2930
movq 0x3cdd(%rip), %rax      # 0x4fc0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x1, %edx
movq %rcx, %rsi
callq 0x1030
movl $0xa, %edi
callq 0x1780
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x134d
xorl %edi, %edi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %r8d
leaq 0x1ce5(%rip), %r9       # 0x3004
leaq 0x1ce3(%rip), %r10      # 0x3009
leaq 0x1ce6(%rip), %rax      # 0x3013
movq %r8, %rdx
movq %r8, %rcx
movq %r10, (%rsp)
movl $0x24, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x1c50
jmp 0x138a
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %r8d
leaq 0x1ca9(%rip), %r9       # 0x3004
leaq 0x1ca7(%rip), %r10      # 0x3009
leaq 0x1caa(%rip), %rax      # 0x3013
movq %r8, %rsi
movq %r8, %rdx
movq %r8, %rcx
movq %r10, (%rsp)
movl $0x24, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x1c50
leaq -0x170(%rbp), %rdi
leaq 0x38c8(%rip), %rsi      # 0x4c60
movl $0x160, %edx            # imm = 0x160
callq 0x10c0
leaq 0x1cdc(%rip), %rdi      # 0x3085
movb $0x0, %al
callq 0x1040
movl $0x0, -0x174(%rbp)
cmpl $0x3, -0x174(%rbp)
jge 0x13f5
jmp 0x13c5
movq -0x10(%rbp), %rdi
movl -0x174(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x174(%rbp)
cltq
leaq -0x170(%rbp), %rsi
shlq $0x4, %rax
addq %rax, %rsi
addq $0x8, %rsi
callq 0x1a10
jmp 0x13ba
movq -0x10(%rbp), %rdi
movq 0x3bd8(%rip), %rax      # 0x4fd8
movq (%rax), %rsi
callq 0x1b30
movl $0x0, -0x174(%rbp)
cmpl $0x3, -0x174(%rbp)
jge 0x1458
movq -0x10(%rbp), %rdi
callq 0x18a0
movl (%rax), %esi
leaq 0x1c62(%rip), %rdi      # 0x308f
movb $0x0, %al
callq 0x1040
movq -0x10(%rbp), %rdi
movq 0x3b99(%rip), %rax      # 0x4fd8
movq (%rax), %rsi
callq 0x1b30
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x1412
movl $0x0, -0x174(%rbp)
cmpl $0x16, -0x174(%rbp)
jge 0x1642
movl -0x174(%rbp), %eax
movl %eax, -0x18c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1810
movl %eax, %ecx
movl -0x18c(%rbp), %eax
cmpl %ecx, %eax
jge 0x14f5
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
leaq 0x1bd1(%rip), %rdi      # 0x3098
movb $0x0, %al
callq 0x1040
movq -0x10(%rbp), %rdi
movslq -0x174(%rbp), %rax
leaq -0x170(%rbp), %rsi
shlq $0x4, %rax
addq %rax, %rsi
addq $0x8, %rsi
callq 0x1a10
jmp 0x162c
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x190(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1850
movl %eax, %ecx
movl -0x190(%rbp), %eax
cmpl %ecx, %eax
jg 0x1590
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x19c(%rbp)
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x198(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1850
movl -0x19c(%rbp), %esi
movq -0x198(%rbp), %rdx
movl %eax, %ecx
leaq 0x1b20(%rip), %rdi      # 0x30a4
movb $0x0, %al
callq 0x1040
jmp 0x162a
movq -0x10(%rbp), %rdi
callq 0x18a0
movq %rax, %rdi
callq 0x1760
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x180(%rbp), %rax
movq (%rax), %rdx
leaq 0x1af3(%rip), %rdi      # 0x30b6
movb $0x0, %al
callq 0x1040
movq -0x180(%rbp), %rax
movslq -0x174(%rbp), %rdx
leaq -0x170(%rbp), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x180(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x180(%rbp), %rax
movq (%rax), %rdx
leaq 0x1a89(%rip), %rdi      # 0x3098
movb $0x0, %al
callq 0x1040
movq -0x10(%rbp), %rdi
movq -0x180(%rbp), %rsi
addq $0x8, %rsi
callq 0x1a10
jmp 0x162c
jmp 0x162e
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x1462
leaq 0x1a79(%rip), %rdi      # 0x30c2
callq 0x11b0
movq -0x10(%rbp), %rdi
movq 0x397f(%rip), %rax      # 0x4fd8
movq (%rax), %rsi
callq 0x1b30
leaq 0x1a68(%rip), %rdi      # 0x30d0
callq 0x11b0
movl $0x0, -0x174(%rbp)
cmpl $0xa, -0x174(%rbp)
jge 0x16d5
movl -0x174(%rbp), %esi
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movslq -0x174(%rbp), %rcx
leaq -0x170(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x1a24(%rip), %rdi      # 0x30e1
movb $0x0, %al
callq 0x1040
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x1677
leaq 0x1a12(%rip), %rdi      # 0x30ee
callq 0x11b0
movq -0x10(%rbp), %rdi
callq 0x1820
cmpl $0x0, %eax
je 0x172b
movq -0x10(%rbp), %rdi
callq 0x18a0
movq %rax, %rdi
callq 0x1760
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x188(%rbp), %rax
movq (%rax), %rdx
leaq 0x19c4(%rip), %rdi      # 0x30e6
movb $0x0, %al
callq 0x1040
jmp 0x16e1
movq -0x10(%rbp), %rdi
movq 0x38a2(%rip), %rax      # 0x4fd8
movq (%rax), %rsi
callq 0x1b30
movq -0x10(%rbp), %rdi
callq 0x17e0
callq 0x2c80
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)