// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2PixelDatabase.cpp
// Function: gaps::R2PixelDatabase::InsertGrid(char const*, gaps::R2Grid const&, double, double, double, int)
int R2PixelDatabase::
InsertGrid(const char *key, const R2Grid& grid,
  double offset, double scale, double exponent, RNBoolean apply_transformation)
{
  // Seek to end of entries
  unsigned long long seek = entries_seek;
  RNFileSeek(fp, seek, RN_FILE_SEEK_SET);

  // Check if need to apply offset, scale, or exponent
  if ((apply_transformation) &&
      ((offset != 0) || (scale != 1) || (exponent != 1))) {
    // Write processed pixels to file
    R2Grid tmp(grid);
    if (exponent != 1) tmp.Pow(exponent);
    if (scale != 1) tmp.Multiply(scale);
    if (offset != 0) tmp.Add(offset);
    if (!tmp.WritePNGStream(fp)) return FALSE;
  }
  else {
    // Write original pixels to file
    if (!grid.WritePNGStream(fp)) return FALSE;
  }

  // Update entries seek
  entries_seek = RNFileTell(fp);
  unsigned int size = entries_seek - seek;
  
  // Insert entry into map
  R2PixelDatabaseEntry entry(key, R2_PIXEL_DATABASE_1_16_PNG_FORMAT, size, seek, offset, scale, exponent);
  map.Insert(key, entry);
  
  // Insert key
  keys.push_back(key);
  
  // Increment number of entries
  entries_count++;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movl %ecx, %r13d
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
movq 0x38(%rbx), %r15
xorl %ebp, %ebp
movq %r15, %rsi
xorl %edx, %edx
callq 0xabc5a
testl %r13d, %r13d
je 0x9b890
xorpd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x9b8a9
jp 0x9b8a9
movsd 0x10(%rsp), %xmm0
ucomisd 0x418ce(%rip), %xmm0    # 0xdd148
jne 0x9b8a9
jp 0x9b8a9
movsd 0x8(%rsp), %xmm0
ucomisd 0x418bc(%rip), %xmm0    # 0xdd148
jne 0x9b8a9
jp 0x9b8a9
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x9ad58
testl %eax, %eax
jne 0x9b93e
jmp 0x9ba66
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x91714
movsd 0x8(%rsp), %xmm0
ucomisd 0x41884(%rip), %xmm0    # 0xdd148
jne 0x9b8c8
jnp 0x9b8d8
leaq 0x40(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
callq 0x9581e
movsd 0x10(%rsp), %xmm0
ucomisd 0x41862(%rip), %xmm0    # 0xdd148
jne 0x9b8ea
jnp 0x9b8fa
leaq 0x40(%rsp), %rdi
movsd 0x10(%rsp), %xmm0
callq 0x956ec
xorpd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x9b90c
jnp 0x9b91c
leaq 0x40(%rsp), %rdi
movsd 0x18(%rsp), %xmm0
callq 0x955b8
movq 0x8(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x9ad58
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x918ec
testl %ebp, %ebp
je 0x9ba7a
movq 0x8(%rbx), %rdi
callq 0xabc6b
movq %rax, 0x38(%rbx)
subl %r15d, %eax
movl $0x1, 0x190(%rsp)
movl %eax, 0x194(%rsp)
movq %r15, 0x198(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x1a0(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x1a8(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x1b0(%rsp)
movb $0x0, 0x110(%rsp)
testq %r14, %r14
je 0x9b9b7
leaq 0x110(%rsp), %rdi
movl $0x7f, %edx
movq %r14, %rsi
callq 0x13280
movb $0x0, 0x18f(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rdx
movq %r14, %rsi
callq 0x9c6a2
leaq 0x40(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movl $0xa8, %edx
callq 0x133f0
movq 0x40(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0x9c9a2
leaq 0x40(%rsp), %rsi
movl $0xa8, %edx
movq %rax, %rdi
callq 0x133f0
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9ba23
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x134f0
leaq 0x40(%rsp), %rdi
leaq 0x7(%rsp), %rdx
movq %r14, %rsi
callq 0x9c6a2
leaq 0x48(%rbx), %rdi
leaq 0x40(%rsp), %rsi
callq 0x9d416
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9ba5e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x134f0
incl 0x34(%rbx)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x9ba66
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9baca
movq 0x50(%rsp), %rsi
jmp 0x9baae
jmp 0x9bab8
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9baca
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x134f0
jmp 0x9baca
movq %rax, %rbx
jmp 0x9baca
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x918ec
movq %rbx, %rdi
callq 0x138b0