// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %ecx, %r12d
movq %rsi, %r13
movq %rdi, -0x38(%rbp)
movq 0x90(%rdi), %rdi
movq (%rdi), %rax
movq %rdx, -0x40(%rbp)
callq *0x50(%rax)
movl %eax, -0x30(%rbp)
testl %eax, %eax
js 0x9d1b
movq -0x38(%rbp), %rax
leaq 0x50(%rax), %r15
leaq -0x60(%rbp), %rbx
leaq -0x70(%rbp), %r14
movq (%r15), %r15
testq %r15, %r15
je 0x9d1b
leaq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0xac98
movq -0x70(%rbp), %rax
cmpq %r13, %rax
ja 0x9c12
movl -0x68(%rbp), %ecx
addq %rcx, %rax
cmpq %r13, %rax
ja 0x9c1c
movq %rbx, %rdi
callq 0x58a8
jmp 0x9be6
leaq -0x190(%rbp), %rdi
callq 0x8e2e
leaq -0x190(%rbp), %rdi
movl $0x3, 0x20(%rdi)
andl $0x1, %r12d
pushq $0xa
popq %rax
subl %r12d, %eax
movl %eax, 0x24(%rdi)
movq %rbx, %rsi
callq 0x52a0
movq -0x40(%rbp), %r15
movq %r13, %rax
subq -0x70(%rbp), %rax
movl -0x64(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq %r15, -0xb8(%rbp)
movq %r15, %rdi
callq 0x5030
movq %rax, %r12
pushq $0x18
popq %rdi
callq 0x54a0
movq %rax, %r14
leaq -0x29(%rbp), %rax
leaq -0x90(%rbp), %rdi
movq %rax, (%rdi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r14)
leaq 0xff3d(%rip), %rax      # 0x19be0
movq %rax, (%r14)
movq %r12, 0x10(%r14)
andq $0x0, 0x8(%rdi)
callq 0xabc4
movq %r12, %rax
leaq -0x190(%rbp), %r12
movups 0xc0(%r12), %xmm0
leaq -0x78(%rbp), %rdi
movups %xmm0, -0x8(%rdi)
movq %rax, 0xc0(%r12)
movq %r14, 0xc8(%r12)
callq 0x7dbe
movq 0xc0(%r12), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x53c0
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movq %r12, %rsi
callq 0xac5a
leaq -0x190(%rbp), %rdi
callq 0x7d8e
movq %rbx, %rdi
callq 0x58a8
movl -0x30(%rbp), %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x51d0
movq %r12, %rdi
callq 0xabda
callq 0x5620
movq %rax, %r14
callq 0x56f0
jmp 0x9d5f
movq %rax, %rdi
callq 0x7562
movq %rax, %r14
jmp 0x9d6b
movq %rax, %r14
leaq -0x190(%rbp), %rdi
callq 0x7d8e
movq %rbx, %rdi
callq 0x58a8
movq %r14, %rdi
callq 0x5780
nop