// File: /workspace/github_cpp_makefile_star2_O3/williewillus[P]panda_scratchpad/src/wrapper/workload.cpp
// Function: wrapper::WriteData(int, unsigned int, unsigned int)
int WriteData(int fd, unsigned int offset, unsigned int size) {
  // Offset into a data block to start working at.
  const unsigned int rounded_offset =
    (offset + (kTestDataSize - 1)) & (~(kTestDataSize - 1));
  // Round down size to 4k for number of full pages to write.
  
  const unsigned int aligned_size = (size >= kTestDataSize) ?
    (size - (rounded_offset - offset)) & ~(kTestDataSize - 1) :
    0;
  unsigned int num_written = 0;

  // The start of the write range is not aligned with our data blocks.
  // Therefore, we should write out part of a data block for this segment,
  // with the first character in the data block aligning with the data block
  // boundary.
  if (rounded_offset != offset) {
    // We should never write more than kTestDataSize of unaligned data at the
    // start.
    const unsigned int to_write = (size < rounded_offset - offset) ?
      size : rounded_offset - offset;
    while (num_written < to_write){
      const unsigned int mod_offset =
        (num_written + offset) & (kTestDataSize - 1);
      assert(mod_offset < kTestDataSize);

      int res = pwrite(fd, kTestDataBlock + mod_offset, to_write - num_written,
          offset + num_written);
      if (res < 0) {
        return res;
      }
      num_written += res;
    }
  }

  // Write out the required number of full pages for this request. The first
  // byte will be aligned with kTestDataSize.
  unsigned int aligned_written = 0;
  while (aligned_written < aligned_size) {
    const unsigned int mod_offset = (aligned_written & (kTestDataSize - 1));
    // Write up to a full page of data at a time.
    int res = pwrite(fd, kTestDataBlock + mod_offset,
        kTestDataSize - mod_offset, offset + num_written);
    if (res < 0) {
      return res;
    }
    num_written += res;
    aligned_written += res;
  } 

  if (num_written == size) {
    return 0;
  }

  // Write out the last partial page of data. The first byte will be aligned
  // with kTestDataSize.
  unsigned int end_written = 0;
  while (num_written < size) {
    assert(end_written < kTestDataSize);
    const unsigned int mod_offset = (end_written & (kTestDataSize - 1));
    int res = pwrite(fd, kTestDataBlock + mod_offset,
        size - num_written, offset + num_written);
    if (res < 0) {
      return res;
    }
    num_written += res;
    end_written += res;
  } 

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r14d
movl %edi, -0x2c(%rbp)
leal 0xfff(%r14), %r15d
movl $0xfffff000, %eax       # imm = 0xFFFFF000
andl %eax, %r15d
xorl %r13d, %r13d
movl %r15d, %ebx
movl $0x0, %r12d
subl %esi, %ebx
movq %rdx, -0x38(%rbp)
je 0x9abc
cmpl %edx, %ebx
cmovael %edx, %ebx
xorl %r12d, %r12d
movl %ebx, %edx
subl %r12d, %edx
jbe 0x9ab3
leal (%r12,%r14), %ecx
movl %ecx, %esi
andl $0xfff, %esi            # imm = 0xFFF
leaq 0x4664(%rip), %rax      # 0xe100
addq %rax, %rsi
movl -0x2c(%rbp), %edi
callq 0x80f0
addl %eax, %r12d
testl %eax, %eax
jns 0x9a82
jmp 0x9b47
movq -0x38(%rbp), %rdx
movl $0xfffff000, %eax       # imm = 0xFFFFF000
leal (%rdx,%r14), %ebx
subl %r15d, %ebx
andl %eax, %ebx
movq -0x38(%rbp), %rcx
cmpl $0x1000, %ecx           # imm = 0x1000
jb 0x9b08
cmpl %r13d, %ebx
jbe 0x9b08
movl %r13d, %eax
andl $0xfff, %eax            # imm = 0xFFF
leaq 0x461b(%rip), %rcx      # 0xe100
leaq (%rcx,%rax), %rsi
movl $0x1000, %edx           # imm = 0x1000
subl %eax, %edx
leal (%r12,%r14), %ecx
movl -0x2c(%rbp), %edi
callq 0x80f0
addl %eax, %r12d
addl %eax, %r13d
testl %eax, %eax
jns 0x9ac5
jmp 0x9b47
xorl %eax, %eax
cmpl %ecx, %r12d
je 0x9b47
xorl %ebx, %ebx
movq -0x38(%rbp), %rdx
subl %r12d, %edx
jbe 0x9b45
cmpl $0x1000, %ebx           # imm = 0x1000
jae 0x9b56
movl %ebx, %esi
leaq 0x45d5(%rip), %rax      # 0xe100
addq %rax, %rsi
leal (%r12,%r14), %ecx
movl -0x2c(%rbp), %edi
callq 0x80f0
addl %eax, %r12d
addl %eax, %ebx
testl %eax, %eax
jns 0x9b11
jmp 0x9b47
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x457c(%rip), %rdi      # 0xe0d9
leaq 0x4528(%rip), %rsi      # 0xe08c
leaq 0x4536(%rip), %rcx      # 0xe0a1
movl $0x62, %edx
callq 0x8210