// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl %edx, %ecx
movq %rsi, %r14
movl 0x20(%rbp), %r10d
movl 0x18(%rbp), %esi
movl %r9d, %eax
movl %edi, %r13d
pushq $0x1
popq %rdx
movq %rdx, -0x38(%rbp)
subq $0x1, %rax
jb 0x3d302
movq -0x38(%rbp), %rdx
imulq %r13, %rdx
jmp 0x3d2ee
movq %rsp, -0x58(%rbp)
movzwl %di, %edi
movq -0x38(%rbp), %r11
movq %r11, %rbx
imulq %r13, %rbx
leaq (%r12,%rbx), %rax
decq %rax
cqto
movq %rbx, -0x88(%rbp)
idivq %rbx
movl %esi, %ebx
leaq 0x8(%rbx), %rdx
movq %rdx, -0x68(%rbp)
imulq %r13, %rdx
addq $0x4, %rdx
addl %esi, %r10d
imull %edi, %r10d
addl $0x4, %r10d
cmpl $0x1, %r9d
cmovneq %rdx, %r10
movq %r10, -0xc0(%rbp)
imulq %rax, %rdx
movl %r8d, %eax
addq %rdx, %rax
leaq -0x48(%rbp), %rdx
movq %rax, -0x50(%rbp)
movq %rax, (%rdx)
xorl %r15d, %r15d
leaq -0x2a(%rbp), %rax
movb %r15b, (%rax)
leaq -0x29(%rbp), %rax
movb %r15b, (%rax)
incl %esi
movq %rsp, %rax
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %rax
movq %rax, %rsp
movq %rbx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
movb %r15b, (%rbx,%rax)
leaq (%r12,%r11), %rax
decq %rax
movq %rax, -0x80(%rbp)
movslq %ecx, %rax
imulq %r11, %rax
movq %rax, -0xa8(%rbp)
imulq %r13, %rax
movq %rax, -0x60(%rbp)
movq %r12, -0x78(%rbp)
movq %r13, -0x70(%rbp)
cmpq %r12, %r15
jge 0x3d509
movq -0x80(%rbp), %rax
subq %r15, %rax
cqto
idivq -0x38(%rbp)
cmpq %r13, %rax
cmovgeq %r13, %rax
movq %rax, -0x98(%rbp)
movw %ax, -0x2c(%rbp)
movq 0x28(%rbp), %r13
movq %r13, %rdi
leaq -0x2a(%rbp), %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1ed3f
movq %r13, %rdi
leaq -0x29(%rbp), %rsi
movq %rbx, %rdx
callq 0x1ed3f
movq %r13, %rdi
leaq -0x2c(%rbp), %rsi
pushq $0x2
popq %rdx
callq 0x1ed3f
movq -0x88(%rbp), %rax
addq %r15, %rax
cmpq %r12, %rax
movq %rax, -0x90(%rbp)
cmovlq %rax, %r12
movq %r12, -0xc8(%rbp)
xorl %ecx, %ecx
movq %r14, -0xa0(%rbp)
cmpq -0xc8(%rbp), %r15
jge 0x3d4bf
movq %r15, %r13
movq -0xb0(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq -0xb8(%rbp), %rbx
movq %rbx, %rdx
movl %ecx, -0x3c(%rbp)
callq 0x63b0
movq %r14, %rdi
movq %r15, %rsi
callq *0x10(%rbp)
movq 0x28(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %r15
movq %rbx, %rdx
callq 0x1ed3f
movq %r12, %rdi
leaq -0x48(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x1ed3f
movl -0x3c(%rbp), %ecx
movq -0xc0(%rbp), %rax
addq %rax, -0x48(%rbp)
incl %ecx
addq -0x38(%rbp), %r15
addq -0xa8(%rbp), %r14
jmp 0x3d44b
movzwl -0x2c(%rbp), %eax
cmpl %ecx, %eax
jne 0x3d520
movq -0x70(%rbp), %r13
movq %r13, %r14
subq -0x98(%rbp), %r14
movq -0x78(%rbp), %r12
movq -0x68(%rbp), %rbx
subq $0x1, %r14
jb 0x3d4f2
movq 0x28(%rbp), %rdi
xorl %esi, %esi
movl %ebx, %edx
callq 0x1e8b6
jmp 0x3d4dd
movq -0xa0(%rbp), %r14
addq -0x60(%rbp), %r14
movq -0x90(%rbp), %r15
jmp 0x3d3c7
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 0x470f4(%rip), %rdi     # 0x8461b
leaq 0x47017(%rip), %rsi     # 0x84545
leaq 0x47101(%rip), %rcx     # 0x84636
movl $0x1eb, %edx            # imm = 0x1EB
callq 0x6460