// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %r8, %r13
movq %rcx, %r12
movq %rdx, -0x40(%rbp)
movq %rdi, %rbx
leaq -0x2c(%rbp), %r15
movl %esi, (%r15)
leaq -0x140(%rbp), %r14
movq %r14, %rdi
callq 0x9da2
movl $0x0, 0x24(%r14)
movq 0x90(%rbx), %r14
leaq 0x8(%rbx), %rdi
movq %rdi, -0x38(%rbp)
movq %r15, %rsi
callq 0xc7c2
testq %rax, %rax
je 0xaa8e
addq $0x10, %rax
movq (%r14), %rcx
leaq -0x1d0(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0xaa3c
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movq -0x40(%rbp), %rdx
movq %r12, %rcx
movq %r13, %r8
callq *0x40(%rax)
movq %rax, %r12
testl %r12d, %r12d
js 0xaa41
movl $0xf000, %eax           # imm = 0xF000
andl -0x1b8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
sete -0x88(%rbp)
je 0xaa1d
movq %r13, -0x70(%rbp)
movl %r12d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r15, -0x68(%rbp)
leaq -0x2c(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0xc7c2
testq %rax, %rax
je 0xaa9a
addq $0x10, %rax
leaq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x5280
movq 0x90(%rbx), %r14
leaq -0x2c(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0xc7c2
testq %rax, %rax
je 0xaa9a
addq $0x10, %rax
leaq -0x118(%rbp), %rdx
movq (%r14), %rcx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0xaa44
movq -0x1a0(%rbp), %rax
xorl %ecx, %ecx
cmpq -0xe8(%rbp), %rax
sete %cl
leal 0x1(,%rcx,2), %eax
movl %eax, -0x120(%rbp)
testl %r12d, %r12d
je 0xaa1d
movq %r15, %rdi
callq 0x5030
movq %rax, %r13
movl $0x18, %edi
callq 0x54a0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x111bb(%rip), %rcx     # 0x1bba8
movq %rcx, (%rax)
movq %r13, 0x10(%rax)
movq %r13, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq %rax, -0x78(%rbp)
testq %rdi, %rdi
je 0xaa0e
callq 0x8a78
movq -0x80(%rbp), %r13
movq %r13, %rdi
movq -0x40(%rbp), %rsi
movq %r15, %rdx
callq 0x53b0
addq $0x78, %rbx
leaq -0x140(%rbp), %rsi
movq %rbx, %rdi
callq 0xbfac
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, %r15
jmp 0xaa44
movslq %eax, %r15
jmp 0xaa44
movslq %r12d, %r15
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa56
callq 0x5460
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0xaa64
callq 0x8a78
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa79
callq 0x5460
movq %r15, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa208(%rip), %rdi      # 0x14c9d
callq 0x5630
leaq 0xa1fc(%rip), %rdi      # 0x14c9d
callq 0x5630
movq %rax, %rdi
callq 0x51d0
movq %r13, %rdi
callq 0x5540
callq 0x5640
movq %rax, %rbx
callq 0x5710
jmp 0xaad2
movq %rax, %rdi
callq 0x7f7a
jmp 0xaacf
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0xbfe8
movq %rbx, %rdi
callq 0x57c0