// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
leaq 0x36aaf(%rip), %rsi     # 0xa2a84
movq %r14, %rdi
callq 0xe530
testq %rax, %rax
je 0x6c120
movq %rax, %r15
leaq 0x20(%rsp), %rdi
movl $0xd431, (%rdi)         # imm = 0xD431
movq (%rbx), %rax
movq %rax, 0x4(%rdi)
movl 0x8(%rbx), %eax
movl %eax, 0xc(%rdi)
movl $0x4, %esi
movl $0x4, %edx
movq %r15, %rcx
callq 0xe8b0
cmpq $0x4, %rax
jne 0x6c129
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movslq (%rbx), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r12
cmpl $0x0, 0x8(%rbx)
setg %al
jle 0x6c15a
xorl %ebp, %ebp
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x1, %r14d
movss 0x3dac3(%rip), %xmm1    # 0xa9b30
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movb %al, 0xb(%rsp)
movl %r15d, %r15d
cmpl $0x0, 0x4(%rbx)
jle 0x6c106
xorl %r13d, %r13d
movslq (%rbx), %rdx
testq %rdx, %rdx
jle 0x6c0dc
movslq 0xc(%rbx), %rax
movslq %r13d, %rcx
imulq %rax, %rcx
addq 0x10(%rbx), %rcx
movl 0x8(%rbx), %eax
cmpl %r14d, %eax
cmovlel %r14d, %eax
addl 0xc(%rsp), %eax
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 0x6c0ae
movl $0x4, %esi
movq %r12, %rdi
movq 0x10(%rsp), %rcx
callq 0xe8b0
movl (%rbx), %ecx
cmpq %rcx, %rax
jne 0x6c13e
incl %r13d
cmpl 0x4(%rbx), %r13d
movss 0x3da2c(%rip), %xmm1    # 0xa9b30
jl 0x6c087
incl %ebp
incl %r15d
decl 0xc(%rsp)
incl %r14d
cmpl 0x8(%rbx), %ebp
setl %al
jl 0x6c073
jmp 0x6c15a
leaq 0x3d2fe(%rip), %rdi     # 0xa9425
jmp 0x6c130
leaq 0x3dd27(%rip), %rdi     # 0xa9e57
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x72a1e
jmp 0x6c179
leaq 0x3dd3e(%rip), %rdi     # 0xa9e83
xorl %ebp, %ebp
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x72a1e
testb $0x1, 0xb(%rsp)
jne 0x6c179
movq %r12, %rdi
callq 0xe5b0
movq 0x10(%rsp), %rdi
callq 0xe430
movl (%rbx), %ebp
imull 0x4(%rbx), %ebp
imull 0x8(%rbx), %ebp
shll $0x2, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq