// File: /workspace/github_c_makefile_star2_O3/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 0x2de0
callq 0x1060
movl $0xc, (%rax)
xorl %r12d, %r12d
jmp 0x309d
movq %rdi, %rbx
leaq 0x40(%rdi), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x2e13
movq 0x20(%r14), %rcx
subq %r15, %rcx
jb 0x2de7
movq 0x10(%r14), %r12
jne 0x2e29
movq (%r14), %rcx
movq 0x8(%r14), %rsi
testq %rcx, %rcx
je 0x2e0e
movq %rsi, 0x8(%rcx)
movq %rcx, (%rsi)
jmp 0x2e38
leaq 0x8(%rbx), %r10
movq 0x8(%rbx), %r13
movl %r13d, %ecx
shrq %cl, %r15
xorl %r14d, %r14d
jmp 0x2ead
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 0x2ea9
movq %r12, %rax
movl %r13d, %ecx
shrq %cl, %rax
movq 0x30(%rbx), %rcx
subq 0x20(%rbx), %rax
movq $0x2, (%rcx,%rax,8)
cmpq $0x2, %r15
jb 0x2e81
leaq -0x1(%r15), %rcx
incq %rax
movq 0x30(%rbx), %rdx
movq $0x3, (%rdx,%rax,8)
incq %rax
decq %rcx
jne 0x2e6d
cmpb $0x1, 0x360c(%rip)      # 0x6494
jne 0x3083
movl %r13d, %ecx
shlq %cl, %r15
movq %r12, %rdi
movl $0xd0, %esi
movq %r15, %rdx
callq 0x1130
jmp 0x3083
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 0x2ed8
callq 0x1060
movl $0xc, (%rax)
jmp 0x3080
movq 0x80(%rbx), %r11
movq 0x90(%rbx), %rbp
leaq (%rsi,%rbp), %rcx
cmpq %r11, %rcx
jb 0x3080
movq 0x88(%rbx), %rdi
cmpq %rdi, %rcx
ja 0x3080
movq %rcx, 0x90(%rbx)
cmpq $-0x1, %rbp
je 0x3080
leaq (%rax,%rbp), %r12
andq %rdx, %r12
cmpq %r12, %rbp
jae 0x2f59
movq %r12, %r8
subq %rbp, %r8
addq %rcx, %r8
cmpq %r11, %r8
setb %r9b
cmpq %rdi, %r8
seta %dil
orb %r9b, %dil
jne 0x2f49
movq %r8, 0x90(%rbx)
cmpq $-0x1, %rcx
jne 0x2f59
cmpq %rbp, %r11
ja 0x30cd
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 0x3038
movq (%rbx), %rsi
movq %rsi, %r8
shrq $0x3, %r8
movabsq $0x1000000000000000, %rdi # imm = 0x1000000000000000
addq %r8, %rdi
cmpq %rcx, %rdi
jae 0x2fc4
movq %r11, %r15
callq 0x1060
movq %r15, %rdx
movl $0xc, (%rax)
movq (%rsp), %rsi
jmp 0x3057
movq %r9, 0x10(%rsp)
movq %r10, (%rsp)
leaq (%rax,%rcx,8), %rax
andq %rdx, %rax
addq %rax, %rsi
xorl %edi, %edi
movq %rsi, 0x8(%rsp)
movl $0x3, %edx
movl $0x22, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x10e0
cmpq $-0x1, %rax
je 0x3045
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 0x3080
movq (%r10), %r13
jmp 0x2e47
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 0x30af
cmpq %rbp, 0x88(%rbx)
jb 0x30af
movq %rbp, 0x90(%rbx)
xorl %r12d, %r12d
testq %r14, %r14
je 0x309d
cmpq $0x0, 0x78(%rbx)
je 0x3099
movq %r14, %rdi
callq 0x1040
jmp 0x309d
movq %r14, 0x78(%rbx)
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x33f2(%rip), %rsi      # 0x64a8
leaq 0x11f4(%rip), %rdi      # 0x42b1
leaq 0x123b(%rip), %rdx      # 0x42ff
leaq 0x13b5(%rip), %rcx      # 0x4480
jmp 0x30e9
movq 0x33d4(%rip), %rsi      # 0x64a8
leaq 0x11d6(%rip), %rdi      # 0x42b1
leaq 0x121d(%rip), %rdx      # 0x42ff
leaq 0x1376(%rip), %rcx      # 0x445f
callq *0x3059(%rip)           # 0x6148
callq 0x1050