// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %r13
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
movl %ecx, %edx
subl %eax, %edx
testl %edx, %edx
jle 0x17b21
leaq 0x58(%r13), %r15
xorl %ebx, %ebx
leaq 0x10(%rsp), %r12
movb (%rax,%rbx), %al
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5430
incq %rbx
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl %ecx, %edx
subl %eax, %edx
movslq %edx, %rdx
cmpq %rdx, %rbx
jl 0x17af4
movq 0x458(%r13), %rdx
leaq 0x3ff(%rdx), %rsi
testq %rdx, %rdx
cmovnsq %rdx, %rsi
sarq $0xa, %rsi
movslq 0x460(%r13), %rdi
cmpq %rdi, %rsi
jle 0x17d00
leaq 0x258(%r13), %r15
movb (%rax), %al
leaq 0x10(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %r15, %rdi
callq 0x5430
movq (%r14), %rax
cmpb $0x0, (%rax)
je 0x17b9e
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbx
movb 0x1(%rax,%r12), %al
movb %al, 0x10(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5430
movq (%r14), %rax
movzbl (%rax), %ecx
incq %r12
cmpq %rcx, %r12
jb 0x17b77
movq 0x458(%r13), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testq %rbx, %rbx
je 0x17c5f
movq %r13, 0x30(%rsp)
leaq 0x10(%rsp), %r13
leaq 0xf(%rsp), %rbp
movq %rbx, %rax
movabsq $0x6666666666666667, %rcx # imm = 0x6666666666666667
imulq %rcx
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x2, %r12
addq %rax, %r12
leal (%r12,%r12), %eax
leal (%rax,%rax,4), %eax
movl %ebx, %ecx
subl %eax, %ecx
addb $0x30, %cl
movb %cl, 0xf(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x186bc
addq $0x9, %rbx
cmpq $0x13, %rbx
movq %r12, %rbx
jae 0x17bce
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
setne %dl
decq %rcx
cmpq %rax, %rcx
seta %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0x30(%rsp), %r13
jne 0x17c5f
incq %rax
movb -0x1(%rax), %dl
movb (%rcx), %sil
movb %sil, -0x1(%rax)
movb %dl, (%rcx)
decq %rcx
leaq 0x1(%rax), %rdx
cmpq %rcx, %rax
movq %rdx, %rax
jb 0x17c44
cmpq $0x0, 0x458(%r13)
jne 0x17c7b
leaq 0xf(%rsp), %rsi
movb $0x30, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x186bc
movl 0x18(%rsp), %eax
subl 0x10(%rsp), %eax
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %r15, %rdi
callq 0x5430
movl 0x18(%rsp), %eax
movq 0x10(%rsp), %rdi
subl %edi, %eax
testl %eax, %eax
jle 0x17cd9
xorl %ebx, %ebx
leaq 0xf(%rsp), %r12
movb (%rdi,%rbx), %al
movb %al, 0xf(%rsp)
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5430
incq %rbx
movl 0x18(%rsp), %eax
movq 0x10(%rsp), %rdi
subl %edi, %eax
cltq
cmpq %rax, %rbx
jl 0x17cad
incl 0x460(%r13)
testq %rdi, %rdi
je 0x17cf2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5390
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq 0x458(%r13), %rdx
subq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x458(%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x17d22
jmp 0x17d22
jmp 0x17d22
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x17d3c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x5670