// File: /workspace/github_c_makefile_star2_O0/USArmyResearchLab[P]ARL_OpenSHMEM/src/xmalloc.c
// Function: map_pages
static void *
map_pages( struct __xmem_struct* xm, size_t pages)
{
    caddr_t result, rresult, tail;
    intptr_t bytes = pages << xm->malloc_pageshift;

    if (bytes < 0 || (size_t)bytes < pages) {
	errno = ENOMEM;
	return NULL;
    }

    if ((result = __sbrk(xm,bytes)) == (void *)-1)
	return NULL;

    /*
     * Round to a page, in case sbrk(2) did not do this for us
     */
    rresult = (caddr_t)pageround((size_t)(uintptr_t)result);
    if (result < rresult) {
	/* make sure we have enough space to fit bytes */
	if (__sbrk(xm,(intptr_t)(rresult - result)) == (void *) -1) {
	    /* we failed, put everything back */
	    if (__brk(xm,result)) {
		wrterror("dbrk(2) failed [internal error]\n");
	    }
	}
    }
    tail = rresult + (size_t)bytes;

    xm->last_idx = ptr2idx(tail) - 1;
    xm->malloc_brk = tail;

    if ((xm->last_idx+1) >= xm->malloc_ninfo && !extend_pgdir(xm,xm->last_idx)) {
	xm->malloc_brk = result;
	xm->last_idx = ptr2idx(xm->malloc_brk) - 1;
	/* Put back break point since we failed. */
	if (__brk(xm,xm->malloc_brk))
	    wrterror("__brk failed [internal error]\n");
		return 0;
    }

    return rresult;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shlq %cl, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jl 0x4054
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x406c
callq 0x1060
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x41d5
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2c40
movq %rax, -0x20(%rbp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x4096
movq $0x0, -0x8(%rbp)
jmp 0x41d5
movq -0x20(%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, -0x28(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x4101
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rax
subq %rax, %rsi
callq 0x2c40
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x40ff
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3920
cmpl $0x0, %eax
je 0x40fd
leaq 0x1423(%rip), %rdi      # 0x551b
callq 0x2c00
jmp 0x40ff
jmp 0x4101
movq -0x28(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq %cl, %rax
movq %rax, %rcx
movq -0x10(%rbp), %rax
subq 0x20(%rax), %rcx
subq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jb 0x41cd
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x41e0
cmpl $0x0, %eax
jne 0x41cd
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq %cl, %rax
movq %rax, %rcx
movq -0x10(%rbp), %rax
subq 0x20(%rax), %rcx
subq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rsi
callq 0x3920
cmpl $0x0, %eax
je 0x41c3
leaq 0x137e(%rip), %rdi      # 0x553c
callq 0x2c00
movq $0x0, -0x8(%rbp)
jmp 0x41d5
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop