// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movl %esi, %r15d
movq %rdi, -0x48(%rbp)
leaq 0xf8(%rdi), %rax
movq %rax, -0x40(%rbp)
leaq -0x50(%rbp), %rbx
pushq $0x8
popq %r12
movl %r15d, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x5600
movq %rax, %r14
testl %r14d, %r14d
js 0xd53f
je 0xd554
movq %r12, %r13
movq -0x50(%rbp), %r12
movq %r12, %r14
bswapq %r14
movq %r14, %rdi
callq 0x5030
movq %rax, %rbx
movq %rax, -0x38(%rbp)
andq $0x0, -0x30(%rbp)
pushq $0x18
popq %rdi
callq 0x54a0
leaq -0x70(%rbp), %rcx
movq %rcx, -0x180(%rbp)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0xc951(%rip), %rcx      # 0x19d30
movq %rcx, (%rax)
movq %rbx, 0x10(%rax)
movq %rax, -0x30(%rbp)
andq $0x0, -0x178(%rbp)
leaq -0x180(%rbp), %rdi
callq 0xd73c
movq -0x38(%rbp), %rax
movq %r12, (%rax)
movq %r13, %r12
movq %r13, %rbx
movq %r14, %rdx
subq %rbx, %rdx
jbe 0xd436
movq -0x38(%rbp), %rsi
addq %rbx, %rsi
movl %r15d, %edi
callq 0x5600
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %rbx
testl %eax, %eax
jg 0xd40b
jmp 0xd532
leaq -0x180(%rbp), %rbx
movq %rbx, %rdi
callq 0x8e2e
leaq -0x70(%rbp), %r13
leaq -0x80(%rbp), %r14
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
callq 0xad3a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8b90
leaq -0x50(%rbp), %rbx
movl %eax, %r14d
leaq -0x78(%rbp), %rdi
callq 0x7dbe
testl %r14d, %r14d
js 0xd559
cmpl $0x6, -0x160(%rbp)
jne 0xd4ca
movq 0xcb33(%rip), %rdi      # 0x19fc0
leaq 0x4e54(%rip), %rsi      # 0x122e8
callq 0x5480
leaq -0x180(%rbp), %r14
movq %rax, %rdi
callq 0x5240
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0xdec0
movq %r13, %rdi
callq 0x7d26
jmp 0xd51c
movq -0x48(%rbp), %rax
movq 0x100(%rax), %rdi
cmpq %rdi, 0xf8(%rax)
leaq -0x180(%rbp), %r14
jne 0xd510
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0xdec0
movq %r13, %rdi
callq 0x7d26
movq -0x48(%rbp), %rax
movq 0x100(%rax), %rdi
addq $-0x18, %rdi
movq %r14, %rsi
callq 0xac5a
movq %r14, %rdi
callq 0x7d8e
leaq -0x30(%rbp), %rdi
callq 0x7dbe
jmp 0xd376
leaq -0x30(%rbp), %rdi
callq 0x7dbe
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
jmp 0xd53f
leaq -0x180(%rbp), %rdi
callq 0x7d8e
leaq -0x30(%rbp), %rdi
callq 0x7dbe
jmp 0xd53f
jmp 0xd572
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x7d26
jmp 0xd593
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x7dbe
jmp 0xd593
jmp 0xd5c8
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
callq 0x7d8e
jmp 0xd5cb
movq %rax, %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0xd752
callq 0x5620
movq %rax, %rbx
callq 0x56f0
jmp 0xd5d4
movq %rax, %rdi
callq 0x7562
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x7dbe
movq %rbx, %rdi
callq 0x5780