// File: /workspace/github_cpp_makefile_star2_O2/williewillus[P]panda_scratchpad/src/wrapper/wrapper.cpp
// Function: wrapper::RecordCmFsOps::CmPwrite(int, void const*, unsigned long, long)
ssize_t RecordCmFsOps::CmPwrite(const int fd, const void *buf,
    const size_t count, const off_t offset) {
  DiskMod mod;
  mod.mod_opts = DiskMod::kNoneOpt;
  // Get current file position and size. If stat fails, then assume lseek will
  // fail too and just bail out.
  struct stat pre_stat_buf;
  // This could be an fstat(), but I don't see a reason to add another call that
  // does only reads to the already large interface of FsFns.
  int res = fns_->FnStat(fd_map_.at(fd), &pre_stat_buf);
  if (res < 0) {
    return res;
  }

  const int write_res = fns_->FnPwrite(fd, buf, count, offset);
  if (write_res < 0) {
    return write_res;
  }

  mod.directory_mod = S_ISDIR(pre_stat_buf.st_mode);

  // TODO(ashmrtn): Support calling write directly on a directory.
  if (!mod.directory_mod) {
    // Copy over as much data as was written and see what the new file size is.
    // This will determine how we set the type of the DiskMod.
    mod.file_mod_location = offset;
    mod.file_mod_len = write_res;
    mod.path = fd_map_.at(fd);

    res = fns_->FnStat(fd_map_.at(fd), &mod.post_mod_stats);
    if (res < 0) {
      return write_res;
    }

    if (pre_stat_buf.st_size != mod.post_mod_stats.st_size) {
      mod.mod_type = DiskMod::kDataMetadataMod;
    } else {
      mod.mod_type = DiskMod::kDataMod;
    }

    if (write_res > 0) {
      mod.file_mod_data.reset(new char[write_res], [](char* c) {delete[] c;});
      memcpy(mod.file_mod_data.get(), buf, write_res);
    }
  }

  mods_.push_back(mod);

  return write_res;
  return fns_->FnPwrite(fd, buf, count, offset);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %r8, %r13
movq %rcx, %r15
movq %rdx, -0x40(%rbp)
movq %rdi, %rbx
leaq -0x2c(%rbp), %r12
movl %esi, (%r12)
leaq -0x160(%rbp), %r14
movq %r14, %rdi
callq 0x8e2e
andl $0x0, 0x24(%r14)
movq 0x90(%rbx), %r14
leaq 0x8(%rbx), %rdi
movq %rdi, -0x38(%rbp)
movq %r12, %rsi
callq 0xb44e
movq (%r14), %rcx
leaq -0x1f0(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0x9a4c
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movq -0x40(%rbp), %rdx
movq %r15, %rcx
movq %r13, %r8
callq *0x40(%rax)
movq %rax, %r15
testl %r15d, %r15d
js 0x9a51
movl $0xf000, %eax           # imm = 0xF000
andl -0x1d8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
sete -0xa8(%rbp)
je 0x9a2d
movq %r13, -0x90(%rbp)
movl %r15d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, -0x88(%rbp)
leaq -0x2c(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0xb44e
leaq -0x160(%rbp), %rdi
movq %rax, %rsi
callq 0x52a0
movq 0x90(%rbx), %r14
leaq -0x2c(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0xb44e
leaq -0x138(%rbp), %rdx
movq (%r14), %rcx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0x9a54
movq -0x1c0(%rbp), %rax
xorl %ecx, %ecx
cmpq -0x108(%rbp), %rax
sete %cl
leal 0x1(,%rcx,2), %eax
movl %eax, -0x140(%rbp)
testl %r15d, %r15d
je 0x9a2d
movq %r12, %rdi
callq 0x5030
movq %rax, %r14
pushq $0x18
popq %rdi
callq 0x54a0
movq %rax, %r13
leaq -0x2d(%rbp), %rax
leaq -0x60(%rbp), %rdi
movq %rax, (%rdi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r13)
leaq 0x1019a(%rip), %rax     # 0x19b80
movq %rax, (%r13)
movq %r14, 0x10(%r13)
andq $0x0, 0x8(%rdi)
callq 0xab2e
movups -0xa0(%rbp), %xmm0
leaq -0x48(%rbp), %rdi
movups %xmm0, -0x8(%rdi)
movq %r14, -0xa0(%rbp)
movq %r13, -0x98(%rbp)
callq 0x7dbe
movq -0xa0(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rdx
callq 0x53c0
addq $0x78, %rbx
leaq -0x160(%rbp), %rsi
movq %rbx, %rdi
callq 0xac5a
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r15, %r12
jmp 0x9a54
movslq %eax, %r12
jmp 0x9a54
movslq %r15d, %r12
leaq -0x160(%rbp), %rdi
callq 0x7d8e
movq %r12, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x51d0
movq %r14, %rdi
callq 0xab44
callq 0x5620
movq %rax, %rbx
callq 0x56f0
jmp 0x9aa1
movq %rax, %rdi
callq 0x7562
jmp 0x9a9e
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x7d8e
movq %rbx, %rdi
callq 0x5780
nop