// File: attractivechaos[P]udb2/stb/./stb.h
// Function: stb__alloc_chunk
static void * stb__alloc_chunk(stb__alloc *src, int size, int align, int pre_align)
{
   void *p;
   stb__chunk *c = stb__chunks(src);

   if (c && size <= stb_alloc_chunk_size) {

      p = stb__try_chunk(c, size, align, pre_align);
      if (p) { ++c->alloc; return p; }

      // try a second chunk to reduce wastage
      if (c->next) {
         p = stb__try_chunk(c->next, size, align, pre_align);
         if (p) { ++c->alloc; return p; }
   
         // put the bigger chunk first, since the second will get buried
         // the upshot of this is that, until it gets allocated from, chunk #2
         // is always the largest remaining chunk. (could formalize
         // this with a heap!)
         stb__sort_chunks(src);
         c = stb__chunks(src);
      }
   }

   // allocate a new chunk
   {
      stb__chunk *n;

      int chunk_size = stb_alloc_chunk_size;
      // we're going to allocate a new chunk to put this in
      if (size > chunk_size)
         chunk_size = size;

      assert(sizeof(*n) + pre_align <= 16);

      // loop trying to allocate a large enough chunk
      // the loop is because the alignment may cause problems if it's big...
      // and we don't know what our chunk alignment is going to be
      while (1) {
         n = (stb__chunk *) malloc(16 + chunk_size);
         if (n == NULL) return NULL;

         n->data_left = chunk_size - sizeof(*n);

         p = stb__try_chunk(n, size, align, pre_align);
         if (p != NULL) {
            n->next = c;
            stb__setchunks(src, n);

            // if we just used up the whole block immediately,
            // move the following chunk up
            n->alloc = 1;
            if (size == chunk_size)
               stb__sort_chunks(src);

            return p;
         }

         free(n);
         chunk_size += 16+align;
      }
   }
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movl %esi, 0x34(%rsp)
movl %edx, 0x30(%rsp)
movl %ecx, 0x2c(%rsp)
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
addq $-0x2, %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x1c92f
movl 0x34(%rsp), %eax
cmpl 0x9ae3(%rip), %eax      # 0x26360
jg 0x1c92f
movq 0x18(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x2c(%rsp), %ecx
callq 0x1ca50
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x1c8c3
movq 0x18(%rsp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x1ca46
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
je 0x1c92d
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x2c(%rsp), %ecx
callq 0x1ca50
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x1c911
movq 0x18(%rsp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x1ca46
movq 0x38(%rsp), %rdi
callq 0x1cb50
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
addq $-0x2, %rax
movq %rax, 0x18(%rsp)
jmp 0x1c92f
movl 0x9a2b(%rip), %eax      # 0x26360
movl %eax, 0xc(%rsp)
movl 0x34(%rsp), %eax
cmpl 0xc(%rsp), %eax
jle 0x1c94b
movl 0x34(%rsp), %eax
movl %eax, 0xc(%rsp)
movslq 0x2c(%rsp), %rax
addq $0x10, %rax
cmpq $0x10, %rax
ja 0x1c95c
jmp 0x1c97b
leaq 0x357d(%rip), %rdi      # 0x1fee0
leaq 0x2a1f(%rip), %rsi      # 0x1f389
movl $0xaa4, %edx            # imm = 0xAA4
leaq 0x3587(%rip), %rcx      # 0x1fefd
callq 0x2140
jmp 0x1c97d
movl 0xc(%rsp), %eax
addl $0x10, %eax
movslq %eax, %rdi
callq 0x2260
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x1c9a7
movq $0x0, 0x40(%rsp)
jmp 0x1ca46
movslq 0xc(%rsp), %rax
subq $0x10, %rax
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x10(%rsp), %rdi
movl 0x34(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x2c(%rsp), %ecx
callq 0x1ca50
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0x1ca28
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x10(%rsp), %rcx
addq $0x2, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rax
movl $0x1, 0xc(%rax)
movl 0x34(%rsp), %eax
cmpl 0xc(%rsp), %eax
jne 0x1ca1c
movq 0x38(%rsp), %rdi
callq 0x1cb50
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rsp)
jmp 0x1ca46
movq 0x10(%rsp), %rdi
callq 0x23c0
movl 0x30(%rsp), %eax
addl $0x10, %eax
addl 0xc(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x1c97d
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq