// File: /workspace/github_cpp_makefile_star2_O2/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 0x31630(%rip), %rsi     # 0x93a31
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x62513
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)
pushq $0x4
popq %rdx
movq %rdx, %rsi
movq %r15, %rcx
callq 0xe8e0
cmpq $0x4, %rax
jne 0x6251c
movq %r15, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movslq (%r14), %rbp
leaq (,%rbp,4), %rax
testq %rbp, %rbp
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
pushq $0x1
popq %rbx
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %r15d, %r15d
cmpl %r15d, 0x8(%r14)
jle 0x62550
xorl %r13d, %r13d
cmpl 0x4(%r14), %r13d
jge 0x62504
movl 0x8(%r14), %eax
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
movq 0x10(%r14), %rsi
addq %r15, %rsi
movl 0xc(%r14), %edx
imull %r13d, %edx
movslq %edx, %rdx
addq %rsi, %rdx
cmpl %ebx, %eax
cmovlel %ebx, %eax
addl 0xc(%rsp), %eax
addq %rbx, %rax
xorl %esi, %esi
movss 0x3860a(%rip), %xmm1    # 0x9aad0
cmpq %rsi, %rcx
je 0x624e4
movzbl (%rdx), %edi
cvtsi2ss %edi, %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%r12,%rsi,4)
incq %rsi
addq %rax, %rdx
jmp 0x624c6
movslq %ebp, %rdx
movq %r12, %rdi
pushq $0x4
popq %rsi
movq 0x10(%rsp), %rcx
callq 0xe8e0
movl (%r14), %ebp
incl %r13d
cmpq %rbp, %rax
je 0x62487
jmp 0x6252a
incq %r15
incq %rbx
decl 0xc(%rsp)
jmp 0x6247a
leaq 0x37e9b(%rip), %rdi     # 0x9a3b5
jmp 0x62523
leaq 0x388d4(%rip), %rdi     # 0x9adf7
xorl %ebp, %ebp
movq %rbx, %rsi
jmp 0x62538
leaq 0x388f2(%rip), %rdi     # 0x9ae23
xorl %ebp, %ebp
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0xe5d0
movq 0x10(%rsp), %rdi
callq 0xe440
movl (%r14), %ebp
imull 0x4(%r14), %ebp
imull 0x8(%r14), %ebp
shll $0x2, %ebp
jmp 0x6253f