// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
leaq -0x2c(%rbp), %rax
movl %esi, (%rax)
movq 0x90(%rdi), %rbx
leaq 0x8(%rdi), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xcc7e
movq (%rax), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x1d0(%rbp), %rdx
callq 0x51c0
movq (%rbx), %rax
leaq -0x140(%rbp), %rsi
leaq -0x260(%rbp), %rdx
movq %rbx, %rdi
callq *0x88(%rax)
movl %eax, %ebx
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaf3a
callq 0x5460
testl %ebx, %ebx
js 0xb0d4
movq 0x90(%r14), %rdi
movl -0x2c(%rbp), %esi
movq (%rdi), %rax
movl %r15d, %edx
movq -0x38(%rbp), %rcx
movq %r13, %r8
callq *0x60(%rax)
movl %eax, %ebx
testl %eax, %eax
js 0xb0d4
movq %r13, -0x40(%rbp)
movq 0x90(%r14), %r13
leaq -0x2c(%rbp), %rsi
movq %r12, %rdi
callq 0xcc7e
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
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0xafc6
callq 0x5460
testl %r13d, %r13d
js 0xb06b
leaq -0x140(%rbp), %r13
movq %r13, %rdi
callq 0x9da2
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 0xcc7e
leaq -0x130(%rbp), %r12
leaq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x5280
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
testb $0x2, %r15b
jne 0xb070
testb $0x8, %r15b
jne 0xb086
testb $0x10, %r15b
jne 0xb0e8
testb $0x1, %r15b
jne 0xb0fa
movl $0x2, -0x11c(%rbp)
jmp 0xb090
movl %r13d, %ebx
jmp 0xb0d4
testb $0x1, %r15b
je 0xb115
movl $0x4, -0x11c(%rbp)
jmp 0xb090
movl $0x5, -0x11c(%rbp)
addq $0x78, %r14
leaq -0x140(%rbp), %rsi
movq %r14, %rdi
callq 0xbfac
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb0b5
callq 0x5460
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0xb0c3
callq 0x8a78
movq -0x140(%rbp), %rdi
cmpq %r12, %rdi
je 0xb0d4
callq 0x5460
movl %ebx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %r15b
jne 0xb106
movl $0x6, -0x11c(%rbp)
jmp 0xb090
movl $0x3, -0x11c(%rbp)
jmp 0xb090
movl $0x7, -0x11c(%rbp)
jmp 0xb090
leaq 0x9ade(%rip), %rdi      # 0x14bfa
leaq 0x9af2(%rip), %rsi      # 0x14c15
leaq 0x9aff(%rip), %rcx      # 0x14c29
movl $0x1be, %edx            # imm = 0x1BE
callq 0x52e0
movq %rax, %rbx
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0xb168
jmp 0xb174
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0xbfe8
jmp 0xb177
movq %rax, %rbx
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb177
callq 0x5460
jmp 0xb177
movq %rax, %rbx
movq %rbx, %rdi
callq 0x57c0
nop