// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rcx, -0x38(%rbp)
movq %rdx, %r12
movq %rdi, %rbx
leaq -0x2c(%rbp), %r14
movl %esi, (%r14)
leaq -0x138(%rbp), %r15
movq %r15, %rdi
callq 0x9da2
movl $0x0, 0x24(%r15)
movq 0x90(%rbx), %r15
leaq 0x8(%rbx), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xc7c2
testq %rax, %rax
je 0xa807
addq $0x10, %rax
movq (%r15), %rcx
leaq -0x1c8(%rbp), %rdx
movq %r15, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
movl %eax, %r15d
testl %eax, %eax
js 0xa7bd
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
xorl %edx, %edx
movl $0x1, %ecx
callq *0x30(%rax)
movq %rax, -0x68(%rbp)
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movq %r12, %r14
movq %r12, %rdx
movq -0x38(%rbp), %rcx
callq *0x38(%rax)
movq %rax, %r15
testl %r15d, %r15d
js 0xa7bd
movl $0xf000, %eax           # imm = 0xF000
andl -0x1b0(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
sete -0x80(%rbp)
je 0xa7aa
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x38(%rbp)
movq %rax, -0x60(%rbp)
leaq -0x2c(%rbp), %rsi
movq %r13, %rdi
callq 0xc7c2
testq %rax, %rax
je 0xa813
addq $0x10, %rax
leaq -0x138(%rbp), %rdi
movq %rax, %rsi
callq 0x5280
movq 0x90(%rbx), %r12
leaq -0x2c(%rbp), %rsi
movq %r13, %rdi
callq 0xc7c2
testq %rax, %rax
je 0xa813
addq $0x10, %rax
leaq -0x110(%rbp), %rdx
movq (%r12), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq *0x88(%rcx)
testl %eax, %eax
js 0xa7bd
movq -0x198(%rbp), %rax
xorl %ecx, %ecx
cmpq -0xe0(%rbp), %rax
sete %cl
leal 0x1(,%rcx,2), %eax
movl %eax, -0x118(%rbp)
testl %r15d, %r15d
je 0xa7aa
movq -0x38(%rbp), %rdi
callq 0x5030
movq %rax, %r13
movl $0x18, %edi
callq 0x54a0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x113de(%rip), %rcx     # 0x1bb58
movq %rcx, (%rax)
movq %r13, 0x10(%rax)
movq %r13, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq %rax, -0x70(%rbp)
testq %rdi, %rdi
je 0xa79b
callq 0x8a78
movq -0x78(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq -0x38(%rbp), %rdx
callq 0x53b0
addq $0x78, %rbx
leaq -0x138(%rbp), %rsi
movq %rbx, %rdi
callq 0xbfac
leaq -0x48(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa7cf
callq 0x5460
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0xa7dd
callq 0x8a78
leaq -0x128(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa7f2
callq 0x5460
movl %r15d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa48f(%rip), %rdi      # 0x14c9d
callq 0x5630
leaq 0xa483(%rip), %rdi      # 0x14c9d
callq 0x5630
movq %rax, %rdi
callq 0x51d0
movq %r13, %rdi
callq 0x5540
callq 0x5640
movq %rax, %rbx
callq 0x5710
jmp 0xa84b
movq %rax, %rdi
callq 0x7f7a
jmp 0xa848
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0xbfe8
movq %rbx, %rdi
callq 0x57c0
nop