// File: /workspace/github_cpp_makefile_star2_O2/williewillus[P]panda_scratchpad/src/wrapper/wrapper.cpp
// Function: wrapper::RecordCmFsOps::CmFallocate(int, int, long, long)
int RecordCmFsOps::CmFallocate(const int fd, const int mode, const off_t offset,
    off_t len) {
  struct stat pre_stat;
  const int pre_stat_res = fns_->FnStat(fd_map_[fd].c_str(), &pre_stat);
  if (pre_stat_res < 0) {
    return pre_stat_res;
  }

  const int res = fns_->FnFallocate(fd, mode, offset, len);
  if (res < 0) {
    return res;
  }

  struct stat post_stat;
  const int post_stat_res = fns_->FnStat(fd_map_[fd].c_str(), &post_stat);
  if (post_stat_res < 0) {
    return post_stat_res;
  }

  DiskMod mod;

  if (pre_stat.st_size != post_stat.st_size ||
      pre_stat.st_blocks != post_stat.st_blocks) {
    mod.mod_type = DiskMod::kDataMetadataMod;
  } else {
    mod.mod_type = DiskMod::kDataMod;
  }

  mod.path = fd_map_[fd];
  mod.file_mod_location = offset;
  mod.file_mod_len = len;

  if (mode & FALLOC_FL_PUNCH_HOLE) {
    // TODO(ashmrtn): Do we want this here? I'm not sure if it will cause
    // failures that we don't want, though man fallocate(2) says that
    // FALLOC_FL_PUNCH_HOLE must also have FALLOC_FL_KEEP_SIZE.
    assert(mode & FALLOC_FL_KEEP_SIZE);
    mod.mod_opts = DiskMod::kPunchHoleKeepSizeOpt;
  } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
    mod.mod_opts = DiskMod::kCollapseRangeOpt;
  } else if (mode & FALLOC_FL_ZERO_RANGE) {
    if (mode & FALLOC_FL_KEEP_SIZE) {
      mod.mod_opts = DiskMod::kZeroRangeKeepSizeOpt;
    } else {
      mod.mod_opts = DiskMod::kZeroRangeOpt;
    }
    /*
  } else if (mode & FALLOC_FL_INSERT_RANGE) {
    // TODO(ashmrtn): Figure out how to check with glibc defines.
    mod.mod_opts = DiskMod::kInsertRangeOpt;
    */
  } else if (mode & FALLOC_FL_KEEP_SIZE) {
    mod.mod_opts = DiskMod::kFallocateKeepSizeOpt;
  } else {
    mod.mod_opts = DiskMod::kFallocateOpt;
  }

  mods_.push_back(mod);

  return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %r8, %r13
movq %rcx, -0x38(%rbp)
movl %edx, %r15d
movq %rdi, %rbx
leaq -0x2c(%rbp), %rax
movl %esi, (%rax)
movq 0x90(%rdi), %r14
leaq 0x8(%rdi), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xb960
movq (%rax), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x1d0(%rbp), %rdx
callq 0x51c0
movq (%r14), %rax
leaq -0x140(%rbp), %rsi
leaq -0x260(%rbp), %rdx
movq %r14, %rdi
callq *0x88(%rax)
movl %eax, %r14d
leaq -0x140(%rbp), %rdi
callq 0x58a8
testl %r14d, %r14d
js 0x9f96
movq 0x90(%rbx), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movl %r15d, %edx
movq -0x38(%rbp), %rcx
movq %r13, %r8
callq *0x60(%rax)
movl %eax, %r14d
testl %eax, %eax
js 0x9f96
movq %r13, -0x40(%rbp)
movq 0x90(%rbx), %r13
leaq -0x2c(%rbp), %rsi
movq %r12, %rdi
callq 0xb960
movq (%rax), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x2d(%rbp), %rdx
callq 0x51c0
movq (%r13), %rax
leaq -0x140(%rbp), %rsi
leaq -0x1d0(%rbp), %rdx
movq %r13, %rdi
callq *0x88(%rax)
movl %eax, %r13d
leaq -0x140(%rbp), %rdi
callq 0x58a8
testl %r13d, %r13d
js 0x9f56
leaq -0x140(%rbp), %r13
movq %r13, %rdi
callq 0x8e2e
movq -0x230(%rbp), %rax
xorq -0x1a0(%rbp), %rax
movq -0x190(%rbp), %rcx
xorq -0x220(%rbp), %rcx
xorl %edx, %edx
orq %rax, %rcx
sete %dl
leal 0x1(,%rdx,2), %eax
movl %eax, 0x20(%r13)
leaq -0x2c(%rbp), %rsi
movq %r12, %rdi
callq 0xb960
leaq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x52a0
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
testb $0x2, %r15b
jne 0x9f5b
testb $0x8, %r15b
jne 0x9f6d
testb $0x10, %r15b
jne 0x9fab
testb $0x1, %r15b
jne 0x9fbd
movl $0x2, -0x11c(%rbp)
jmp 0x9f77
movl %r13d, %r14d
jmp 0x9f96
testb $0x1, %r15b
je 0x9fd5
movl $0x4, -0x11c(%rbp)
jmp 0x9f77
movl $0x5, -0x11c(%rbp)
addq $0x78, %rbx
leaq -0x140(%rbp), %rsi
movq %rbx, %rdi
callq 0xac5a
leaq -0x140(%rbp), %rdi
callq 0x7d8e
movl %r14d, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %r15b
jne 0x9fc9
movl $0x6, -0x11c(%rbp)
jmp 0x9f77
movl $0x3, -0x11c(%rbp)
jmp 0x9f77
movl $0x7, -0x11c(%rbp)
jmp 0x9f77
leaq 0x7bee(%rip), %rdi      # 0x11bca
leaq 0x7c02(%rip), %rsi      # 0x11be5
leaq 0x7c0f(%rip), %rcx      # 0x11bf9
movl $0x1be, %edx            # imm = 0x1BE
callq 0x5310
jmp 0xa009
jmp 0xa01a
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x7d8e
jmp 0xa01d
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x58a8
jmp 0xa01d
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5780
nop