// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
addq 0x10(%rcx), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
xorq $-0x1, %rcx
andq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x3bb9
callq 0x1060
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x3d85
movq -0x40(%rbp), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x3c8b
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
cmpq -0x18(%rbp), %rax
jae 0x3bf3
jmp 0x3c7b
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x3c46
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x38(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3c2a
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x3c8b
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
addq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x20(%rax)
jmp 0x3c8b
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x3bd5
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
shrq %cl, %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3cb6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x4020
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x3d52
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq %cl, %rax
movq -0x10(%rbp), %rcx
subq 0x20(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x40(%rbp), %rcx
movl $0x2, %edx
movq %rdx, (%rax,%rcx,8)
movq $0x1, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x3d2a
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x40(%rbp), %rcx
addq -0x30(%rbp), %rcx
movl $0x3, %edx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x3cf9
cmpl $0x0, 0x3763(%rip)      # 0x7494
je 0x3d50
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
shlq %cl, %rdx
movl $0xd0, %esi
callq 0x1130
jmp 0x3d52
cmpq $0x0, -0x28(%rbp)
je 0x3d7d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
jne 0x3d72
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
jmp 0x3d7b
movq -0x28(%rbp), %rdi
callq 0x3980
jmp 0x3d7d
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop