// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
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), %r14
movq %rax, (%rdx)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x10629
movq (%rbx), %rcx
movq 0x10(%rdi), %rdx
movq %rcx, 0x8(%rdx)
movq %rax, (%rbx)
callq 0x2240
movq 0x8(%rbx), %rdi
callq 0x2240
movq 0x18(%rbx), %r12
bsrq %r12, %r15
xorl $0x3f, %r15d
addl %r15d, %r15d
movl $0x81, %edi
subq %r15, %rdi
movl $0x8, %esi
callq 0x2110
testq %rax, %rax
je 0x10798
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x106e3
movq %rcx, %rdx
movq 0x8(%rcx), %rcx
movq (%rdx), %r8
movslq 0x18(%r8), %rdi
movq (%rax,%rdi,8), %rsi
testq %rsi, %rsi
je 0x106b1
movq (%rsi), %r9
movss 0x1c(%r9), %xmm0
ucomiss 0x1c(%r8), %xmm0
jbe 0x106b6
movq 0x8(%r8), %r9
movq %r9, 0x8(%rsi)
movq %rsi, 0x8(%r8)
leal 0x1(%rdi), %r9d
movl %r9d, 0x18(%r8)
cmpq $0x0, 0x10(%r8)
jne 0x106d5
movq %rsi, 0x10(%r8)
jmp 0x106d5
movq %rdx, %r8
jmp 0x106d8
movq 0x8(%r9), %r8
movq %r8, 0x8(%rdx)
movq %rdx, 0x8(%r9)
incl 0x18(%r9)
cmpq $0x0, 0x10(%r9)
jne 0x106d1
movq %rdx, 0x10(%r9)
movq (%rax,%rdi,8), %rdx
xorl %r8d, %r8d
movq %r8, (%rax,%rdi,8)
testq %rsi, %rsi
jne 0x10670
jmp 0x10664
decq %r12
movq %r12, 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
je 0x1072c
movl $0x80, %ecx
leaq 0x8(%rbx), %rdx
leaq 0x10(%rbx), %rsi
subq %r15, %rcx
xorl %edi, %edi
movq (%rax,%rdi,8), %r8
incq %rdi
testq %r8, %r8
je 0x1070c
movq %r8, (%rbx)
movq $0x0, 0x8(%r8)
movq %r8, 0x8(%rbx)
cmpq %rcx, %rdi
jbe 0x10743
movq %rax, %rdi
callq 0x2240
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
decq %rdi
movq 0x8(%rax,%rdi,8), %r8
testq %r8, %r8
je 0x10783
movq (%rbx), %r9
movq %r9, 0x8(%r8)
movq %r8, (%rbx)
movq 0x8(%rbx), %r9
testq %r9, %r9
je 0x10776
movq (%r9), %r9
movss 0x1c(%r9), %xmm0
movq (%r8), %r9
ucomiss 0x1c(%r9), %xmm0
jbe 0x1078d
movq $0x0, (%rsi)
movq %rdx, %r9
movq %r8, (%r9)
incq %rdi
cmpq %rcx, %rdi
jb 0x10746
jmp 0x1072c
cmpq $0x0, (%rsi)
movq %rsi, %r9
jne 0x10783
jmp 0x10780
callq 0x30d6