// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %rbx
movl %edx, %ecx
movq %rsi, -0x38(%rbp)
movl 0x20(%rbp), %r10d
movl 0x18(%rbp), %esi
movl %edi, %r15d
testl %r9d, %r9d
je 0x436f1
movl %r9d, %eax
movl $0x1, %r11d
imulq %r15, %r11
decq %rax
jne 0x436e6
jmp 0x436f7
movl $0x1, %r11d
movq %rsp, -0x58(%rbp)
movq %r11, %r14
imulq %r15, %r14
leaq (%rbx,%r14), %rax
decq %rax
cqto
movq %r14, -0x98(%rbp)
idivq %r14
movl %esi, %r14d
leaq 0x8(%r14), %rdx
addl %esi, %r10d
imull %edi, %r10d
movq %rdx, -0x80(%rbp)
imulq %r15, %rdx
addq $0x4, %rdx
addl $0x4, %r10d
cmpl $0x1, %r9d
cmovneq %rdx, %r10
movq %r10, -0xc0(%rbp)
imulq %rax, %rdx
movl %r8d, %eax
addq %rdx, %rax
movq %rax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
xorl %eax, %eax
movb %al, -0x2a(%rbp)
movb %al, -0x29(%rbp)
incl %esi
movq %rsp, %rdx
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %rdx
movq %rdx, %rsp
movq %r14, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
movb %al, (%r14,%rdx)
testq %rbx, %rbx
jle 0x4392c
movq 0x28(%rbp), %r13
leaq -0x1(%rbx), %rax
addq %r11, %rax
movq %rax, -0x78(%rbp)
movslq %ecx, %rax
imulq %r11, %rax
movq %rax, -0xb8(%rbp)
imulq %r15, %rax
movq %rax, -0x60(%rbp)
xorl %r12d, %r12d
movq %rbx, -0x70(%rbp)
movq %r15, -0x68(%rbp)
movq %r11, -0x40(%rbp)
movq -0x78(%rbp), %rax
subq %r12, %rax
cqto
idivq %r11
cmpq %r15, %rax
movq %rax, -0xb0(%rbp)
cmovlq %rax, %r15
movq %r15, -0xa0(%rbp)
movw %r15w, -0x2c(%rbp)
movl $0x1, %edx
movq %r13, %rdi
leaq -0x2a(%rbp), %rsi
callq 0x23138
movl $0x1, %edx
movq %r13, %rdi
leaq -0x29(%rbp), %rsi
callq 0x23138
movl $0x2, %edx
movq %r13, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x23138
movq -0x98(%rbp), %rax
addq %r12, %rax
cmpq %rbx, %rax
movq %rax, -0xa8(%rbp)
cmovlq %rax, %rbx
movl $0x0, %r15d
cmpq %rbx, %r12
movq -0x90(%rbp), %r13
movq -0x88(%rbp), %rdi
jge 0x438cc
xorl %r15d, %r15d
movq -0x38(%rbp), %r14
movq %rbx, -0xc8(%rbp)
movq %r12, -0xd0(%rbp)
movq %rdi, %r12
xorl %esi, %esi
movq %r13, %rdx
callq 0x63b0
movq %r14, %rdi
movq %r12, %rsi
callq *0x10(%rbp)
movq 0x28(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x23138
movl $0x8, %edx
movq %rbx, %rdi
leaq -0x48(%rbp), %rsi
callq 0x23138
movq -0xb8(%rbp), %rsi
movq %r12, %rdi
movq -0xd0(%rbp), %r12
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rbx
addq %rcx, -0x48(%rbp)
incl %r15d
addq -0x40(%rbp), %r12
addq %rsi, %r14
cmpq %rbx, %r12
jl 0x4385a
movq 0x28(%rbp), %r13
movq -0x40(%rbp), %r14
movzwl -0x2c(%rbp), %eax
cmpl %eax, %r15d
jne 0x43943
movq -0x68(%rbp), %r15
cmpq %r15, -0xb0(%rbp)
movq -0x80(%rbp), %r12
jge 0x4390a
movq %r15, %rbx
subq -0xa0(%rbp), %rbx
movq %r13, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x22aa8
decq %rbx
jne 0x438f8
movq -0x60(%rbp), %rax
addq %rax, -0x38(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, %r12
movq -0x70(%rbp), %rbx
cmpq %rbx, %rax
movq %r14, %r11
jl 0x437be
movq -0x58(%rbp), %rsp
movq -0x50(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x51dd1(%rip), %rdi     # 0x9571b
leaq 0x51cf4(%rip), %rsi     # 0x95645
leaq 0x51dde(%rip), %rcx     # 0x95736
movl $0x1eb, %edx            # imm = 0x1EB
callq 0x6460