// File: ledbit[P]varint/lesqlite2.cpp
// Function: lesqlite2_encode(std::vector<unsigned long, std::allocator<unsigned long>> const&)
vector<uint8_t> lesqlite2_encode(const vector<uint64_t> &in) {
  vector<uint8_t> out;
  for (auto x : in) {
    if (x < cut1) {
      // 1 byte.
      out.push_back(x);
    } else if (x < limit1) {
      // 2 bytes encode 14 bits.
      x -= offset1;
      out.push_back(cut1 + (x >> 8));
      out.push_back(x & 0xff);
    } else if (x < limit2) {
      // 3 bytes encode 19 bits.
      x -= offset2;
      out.push_back(cut2 + (x >> 16));
      // Next 2 bytes in little-endian.
      out.push_back(x & 0xff);
      x >>= 8;
      out.push_back(x & 0xff);
    } else {
      // 4-9 bytes, no offset.
      unsigned bits = 64 - count_leading_zeros_64(x);
      unsigned bytes = (bits + 7) / 8;
      out.push_back(cut3 + (bytes - 3));
      for (unsigned n = 0; n < bytes; n++) {
        out.push_back(x & 0xff);
        x >>= 8;
      }
    }
  }
  return out;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x78(%rbp)
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
callq 0x14a0
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x14c0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x14f0
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1520
testb $0x1, %al
jne 0x249f
jmp 0x262c
leaq -0x28(%rbp), %rdi
callq 0x1560
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0xb2, -0x38(%rbp)
jae 0x24ed
movq -0x78(%rbp), %rdi
movb -0x38(%rbp), %al
movb %al, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
callq 0x1580
jmp 0x24ce
jmp 0x261c
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x15d0
jmp 0x264c
cmpq $0x40b2, -0x38(%rbp)    # imm = 0x40B2
jae 0x2536
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rax
addq $-0xb2, %rax
movq %rax, -0x38(%rbp)
movb -0x37(%rbp), %al
addb $-0x4e, %al
movb %al, -0x4d(%rbp)
leaq -0x4d(%rbp), %rsi
callq 0x1580
jmp 0x251c
movq -0x78(%rbp), %rdi
movb -0x38(%rbp), %al
movb %al, -0x4e(%rbp)
leaq -0x4e(%rbp), %rsi
callq 0x1580
jmp 0x2531
jmp 0x261a
cmpq $0x840b2, -0x38(%rbp)   # imm = 0x840B2
jae 0x259d
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rax
addq $-0x40b2, %rax          # imm = 0xBF4E
movq %rax, -0x38(%rbp)
movb -0x36(%rbp), %al
addb $-0xe, %al
movb %al, -0x4f(%rbp)
leaq -0x4f(%rbp), %rsi
callq 0x1580
jmp 0x2565
movq -0x78(%rbp), %rdi
movb -0x38(%rbp), %al
movb %al, -0x50(%rbp)
leaq -0x50(%rbp), %rsi
callq 0x1580
jmp 0x257a
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rax
shrq $0x8, %rax
movq %rax, -0x38(%rbp)
movb -0x38(%rbp), %al
movb %al, -0x51(%rbp)
leaq -0x51(%rbp), %rsi
callq 0x1580
jmp 0x259b
jmp 0x2618
movq -0x38(%rbp), %rdi
callq 0x2400
movl %eax, -0x7c(%rbp)
jmp 0x25ab
movq -0x78(%rbp), %rdi
movl -0x7c(%rbp), %ecx
movl $0x40, %eax
subl %ecx, %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, -0x5c(%rbp)
movb -0x5c(%rbp), %al
addb $-0x9, %al
movb %al, -0x5d(%rbp)
leaq -0x5d(%rbp), %rsi
callq 0x1580
jmp 0x25db
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x2616
movq -0x78(%rbp), %rdi
movb -0x38(%rbp), %al
movb %al, -0x65(%rbp)
leaq -0x65(%rbp), %rsi
callq 0x1580
jmp 0x25ff
movq -0x38(%rbp), %rax
shrq $0x8, %rax
movq %rax, -0x38(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x25e2
jmp 0x2618
jmp 0x261a
jmp 0x261c
jmp 0x261e
leaq -0x28(%rbp), %rdi
callq 0x15b0
jmp 0x2489
movb $0x1, -0x11(%rbp)
testb $0x1, -0x11(%rbp)
jne 0x263f
movq -0x78(%rbp), %rdi
callq 0x15d0
movq -0x70(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x1180
nopw %cs:(%rax,%rax)