// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
leal 0xfff(%rsi), %edi
movl $0xfffff000, %edx       # imm = 0xFFFFF000
andl %edx, %edi
xorl %ebx, %ebx
movl %edi, %r15d
movl $0x0, %r13d
movq %rsi, -0x40(%rbp)
subl %esi, %r15d
je 0x9b00
movl %edi, -0x44(%rbp)
movq -0x38(%rbp), %rax
cmpl %eax, %r15d
cmovael %eax, %r15d
xorl %r13d, %r13d
movl %r13d, %r14d
movl %r15d, %edx
subl %r13d, %edx
jbe 0x9aec
movq -0x40(%rbp), %rax
leal (%r14,%rax), %ecx
movl %ecx, %esi
andl $0xfff, %esi            # imm = 0xFFF
leaq 0x4638(%rip), %rax      # 0xe100
addq %rax, %rsi
movl -0x2c(%rbp), %edi
callq 0x80f0
testl %eax, %eax
movl $0x0, %r13d
cmovnsl %eax, %r13d
addl %r14d, %r13d
testl %eax, %eax
cmovsl %eax, %r12d
jns 0x9aa6
jmp 0x9aef
movl %r14d, %r13d
cmpl %r14d, %r15d
movl $0xfffff000, %edx       # imm = 0xFFFFF000
movl -0x44(%rbp), %edi
ja 0x9bc5
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
leal (%rax,%rcx), %r14d
subl %edi, %r14d
andl %edx, %r14d
movl %r12d, %r15d
cmpl $0x1000, -0x38(%rbp)    # imm = 0x1000
jb 0x9b6b
cmpl %ebx, %r14d
jbe 0x9b6b
movl %ebx, %eax
andl $0xfff, %eax            # imm = 0xFFF
leaq 0x45cf(%rip), %rcx      # 0xe100
leaq (%rcx,%rax), %rsi
movl $0x1000, %edx           # imm = 0x1000
subl %eax, %edx
movq -0x40(%rbp), %rax
leal (%rax,%r13), %ecx
movl -0x2c(%rbp), %edi
callq 0x80f0
testl %eax, %eax
movl $0x0, %ecx
cmovnsl %eax, %ecx
addl %ecx, %ebx
addl %r13d, %ecx
movl %ecx, %r13d
testl %eax, %eax
cmovsl %eax, %r15d
movl %r15d, %r12d
jns 0x9b15
jmp 0x9bc5
xorl %r12d, %r12d
cmpl -0x38(%rbp), %r13d
je 0x9bc5
xorl %ebx, %ebx
movl %r15d, %r12d
movq -0x38(%rbp), %rdx
subl %r13d, %edx
jbe 0x9bc2
cmpl $0x1000, %ebx           # imm = 0x1000
jae 0x9bd7
movl %ebx, %esi
leaq 0x456d(%rip), %rax      # 0xe100
addq %rax, %rsi
movq -0x40(%rbp), %rax
leal (%rax,%r13), %ecx
movl -0x2c(%rbp), %edi
callq 0x80f0
testl %eax, %eax
movl $0x0, %ecx
cmovnsl %eax, %ecx
addl %ecx, %ebx
addl %r13d, %ecx
movl %ecx, %r13d
testl %eax, %eax
cmovsl %eax, %r12d
jns 0x9b79
jmp 0x9bc5
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44fb(%rip), %rdi      # 0xe0d9
leaq 0x44a7(%rip), %rsi      # 0xe08c
leaq 0x44b5(%rip), %rcx      # 0xe0a1
movl $0x62, %edx
callq 0x8210