// File: /workspace/github_cpp_makefile_star2_O1/cloudinary[P]fuif/./export/write_pam.h
// Function: write_PAM_file(char const*, Image const&)
bool write_PAM_file(const char *output_image, const Image &image) {

  int nb_channels = image.channel.size();
  bool debugstuff = false;

  if (nb_channels > ((image.colormodel & 48) == 16 ? 5 : 4)) {
    e_printf("Too many channels. Saving just the first 3 channels (instead of %i)\n",nb_channels);
    nb_channels = 3; // just use the first 3. debug stuff
    debugstuff = true;
  }
  if (nb_channels == 0) {
    e_printf("Cannot save a zero-channel 'image' as PAM.\n");
    return false;
  }

  int w = image.channel[0].w;
  int h = image.channel[0].h;
  if (image.w < w) w = image.w;
  if (image.h < h) h = image.h;
  for (int i=1; i<nb_channels; i++) {
    if (image.channel[i].w < w) nb_channels = 1;
    if (image.channel[i].h < h) nb_channels = 1;
    if (nb_channels == 1) e_printf("Not saving channel %i since dimensions (%ix%i) do not correspond with the image dimensions (%ix%i).\n",i,image.channel[i].w,image.channel[i].h,w,h);
  }

  if (nb_channels > image.nb_channels) {debugstuff=true; nb_channels=1;}

  FILE *fp = fopen(output_image,"wb");
  if (!fp) {
    return false;
  }


  int components=3;
  bool cmyk = false;
  if ((image.colormodel & 48) == 0) { // RGB
    if (nb_channels == 4) components=4;
  } else if ((image.colormodel & 48) == 16) { // CMYK
    if (nb_channels == 5) components=4;
    if (nb_channels > 3) cmyk = true;
  }
  if (image.colormodel != 0 && image.colormodel != 16) {
    v_printf(1,"Warning: saving raw pixel data without a color profile, even though the image is not sRGB.\n");
  }
  if (nb_channels == 1) components = 1;
  if (nb_channels == 2) components = 2;

  int bit_depth = 8, bytes_per_value=1;
  if (image.channel[0].minval < 0) {bit_depth = 8; bytes_per_value=1;} // this is just for debugging things


  if (image.maxval > 255) {bit_depth = 16; bytes_per_value=2;}
  if (image.maxval > 65535) { e_printf("Warning: cannot save as PAM since bit depth is higher than 16-bit. Doing it anyway.\n");}

  if (image.nb_channels == 0) {
    // visualize signed stuff
    int range = image.channel[0].maxval - image.channel[0].minval;
    v_printf(3,"Visualizing %ix%i channel with range %i..%i\n",w,h,image.channel[0].minval,image.channel[0].maxval);
    if (range > 255) {bit_depth = 16; bytes_per_value=2;}
    if (image.real_nb_channels == 0) {
    fprintf(fp,"P6\n%u %u\n%i\n", w, h, 255);
    for (int y = 0; y < h; y++) {
     for (int x = 0; x < w; x++) {
      for (int c=0; c<components; c++) {
        // 5461 is one bit
        fputc(CLAMP(image.channel[c].value(y,x)*255/5461, 0, 255),fp);
        fputc(CLAMP(image.channel[c].value(y,x)*50/5461, 0, 255),fp);
        fputc(CLAMP(image.channel[c].value(y,x)*12/5461, 0, 255),fp);
      }
     }
    }
    } else {
    fprintf(fp,"P5\n%u %u\n%i\n", w, h, range);
    for (int y = 0; y < h; y++) {
     for (int x = 0; x < w; x++) {
      for (int c=0; c<components; c++) {
        if (bytes_per_value > 1) fputc((image.channel[c].value(y,x) - image.channel[0].minval) >> 8,fp);
        fputc((image.channel[c].value(y,x) - image.channel[0].minval) & 0xFF,fp);
      }
     }
    }
    }
    fclose(fp);
    return 0;
  }

  if (components == 1) fprintf(fp,"P5\n%u %u\n%i\n", w, h, image.maxval);
  if (components == 2) fprintf(fp,"P7\nWIDTH %u\nHEIGHT %u\nDEPTH 2\nMAXVAL %i\nTUPLTYPE GRAYSCALE_ALPHA\nENDHDR\n", w, h, image.maxval);
  if (components == 3) fprintf(fp,"P6\n%u %u\n%i\n", w, h, image.maxval);
  if (components == 4) fprintf(fp,"P7\nWIDTH %u\nHEIGHT %u\nDEPTH 4\nMAXVAL %i\nTUPLTYPE RGB_ALPHA\nENDHDR\n", w, h, image.maxval);
  v_printf(10,"Writing %i-channel PAM file (range %i..%i)\n",components,image.minval,image.maxval);

  if (cmyk) {
    v_printf(1,"Warning: converting CMYK image to RGB in order to save it as PAM.\n");
    for (int y = 0; y < h; y++) {
     for (int x = 0; x < w; x++) {
      for (int c=0; c<3; c++) {
        if (bytes_per_value > 1) fputc(CLAMP( (image.maxval-image.channel[c].value_nocheck(y,x))*image.channel[3].value_nocheck(y,x)/image.maxval,image.minval,image.maxval) >> 8,fp);
        fputc(CLAMP( (image.maxval-image.channel[c].value_nocheck(y,x))*image.channel[3].value_nocheck(y,x)/image.maxval,image.minval,image.maxval) & 0xFF,fp);
      }
      if (nb_channels == 5) {
        if (bytes_per_value > 1) fputc(CLAMP(image.channel[4].value_nocheck(y,x),image.minval,image.maxval) >> 8,fp);
        fputc(CLAMP(image.channel[4].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
      }
     }
    }
  } else {
    if (bytes_per_value > 1) {
    for (int y = 0; y < h; y++) {
     for (int x = 0; x < w; x++) {
      for (int c=0; c<components; c++) {
        fputc(CLAMP(image.channel[c].value_nocheck(y,x),image.minval,image.maxval) >> 8,fp);
        fputc(CLAMP(image.channel[c].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
      }
     }
    }
    } else {
      if (components == 3) {
        for (int y = 0; y < h; y++) {
         for (int x = 0; x < w; x++) {
            fputc(CLAMP(image.channel[0].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
            fputc(CLAMP(image.channel[1].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
            fputc(CLAMP(image.channel[2].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
         }
        }
      } else {
        for (int y = 0; y < h; y++) {
         for (int x = 0; x < w; x++) {
          for (int c=0; c<components; c++) {
            fputc(CLAMP(image.channel[c].value_nocheck(y,x),image.minval,image.maxval) & 0xFF,fp);
          }
         }
        }
      }
    }
  }

  fclose(fp);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rsi), %r12
subq (%rsi), %r12
shrq $0x6, %r12
movl 0x70(%rsi), %eax
andl $0x30, %eax
xorl %ecx, %ecx
cmpl $0x10, %eax
sete %cl
orl $0x4, %ecx
cmpl %r12d, %ecx
jge 0x20902
leaq 0x6b19(%rip), %rdi      # 0x2740b
movl %r12d, %esi
xorl %eax, %eax
callq 0x3796
movl $0x3, %r12d
testl %r12d, %r12d
je 0x20a01
movq %r14, 0x8(%rsp)
movq (%rbx), %rax
movslq 0x18(%rax), %rdx
movl 0x1c(%rax), %esi
movslq 0x30(%rbx), %rcx
cmpl %edx, %ecx
movq %rdx, 0x20(%rsp)
movl %edx, %eax
movq %rcx, 0x18(%rsp)
cmovll %ecx, %eax
movl %eax, 0x4(%rsp)
movl 0x34(%rbx), %eax
cmpl %esi, %eax
cmovll %eax, %esi
cmpl $0x2, %r12d
movl %esi, (%rsp)
jl 0x209a4
movl $0x1, %r13d
movl $0x5c, %r14d
movl $0x1, %ebp
leaq 0x6b20(%rip), %r15      # 0x2747c
movq (%rbx), %rax
movl -0x4(%rax,%r14), %edx
movl (%rax,%r14), %ecx
cmpl 0x4(%rsp), %edx
cmovll %ebp, %r12d
cmpl %esi, %ecx
cmovll %ebp, %r12d
cmpl $0x1, %r12d
jne 0x20995
movq %r15, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %r8d
movl (%rsp), %r9d
xorl %eax, %eax
callq 0x3796
movl (%rsp), %esi
incq %r13
movslq %r12d, %rax
addq $0x40, %r14
cmpq %rax, %r13
jl 0x2095c
cmpl 0x64(%rbx), %r12d
movl $0x1, %eax
cmovlel %r12d, %eax
movl %eax, 0x10(%rsp)
leaq 0x609f(%rip), %rsi      # 0x26a5b
movq 0x8(%rsp), %rdi
callq 0x3370
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x21232
movl 0x70(%rbx), %eax
movl %eax, %ecx
andl $0x30, %ecx
cmpl $0x10, %ecx
je 0x20a14
testl %ecx, %ecx
movl 0x4(%rsp), %r15d
movq 0x18(%rsp), %rbp
jne 0x20a39
xorl %r13d, %r13d
cmpl $0x4, 0x10(%rsp)
sete %r13b
addl $0x3, %r13d
jmp 0x20a3f
leaq 0x6baf(%rip), %rdi      # 0x275b7
xorl %eax, %eax
callq 0x3796
jmp 0x21232
xorl %r13d, %r13d
movl 0x10(%rsp), %ecx
cmpl $0x5, %ecx
sete %r13b
addl $0x3, %r13d
cmpl $0x4, %ecx
setge %r14b
movl 0x4(%rsp), %r15d
movq 0x18(%rsp), %rbp
jmp 0x20a42
movl $0x3, %r13d
xorl %r14d, %r14d
testl $0xffffffef, %eax       # imm = 0xFFFFFFEF
je 0x20a5c
leaq 0x6b93(%rip), %rsi      # 0x275e3
movl $0x1, %edi
xorl %eax, %eax
callq 0x1d0d6
movl 0x10(%rsp), %eax
cmpl $0x1, %eax
cmovel %eax, %r13d
cmpl $0x2, %eax
cmovel %eax, %r13d
movl 0x60(%rbx), %r12d
cmpl $0x10000, %r12d         # imm = 0x10000
jl 0x20a89
leaq 0x6bbd(%rip), %rdi      # 0x2763f
xorl %eax, %eax
callq 0x3796
cmpl $0x0, 0x64(%rbx)
je 0x20aaf
cmpl $0x1, %r13d
je 0x20c0c
cmpl $0x2, %r13d
jne 0x20c29
leaq 0x6c31(%rip), %rsi      # 0x276db
jmp 0x20c13
movl %r13d, 0x10(%rsp)
movl $0x100, %r14d           # imm = 0x100
cmpl %r14d, %r12d
setge %bpl
movq (%rbx), %rax
movswl 0x22(%rax), %r9d
movswl 0x20(%rax), %r8d
movl %r9d, %r13d
subl %r8d, %r13d
leaq 0x6bb9(%rip), %rsi      # 0x27694
movl $0x3, %edi
movl %r15d, %edx
movl (%rsp), %r12d
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1d0d6
cmpl %r14d, %r13d
movl %r15d, %edx
setge %r15b
cmpl $0x0, 0x68(%rbx)
je 0x20ffd
leaq 0x6bc2(%rip), %rsi      # 0x276ce
xorl %r14d, %r14d
movq 0x8(%rsp), %rdi
movl %r12d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x3400
testl %r12d, %r12d
jle 0x21228
orb %r15b, %bpl
movl 0x10(%rsp), %eax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x20bfa
xorl %r12d, %r12d
movl $0x24, %r13d
movq 0x10(%rsp), %r15
testb %bpl, %bpl
je 0x20b9b
movq (%rbx), %rax
leaq (%rax,%r13), %rcx
movl -0xc(%rax,%r13), %edx
imull %r14d, %edx
addl %r12d, %edx
movslq %edx, %rdx
movq -0x24(%rax,%r13), %rsi
movq -0x1c(%rax,%r13), %rdi
subq %rsi, %rdi
sarq %rdi
cmpq %rdx, %rdi
leaq (%rsi,%rdx,2), %rdx
cmovbeq %rcx, %rdx
movswl (%rdx), %edi
movswl 0x20(%rax), %eax
subl %eax, %edi
sarl $0x8, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
leaq (%rax,%r13), %rcx
movl -0xc(%rax,%r13), %edx
imull %r14d, %edx
addl %r12d, %edx
movslq %edx, %rdx
movq -0x24(%rax,%r13), %rsi
movq -0x1c(%rax,%r13), %rdi
subq %rsi, %rdi
sarq %rdi
cmpq %rdx, %rdi
leaq (%rsi,%rdx,2), %rdx
cmovbeq %rcx, %rdx
movzwl (%rdx), %ecx
subl 0x20(%rax), %ecx
movzbl %cl, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
addq $0x40, %r13
decq %r15
jne 0x20b4f
incl %r12d
cmpl 0x4(%rsp), %r12d
jne 0x20b44
incl %r14d
cmpl (%rsp), %r14d
jne 0x20b36
jmp 0x21228
leaq 0x6abb(%rip), %rsi      # 0x276ce
movl 0x60(%rbx), %r8d
movq 0x8(%rsp), %rdi
movl %r15d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x3400
cmpl $0x3, %r13d
jne 0x20c4c
movl 0x60(%rbx), %r8d
leaq 0x6a87(%rip), %rsi      # 0x276c1
movq 0x8(%rsp), %rdi
movl %r15d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x3400
cmpl $0x4, %r13d
jne 0x20c6f
movl 0x60(%rbx), %r8d
leaq 0x6ac7(%rip), %rsi      # 0x27724
movq 0x8(%rsp), %rdi
movl %r15d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x3400
movl 0x5c(%rbx), %ecx
movl 0x60(%rbx), %r8d
leaq 0x6aea(%rip), %rsi      # 0x27767
movl $0xa, %edi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1d0d6
testb %r14b, %r14b
je 0x20e3e
leaq 0x6af7(%rip), %rsi      # 0x27793
xorl %ebp, %ebp
movl $0x1, %edi
xorl %eax, %eax
callq 0x1d0d6
cmpl $0x0, (%rsp)
jle 0x21228
movl 0x4(%rsp), %r15d
testl %r15d, %r15d
jle 0x20e2e
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpl $0x100, %r12d           # imm = 0x100
jl 0x20d2f
movl 0x60(%rbx), %ecx
movq (%rbx), %rax
movl 0x18(%rax,%r14), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq (%rax,%r14), %rsi
movswl (%rsi,%rdx,2), %edx
movl %ecx, %esi
subl %edx, %esi
movl 0xd8(%rax), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq 0xc0(%rax), %rax
movswl (%rax,%rdx,2), %eax
imull %esi, %eax
cltd
idivl %ecx
movl %eax, %edx
cmpl %ecx, %eax
jl 0x20d19
movl %ecx, %edx
movl 0x5c(%rbx), %edi
cmpl %edi, %eax
jl 0x20d22
movl %edx, %edi
sarl $0x8, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movl 0x60(%rbx), %ecx
movq (%rbx), %rax
movl 0x18(%rax,%r14), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq (%rax,%r14), %rsi
movswl (%rsi,%rdx,2), %edx
movl %ecx, %esi
subl %edx, %esi
movl 0xd8(%rax), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq 0xc0(%rax), %rax
movswl (%rax,%rdx,2), %eax
imull %esi, %eax
cltd
idivl %ecx
movl 0x5c(%rbx), %edx
movl %eax, %esi
cmpl %ecx, %eax
jl 0x20d7a
movl %ecx, %esi
cmpl %edx, %eax
jl 0x20d80
movl %esi, %edx
movzbl %dl, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
addq $0x40, %r14
cmpq $0xc0, %r14
jne 0x20cc8
cmpl $0x5, 0x10(%rsp)
jne 0x20e22
cmpl $0x100, %r12d           # imm = 0x100
jl 0x20de8
movq (%rbx), %rax
movl 0x118(%rax), %ecx
imull %ebp, %ecx
addl %r13d, %ecx
movslq %ecx, %rcx
movq 0x100(%rax), %rax
movswl (%rax,%rcx,2), %eax
movl 0x5c(%rbx), %ecx
movl 0x60(%rbx), %edi
cmpl %eax, %edi
cmovgel %eax, %edi
cmpl %eax, %ecx
cmovgl %ecx, %edi
sarl $0x8, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
movl 0x118(%rax), %ecx
imull %ebp, %ecx
addl %r13d, %ecx
movslq %ecx, %rcx
movq 0x100(%rax), %rax
movswl (%rax,%rcx,2), %eax
movl 0x5c(%rbx), %ecx
movl 0x60(%rbx), %edx
cmpl %eax, %edx
cmovgel %eax, %edx
cmpl %eax, %ecx
cmovgl %ecx, %edx
movzbl %dl, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
incl %r13d
cmpl %r15d, %r13d
jne 0x20cc5
incl %ebp
cmpl (%rsp), %ebp
jne 0x20cb9
jmp 0x21228
cmpl $0x100, %r12d           # imm = 0x100
jl 0x20f03
cmpl $0x0, (%rsp)
jle 0x21228
movl %r13d, %r14d
xorl %r15d, %r15d
cmpl $0x0, 0x4(%rsp)
jle 0x20ef1
xorl %ebp, %ebp
movq %r14, %r12
xorl %r13d, %r13d
movq (%rbx), %rax
movl 0x18(%rax,%r13), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movq (%rax,%r13), %rax
movswl (%rax,%rcx,2), %eax
movl 0x60(%rbx), %edi
cmpl %eax, %edi
jl 0x20e90
movl %eax, %edi
movl 0x5c(%rbx), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %edi
sarl $0x8, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
movl 0x18(%rax,%r13), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movq (%rax,%r13), %rax
movswl (%rax,%rcx,2), %ecx
movl 0x60(%rbx), %eax
cmpl %ecx, %eax
jl 0x20ec7
movl %ecx, %eax
movl 0x5c(%rbx), %edx
cmpl %ecx, %edx
cmovgl %edx, %eax
movzbl %al, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
addq $0x40, %r13
decq %r12
jne 0x20e6e
incl %ebp
cmpl 0x4(%rsp), %ebp
jne 0x20e68
incl %r15d
cmpl (%rsp), %r15d
jne 0x20e5b
jmp 0x21228
cmpl $0x3, %r13d
movl (%rsp), %eax
jne 0x211be
testl %eax, %eax
jle 0x21228
movq 0x20(%rsp), %rax
cmpq %rbp, %rax
cmovlq %rax, %rbp
xorl %r14d, %r14d
movl $0xff, %r15d
cmpl $0x0, 0x4(%rsp)
jle 0x20feb
movslq %r14d, %r12
xorl %r13d, %r13d
movq (%rbx), %rax
movslq 0x18(%rax), %rcx
imulq %r12, %rcx
addq %r13, %rcx
movq (%rax), %rax
movswl (%rax,%rcx,2), %eax
movl 0x60(%rbx), %edi
cmpl %eax, %edi
jl 0x20f5c
movl %eax, %edi
movl 0x5c(%rbx), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %edi
andl %r15d, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
movslq 0x58(%rax), %rcx
imulq %r12, %rcx
addq %r13, %rcx
movq 0x40(%rax), %rax
movswl (%rax,%rcx,2), %eax
movl 0x60(%rbx), %edi
cmpl %eax, %edi
jl 0x20f90
movl %eax, %edi
movl 0x5c(%rbx), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %edi
andl %r15d, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
movslq 0x98(%rax), %rcx
imulq %r12, %rcx
addq %r13, %rcx
movq 0x80(%rax), %rax
movswl (%rax,%rcx,2), %ecx
movl 0x5c(%rbx), %eax
movl 0x60(%rbx), %edi
cmpl %ecx, %edi
jl 0x20fcd
movl %ecx, %edi
cmpl %ecx, %eax
cmovgl %eax, %edi
andl %r15d, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
incq %r13
cmpl %r13d, %ebp
jne 0x20f3e
incl %r14d
cmpl (%rsp), %r14d
jne 0x20f2d
jmp 0x21228
leaq 0x66bd(%rip), %rsi      # 0x276c1
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
movl %r12d, %ecx
movl $0xff, %r8d
xorl %eax, %eax
callq 0x3400
testl %r12d, %r12d
jle 0x21228
movl 0x10(%rsp), %eax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x211b1
xorl %r13d, %r13d
movl $0x24, %r14d
movq 0x10(%rsp), %r15
movq (%rbx), %rax
leaq (%rax,%r14), %rcx
movl -0xc(%rax,%r14), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq -0x24(%rax,%r14), %rsi
movq -0x1c(%rax,%r14), %rax
subq %rsi, %rax
sarq %rax
cmpq %rdx, %rax
leaq (%rsi,%rdx,2), %rax
cmovbeq %rcx, %rax
movswl (%rax), %eax
xorl %r12d, %r12d
movl $0x0, %edi
cmpl $-0x15, %eax
jl 0x210b3
movl $0xff, %edi
cmpl $0x156a, %eax           # imm = 0x156A
jg 0x210b3
movl %eax, %ecx
shll $0x8, %ecx
subl %eax, %ecx
movslq %ecx, %rdi
imulq $-0x3ffcfff3, %rdi, %rax # imm = 0xC003000D
shrq $0x20, %rax
addl %eax, %edi
movl %edi, %eax
shrl $0x1f, %eax
sarl $0xc, %edi
addl %eax, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
leaq (%rax,%r14), %rcx
movl -0xc(%rax,%r14), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq -0x24(%rax,%r14), %rsi
movq -0x1c(%rax,%r14), %rax
subq %rsi, %rax
sarq %rax
cmpq %rdx, %rax
leaq (%rsi,%rdx,2), %rax
cmovbeq %rcx, %rax
movswl (%rax), %eax
cmpl $-0x6d, %eax
jl 0x21123
movl $0xff, %r12d
cmpl $0x6d38, %eax           # imm = 0x6D38
jg 0x21123
imull $0x32, %eax, %eax
movslq %eax, %r12
imulq $-0x3ffcfff3, %r12, %rax # imm = 0xC003000D
shrq $0x20, %rax
addl %eax, %r12d
movl %r12d, %eax
shrl $0x1f, %eax
sarl $0xc, %r12d
addl %eax, %r12d
movl %r12d, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
movq (%rbx), %rax
leaq (%rax,%r14), %rcx
movl -0xc(%rax,%r14), %edx
imull %ebp, %edx
addl %r13d, %edx
movslq %edx, %rdx
movq -0x24(%rax,%r14), %rsi
movq -0x1c(%rax,%r14), %rax
subq %rsi, %rax
sarq %rax
cmpq %rdx, %rax
leaq (%rsi,%rdx,2), %rax
cmovbeq %rcx, %rax
movswl (%rax), %eax
xorl %edi, %edi
cmpl $0xfffffe39, %eax       # imm = 0xFFFFFE39
jl 0x2118c
shll $0x2, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
imulq $-0x3ffcfff3, %rdi, %rax # imm = 0xC003000D
shrq $0x20, %rax
addl %eax, %edi
movl %edi, %eax
shrl $0x1f, %eax
sarl $0xc, %edi
addl %eax, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
addq $0x40, %r14
decq %r15
jne 0x21046
incl %r13d
cmpl 0x4(%rsp), %r13d
jne 0x2103b
incl %ebp
cmpl (%rsp), %ebp
jne 0x2102d
jmp 0x21228
testl %eax, %eax
jle 0x21228
movl %r13d, %r14d
xorl %r15d, %r15d
cmpl $0x0, 0x4(%rsp)
jle 0x2121f
xorl %ebp, %ebp
movq %r14, %r12
xorl %r13d, %r13d
movq (%rbx), %rax
movl 0x18(%rax,%r13), %ecx
imull %r15d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movq (%rax,%r13), %rax
movswl (%rax,%rcx,2), %ecx
movl 0x60(%rbx), %eax
cmpl %ecx, %eax
jl 0x211f9
movl %ecx, %eax
movl 0x5c(%rbx), %edx
cmpl %ecx, %edx
cmovgl %edx, %eax
movzbl %al, %edi
movq 0x8(%rsp), %rsi
callq 0x33a0
addq $0x40, %r13
decq %r12
jne 0x211d7
incl %ebp
cmpl 0x4(%rsp), %ebp
jne 0x211d1
incl %r15d
cmpl (%rsp), %r15d
jne 0x211c8
movq 0x8(%rsp), %rdi
callq 0x32a0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq