// File: williewillus[P]panda_scratchpad/src/wrapper/wrapper.cpp
// Function: wrapper::RecordCmFsOps::CmMsync(void*, unsigned long, int)
int RecordCmFsOps::CmMsync(void *addr, const size_t length, const int flags) {
  const int res = fns_->FnMsync(addr, length, flags);
  if (res < 0) {
    return res;
  }

  // Check which file this belongs to. We need to do a search because they may
  // not have passed the address that was returned in mmap.
  for (const pair<long long, tuple<string, unsigned int, unsigned int>> &kv :
      mmap_map_) {
    if (addr >= (void*) kv.first &&
        addr < (void*) (kv.first + std::get<2>(kv.second))) {
      // This is the mapping you're looking for.
      DiskMod mod;
      mod.mod_type = DiskMod::kDataMod;
      mod.mod_opts = (flags & MS_ASYNC) ?
        DiskMod::kMsAsyncOpt : DiskMod::kMsSyncOpt;
      mod.path = std::get<0>(kv.second);
      // Offset into the file is the offset given in mmap plus the how far addr
      // is from the pointer returned by mmap.
      mod.file_mod_location =
        std::get<1>(kv.second) + ((long long) addr - kv.first);
      mod.file_mod_len = length;

      // Copy over the data that is being sync-ed. We don't know how it is
      // different than what was there to start with, but we'll have it!
      mod.file_mod_data.reset(new char[length], [](char* c) {delete[] c;});
      memcpy(mod.file_mod_data.get(), addr, length);

      mods_.push_back(mod);
      break;
    }
  }

  return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
movq 0x90(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq (%rdi), %rax
callq *0x50(%rax)
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0x10e81
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x110a9
movq -0x198(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x13ff0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x14020
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x14050
testb $0x1, %al
jne 0x10ec0
jmp 0x110a3
leaq -0x38(%rbp), %rdi
callq 0x14080
movq %rax, %rsi
leaq -0x78(%rbp), %rdi
callq 0x140b0
leaq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
cmpq %rcx, %rax
jb 0x11067
movq -0x18(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
movq -0x48(%rbp), %rdi
addq $0x8, %rdi
callq 0x140f0
movq -0x1a8(%rbp), %rcx
movq %rax, %rdx
movq -0x1a0(%rbp), %rax
movl (%rdx), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x11067
leaq -0x178(%rbp), %rdi
callq 0xe650
jmp 0x10f44
movl $0x3, -0x158(%rbp)
movl -0x24(%rbp), %ecx
andl $0x1, %ecx
movl $0xa, %eax
subl %ecx, %eax
movl %eax, -0x154(%rbp)
movq -0x48(%rbp), %rdi
addq $0x8, %rdi
callq 0x14110
movq %rax, %rsi
leaq -0x178(%rbp), %rdi
callq 0x6060
jmp 0x10f7f
movq -0x48(%rbp), %rdi
addq $0x8, %rdi
callq 0x14130
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rdx
movq (%rdx), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x6030
movq %rax, -0x1b0(%rbp)
jmp 0x10fd1
movq -0x1b0(%rbp), %rsi
movq -0x1b8(%rbp), %rdi
callq 0x110d0
jmp 0x10fe6
leaq -0xb8(%rbp), %rdi
callq 0xf430
movq %rax, %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x6450
movq -0x198(%rbp), %rdi
addq $0x78, %rdi
leaq -0x178(%rbp), %rsi
callq 0x13df0
jmp 0x1101b
movl $0x2, -0x18c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0xbf60
jmp 0x11071
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
jmp 0x11098
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
leaq -0x178(%rbp), %rdi
callq 0xbf60
jmp 0x11098
movl $0x0, -0x18c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x14150
movl -0x18c(%rbp), %eax
testl %eax, %eax
je 0x11088
jmp 0x11086
jmp 0x110a3
jmp 0x1108a
leaq -0x38(%rbp), %rdi
callq 0x14170
jmp 0x10eaa
leaq -0x78(%rbp), %rdi
callq 0x14150
jmp 0x110b5
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0x180(%rbp), %rdi
callq 0x68a0
nopw %cs:(%rax,%rax)