// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x889c
movq %rax, %r13
testq %r15, %r15
je 0x893f
movq %r13, (%r15)
movq 0x1164a(%rip), %rsi     # 0x19f90
movq %r13, %rdi
callq 0x5700
movq %rax, %r15
movq %rax, -0x40(%rbp)
pushq $0x18
popq %rdi
callq 0x54a0
movq %rax, %r12
leaq -0x29(%rbp), %rax
leaq -0x60(%rbp), %rdi
movq %rax, (%rdi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r12)
leaq 0x10e1f(%rip), %rax     # 0x197a0
movq %rax, (%r12)
movq %r15, 0x10(%r12)
movq %r12, -0x38(%rbp)
andq $0x0, 0x8(%rdi)
callq 0x8e70
testq %r15, %r15
je 0x89d2
bswapq %r13
movq %r13, (%r15)
movl 0x20(%r14), %r13d
movl %r13d, %eax
rolw $0x8, %ax
movzwl 0x24(%r14), %ecx
rolw $0x8, %cx
movw %ax, 0x8(%r15)
movw %cx, 0xa(%r15)
cmpl $0x6, %r13d
je 0x89cd
cmpl $0x8, %r13d
jne 0x89ff
movq %r15, (%rbx)
jmp 0x89d6
andq $0x0, (%rbx)
andq $0x0, -0x38(%rbp)
movq %r12, 0x8(%rbx)
andq $0x0, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7dbe
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
addq $0xc, %rdi
movl 0x8(%r14), %eax
movq %rax, -0x50(%rbp)
leal 0x1(%rax), %edx
movq %rdx, -0x48(%rbp)
movq (%r14), %rsi
callq 0x53c0
movq -0x50(%rbp), %rsi
movb 0xb8(%r14), %al
movq -0x48(%rbp), %rcx
movb %al, 0xc(%r15,%rcx)
movl %esi, %ecx
addl $0x2, %ecx
js 0x8a5d
cmpl $0x7, %r13d
je 0x89cd
testl %r13d, %r13d
je 0x89cd
testb %al, %al
jne 0x8a68
addl $0xe, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x8b2a
testl %eax, %eax
jns 0x89cd
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
jmp 0x89e4
callq 0x8b06
movq %rax, %rdi
callq 0x51d0
movq %r15, %rdi
callq 0x8e86
callq 0x5620
movq %rax, %rbx
callq 0x56f0
movq %rbx, %rdi
callq 0x5780
movq %rax, %rdi
callq 0x7562