// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/bPlusTree.c
// Function: writeIndexLevel
static bits32 writeIndexLevel(bits16 blockSize, 
	void *itemArray, int itemSize, long itemCount, 
	bits32 indexOffset, int level, 
	void (*fetchKey)(const void *va, char *keyBuf), bits32 keySize, bits32 valSize,
	FILE *f)
/* Write out a non-leaf level. */
{
char *items = itemArray;

/* Calculate number of nodes to write at this level. */
long slotSizePer = xToY(blockSize, level);   // Number of items per slot in node
long nodeSizePer = slotSizePer * blockSize;  // Number of items per node
long nodeCount = (itemCount + nodeSizePer - 1)/nodeSizePer;	


/* Calculate sizes and offsets. */
long bytesInIndexBlock = (bptBlockHeaderSize + blockSize * (keySize+sizeof(bits64)));
long bytesInLeafBlock = (bptBlockHeaderSize + blockSize * (keySize+valSize));
bits64 bytesInNextLevelBlock = (level == 1 ? bytesInLeafBlock : bytesInIndexBlock);
bits64 levelSize = nodeCount * bytesInIndexBlock;
bits64 endLevel = indexOffset + levelSize;
bits64 nextChild = endLevel;


UBYTE isLeaf = FALSE;
UBYTE reserved = 0;

long i,j;
char keyBuf[keySize+1];
keyBuf[keySize] = 0;
for (i=0; i<itemCount; i += nodeSizePer)
    {
    /* Calculate size of this block */
    long countOne = (itemCount - i + slotSizePer - 1)/slotSizePer;
    if (countOne > blockSize)
        countOne = blockSize;
    bits16 shortCountOne = countOne;

    /* Write block header. */
    writeOne(f, isLeaf);
    writeOne(f, reserved);
    writeOne(f, shortCountOne);

    /* Write out the slots that are used one by one, and do sanity check. */
    int slotsUsed = 0;
    long endIx = i + nodeSizePer;
    if (endIx > itemCount)
        endIx = itemCount;
    for (j=i; j<endIx; j += slotSizePer)
        {
	void *item = items + j*itemSize;
	memset(keyBuf, 0, keySize);
	(*fetchKey)(item, keyBuf);
	mustWrite(f, keyBuf, keySize);
	writeOne(f, nextChild);
	nextChild += bytesInNextLevelBlock;
	++slotsUsed;
	}
    assert(slotsUsed == shortCountOne);

    /* Write out empty slots as all zero. */
    int slotSize = keySize + sizeof(bits64);
    for (j=countOne; j<blockSize; ++j)
	repeatCharOut(f, 0, slotSize);
    }
return endLevel;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movw %di, %ax
movq 0x28(%rbp), %rdi
movl 0x20(%rbp), %edi
movl 0x18(%rbp), %edi
movq 0x10(%rbp), %rdi
movw %ax, -0x2(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movzwl -0x2(%rbp), %edi
movl -0x28(%rbp), %esi
callq 0x624e0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movzwl -0x2(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
addq -0x40(%rbp), %rax
subq $0x1, %rax
cqto
idivq -0x40(%rbp)
movq %rax, -0x48(%rbp)
movzwl -0x2(%rbp), %eax
movl 0x18(%rbp), %ecx
addq $0x8, %rcx
imulq %rcx, %rax
addq $0x4, %rax
movq %rax, -0x50(%rbp)
movzwl -0x2(%rbp), %eax
movl 0x18(%rbp), %ecx
addl 0x20(%rbp), %ecx
imull %ecx, %eax
addl $0x4, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
cmpl $0x1, -0x28(%rbp)
jne 0x619c1
movq -0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x619cc
movq -0x50(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x24(%rbp), %eax
movq -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movb $0x0, -0x79(%rbp)
movb $0x0, -0x7a(%rbp)
movl 0x18(%rbp), %eax
incl %eax
movl %eax, %ecx
movq %rsp, %rax
movq %rax, -0x98(%rbp)
movq %rcx, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, -0xd8(%rbp)
movq %rax, %rsp
movq %rcx, -0xa0(%rbp)
movl 0x18(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x61c6b
movq -0x20(%rbp), %rax
subq -0x88(%rbp), %rax
addq -0x38(%rbp), %rax
subq $0x1, %rax
cqto
idivq -0x38(%rbp)
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movzwl -0x2(%rbp), %ecx
cmpq %rcx, %rax
jle 0x61a96
movzwl -0x2(%rbp), %eax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movw %ax, -0xaa(%rbp)
movq 0x28(%rbp), %rdi
leaq -0x79(%rbp), %rsi
movl $0x1, %edx
callq 0x2f8b0
movq 0x28(%rbp), %rdi
leaq -0x7a(%rbp), %rsi
movl $0x1, %edx
callq 0x2f8b0
movq 0x28(%rbp), %rdi
leaq -0xaa(%rbp), %rsi
movl $0x2, %edx
callq 0x2f8b0
movl $0x0, -0xb0(%rbp)
movq -0x88(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0x20(%rbp), %rax
jle 0x61b11
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jge 0x61bd0
movq -0xd8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq -0x90(%rbp), %rcx
movslq -0x14(%rbp), %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movl 0x18(%rbp), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x63a0
movq -0xd8(%rbp), %rsi
movq 0x10(%rbp), %rax
movq -0xc0(%rbp), %rdi
callq *%rax
movq -0xd8(%rbp), %rsi
movq 0x28(%rbp), %rdi
movl 0x18(%rbp), %eax
movl %eax, %edx
callq 0x2f8b0
movq 0x28(%rbp), %rdi
leaq -0x78(%rbp), %rsi
movl $0x8, %edx
callq 0x2f8b0
movq -0x60(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x61b1f
movl -0xb0(%rbp), %eax
movzwl -0xaa(%rbp), %ecx
cmpl %ecx, %eax
jne 0x61be3
jmp 0x61c02
leaq 0x7d982(%rip), %rdi     # 0xdf56c
leaq 0x7d8a5(%rip), %rsi     # 0xdf496
movl $0x1eb, %edx            # imm = 0x1EB
leaq 0x7d98a(%rip), %rcx     # 0xdf587
callq 0x6450
movl 0x18(%rbp), %eax
addq $0x8, %rax
movl %eax, -0xc4(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movzwl -0x2(%rbp), %ecx
cmpq %rcx, %rax
jge 0x61c52
movq 0x28(%rbp), %rdi
movl -0xc4(%rbp), %edx
xorl %esi, %esi
callq 0x2efd0
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x61c1d
jmp 0x61c54
movq -0x40(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x61a4a
movq -0x70(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop