// File: /workspace/github_cpp_makefile_star2_O2/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 0x9c462
testl %r13d, %r13d
je 0x8d864
xorpd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x8d87d
jp 0x8d87d
movsd 0x10(%rsp), %xmm0
ucomisd 0x3a8fa(%rip), %xmm0    # 0xc8148
jne 0x8d87d
jp 0x8d87d
movsd 0x8(%rsp), %xmm0
ucomisd 0x3a8e8(%rip), %xmm0    # 0xc8148
jne 0x8d87d
jp 0x8d87d
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x8ce5c
testl %eax, %eax
jne 0x8d912
jmp 0x8d9ce
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x844f0
movsd 0x8(%rsp), %xmm0
ucomisd 0x3a8b0(%rip), %xmm0    # 0xc8148
jne 0x8d89c
jnp 0x8d8ac
leaq 0x40(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
callq 0x880f4
movsd 0x10(%rsp), %xmm0
ucomisd 0x3a88e(%rip), %xmm0    # 0xc8148
jne 0x8d8be
jnp 0x8d8ce
leaq 0x40(%rsp), %rdi
movsd 0x10(%rsp), %xmm0
callq 0x87fce
xorpd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x8d8e0
jnp 0x8d8f0
leaq 0x40(%rsp), %rdi
movsd 0x18(%rsp), %xmm0
callq 0x87ed0
movq 0x8(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x8ce5c
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
callq 0x846a8
testl %ebp, %ebp
je 0x8d9e2
movq 0x8(%rbx), %rdi
callq 0x9c473
movq %rax, 0x38(%rbx)
subl %r15d, %eax
leaq 0x110(%rsp), %rdi
pushq $0x1
popq %rbp
movq %r14, %rsi
movl %ebp, %edx
movl %eax, %ecx
movq %r15, %r8
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
callq 0x8e46c
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rdx
movq %r14, %rsi
callq 0x5edd2
leaq 0x40(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movl $0xa8, %edx
callq 0x13400
movq 0x40(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0x8e764
leaq 0x40(%rsp), %rsi
movl $0xa8, %edx
movq %rax, %rdi
callq 0x13400
leaq 0x20(%rsp), %rdi
callq 0x13230
leaq 0x40(%rsp), %rdi
leaq 0x7(%rsp), %rdx
movq %r14, %rsi
callq 0x5edd2
leaq 0x48(%rbx), %rdi
leaq 0x40(%rsp), %rsi
callq 0x8eece
leaq 0x40(%rsp), %rdi
callq 0x13230
incl 0x34(%rbx)
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 0x8d9ce
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x8d9fa
jmp 0x8da01
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x13230
jmp 0x8da13
movq %rax, %rbx
jmp 0x8da13
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x846a8
movq %rbx, %rdi
callq 0x138f0
nop