// File: williewillus[P]panda_scratchpad/src/utils/Tester.cpp
// Function: Tester::GetChangeData(int)
int Tester::GetChangeData(const int fd) {
  // Need to read a 64-bit value, switch it to big endian to figure out how much
  // we need to read, read that new data amount, and add it all to a buffer.
  while (true) {
    // Get the next DiskMod size.
    uint64_t buf;
    const int read_res = read(fd, (void *) &buf, sizeof(uint64_t));
    if (read_res < 0) {
      return read_res;
    } else if (read_res == 0) {
      // No more data to read.
      break;
    }

    uint64_t next_chunk_size = be64toh(buf);

    // Read the next DiskMod.
    shared_ptr<char> data(new char[next_chunk_size], [](char *c) {delete[] c;});
    memcpy(data.get(), (void *) &buf, sizeof(uint64_t));
    unsigned long long int read_data = sizeof(uint64_t);
    while (read_data < next_chunk_size) {
      const int res = read(fd, data.get() + read_data,
          next_chunk_size - read_data);
      if (res <= 0) {
        // We shouldn't find a size for a DiskMod without the rest of the
        // DiskMod.
        return -1;
      }
      read_data += res;
    }

    DiskMod mod;
    const int res = DiskMod::Deserialize(data, mod);
    if (res < 0) {
      return res;
    }

    if (mod.mod_type == DiskMod::kCheckpointMod) {
      // We found a checkpoint, so switch to a new set of DiskMods.
      cout << "Found a checkpoint" << endl;
      mods_.push_back(vector<DiskMod>());
    } else {
      if (mods_.empty()) {
        // We're just starting, so give us a place to put the mods.
        mods_.push_back(vector<DiskMod>());
      }
      // Just append this DiskMod to the end of the last set of DiskMods.
      mods_.back().push_back(mod);
    }
  }

  return SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movl -0x14(%rbp), %edi
leaq -0x20(%rbp), %rsi
movl $0x8, %edx
callq 0x6700
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1b3e2
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1b712
cmpl $0x0, -0x24(%rbp)
jne 0x1b3ed
jmp 0x1b70b
jmp 0x1b3ef
movq -0x20(%rbp), %rdi
callq 0x1b730
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6030
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x1b7e0
leaq -0x40(%rbp), %rdi
callq 0xf430
movq -0x20(%rbp), %rcx
movq %rcx, (%rax)
movq $0x8, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x1b4af
movl -0x14(%rbp), %eax
movl %eax, -0x1d4(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xf430
movl -0x1d4(%rbp), %edi
movq %rax, %rsi
movq -0x50(%rbp), %rax
addq %rax, %rsi
movq -0x30(%rbp), %rdx
subq %rax, %rdx
callq 0x6700
movq %rax, -0x1d0(%rbp)
jmp 0x1b46a
movq -0x1d0(%rbp), %rax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jg 0x1b49e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x68(%rbp)
jmp 0x1b6db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1b700
movslq -0x54(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x1b429
leaq -0x168(%rbp), %rdi
callq 0xe650
jmp 0x1b4bd
leaq -0x180(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x14410
movq -0x1e0(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0xe260
movl %eax, -0x1d8(%rbp)
jmp 0x1b4ef
leaq -0x180(%rbp), %rdi
callq 0xbfa0
movl -0x1d8(%rbp), %eax
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
jge 0x1b542
movl -0x16c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x68(%rbp)
jmp 0x1b6cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x180(%rbp), %rdi
callq 0xbfa0
jmp 0x1b6f4
cmpl $0x6, -0x148(%rbp)
jne 0x1b610
movq 0x19a6a(%rip), %rdi     # 0x34fc0
leaq 0x9f7c(%rip), %rsi      # 0x254d9
callq 0x6520
movq %rax, -0x1e8(%rbp)
jmp 0x1b56b
movq -0x1e8(%rbp), %rdi
movq 0x19a1f(%rip), %rsi     # 0x34f98
callq 0x6580
jmp 0x1b580
movq -0x1c8(%rbp), %rax
addq $0xf8, %rax
movq %rax, -0x1f8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x1a0(%rbp)
movq $0x0, -0x190(%rbp)
leaq -0x1a0(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x13dd0
movq -0x1f8(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
callq 0x1c790
jmp 0x1b5d1
leaq -0x1a0(%rbp), %rdi
callq 0xbdb0
jmp 0x1b6c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1b6f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0xbdb0
jmp 0x1b6f4
movq -0x1c8(%rbp), %rdi
addq $0xf8, %rdi
callq 0x1c7c0
testb $0x1, %al
jne 0x1b629
jmp 0x1b6a2
movq -0x1c8(%rbp), %rax
addq $0xf8, %rax
movq %rax, -0x208(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x1c0(%rbp)
movq $0x0, -0x1b0(%rbp)
leaq -0x1c0(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x13dd0
movq -0x208(%rbp), %rdi
movq -0x200(%rbp), %rsi
callq 0x1c790
jmp 0x1b67a
leaq -0x1c0(%rbp), %rdi
callq 0xbdb0
jmp 0x1b6a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0xbdb0
jmp 0x1b6f4
movq -0x1c8(%rbp), %rdi
addq $0xf8, %rdi
callq 0x1c800
movq %rax, %rdi
leaq -0x168(%rbp), %rsi
callq 0x13df0
jmp 0x1b6c6
jmp 0x1b6c8
movl $0x0, -0x68(%rbp)
leaq -0x168(%rbp), %rdi
callq 0xbf60
leaq -0x40(%rbp), %rdi
callq 0xbfa0
movl -0x68(%rbp), %eax
testl %eax, %eax
je 0x1b6ef
jmp 0x1b6ed
jmp 0x1b712
jmp 0x1b3bd
leaq -0x168(%rbp), %rdi
callq 0xbf60
leaq -0x40(%rbp), %rdi
callq 0xbfa0
jmp 0x1b71e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x68a0
nopw (%rax,%rax)