// File: /workspace/github_cpp_makefile_star2_O1/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 0x9e18
movl $0x0, 0x24(%r14)
movq 0x90(%rbx), %r14
leaq 0x8(%rbx), %rdi
movq %rdi, -0x38(%rbp)
movq %r15, %rsi
callq 0xc802
movq (%r14), %rcx
leaq -0x1d0(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0xaa68
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 0xaa6d
movl $0xf000, %eax           # imm = 0xF000
andl -0x1b8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
sete -0x88(%rbp)
je 0xaa49
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 0xc802
leaq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x5280
movq 0x90(%rbx), %r14
leaq -0x2c(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0xc802
leaq -0x118(%rbp), %rdx
movq (%r14), %rcx
movq %r14, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0xaa70
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 0xaa49
movq %r15, %rdi
callq 0x5030
movq %rax, %r13
movl $0x18, %edi
callq 0x54a0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x1118c(%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 0xaa39
callq 0x8af0
movq -0x80(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r15, %rdx
callq 0x53b0
addq $0x78, %rbx
leaq -0x140(%rbp), %rsi
movq %rbx, %rdi
callq 0xbfa2
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r12, %r15
jmp 0xaa70
movslq %eax, %r15
jmp 0xaa70
movslq %r12d, %r15
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa82
callq 0x5460
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0xaa90
callq 0x8af0
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaaa5
callq 0x5460
movq %r15, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x51d0
movq %r13, %rdi
callq 0x5540
callq 0x5640
movq %rax, %rbx
callq 0x5710
jmp 0xaae6
movq %rax, %rdi
callq 0x8002
jmp 0xaae3
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0xbfde
movq %rbx, %rdi
callq 0x57c0