// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: bpmnode_sort(BPMNode*, unsigned long)
static void bpmnode_sort(BPMNode* leaves, size_t num)
{
  BPMNode* mem = (BPMNode*)lodepng_malloc(sizeof(*leaves) * num);
  size_t width, counter = 0;
  for(width = 1; width < num; width *= 2)
  {
    BPMNode* a = (counter & 1) ? mem : leaves;
    BPMNode* b = (counter & 1) ? leaves : mem;
    size_t p;
    for(p = 0; p < num; p += 2 * width)
    {
      size_t q = (p + width > num) ? num : (p + width);
      size_t r = (p + 2 * width > num) ? num : (p + 2 * width);
      size_t i = p, j = q, k;
      for(k = p; k < r; k++)
      {
        if(i < q && (j >= r || a[i].weight <= a[j].weight)) b[k] = a[i++];
        else b[k] = a[j++];
      }
    }
    counter++;
  }
  if(counter & 1) memcpy(leaves, mem, sizeof(*leaves) * num);
  lodepng_free(mem);
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
imulq $0x18, 0x78(%rsp), %rdi
callq 0x6cd0
movq %rax, 0x70(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x1, 0x68(%rsp)
movq 0x68(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x7472
movq 0x60(%rsp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x7261
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
jmp 0x726e
movq 0x80(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x7296
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x72a0
movq 0x70(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x48(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x7452
movq 0x48(%rsp), %rax
addq 0x68(%rsp), %rax
cmpq 0x78(%rsp), %rax
jbe 0x72e0
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x72ef
movq 0x48(%rsp), %rax
addq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq 0x68(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
cmpq 0x78(%rsp), %rax
jbe 0x731b
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
jmp 0x732f
movq 0x48(%rsp), %rax
movq 0x68(%rsp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x38(%rsp), %rax
jae 0x7439
movq 0x30(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x73e3
movq 0x28(%rsp), %rax
cmpq 0x38(%rsp), %rax
jae 0x73a0
movq 0x58(%rsp), %rax
imulq $0x18, 0x30(%rsp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq 0x58(%rsp), %rcx
imulq $0x18, 0x28(%rsp), %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jg 0x73e3
movq 0x58(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x30(%rsp)
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq 0x50(%rsp), %rax
imulq $0x18, 0x20(%rsp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x7424
movq 0x58(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x28(%rsp)
imulq $0x18, %rax, %rax
addq %rax, %rcx
movq 0x50(%rsp), %rax
imulq $0x18, 0x20(%rsp), %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x7426
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x7356
jmp 0x743b
movq 0x68(%rsp), %rax
shlq %rax
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0x72b3
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
shlq %rax
movq %rax, 0x68(%rsp)
jmp 0x7236
movq 0x60(%rsp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x7499
movq 0x80(%rsp), %rdi
movq 0x70(%rsp), %rsi
imulq $0x18, 0x78(%rsp), %rdx
callq 0x3170
movq 0x70(%rsp), %rdi
callq 0x78b0
addq $0x88, %rsp
retq
nopl (%rax,%rax)