// File: /workspace/github_cpp_makefile_star2_O1/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 0xccf0
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 0xaf3e
callq 0x5460
testl %ebx, %ebx
js 0xb0d8
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 0xb0d8
movq %r13, -0x40(%rbp)
movq 0x90(%r14), %r13
leaq -0x2c(%rbp), %rsi
movq %r12, %rdi
callq 0xccf0
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 0xafca
callq 0x5460
testl %r13d, %r13d
js 0xb06f
leaq -0x140(%rbp), %r13
movq %r13, %rdi
callq 0x9e18
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 0xccf0
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 0xb074
testb $0x8, %r15b
jne 0xb08a
testb $0x10, %r15b
jne 0xb0ec
testb $0x1, %r15b
jne 0xb0fe
movl $0x2, -0x11c(%rbp)
jmp 0xb094
movl %r13d, %ebx
jmp 0xb0d8
testb $0x1, %r15b
je 0xb119
movl $0x4, -0x11c(%rbp)
jmp 0xb094
movl $0x5, -0x11c(%rbp)
addq $0x78, %r14
leaq -0x140(%rbp), %rsi
movq %r14, %rdi
callq 0xbfa2
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb0b9
callq 0x5460
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0xb0c7
callq 0x8af0
movq -0x140(%rbp), %rdi
cmpq %r12, %rdi
je 0xb0d8
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 0xb10a
movl $0x6, -0x11c(%rbp)
jmp 0xb094
movl $0x3, -0x11c(%rbp)
jmp 0xb094
movl $0x7, -0x11c(%rbp)
jmp 0xb094
leaq 0x9aea(%rip), %rdi      # 0x14c0a
leaq 0x9afe(%rip), %rsi      # 0x14c25
leaq 0x9b0b(%rip), %rcx      # 0x14c39
movl $0x1be, %edx            # imm = 0x1BE
callq 0x52e0
movq %rax, %rbx
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0xb16c
jmp 0xb178
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0xbfde
jmp 0xb17b
movq %rax, %rbx
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xb17b
callq 0x5460
jmp 0xb17b
movq %rax, %rbx
movq %rbx, %rdi
callq 0x57c0
nop