// File: prakhar2808[P]DBEngine/dbengine/sstable.cc
// Function: sstable::writeInFile(std::vector<unsigned char, std::allocator<unsigned char>>&)
void sstable::writeInFile(std::vector<unsigned char>& buffer) {
  // Writing in SSTable.
  for(int i = 0; i < (int)buffer.size(); ++i) {
    writefd << buffer[i];
  }
  
  // Check if this is the first key in the block, if yes then put in index.
  if((no_bytes / FS_BLOCK_SIZE) > total_blocks) {
    // Length of key
    writeIndexfd << buffer[0];
    // The key
    for(int i = 1; i < (int)buffer[0] + 1; ++i) {
      writeIndexfd << buffer[i];
    }
    long long cpy_no_bytes = no_bytes;
    std::vector<unsigned char> offset;
    while(cpy_no_bytes) {
      offset.push_back((unsigned char)(cpy_no_bytes % 10 + '0'));
      cpy_no_bytes /= 10;
    }
    std::reverse(offset.begin(), offset.end());
    if(no_bytes == 0) {
      offset.push_back('0');
    }
    // Length of the offset's string representation
    writeIndexfd << (unsigned char)(offset.size());
    // The offset's string representation
    for(int i = 0; i < (int)offset.size(); ++i) {
      writeIndexfd << offset[i];
    }
    ++total_blocks;
  }
  
  // Updating total number of bytes written in the SSTable.
  no_bytes += (long long)buffer.size();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rdi
callq 0x39770
movq %rax, %rcx
movl -0x74(%rbp), %eax
cmpl %ecx, %eax
jge 0x38a5b
movq -0x70(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x14(%rbp), %rsi
callq 0x39790
movq -0x80(%rbp), %rdi
movzbl (%rax), %esi
callq 0x67a0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x38a12
movq -0x70(%rbp), %rcx
movq 0x458(%rcx), %rax
movl $0x400, %esi            # imm = 0x400
cqto
idivq %rsi
movslq 0x460(%rcx), %rcx
cmpq %rcx, %rax
jle 0x38ca9
movq -0x70(%rbp), %rax
addq $0x258, %rax            # imm = 0x258
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x39790
movq -0x88(%rbp), %rdi
movzbl (%rax), %esi
callq 0x67a0
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x39790
movq %rax, %rcx
movl -0x8c(%rbp), %eax
movzbl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x38b15
movq -0x70(%rbp), %rax
addq $0x258, %rax            # imm = 0x258
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x18(%rbp), %rsi
callq 0x39790
movq -0x98(%rbp), %rdi
movzbl (%rax), %esi
callq 0x67a0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x38ab4
movq -0x70(%rbp), %rax
movq 0x458(%rax), %rax
movq %rax, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x397b0
cmpq $0x0, -0x20(%rbp)
je 0x38baa
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movabsq $0x6666666666666667, %rcx # imm = 0x6666666666666667
imulq %rcx
movq -0xa0(%rbp), %rax
movq %rdx, %rcx
shrq $0x3f, %rcx
shrl $0x2, %edx
addl %ecx, %edx
addl %edx, %edx
movl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
addb $0x30, %al
movb %al, -0x39(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x39(%rbp), %rsi
callq 0x397d0
jmp 0x38b7c
movq -0x20(%rbp), %rax
movl $0xa, %ecx
cqto
idivq %rcx
movq %rax, -0x20(%rbp)
jmp 0x38b2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x398a0
jmp 0x38cd0
leaq -0x38(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x39840
movq -0xa8(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x39870
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x39800
jmp 0x38bdd
movq -0x70(%rbp), %rax
cmpq $0x0, 0x458(%rax)
jne 0x38c00
movb $0x30, -0x61(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x61(%rbp), %rsi
callq 0x397d0
jmp 0x38bfe
jmp 0x38c00
movq -0x70(%rbp), %rax
addq $0x258, %rax            # imm = 0x258
movq %rax, -0xb0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x39770
movq -0xb0(%rbp), %rdi
movzbl %al, %esi
callq 0x67a0
jmp 0x38c2b
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0xb4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x39770
movq %rax, %rcx
movl -0xb4(%rbp), %eax
cmpl %ecx, %eax
jge 0x38c8d
movq -0x70(%rbp), %rax
addq $0x258, %rax            # imm = 0x258
movq %rax, -0xc0(%rbp)
movslq -0x68(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x39790
movq -0xc0(%rbp), %rdi
movzbl (%rax), %esi
callq 0x67a0
jmp 0x38c80
jmp 0x38c82
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x38c32
movq -0x70(%rbp), %rax
movl 0x460(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x460(%rax)
leaq -0x38(%rbp), %rdi
callq 0x398a0
movq -0x10(%rbp), %rdi
callq 0x39770
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq 0x458(%rax), %rcx
movq %rcx, 0x458(%rax)
addq $0xc0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x68c0
nopl (%rax)