// File: /workspace/github_c_makefile_star2_O0/USArmyResearchLab[P]ARL_OpenSHMEM/src/xmalloc.c
// Function: irealloc
static void *
irealloc( struct __xmem_struct* xm, void *ptr, size_t size)
{
    void *p;
    size_t osize, idx;
    struct pginfo **mp;
    size_t i;

    idx = ptr2idx(ptr);

    if (idx < xm->malloc_pageshift) {
	wrtwarning("junk pointer, too low to make sense.\n");
	return 0;
    }

    if (idx > xm->last_idx) {
	wrtwarning("junk pointer, too high to make sense.\n");
	return 0;
    }

    mp = &xm->page_dir[idx];

    if (*mp == MALLOC_FIRST) {			/* Page allocation */

	/* Check the pointer */
	if ((size_t)(uintptr_t)ptr & xm->malloc_pagemask) {
	    wrtwarning("modified (page-) pointer.\n");
	    return NULL;
	}

	/* Find the size in bytes */
	for (osize = xm->malloc_pagesize; *++mp == MALLOC_FOLLOW;)
	    osize += xm->malloc_pagesize;

        if (!xm->malloc_realloc && 			/* unless we have to, */
	  size <= osize && 			/* .. or are too small, */
	  size > (osize - xm->malloc_pagesize)) {	/* .. or can free a page, */
	    if (malloc_junk)
		memset((u_char *)ptr + size, SOME_JUNK, osize-size);
	    return ptr;				/* don't do anything. */
	}

    } else if (*mp >= MALLOC_MAGIC) {		/* Chunk allocation */

	/* Check the pointer for sane values */
	if (((size_t)(uintptr_t)ptr & ((*mp)->size-1))) {
	    wrtwarning("modified (chunk-) pointer.\n");
	    return NULL;
	}

	/* Find the chunk index in the page */
	i = ((size_t)(uintptr_t)ptr & xm->malloc_pagemask) >> (*mp)->shift;

	/* Verify that it isn't a free chunk already */
        if ((*mp)->bits[i/MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
	    wrtwarning("chunk is already free.\n");
	    return NULL;
	}

	osize = (*mp)->size;

	if (!xm->malloc_realloc &&		/* Unless we have to, */
	  size <= osize && 		/* ..or are too small, */
	  (size > osize / 2 ||	 	/* ..or could use a smaller size, */
	  osize == malloc_minsize)) {	/* ..(if there is one) */
	    if (malloc_junk)
		memset((u_char *)ptr + size, SOME_JUNK, osize-size);
	    return ptr;			/* ..Don't do anything */
	}

    } else {
	wrtwarning("pointer to wrong page.\n");
	return NULL;
    }

    p = imalloc(xm,size);

    if (p != NULL) {
	/* copy the lesser of the two sizes, and free the old one */
	if (!size || !osize)
	    ;
	else if (osize < size)
	    memcpy(p, ptr, osize);
	else
	    memcpy(p, ptr, size);
	ifree(xm,ptr);
    } 
    return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq %cl, %rax
movq -0x10(%rbp), %rcx
subq 0x20(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2f16
leaq 0x24ff(%rip), %rdi      # 0x5403
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jbe 0x2f3d
leaq 0x24fe(%rip), %rdi      # 0x5429
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x2, %ecx
cmpq %rcx, (%rax)
jne 0x3024
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
andq 0x10(%rcx), %rax
cmpq $0x0, %rax
je 0x2f91
leaq 0x2500(%rip), %rdi      # 0x547f
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x40(%rbp)
movl $0x3, %ecx
cmpq %rcx, 0x8(%rax)
jne 0x2fc7
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x2f9c
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x301f
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x301f
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rdx
subq (%rdx), %rcx
cmpq %rcx, %rax
jbe 0x301f
cmpl $0x0, 0x449e(%rip)      # 0x7494
je 0x3012
movq -0x18(%rbp), %rdi
addq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rdx
subq -0x20(%rbp), %rdx
movl $0xd0, %esi
callq 0x1130
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x31c6
jmp 0x3159
movq -0x40(%rbp), %rax
movl $0x4, %ecx
cmpq %rcx, (%rax)
jb 0x3141
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rcx), %rcx
movzwl 0x10(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x306d
leaq 0x2457(%rip), %rdi      # 0x54b2
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
andq 0x10(%rcx), %rax
movq -0x40(%rbp), %rcx
movq (%rcx), %rcx
movzwl 0x12(%rcx), %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
shrq $0x5, %rcx
movl 0x18(%rax,%rcx,4), %eax
movq -0x48(%rbp), %rcx
andq $0x1f, %rcx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x30d5
leaq 0x240b(%rip), %rdi      # 0x54ce
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
movq -0x40(%rbp), %rax
movq (%rax), %rax
movzwl 0x10(%rax), %eax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x313f
movq -0x20(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x313f
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
ja 0x310f
cmpq $0x10, -0x30(%rbp)
jne 0x313f
cmpl $0x0, 0x437e(%rip)      # 0x7494
je 0x3132
movq -0x18(%rbp), %rdi
addq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rdx
subq -0x20(%rbp), %rdx
movl $0xd0, %esi
callq 0x1130
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x31c6
jmp 0x3157
leaq 0x231f(%rip), %rdi      # 0x5467
callq 0x2ce0
movq $0x0, -0x8(%rbp)
jmp 0x31c6
jmp 0x3159
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2df0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x31be
cmpq $0x0, -0x20(%rbp)
je 0x317f
cmpq $0x0, -0x30(%rbp)
jne 0x3181
jmp 0x31b1
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x319e
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1190
jmp 0x31af
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x1190
jmp 0x31b1
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2d20
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq