// File: /workspace/github_cpp_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movl %ecx, %r13d
movq %rsi, %r12
movq %rdi, -0x38(%rbp)
movq 0x90(%rdi), %rdi
movq (%rdi), %rax
movq %rdx, -0x70(%rbp)
callq *0x50(%rax)
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0xae0d
movq -0x38(%rbp), %rax
movq 0x50(%rax), %r14
testq %r14, %r14
je 0xae0d
leaq -0x58(%rbp), %rbx
leaq -0x48(%rbp), %r15
movq 0x8(%r14), %rax
movq %rax, -0x68(%rbp)
movl 0x10(%r14), %eax
movl %eax, -0x60(%rbp)
movl 0x18(%r14), %eax
movl %eax, -0x5c(%rbp)
movq %r15, -0x58(%rbp)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x8052
movq -0x68(%rbp), %rax
cmpq %r12, %rax
ja 0xace2
movl -0x60(%rbp), %ecx
addq %rcx, %rax
cmpq %r12, %rax
ja 0xacfd
movq -0x58(%rbp), %rdi
cmpq %r15, %rdi
je 0xacf0
callq 0x5460
movq (%r14), %r14
testq %r14, %r14
jne 0xaca1
jmp 0xae0d
leaq -0x170(%rbp), %rdi
callq 0x9e18
leaq -0x170(%rbp), %rdi
movl $0x3, 0x20(%rdi)
andl $0x1, %r13d
movl $0xa, %eax
subl %r13d, %eax
movl %eax, 0x24(%rdi)
leaq -0x58(%rbp), %rsi
callq 0x5280
movq -0x70(%rbp), %r14
movq %r12, %rax
subq -0x68(%rbp), %rax
movl -0x5c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq %r14, -0x98(%rbp)
movq %r14, %rdi
callq 0x5030
movq %rax, %rbx
movl $0x18, %edi
callq 0x54a0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x10e80(%rip), %rcx     # 0x1bbf8
movq %rcx, (%rax)
movq %rbx, 0x10(%rax)
movq %rbx, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq %rax, -0xa8(%rbp)
testq %rdi, %rdi
je 0xad9e
callq 0x8af0
leaq -0x170(%rbp), %rbx
movq 0xc0(%rbx), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x53b0
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movq %rbx, %rsi
callq 0xbfa2
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xadd9
callq 0x5460
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0xadea
callq 0x8af0
leaq -0x160(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xadff
callq 0x5460
movq -0x58(%rbp), %rdi
cmpq %r15, %rdi
je 0xae0d
callq 0x5460
movl -0x2c(%rbp), %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0x5540
callq 0x5640
movq %rax, %rbx
callq 0x5710
jmp 0xae51
movq %rax, %rdi
callq 0x8002
movq %rax, %rbx
jmp 0xae5d
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
callq 0xbfde
movq -0x58(%rbp), %rdi
cmpq %r15, %rdi
je 0xae6b
callq 0x5460
movq %rbx, %rdi
callq 0x57c0
nop