// File: /workspace/github_cpp_makefile_star2_O2/williewillus[P]panda_scratchpad/src/wrapper/wrapper.cpp
// Function: wrapper::RecordCmFsOps::CmWrite(int, void const*, unsigned long)
int RecordCmFsOps::CmWrite(const int fd, const void *buf, const size_t count) {
  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;
  }

  mod.file_mod_location = fns_->FnLseek(fd, 0, SEEK_CUR);
  if (mod.file_mod_location < 0) {
    return mod.file_mod_location;
  }

  const int write_res = fns_->FnWrite(fd, buf, count);
  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_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;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rcx, %r12
movq %rdx, -0x38(%rbp)
movq %rdi, %rbx
leaq -0x2c(%rbp), %r14
movl %esi, (%r14)
leaq -0x160(%rbp), %r15
movq %r15, %rdi
callq 0x8e2e
andl $0x0, 0x24(%r15)
movq 0x90(%rbx), %r15
leaq 0x8(%rbx), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xb44e
movq (%r15), %rcx
leaq -0x1f0(%rbp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
movl %eax, %r15d
testl %eax, %eax
js 0x980e
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
callq *0x30(%rax)
movq %rax, -0x90(%rbp)
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movq -0x38(%rbp), %rdx
movq %r12, %rcx
callq *0x38(%rax)
movq %rax, %r15
testl %r15d, %r15d
js 0x980e
movl $0xf000, %eax           # imm = 0xF000
andl -0x1d8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
sete -0xa8(%rbp)
je 0x97fb
movl %r15d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq %r14, -0x88(%rbp)
leaq -0x2c(%rbp), %rsi
movq %r13, %rdi
callq 0xb44e
leaq -0x160(%rbp), %rdi
movq %rax, %rsi
callq 0x52a0
movq 0x90(%rbx), %r12
leaq -0x2c(%rbp), %rsi
movq %r13, %rdi
callq 0xb44e
leaq -0x138(%rbp), %rdx
movq (%r12), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0x980e
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 0x97fb
movq %r14, %rdi
callq 0x5030
movq %rax, %r12
movq %r14, -0x40(%rbp)
pushq $0x18
popq %rdi
callq 0x54a0
movq %rax, %r13
leaq -0x48(%rbp), %r14
leaq -0x2d(%rbp), %rax
leaq -0x60(%rbp), %rdi
movq %rax, (%rdi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r13)
leaq 0x1036d(%rip), %rax     # 0x19b20
movq %rax, (%r13)
movq %r12, 0x10(%r13)
andq $0x0, 0x8(%rdi)
callq 0xaa98
movups -0xa0(%rbp), %xmm0
movups %xmm0, -0x8(%r14)
movq %r12, -0xa0(%rbp)
movq %r13, -0x98(%rbp)
movq %r14, %rdi
callq 0x7dbe
movq -0xa0(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x53c0
addq $0x78, %rbx
leaq -0x160(%rbp), %rsi
movq %rbx, %rdi
callq 0xac5a
leaq -0x160(%rbp), %rdi
callq 0x7d8e
movl %r15d, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x51d0
movq %r12, %rdi
callq 0xaaae
callq 0x5620
movq %rax, %rbx
callq 0x56f0
jmp 0x985b
movq %rax, %rdi
callq 0x7562
jmp 0x9858
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x7d8e
movq %rbx, %rdi
callq 0x5780
nop