// File: /workspace/github_cpp_makefile_star2_O3/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, %r14d
movq %rdi, -0x48(%rbp)
leaq 0xf8(%rdi), %rax
movq %rax, -0x40(%rbp)
leaq -0x50(%rbp), %rsi
leaq -0x180(%rbp), %r15
movl $0x8, %edx
movl %r14d, %edi
callq 0x5620
testl %eax, %eax
js 0xfc78
je 0xfc7d
movq %r15, %rbx
movq -0x50(%rbp), %r15
movq %r15, %r12
bswapq %r12
movq %r12, %rdi
callq 0x5030
movq %rax, %r13
movl $0x18, %edi
callq 0x54a0
movq %rax, %rcx
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rcx)
leaq 0xc298(%rip), %rax      # 0x1bd38
movq %rax, (%rcx)
movq %rcx, -0x38(%rbp)
movq %r13, 0x10(%rcx)
movq %r15, (%r13)
movl $0x8, %r15d
movq %r12, %rdx
subq %r15, %rdx
jbe 0xfadd
leaq (%r15,%r13), %rsi
movl %r14d, %edi
callq 0x5620
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, %r15
testl %eax, %eax
jg 0xfab5
jmp 0xfc53
movq %rbx, %r15
movq %rbx, %rdi
callq 0x9da2
movq %r13, -0x60(%rbp)
movq -0x38(%rbp), %rbx
movq %rbx, -0x58(%rbp)
movq 0xc4c5(%rip), %rax      # 0x1bfc0
cmpb $0x0, (%rax)
leaq -0x80(%rbp), %r12
je 0xfb09
incl 0x8(%rbx)
jmp 0xfb0d
lock
incl 0x8(%rbx)
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x9b20
movl %eax, %r13d
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0xfb2a
callq 0x8a78
testl %r13d, %r13d
js 0xfba7
cmpl $0x6, -0x160(%rbp)
jne 0xfbad
movl $0x12, %edx
movq 0xc484(%rip), %rdi      # 0x1bfc8
leaq 0x57cd(%rip), %rsi      # 0x15318
callq 0x54f0
movq 0xc471(%rip), %rcx      # 0x1bfc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x5470
movsbl %al, %esi
movq 0xc453(%rip), %rdi      # 0x1bfc8
callq 0x5040
movq %rax, %rdi
callq 0x5300
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x1047a
movq %r12, %rdi
callq 0x89e6
jmp 0xfbfb
movl %r13d, -0x2c(%rbp)
jmp 0xfbfb
movq -0x48(%rbp), %rax
movq 0x100(%rax), %rdi
cmpq %rdi, 0xf8(%rax)
jne 0xfbef
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x40(%rbp), %rdi
movq %r12, %rsi
callq 0x1047a
movq %r12, %rdi
callq 0x89e6
movq -0x48(%rbp), %rax
movq 0x100(%rax), %rdi
addq $-0x18, %rdi
movq %r15, %rsi
callq 0xbfac
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0xfc13
callq 0x5460
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0xfc24
callq 0x8a78
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
cmpq %rax, %rdi
je 0xfc3c
callq 0x5460
movq %rbx, %rdi
callq 0x8a78
testl %r13d, %r13d
leaq -0x50(%rbp), %rsi
jns 0xfa4b
jmp 0xfc63
movq -0x38(%rbp), %rdi
callq 0x8a78
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, -0x2c(%rbp)
jmp 0xfc63
movl $0x0, -0x2c(%rbp)
jmp 0xfc63
jmp 0xfc88
movq %rax, %r14
leaq -0x80(%rbp), %rdi
callq 0x89e6
jmp 0xfcae
movq %rax, %r14
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0xfcae
callq 0x8a78
jmp 0xfcae
jmp 0xfce3
movq %rax, %r14
leaq -0x180(%rbp), %rdi
callq 0xbfe8
jmp 0xfce6
movq %rax, %rdi
callq 0x51d0
movq %r13, %rdi
callq 0x5540
callq 0x5640
movq %rax, %r14
callq 0x5710
jmp 0xfcef
movq %rax, %rdi
callq 0x7f7a
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x8a78
movq %r14, %rdi
callq 0x57c0
nop