// File: /workspace/github_cpp_makefile_star2_O3/williewillus[P]panda_scratchpad/src/wrapper/DiskMod.cpp
// Function: wrapper::DiskMod::Serialize(wrapper::DiskMod&, unsigned long long*)
shared_ptr<char> DiskMod::Serialize(DiskMod &dm, unsigned long long *size) {
  // Standard code to serialize the front part of the DiskMod.
  // Get a block large enough for this DiskMod.
  const uint64_t mod_size = dm.GetSerializeSize();
  if (size != nullptr) {
    *size = mod_size;
  }
  // TODO(ashmrtn): May want to split this if it is very large.
  shared_ptr<char> res_ptr(new (std::nothrow) char[mod_size],
      [](char *c) {delete[] c;});
  char *buf = res_ptr.get();
  if (buf == nullptr) {
    return res_ptr;
  }

  const uint64_t mod_size_be = htobe64(mod_size);
  memcpy(buf, &mod_size_be, sizeof(uint64_t));
  unsigned int buf_offset = sizeof(uint64_t);

  int res = SerializeHeader(buf, buf_offset, dm);
  if (res < 0) {
    return shared_ptr<char>(nullptr);
  }
  buf_offset += res;

  // kCheckpointMod and kSyncMod don't need anything done after the type.
  if (!(dm.mod_type == DiskMod::kCheckpointMod ||
      dm.mod_type == DiskMod::kSyncMod)) {
    res = SerializeChangeHeader(buf, buf_offset, dm);
    if (res < 0) {
      return shared_ptr<char>(nullptr);
    }

    if (dm.mod_type == DiskMod::kFsyncMod ||
        dm.mod_type == DiskMod::kCreateMod) {
      return res_ptr;
    }

    buf_offset += res;
    if (dm.directory_mod) {
      // We changed a directory, only put that down.
      res = SerializeDirectoryMod(buf, buf_offset, dm);
      if (res < 0) {
        return shared_ptr<char>(nullptr);
      }
      buf_offset += res;
    } else {
      // TODO(ashmrtn): *Technically* fallocate and friends can be called on a
      // directory file descriptor. The current code will not play well with
      // that.
      // We changed a file, only put that down.
      res = SerializeDataRange(buf, buf_offset, dm);
      if (res < 0) {
        return shared_ptr<char>(nullptr);
      }
      buf_offset += res;
    }
  }

  return res_ptr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x97e4
movq %rax, %r13
testq %r14, %r14
je 0x9883
movq %r13, (%r14)
movq 0x126fe(%rip), %rsi     # 0x1bf88
movq %r13, %rdi
callq 0x5720
movq %rax, %r14
movl $0x18, %edi
callq 0x54a0
movq %rax, %r12
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x11f40(%rip), %rax     # 0x1b7f8
movq %rax, (%r12)
movq %r14, 0x10(%r12)
testq %r14, %r14
je 0x98fd
bswapq %r13
movq %r13, (%r14)
movl 0x20(%r15), %r13d
movl %r13d, %eax
rolw $0x8, %ax
movl 0x24(%r15), %r8d
movl %r8d, %ecx
rolw $0x8, %cx
movw %ax, 0x8(%r14)
movw %cx, 0xa(%r14)
cmpl $0x6, %r13d
je 0x98f8
cmpl $0x8, %r13d
jne 0x991a
movq %r14, (%rbx)
jmp 0x9904
movq $0x0, (%rbx)
movq %r12, 0x8(%rbx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%r14), %rdi
movq (%r15), %rsi
movq 0x8(%r15), %rax
movq %rax, -0x40(%rbp)
leal 0x1(%rax), %edx
movq %rdx, -0x38(%rbp)
movl %r8d, -0x2c(%rbp)
callq 0x53b0
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %edi
movb 0xb8(%r15), %al
movq -0x38(%rbp), %rcx
movb %al, 0xc(%r14,%rcx)
movl %esi, %ecx
addl $0x2, %ecx
js 0x99c4
cmpl $0x7, %r13d
je 0x98f8
testl %r13d, %r13d
je 0x98f8
testb %al, %al
jne 0x99d7
addl $0xe, %esi
movq 0xd0(%r15), %rax
bswapq %rax
movq %rax, (%r14,%rsi)
movq 0xd8(%r15), %rdx
movq %rdx, %rax
bswapq %rax
movq %rax, 0x8(%r14,%rsi)
cmpl $0x9, %r13d
sete %al
addl $-0x2, %edi
cmpl $0x7, %edi
setb %cl
orb %al, %cl
jne 0x98f8
movq %r14, %rdi
addq %rsi, %rdi
addq $0x10, %rdi
movq 0xc0(%r15), %rsi
movq %rdx, %r15
callq 0x53b0
addl $0x10, %r15d
jns 0x98f8
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movq %r12, %rdi
callq 0x8a78
jmp 0x9908
leaq 0xb134(%rip), %rdi      # 0x14b12
leaq 0xb144(%rip), %rsi      # 0x14b29
leaq 0xb151(%rip), %rcx      # 0x14b3d
movl $0xce, %edx
callq 0x52e0
movq %rax, %rdi
callq 0x51d0
testq %r14, %r14
je 0x9a0b
movq %r14, %rdi
callq 0x5540
callq 0x5640
movq %rax, %rbx
callq 0x5710
movq %rbx, %rdi
callq 0x57c0
movq %rax, %rdi
callq 0x7f7a