// File: SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::merge(std::vector<Pair, std::allocator<Pair>>, std::vector<Pair, std::allocator<Pair>>)
std::vector<Pair> KVStore::merge(std::vector<Pair> a, std::vector<Pair> b) {
    std::vector<Pair> tmp;
    size_t i = 0, j = 0;
    while (i < a.size() && j < b.size()) {
        if (a[i].key < b[j].key)
            tmp.push_back(a[i++]);
        else if (a[i].key > b[j].key)
            tmp.push_back(b[j++]);
        else {
            // two entries with the same key, keeps the one with newer timestamp
            Pair p = a[i].time > b[j].time ? a[i] : b[j];
            tmp.push_back(p);
            i++;
            j++;
        }
    }
    while (i < a.size()) {
        tmp.push_back(a[i++]);
    }
    while (j < b.size()) {
        tmp.push_back(b[j++]);
    }
    // std::clog << "tmp merged: " << std::endl;
    // for (auto i : tmp) {
    //     std::clog << i.key << " " << i.val << std::endl;
    // }
    return tmp;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rcx, -0x98(%rbp)
movq %rdx, -0x90(%rbp)
movq %rdi, -0x88(%rbp)
movq %rdi, %rax
movq %rax, -0x80(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
callq 0xce50
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0xda60
movq -0xa8(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x99(%rbp)
jae 0xb13b
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
callq 0xda60
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
setb %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0xb14a
jmp 0xb300
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xda90
movq -0x98(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rsi
callq 0xda90
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq (%rcx), %rax
jae 0xb1c4
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
incq %rax
movq %rax, -0x30(%rbp)
callq 0xda90
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0xd930
jmp 0xb1ae
jmp 0xb2fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xb3cd
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xda90
movq -0x98(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x38(%rbp), %rsi
callq 0xda90
movq %rax, %rcx
movq -0xc0(%rbp), %rax
cmpq (%rcx), %rax
jbe 0xb22d
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
incq %rax
movq %rax, -0x38(%rbp)
callq 0xda90
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0xd930
jmp 0xb228
jmp 0xb2f9
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xda90
movq -0x98(%rbp), %rdi
movq 0x8(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x38(%rbp), %rsi
callq 0xda90
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq 0x8(%rcx), %rax
jle 0xb281
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xda90
movq %rax, -0xd0(%rbp)
jmp 0xb298
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xda90
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0xdab0
jmp 0xb2aa
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xd930
jmp 0xb2bc
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xcf10
jmp 0xb2f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xcf10
jmp 0xb3cd
jmp 0xb2fb
jmp 0xb0e0
jmp 0xb302
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0xda60
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0xb355
movq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq %rsi, %rax
incq %rax
movq %rax, -0x30(%rbp)
callq 0xda90
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0xd930
jmp 0xb353
jmp 0xb302
jmp 0xb357
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rax
movq %rax, -0xe0(%rbp)
callq 0xda60
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jae 0xb3aa
movq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
incq %rax
movq %rax, -0x38(%rbp)
callq 0xda90
movq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0xd930
jmp 0xb3a8
jmp 0xb357
movb $0x1, -0x21(%rbp)
testb $0x1, -0x21(%rbp)
jne 0xb3c0
movq -0x88(%rbp), %rdi
callq 0xcf30
movq -0x80(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0xcf30
movq -0x40(%rbp), %rdi
callq 0x4530
nopw %cs:(%rax,%rax)