// File: /workspace/github_cpp_makefile_star2_O2/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 0xb8f0
cmpl %edx, %ebx
cmovael %edx, %ebx
xorl %r12d, %r12d
movl %ebx, %edx
subl %r12d, %edx
jbe 0xb8e7
leal (%r12,%r14), %ecx
movl %ecx, %esi
andl $0xfff, %esi            # imm = 0xFFF
leaq 0x3800(%rip), %rax      # 0xf0d0
addq %rax, %rsi
movl -0x2c(%rbp), %edi
callq 0xa110
addl %eax, %r12d
testl %eax, %eax
jns 0xb8b6
jmp 0xb97b
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 0xb93c
cmpl %r13d, %ebx
jbe 0xb93c
movl %r13d, %eax
andl $0xfff, %eax            # imm = 0xFFF
leaq 0x37b7(%rip), %rcx      # 0xf0d0
leaq (%rcx,%rax), %rsi
movl $0x1000, %edx           # imm = 0x1000
subl %eax, %edx
leal (%r12,%r14), %ecx
movl -0x2c(%rbp), %edi
callq 0xa110
addl %eax, %r12d
addl %eax, %r13d
testl %eax, %eax
jns 0xb8f9
jmp 0xb97b
xorl %eax, %eax
cmpl %ecx, %r12d
je 0xb97b
xorl %ebx, %ebx
movq -0x38(%rbp), %rdx
subl %r12d, %edx
jbe 0xb979
cmpl $0x1000, %ebx           # imm = 0x1000
jae 0xb98a
movl %ebx, %esi
leaq 0x3771(%rip), %rax      # 0xf0d0
addq %rax, %rsi
leal (%r12,%r14), %ecx
movl -0x2c(%rbp), %edi
callq 0xa110
addl %eax, %r12d
addl %eax, %ebx
testl %eax, %eax
jns 0xb945
jmp 0xb97b
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x371e(%rip), %rdi      # 0xf0af
leaq 0x36ca(%rip), %rsi      # 0xf062
leaq 0x36d8(%rip), %rcx      # 0xf077
pushq $0x62
popq %rdx
callq 0xa2a0