// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: calc_cct(int, int, double, double, double)
DATA_SIZE calc_cct(int type, int undo_scale, double red, double green, double blue)
{
int i;
DATA_SIZE val;
DATA_SIZE dif;
DATA_SIZE pct;
DATA_SIZE red_uw, green_uw, blue_uw;
DATA_SIZE ccx, ccy, ccz;
DATA_SIZE n;
DATA_SIZE cct;

   if(undo_scale) {        // red, green and blue came from get_plot_q() values
      if(show_color_uw) {  // we need to undo the scale factor that was applied to those values 
         red *= RED_SENS;     // blue
         green *= GREEN_SENS; // green
         blue *= BLUE_SENS;   // red
      }
      else if(show_color_pct) {
         // !!!!!!! we cant properly undo those values
         red_uw = (DATA_SIZE) (red * (red + green + blue));
         green_uw = (DATA_SIZE) (green * (red + green + blue));
         blue_uw = (DATA_SIZE) (blue * (red + green + blue));

         red = red_uw;
         green = green_uw;
         blue = blue_uw;
      }
   }
if(0 && cct_dbg) sprintf(plot_title, "ty=%d r=%f g=%f b=%f", type, red, green, blue);

   if(type == 0) {
      if(blue == 0.0) return 0.0;
      val = (DATA_SIZE) ((red / blue) * cct_cal);
val = rb_m * val + rb_b;

      for(i=0; i<(int) NUM_CCT; i++) {
         if(val < cctt[i].ratio) {
            if(i == 0) return 99999.999F;

            dif = cctt[i].ratio - cctt[i-1].ratio;
            if(dif == 0.0F) return 0.0F;
            pct = (val - cctt[i-1].ratio) / dif;

            dif = cctt[i].k - cctt[i-1].k;
// sprintf(plot_title, "red=%f  rob=%f  val=%f  hz=%f  i=%d  pct=%f  dif=%f", red, r_over_b, val, red_hz/blue_hz, i, pct, dif);
            dif *= pct;
            return cctt[i-1].k + dif;
         }
      }
   }
   else {
      red_uw = (DATA_SIZE) red / RED_SENS;
      green_uw = (DATA_SIZE) green / GREEN_SENS;
      blue_uw = (DATA_SIZE) blue /  BLUE_SENS;

      calc_cie(red_uw, green_uw, blue_uw);
      if(cie_sum) {
         ccx = cie_x / cie_sum;
         ccy = cie_y / cie_sum;
         ccz = cie_z / cie_sum;
         if(type == 1) {
            n = (ccx - 0.3320F) / (ccy - 0.1858F);
            cct = (-449.0F*n*n*n) + (3525.0F*n*n) - (6823.3F*n) + 5520.33F;
            if((cct < 0.0F) || (cct > 100000.0F)) cct = 0.0F;
            cct /= cct1_cal;  // apply bogus correction factor
            return cct;
         }
         else if(type == 2) {
            n = (ccx - 0.3366F) / (ccy - 0.1735F);
            cct = (DATA_SIZE) ((-949.86315F) + 6253.80338F*exp(-n/0.91259F) + 28.70599F*exp(-n/0.20039) + 0.00004F*exp(-n/0.07125F));
            if((cct < 0.0F) || (cct > 100000.0F)) cct = 0.0F;
            cct /= cct2_cal;  // apply bogus correction factor
            return cct;
         }
      }
      else return 0.0;
   }

   return 1.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebx
testl %esi, %esi
je 0x7aae4
leaq 0x2924d3(%rip), %rax    # 0x30cf6b
cmpb $0x0, (%rax)
je 0x7aac0
leaq 0x29244c(%rip), %rax    # 0x30cef0
mulsd (%rax), %xmm0
leaq 0x292439(%rip), %rax    # 0x30cee8
mulsd (%rax), %xmm1
leaq 0x292426(%rip), %rax    # 0x30cee0
mulsd (%rax), %xmm2
jmp 0x7aae4
leaq 0x2924a5(%rip), %rax    # 0x30cf6c
cmpb $0x0, (%rax)
je 0x7aae4
movapd %xmm0, %xmm3
addsd %xmm1, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm3, %xmm2
testl %ebx, %ebx
je 0x7abce
leaq 0x2923fd(%rip), %rax    # 0x30cef0
divsd (%rax), %xmm0
leaq 0x2923ea(%rip), %rax    # 0x30cee8
divsd (%rax), %xmm1
leaq 0x2923d7(%rip), %rax    # 0x30cee0
divsd (%rax), %xmm2
callq 0x7a95a
movsd 0x2f62be(%rip), %xmm1   # 0x370dd8
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm1
jne 0x7ab2a
jnp 0x7acfd
movsd 0x2f628e(%rip), %xmm4   # 0x370dc0
divsd %xmm1, %xmm4
movsd 0x2f628a(%rip), %xmm0   # 0x370dc8
divsd %xmm1, %xmm0
cmpl $0x2, %ebx
je 0x7ac3c
cmpl $0x1, %ebx
jne 0x7acf5
addsd 0xa3834(%rip), %xmm4    # 0x11e390
addsd 0xa3834(%rip), %xmm0    # 0x11e398
divsd %xmm0, %xmm4
movsd 0xa3830(%rip), %xmm0    # 0x11e3a0
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
movsd 0xa3828(%rip), %xmm1    # 0x11e3a8
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm1
mulsd %xmm4, %xmm0
addsd %xmm1, %xmm0
mulsd 0xa3818(%rip), %xmm4    # 0x11e3b0
addsd %xmm0, %xmm4
addsd 0xa3814(%rip), %xmm4    # 0x11e3b8
movsd 0x8e51c(%rip), %xmm0    # 0x1090c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x29238f(%rip), %rax    # 0x30cf58
jmp 0x7acef
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm2
jne 0x7abde
jnp 0x7acfd
divsd %xmm2, %xmm0
leaq 0x292367(%rip), %rax    # 0x30cf50
mulsd (%rax), %xmm0
leaq 0x1a05fc(%rip), %rax    # 0x21b1f0
leaq 0x1a05fd(%rip), %rcx    # 0x21b1f8
mulsd (%rax), %xmm0
addsd (%rcx), %xmm0
xorl %eax, %eax
leaq 0xef984(%rip), %rcx     # 0x16a590
movsd 0x8e45c(%rip), %xmm2    # 0x109070
movss (%rcx,%rax,8), %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
ja 0x7ad08
incq %rax
cmpq $0x13, %rax
jne 0x7ac14
movapd %xmm2, %xmm4
jmp 0x7acfd
addsd 0xa3704(%rip), %xmm4    # 0x11e348
addsd 0xa3704(%rip), %xmm0    # 0x11e350
xorpd 0x8e55c(%rip), %xmm4    # 0x1091b0
divsd %xmm0, %xmm4
movapd %xmm4, -0x20(%rbp)
divsd 0xa36f3(%rip), %xmm4    # 0x11e358
movapd %xmm4, %xmm0
callq 0x96f0
mulsd 0xa36ea(%rip), %xmm0    # 0x11e360
addsd 0xa36ea(%rip), %xmm0    # 0x11e368
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0xa36e0(%rip), %xmm0    # 0x11e370
callq 0x96f0
mulsd 0xa36db(%rip), %xmm0    # 0x11e378
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movapd -0x20(%rbp), %xmm0
divsd 0xa36cc(%rip), %xmm0    # 0x11e380
callq 0x96f0
movapd %xmm0, %xmm4
mulsd 0xa36c3(%rip), %xmm4    # 0x11e388
addsd -0x10(%rbp), %xmm4
movsd 0x8e3f6(%rip), %xmm0    # 0x1090c8
cmpltsd %xmm4, %xmm0
andnpd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
leaq 0x292271(%rip), %rax    # 0x30cf60
divsd (%rax), %xmm4
jmp 0x7acfd
movsd 0x8e373(%rip), %xmm4    # 0x109070
movapd %xmm4, %xmm0
addq $0x18, %rsp
popq %rbx
popq %rbp
retq
testq %rax, %rax
je 0x7ad59
leal -0x1(%rax), %edx
movss (%rcx,%rdx,8), %xmm2
subss %xmm2, %xmm1
xorpd %xmm3, %xmm3
ucomiss %xmm3, %xmm1
jne 0x7ad24
jnp 0x7acfd
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
subsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movss 0x4(%rcx,%rax,8), %xmm1
movss 0x4(%rcx,%rdx,8), %xmm2
subss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm0, %xmm1
xorps %xmm4, %xmm4
cvtss2sd %xmm2, %xmm4
addsd %xmm1, %xmm4
jmp 0x7acfd
movsd 0x8e367(%rip), %xmm4    # 0x1090c8
jmp 0x7acfd