// File: /workspace/github_c_makefile_star2_O2/Ziddykins[P]Dawn/src/util.c
// Function: priority_remove_min
void * priority_remove_min(PriorityQueue pq) {
    //save element to return for later use
    void *elem = pq->min->tree->elem;

    //remove minimum
    if (pq->min_prev) {
        pq->min_prev->next = pq->min->next;
    } else {
        pq->head = pq->min->next;
    }
    //split min tree into subtrees and insert them
    if(pq->min->tree->children) {
        pq->min->tree->last_child->next = pq->head;
        pq->head = pq->min->tree->children;
    }
    free(pq->min->tree);
    free(pq->min);

    struct priority_node **trees; //needed for cleanup algorithm
    size_t const len = (size_t)(2*ceillog2ul(pq->size)+1);
    CALLEXIT(!(trees = calloc(len, sizeof trees)));

    struct priority_node * current_node = pq->head;

    while(current_node) {
        struct priority_node * process_node = current_node;
        current_node = current_node->next;
        while(1) {
            int degree = process_node->tree->degree;
            if(trees[degree] == NULL) {
                trees[degree] = process_node;
                break;
            } else {
                if(trees[degree]->tree->priority > process_node->tree->priority) {
                    priority_add_child(process_node->tree, trees[degree]);
                } else {
                    priority_add_child(trees[degree]->tree, process_node);
                    process_node = trees[degree];
                }
                trees[degree] = NULL;
            }
        }
    }

    pq->size--;
    pq->head = NULL;
    pq->min = NULL;
    pq->min_prev = NULL;
    if(pq->size) {
        size_t i;
        for(i = 0; !trees[i]; i++);
        pq->head = trees[i];
        pq->head->next = NULL;
        pq->min = pq->head;

        for(i++; i < len; i++) {
            if(trees[i]) {
                trees[i]->next = pq->head;
                pq->head = trees[i];
                priority_update_min(pq);
            }
        }
    }

    free(trees);
    return elem;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x10(%rdi), %rcx
movq (%rax), %rdi
movq 0x8(%rax), %rax
leaq 0x8(%rcx), %rdx
testq %rcx, %rcx
cmoveq %rbx, %rdx
movq (%rdi), %rcx
movq %rcx, (%rsp)
movq %rax, (%rdx)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0xf476
movq (%rbx), %rcx
movq 0x10(%rdi), %rdx
movq %rcx, 0x8(%rdx)
movq %rax, (%rbx)
callq 0x2250
movq 0x8(%rbx), %rdi
callq 0x2250
bsrq 0x18(%rbx), %r14
xorl $0x3f, %r14d
addl %r14d, %r14d
movl $0x81, %edi
subq %r14, %rdi
pushq $0x8
popq %rsi
callq 0x2110
testq %rax, %rax
je 0xf573
movq %rax, %r15
movl $0x80, %r13d
subq %r14, %r13
movq (%rbx), %r12
testq %r12, %r12
je 0xf507
movq 0x8(%r12), %rbp
movq (%r12), %rdi
movslq 0x18(%rdi), %r14
movq (%r15,%r14,8), %rsi
testq %rsi, %rsi
je 0xf4fe
movq (%rsi), %rax
movss 0x1c(%rax), %xmm0
ucomiss 0x1c(%rdi), %xmm0
jbe 0xf4e8
callq 0xf587
jmp 0xf4f7
movq %rax, %rdi
movq %r12, %rsi
callq 0xf587
movq (%r15,%r14,8), %r12
andq $0x0, (%r15,%r14,8)
jmp 0xf4c2
movq %r12, (%r15,%r14,8)
movq %rbp, %r12
jmp 0xf4b8
andq $0x0, 0x10(%rbx)
decq 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
je 0xf558
xorl %r12d, %r12d
movq (%r15,%r12,8), %rax
incq %r12
testq %rax, %rax
je 0xf51b
movq %rax, (%rbx)
andq $0x0, 0x8(%rax)
movq %rax, 0x8(%rbx)
cmpq %r13, %r12
ja 0xf558
movq (%r15,%r12,8), %rax
testq %rax, %rax
je 0xf553
movq (%rbx), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0xf5a3
incq %r12
jmp 0xf533
movq %r15, %rdi
callq 0x2250
movq (%rsp), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8701(%rip), %rdi      # 0x17c7b
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270