// File: /workspace/github_c_makefile_star2_O0/USArmyResearchLab[P]ARL_OpenSHMEM/src/xmalloc.c
// Function: malloc_make_chunks
static inline int
malloc_make_chunks( struct __xmem_struct* xm, int bits)
{
    struct  pginfo *bp;
    void *pp;
    int i, k;
    long l;

    /* Allocate a new bucket */
    pp = malloc_pages(xm, xm->malloc_pagesize);
    if (pp == NULL)
	return 0;

    /* Find length of admin structure */
    l = (long)offsetof(struct pginfo, bits[0]);
    l += (long)sizeof bp->bits[0] *
	(((xm->malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS);

    /* Don't waste more than two chunks on this */
	/* XXX removed the option of mixing bookkeeping and user allocation -DAR */
	bp = __host_malloc((size_t)l);
	if (bp == NULL) {
	    ifree(xm,pp);
	    return 0;
    }

    bp->size = (1<<bits);
    bp->shift = bits;
    bp->total = bp->free = (u_short)(xm->malloc_pagesize >> bits);
    bp->page = pp;

    /* set all valid bits in the bitmap */
    k = bp->total;
    i = 0;

    /* Do a bunch at a time */
    for(;k-i >= MALLOC_BITS; i += MALLOC_BITS)
	bp->bits[i / MALLOC_BITS] = ~0U;

    for(; i < k; i++)
        bp->bits[i/MALLOC_BITS] |= 1<<(i%MALLOC_BITS);

    if (bp == bp->page) {
	/* Mark the ones we stole for ourselves */
	for(i = 0; l > 0; i++) {
	    bp->bits[i / MALLOC_BITS] &= ~(1 << (i % MALLOC_BITS));
	    bp->free--;
	    bp->total--;
	    l -= (long)(1 << bits);
	}
    }

    /* MALLOC_LOCK */

    xm->page_dir[ptr2idx(pp)] = bp;

    bp->next = xm->page_dir[bits];
    xm->page_dir[bits] = bp;

    /* MALLOC_UNLOCK */

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x3b60
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x3dc6
movl $0x0, -0x4(%rbp)
jmp 0x4017
movq $0x18, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
shrq %cl, %rax
addq $0x20, %rax
subq $0x1, %rax
shrq $0x5, %rax
shlq $0x2, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2cc0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3e20
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x2d20
movl $0x0, -0x4(%rbp)
jmp 0x4017
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x10(%rax)
movl -0x14(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
shrq %cl, %rax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x20(%rbp), %rax
movw %cx, 0x16(%rax)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movzwl 0x16(%rax), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %eax
subl -0x2c(%rbp), %eax
cmpl $0x20, %eax
jl 0x3ebb
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x2c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
movl $0xffffffff, 0x18(%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %eax
addl $0x20, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3e81
jmp 0x3ebd
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x3f0e
movl -0x2c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl -0x4c(%rbp), %edx
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
orl 0x18(%rax,%rcx,4), %edx
movl %edx, 0x18(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3ebd
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x3fbb
movl $0x0, -0x2c(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x3fb9
movl -0x2c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl $0x1, %eax
shll %cl, %eax
xorl $-0x1, %eax
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x2c(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl -0x5c(%rbp), %edx
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
andl 0x18(%rax,%rcx,4), %edx
movl %edx, 0x18(%rax,%rcx,4)
movq -0x20(%rbp), %rax
movw 0x14(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x14(%rax)
movq -0x20(%rbp), %rax
movw 0x16(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x16(%rax)
movl -0x14(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movslq %eax, %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3f27
jmp 0x3fbb
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq %cl, %rsi
movq %rsi, %rcx
movq -0x10(%rbp), %rsi
subq 0x20(%rsi), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq