// File: /workspace/github_c_makefile_star2_O2/USArmyResearchLab[P]ARL_OpenSHMEM/src/xmalloc.c
// Function: malloc_pages
static void *
malloc_pages( struct __xmem_struct* xm, size_t size)
{
    void *p, *delay_free = NULL;
    size_t i;
    struct pgfree *pf;
    size_t idx;

    idx = pageround(size);
    if (idx < size) {
	errno = ENOMEM;
	return NULL;
    } else
	size = idx;

    p = NULL;

    /* Look for free pages before asking for more */
    for(pf = xm->free_list.next; pf; pf = pf->next) {

#ifdef MALLOC_EXTRA_SANITY
	if (pf->size & malloc_pagemask)
	    wrterror("(ES): junk length entry on free_list.\n");
	if (!pf->size)
	    wrterror("(ES): zero length entry on free_list.\n");
	if (pf->page == pf->end)
	    wrterror("(ES): zero entry on free_list.\n");
	if (pf->page > pf->end)
	    wrterror("(ES): sick entry on free_list.\n");
	if ((void*)pf->page >= (void*)__sbrk(xm,0))
	    wrterror("(ES): entry on free_list past brk.\n");
	if (page_dir[ptr2idx(pf->page)] != MALLOC_FREE)
	    wrterror("(ES): non-free first page on free-list.\n");
	if (page_dir[ptr2idx(pf->end)-1] != MALLOC_FREE)
	    wrterror("(ES): non-free last page on free-list.\n");
#endif /* MALLOC_EXTRA_SANITY */

	if (pf->size < size)
	    continue;

	if (pf->size == size) {
	    p = pf->page;
	    if (pf->next != NULL)
		    pf->next->prev = pf->prev;
	    pf->prev->next = pf->next;
	    delay_free = pf;
	    break;
	} 

	p = pf->page;
	pf->page = (char *)pf->page + size;
	pf->size -= size;
	break;
    }

#ifdef MALLOC_EXTRA_SANITY
    if (p != NULL && page_dir[ptr2idx(p)] != MALLOC_FREE)
	wrterror("(ES): allocated non-free page on free-list.\n");
#endif /* MALLOC_EXTRA_SANITY */

    size >>= xm->malloc_pageshift;

    /* Map new pages */
    if (p == NULL)
	p = map_pages(xm, size);

    if (p != NULL) {

	idx = ptr2idx(p);
	xm->page_dir[idx] = MALLOC_FIRST;
	for (i=1;i<size;i++)
	    xm->page_dir[idx+i] = MALLOC_FOLLOW;

	if (malloc_junk)
	    memset(p, SOME_JUNK, size << xm->malloc_pageshift);
    }

    if (delay_free) {
	if (xm->px == NULL)
	    xm->px = delay_free;
	else
	    __host_free(delay_free);
    }

    return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x10(%rdi), %rax
leaq (%rax,%rsi), %r15
movq %rax, %rdx
notq %rdx
andq %rdx, %r15
cmpq %rsi, %r15
jae 0x2bea
callq 0x1060
movl $0xc, (%rax)
xorl %r12d, %r12d
jmp 0x2eb0
movq %rdi, %rbx
leaq 0x40(%rdi), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x2c1d
movq 0x20(%r14), %rcx
subq %r15, %rcx
jb 0x2bf1
movq 0x10(%r14), %r12
jne 0x2c33
movq (%r14), %rcx
movq 0x8(%r14), %rsi
testq %rcx, %rcx
je 0x2c18
movq %rsi, 0x8(%rcx)
movq %rcx, (%rsi)
jmp 0x2c42
leaq 0x8(%rbx), %r10
movq 0x8(%rbx), %r13
movl %r13d, %ecx
shrq %cl, %r15
xorl %r14d, %r14d
jmp 0x2cc3
leaq (%r12,%r15), %rsi
movq %rsi, 0x10(%r14)
movq %rcx, 0x20(%r14)
xorl %r14d, %r14d
movq 0x8(%rbx), %r13
movl %r13d, %ecx
shrq %cl, %r15
testq %r12, %r12
je 0x2cbf
movq %r12, %rax
movl %r13d, %ecx
shrq %cl, %rax
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rdx
movq %rax, %rsi
subq %rcx, %rsi
movq $0x2, (%rdx,%rsi,8)
shlq $0x3, %rax
shlq $0x3, %rcx
subq %rcx, %rax
pushq $0x1
popq %rcx
cmpq %r15, %rcx
jae 0x2c97
movq 0x30(%rbx), %rdx
addq %rax, %rdx
movq $0x3, (%rdx,%rcx,8)
incq %rcx
jmp 0x2c7e
cmpb $0x1, 0x37f6(%rip)      # 0x6494
jne 0x2e96
movl %r13d, %ecx
shlq %cl, %r15
movq %r12, %rdi
movl $0xd0, %esi
movq %r15, %rdx
callq 0x1130
jmp 0x2e96
leaq 0x8(%rbx), %r10
movq %r15, %rsi
movl %r13d, %ecx
shlq %cl, %rsi
testq %rsi, %rsi
setns %cl
cmpq %r15, %rsi
setae %dil
testb %dil, %cl
jne 0x2cee
callq 0x1060
movl $0xc, (%rax)
jmp 0x2e93
movq 0x80(%rbx), %r11
movq 0x90(%rbx), %rbp
leaq (%rsi,%rbp), %rcx
cmpq %r11, %rcx
jb 0x2e93
movq 0x88(%rbx), %rdi
cmpq %rdi, %rcx
ja 0x2e93
movq %rcx, 0x90(%rbx)
cmpq $-0x1, %rbp
je 0x2e93
leaq (%rax,%rbp), %r12
andq %rdx, %r12
cmpq %r12, %rbp
jae 0x2d6f
movq %r12, %r8
subq %rbp, %r8
addq %rcx, %r8
cmpq %r11, %r8
setb %r9b
cmpq %rdi, %r8
seta %dil
orb %r9b, %dil
jne 0x2d5f
movq %r8, 0x90(%rbx)
cmpq $-0x1, %rcx
jne 0x2d6f
cmpq %rbp, %r11
ja 0x2ece
movq %rbp, 0x90(%rbx)
addq %r12, %rsi
movq %rsi, %rdi
movl %r13d, %ecx
shrq %cl, %rdi
movq 0x20(%rbx), %r8
movq 0x38(%rbx), %r9
movq %r8, %rcx
notq %rcx
movq %rcx, (%rsp)
addq %rdi, %rcx
movq %rcx, 0x28(%rbx)
movq %rsi, 0x70(%rbx)
subq %r8, %rdi
cmpq %r9, %rdi
jb 0x2e4b
movq (%rbx), %r8
movq %r8, %rsi
shrq $0x3, %rsi
movabsq $0x1000000000000000, %rdi # imm = 0x1000000000000000
addq %rsi, %rdi
cmpq %rcx, %rdi
jae 0x2dda
movq %r11, %r15
callq 0x1060
movq %r15, %rdx
movl $0xc, (%rax)
movq (%rsp), %rsi
jmp 0x2e6a
movq %r9, 0x10(%rsp)
movq %r10, (%rsp)
leaq (%rax,%rcx,8), %rax
andq %rdx, %rax
addq %rax, %r8
pushq $0x3
popq %rdx
pushq $0x22
popq %rcx
movq %r8, %rsi
pushq $-0x1
popq %r8
xorl %edi, %edi
movq %rsi, 0x8(%rsp)
xorl %r9d, %r9d
callq 0x10e0
cmpq $-0x1, %rax
je 0x2e58
movq %rax, %r13
movq 0x10(%rsp), %rbp
shlq $0x3, %rbp
movq 0x30(%rbx), %rsi
movq %rax, %rdi
movq %rbp, %rdx
callq 0x1190
movq 0x8(%rsp), %rax
shrq $0x3, %rax
movq %rax, 0x38(%rbx)
movq 0x30(%rbx), %rdi
movq %r13, 0x30(%rbx)
movq %rbp, %rsi
callq 0x11c0
movq (%rsp), %r10
testq %r12, %r12
je 0x2e93
movq (%r10), %r13
jmp 0x2c51
movq 0x8(%rbx), %r13
movq 0x20(%rbx), %rsi
movq 0x80(%rbx), %rdx
notq %rsi
movq %rbp, %rax
movl %r13d, %ecx
shrq %cl, %rax
movq %rbp, 0x70(%rbx)
addq %rsi, %rax
movq %rax, 0x28(%rbx)
cmpq %rbp, %rdx
ja 0x2ec2
cmpq %rbp, 0x88(%rbx)
jb 0x2ec2
movq %rbp, 0x90(%rbx)
xorl %r12d, %r12d
testq %r14, %r14
je 0x2eb0
cmpq $0x0, 0x78(%rbx)
je 0x2eac
movq %r14, %rdi
callq 0x1040
jmp 0x2eb0
movq %r14, 0x78(%rbx)
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15b7(%rip), %rdi      # 0x4480
callq 0x2531
leaq 0x158a(%rip), %rdi      # 0x445f
callq 0x2531
nop