// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq 0x10(%rdi), %rax
leaq (%rax,%rsi), %r15
movq %rax, %rdx
notq %rdx
andq %rdx, %r15
cmpq %rsi, %r15
jae 0x2d4e
callq 0x1060
movl $0xc, (%rax)
xorl %r12d, %r12d
jmp 0x2fd9
movq %rdi, %rbx
leaq 0x40(%rdi), %r14
xorl %r12d, %r12d
movq (%r14), %r14
testq %r14, %r14
je 0x2d98
movq 0x20(%r14), %rcx
subq %r15, %rcx
jb 0x2d58
jne 0x2d88
movq (%r14), %rcx
movq 0x10(%r14), %r12
testq %rcx, %rcx
je 0x2d7f
movq 0x8(%r14), %rsi
movq %rsi, 0x8(%rcx)
movq 0x8(%r14), %rsi
movq %rcx, (%rsi)
jmp 0x2d9b
movq 0x10(%r14), %r12
leaq (%r12,%r15), %rsi
movq %rsi, 0x10(%r14)
movq %rcx, 0x20(%r14)
xorl %r14d, %r14d
movq 0x8(%rbx), %rcx
shrq %cl, %r15
testq %r12, %r12
jne 0x2f63
movq %r15, %rsi
shlq %cl, %rsi
testq %rsi, %rsi
setns %dil
cmpq %r15, %rsi
setae %r8b
testb %r8b, %dil
jne 0x2dd4
callq 0x1060
movl $0xc, (%rax)
jmp 0x2f60
movq 0x80(%rbx), %rdi
movq 0x90(%rbx), %r8
leaq (%r8,%rsi), %r9
movq $-0x1, %r13
cmpq %rdi, %r9
jb 0x2e05
cmpq 0x88(%rbx), %r9
ja 0x2e05
movq %r9, 0x90(%rbx)
movq %r8, %r13
cmpq $-0x1, %r13
je 0x2f60
leaq (%rax,%r13), %r12
andq %rdx, %r12
cmpq %r12, %r13
jae 0x2e63
movq %r12, %r8
subq %r13, %r8
movq 0x90(%rbx), %r9
addq %r9, %r8
cmpq %rdi, %r8
jb 0x2e46
cmpq 0x88(%rbx), %r8
ja 0x2e46
movq %r8, 0x90(%rbx)
cmpq $-0x1, %r9
jne 0x2e63
cmpq %r13, %rdi
ja 0x2feb
cmpq %r13, 0x88(%rbx)
jb 0x2feb
movq %r13, 0x90(%rbx)
addq %r12, %rsi
movq %rsi, %rdi
shrq %cl, %rdi
movq 0x20(%rbx), %r8
movq 0x38(%rbx), %r9
movq %r8, %rcx
notq %rcx
addq %rdi, %rcx
movq %rcx, 0x28(%rbx)
movq %rsi, 0x70(%rbx)
subq %r8, %rdi
cmpq %r9, %rdi
jb 0x2f63
movq (%rbx), %rbp
movq %rbp, %rsi
shrq $0x3, %rsi
movabsq $0x1000000000000000, %rdi # imm = 0x1000000000000000
addq %rsi, %rdi
cmpq %rcx, %rdi
jae 0x2eba
callq 0x1060
movl $0xc, (%rax)
jmp 0x2f24
movq %r9, (%rsp)
leaq (%rax,%rcx,8), %rax
andq %rdx, %rax
addq %rax, %rbp
xorl %edi, %edi
movq %rbp, %rsi
movl $0x3, %edx
movl $0x22, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x10e0
cmpq $-0x1, %rax
je 0x2f24
movq (%rsp), %r13
shlq $0x3, %r13
movq 0x30(%rbx), %rsi
movq %rax, %rdi
movq %r13, %rdx
movq %rax, (%rsp)
callq 0x1190
shrq $0x3, %rbp
movq %rbp, 0x38(%rbx)
movq 0x30(%rbx), %rdi
movq (%rsp), %rax
movq %rax, 0x30(%rbx)
movq %r13, %rsi
callq 0x11c0
jmp 0x2f63
movq %r13, 0x70(%rbx)
movb 0x8(%rbx), %cl
movq %r13, %rax
shrq %cl, %rax
movq 0x20(%rbx), %rcx
notq %rcx
addq %rax, %rcx
movq %rcx, 0x28(%rbx)
cmpq %r13, 0x80(%rbx)
ja 0x3009
cmpq %r13, 0x88(%rbx)
jb 0x3009
movq %r13, 0x90(%rbx)
xorl %r12d, %r12d
testq %r12, %r12
je 0x2fbf
movq 0x8(%rbx), %rcx
movq %r12, %rax
shrq %cl, %rax
subq 0x20(%rbx), %rax
movq 0x30(%rbx), %rdx
movq $0x2, (%rdx,%rax,8)
cmpq $0x2, %r15
jb 0x2fa3
leaq -0x1(%r15), %rdx
incq %rax
movq 0x30(%rbx), %rsi
movq $0x3, (%rsi,%rax,8)
incq %rax
decq %rdx
jne 0x2f8f
cmpb $0x1, 0x34ea(%rip)      # 0x6494
jne 0x2fbf
shlq %cl, %r15
movq %r12, %rdi
movl $0xd0, %esi
movq %r15, %rdx
callq 0x1130
testq %r14, %r14
je 0x2fd9
cmpq $0x0, 0x78(%rbx)
je 0x2fd5
movq %r14, %rdi
callq 0x1040
jmp 0x2fd9
movq %r14, 0x78(%rbx)
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x34b6(%rip), %rsi      # 0x64a8
leaq 0x12b8(%rip), %rdi      # 0x42b1
leaq 0x12ff(%rip), %rdx      # 0x42ff
leaq 0x1458(%rip), %rcx      # 0x445f
jmp 0x3025
movq 0x3498(%rip), %rsi      # 0x64a8
leaq 0x129a(%rip), %rdi      # 0x42b1
leaq 0x12e1(%rip), %rdx      # 0x42ff
leaq 0x145b(%rip), %rcx      # 0x4480
callq *0x311d(%rip)           # 0x6148
callq 0x1050