// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::WriteRAWFile(char const*) const
int R2Image::
WriteRAWFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    RNFail("Unable to open raw image file %s", filename);
    return 0;
  }

  // Write header
  static const unsigned int magic = 54321;
  unsigned int header[4] = { magic, (unsigned int) width, (unsigned int) height, (unsigned int) ncomponents };
  if (fwrite(header, sizeof(unsigned int), 4, fp) != 4) {
    RNFail("Unable to write header to raw image file %s", filename);
    return 0;
  }

  // Allocate buffer
  float *buf = new float [ width ];
  if (!buf) {
    RNFail("Unable to allocate buffer for raw image file %s", filename);
    return 0;
  }

  // Write data for each component
  for (int i = 0; i < ncomponents; i++) {
    for (int j = 0; j < height; j++) {
      // Load buffer
      const unsigned char *pixelsp = Pixels(j);
      for (int k = 0; k < width; k++) {
        for (int c = 0; c < i; c++) pixelsp++;
        buf[k] = *(pixelsp++) / 255.0F;
        for (int c = i+1; c < ncomponents; c++) pixelsp++;
      }

      // Write buffer
      if (fwrite(buf, sizeof(float), width, fp) != (unsigned int) width) {
        RNFail("Unable to write data to raw image file %s", filename);
        return 0;
      }
    }
  }

  // Free buffer
  delete [] buf;

  // Close file
  fclose(fp);

  // Return number of bytes written
  return width * height * ncomponents * sizeof(float);  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x37856(%rip), %rsi     # 0xa2a81
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x6b3a4
movq %rax, %r15
leaq 0x20(%rsp), %rdi
movl $0xd431, (%rdi)         # imm = 0xD431
movq (%r14), %rax
movq %rax, 0x4(%rdi)
movl 0x8(%r14), %eax
movl %eax, 0xc(%rdi)
movl $0x4, %esi
movl $0x4, %edx
movq %r15, %rcx
callq 0xe8b0
cmpq $0x4, %rax
jne 0x6b3ad
movq %r15, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movslq (%r14), %r13
leaq (,%r13,4), %rax
testq %r13, %r13
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
movl 0x8(%r14), %ecx
testl %ecx, %ecx
jle 0x6b380
movl 0x4(%r14), %eax
xorl %ebp, %ebp
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x1, 0x8(%rsp)
movss 0x3e887(%rip), %xmm1    # 0xa9b50
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %r15d
testl %eax, %eax
jle 0x6b36b
xorl %ebx, %ebx
testl %r13d, %r13d
jle 0x6b334
movslq 0xc(%r14), %rax
movslq %ebx, %rcx
imulq %rax, %rcx
addq 0x10(%r14), %rcx
movl 0x8(%r14), %eax
movl 0x8(%rsp), %edx
cmpl %edx, %eax
cmovlel %edx, %eax
addl 0xc(%rsp), %eax
movl %r13d, %edx
xorl %esi, %esi
testl %ebp, %ebp
leaq 0x1(%rcx,%r15), %rdi
cmoveq %rcx, %rdi
movzbl (%rdi), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%r12,%rsi,4)
leaq (%rdi,%rax), %rcx
incq %rcx
incq %rsi
cmpq %rsi, %rdx
jne 0x6b306
movslq %r13d, %rdx
movl $0x4, %esi
movq %r12, %rdi
movq 0x10(%rsp), %rcx
callq 0xe8b0
movl (%r14), %r13d
cmpq %r13, %rax
jne 0x6b3bb
incl %ebx
movl 0x4(%r14), %eax
cmpl %eax, %ebx
movss 0x3e7ef(%rip), %xmm1    # 0xa9b50
jl 0x6b2dc
movl 0x8(%r14), %ecx
incl %ebp
incl %r15d
decl 0xc(%rsp)
incl 0x8(%rsp)
cmpl %ecx, %ebp
jl 0x6b2cf
movq %r12, %rdi
callq 0xe5b0
movq 0x10(%rsp), %rdi
callq 0xe430
movl (%r14), %ebp
imull 0x4(%r14), %ebp
imull 0x8(%r14), %ebp
shll $0x2, %ebp
jmp 0x6b3d0
leaq 0x3e07a(%rip), %rdi     # 0xa9425
jmp 0x6b3b4
leaq 0x3eac3(%rip), %rdi     # 0xa9e77
xorl %ebp, %ebp
movq %rbx, %rsi
jmp 0x6b3c9
leaq 0x3eae1(%rip), %rdi     # 0xa9ea3
xorl %ebp, %ebp
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq