// File: 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:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x138(%rsp)
movq %rsi, 0x130(%rsp)
movq 0x130(%rsp), %rdi
callq 0x4b80
movl %eax, 0x12c(%rsp)
movb $0x0, 0x12b(%rsp)
movl 0x12c(%rsp), %eax
movq 0x130(%rsp), %rcx
movl 0x70(%rcx), %esi
andl $0x30, %esi
movl $0x4, %ecx
movl $0x5, %edx
cmpl $0x10, %esi
cmovel %edx, %ecx
cmpl %ecx, %eax
jle 0x3f604
movl 0x12c(%rsp), %esi
leaq 0x8d23(%rip), %rdi      # 0x4830d
movb $0x0, %al
callq 0x3a670
movl $0x3, 0x12c(%rsp)
movb $0x1, 0x12b(%rsp)
cmpl $0x0, 0x12c(%rsp)
jne 0x3f629
leaq 0x8ec9(%rip), %rdi      # 0x484de
movb $0x0, %al
callq 0x3a670
movb $0x0, 0x147(%rsp)
jmp 0x40ed0
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movl 0x18(%rax), %eax
movl %eax, 0x124(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movl 0x1c(%rax), %eax
movl %eax, 0x120(%rsp)
movq 0x130(%rsp), %rax
movl 0x30(%rax), %eax
cmpl 0x124(%rsp), %eax
jge 0x3f685
movq 0x130(%rsp), %rax
movl 0x30(%rax), %eax
movl %eax, 0x124(%rsp)
movq 0x130(%rsp), %rax
movl 0x34(%rax), %eax
cmpl 0x120(%rsp), %eax
jge 0x3f6ab
movq 0x130(%rsp), %rax
movl 0x34(%rax), %eax
movl %eax, 0x120(%rsp)
movl $0x1, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
cmpl 0x12c(%rsp), %eax
jge 0x3f7b5
movq 0x130(%rsp), %rdi
movslq 0x11c(%rsp), %rsi
callq 0x13390
movl 0x18(%rax), %eax
cmpl 0x124(%rsp), %eax
jge 0x3f6f6
movl $0x1, 0x12c(%rsp)
movq 0x130(%rsp), %rdi
movslq 0x11c(%rsp), %rsi
callq 0x13390
movl 0x1c(%rax), %eax
cmpl 0x120(%rsp), %eax
jge 0x3f722
movl $0x1, 0x12c(%rsp)
cmpl $0x1, 0x12c(%rsp)
jne 0x3f79d
movl 0x11c(%rsp), %eax
movl %eax, 0xb0(%rsp)
movq 0x130(%rsp), %rdi
movslq 0x11c(%rsp), %rsi
callq 0x13390
movl 0x18(%rax), %eax
movl %eax, 0xb4(%rsp)
movq 0x130(%rsp), %rdi
movslq 0x11c(%rsp), %rsi
callq 0x13390
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
movl 0x1c(%rax), %ecx
movl 0x124(%rsp), %r8d
movl 0x120(%rsp), %r9d
leaq 0x8be8(%rip), %rdi      # 0x4837e
movb $0x0, %al
callq 0x3a670
jmp 0x3f79f
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x3f6b6
movl 0x12c(%rsp), %eax
movq 0x130(%rsp), %rcx
cmpl 0x64(%rcx), %eax
jle 0x3f7dc
movb $0x1, 0x12b(%rsp)
movl $0x1, 0x12c(%rsp)
movq 0x138(%rsp), %rdi
leaq 0x816c(%rip), %rsi      # 0x47957
callq 0x3460
movq %rax, 0x110(%rsp)
cmpq $0x0, 0x110(%rsp)
jne 0x3f810
movb $0x0, 0x147(%rsp)
jmp 0x40ed0
movl $0x3, 0x10c(%rsp)
movb $0x0, 0x10b(%rsp)
movq 0x130(%rsp), %rax
movl 0x70(%rax), %eax
andl $0x30, %eax
cmpl $0x0, %eax
jne 0x3f84d
cmpl $0x4, 0x12c(%rsp)
jne 0x3f84b
movl $0x4, 0x10c(%rsp)
jmp 0x3f88b
movq 0x130(%rsp), %rax
movl 0x70(%rax), %eax
andl $0x30, %eax
cmpl $0x10, %eax
jne 0x3f889
cmpl $0x5, 0x12c(%rsp)
jne 0x3f875
movl $0x4, 0x10c(%rsp)
cmpl $0x3, 0x12c(%rsp)
jle 0x3f887
movb $0x1, 0x10b(%rsp)
jmp 0x3f889
jmp 0x3f88b
movq 0x130(%rsp), %rax
cmpl $0x0, 0x70(%rax)
je 0x3f8ba
movq 0x130(%rsp), %rax
cmpl $0x10, 0x70(%rax)
je 0x3f8ba
movl $0x1, %edi
leaq 0x8c57(%rip), %rsi      # 0x4850a
movb $0x0, %al
callq 0x3a770
cmpl $0x1, 0x12c(%rsp)
jne 0x3f8cf
movl $0x1, 0x10c(%rsp)
cmpl $0x2, 0x12c(%rsp)
jne 0x3f8e4
movl $0x2, 0x10c(%rsp)
movl $0x8, 0x104(%rsp)
movl $0x1, 0x100(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movswl 0x20(%rax), %eax
cmpl $0x0, %eax
jge 0x3f92a
movl $0x8, 0x104(%rsp)
movl $0x1, 0x100(%rsp)
movq 0x130(%rsp), %rax
cmpl $0xff, 0x60(%rax)
jle 0x3f951
movl $0x10, 0x104(%rsp)
movl $0x2, 0x100(%rsp)
movq 0x130(%rsp), %rax
cmpl $0xffff, 0x60(%rax)     # imm = 0xFFFF
jle 0x3f970
leaq 0x8bfd(%rip), %rdi      # 0x48566
movb $0x0, %al
callq 0x3a670
movq 0x130(%rsp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x3ffe9
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movswl 0x22(%rax), %eax
movl %eax, 0xa0(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movq %rax, %rcx
movl 0xa0(%rsp), %eax
movswl 0x20(%rcx), %ecx
subl %ecx, %eax
movl %eax, 0xfc(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0xa8(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movswl 0x20(%rax), %eax
movl %eax, 0xac(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movl 0xa4(%rsp), %edx
movl 0xa8(%rsp), %ecx
movl 0xac(%rsp), %r8d
movswl 0x22(%rax), %r9d
movl $0x3, %edi
leaq 0x8b85(%rip), %rsi      # 0x485bb
movb $0x0, %al
callq 0x3a770
cmpl $0xff, 0xfc(%rsp)
jle 0x3fa60
movl $0x10, 0x104(%rsp)
movl $0x2, 0x100(%rsp)
movq 0x130(%rsp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x3fe2f
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
leaq 0x8b59(%rip), %rsi      # 0x485e8
movl $0xff, %r8d
movb $0x0, %al
callq 0x3500
movl $0x0, 0xf8(%rsp)
movl 0xf8(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x3fe2a
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x3fe12
movl $0x0, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x3fdfa
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xff, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0x0, %eax
jge 0x3fb46
xorl %eax, %eax
movl %eax, 0x9c(%rsp)
jmp 0x3fbe4
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xff, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0xff, %eax
jle 0x3fb95
movl $0xff, %eax
movl %eax, 0x98(%rsp)
jmp 0x3fbd6
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xff, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0x32, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0x0, %eax
jge 0x3fc42
xorl %eax, %eax
movl %eax, 0x94(%rsp)
jmp 0x3fcda
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0x32, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0xff, %eax
jle 0x3fc8e
movl $0xff, %eax
movl %eax, 0x90(%rsp)
jmp 0x3fccc
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0x32, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xc, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0x0, %eax
jge 0x3fd38
xorl %eax, %eax
movl %eax, 0x8c(%rsp)
jmp 0x3fdd0
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xc, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
cmpl $0xff, %eax
jle 0x3fd84
movl $0xff, %eax
movl %eax, 0x88(%rsp)
jmp 0x3fdc2
movq 0x130(%rsp), %rdi
movslq 0xf0(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xf8(%rsp), %esi
movl 0xf4(%rsp), %edx
callq 0x12ba0
cwtl
imull $0xc, %eax, %eax
movl $0x1555, %ecx           # imm = 0x1555
cltd
idivl %ecx
movl %eax, 0x88(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x3fae5
jmp 0x3fdfc
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x3fac6
jmp 0x3fe14
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x3faa7
jmp 0x3ffcf
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movl 0xfc(%rsp), %r8d
leaq 0x87a1(%rip), %rsi      # 0x485f5
movb $0x0, %al
callq 0x3500
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x3ffcd
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x3ffb5
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x3ff9d
cmpl $0x1, 0x100(%rsp)
jle 0x3ff23
movq 0x130(%rsp), %rdi
movslq 0xe4(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xec(%rsp), %esi
movl 0xe8(%rsp), %edx
callq 0x12ba0
cwtl
movl %eax, 0x84(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movl 0x84(%rsp), %edi
movswl 0x20(%rax), %eax
subl %eax, %edi
sarl $0x8, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movslq 0xe4(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xec(%rsp), %esi
movl 0xe8(%rsp), %edx
callq 0x12ba0
cwtl
movl %eax, 0x80(%rsp)
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movl 0x80(%rsp), %edi
movswl 0x20(%rax), %eax
subl %eax, %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x3fea4
jmp 0x3ff9f
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x3fe85
jmp 0x3ffb7
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3fe66
jmp 0x3ffcf
movq 0x110(%rsp), %rdi
callq 0x3350
movb $0x0, 0x147(%rsp)
jmp 0x40ed0
cmpl $0x1, 0x10c(%rsp)
jne 0x40023
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movq 0x130(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x85d9(%rip), %rsi      # 0x485f5
movb $0x0, %al
callq 0x3500
cmpl $0x2, 0x10c(%rsp)
jne 0x4005d
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movq 0x130(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x85ac(%rip), %rsi      # 0x48602
movb $0x0, %al
callq 0x3500
cmpl $0x3, 0x10c(%rsp)
jne 0x40097
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movq 0x130(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x8558(%rip), %rsi      # 0x485e8
movb $0x0, %al
callq 0x3500
cmpl $0x4, 0x10c(%rsp)
jne 0x400d1
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movq 0x130(%rsp), %rax
movl 0x60(%rax), %r8d
leaq 0x8581(%rip), %rsi      # 0x4864b
movb $0x0, %al
callq 0x3500
movl 0x10c(%rsp), %edx
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %ecx
movq 0x130(%rsp), %rax
movl 0x60(%rax), %r8d
movl $0xa, %edi
leaq 0x8593(%rip), %rsi      # 0x4868e
movb $0x0, %al
callq 0x3a770
testb $0x1, 0x10b(%rsp)
je 0x407a9
movl $0x1, %edi
leaq 0x859e(%rip), %rsi      # 0x486ba
movb $0x0, %al
callq 0x3a770
movl $0x0, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x407a4
movl $0x0, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x4078c
movl $0x0, 0xd8(%rsp)
cmpl $0x3, 0xd8(%rsp)
jge 0x405a7
cmpl $0x1, 0x100(%rsp)
jle 0x4038b
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x78(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x78(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x7c(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x7c(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40234
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x74(%rsp)
jmp 0x40377
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x6c(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x6c(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x70(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x70(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x402e0
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x68(%rsp)
jmp 0x4036f
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x60(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x60(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x64(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x64(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movl %eax, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %edi
sarl $0x8, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x58(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x58(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x5c(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40437
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x54(%rsp)
jmp 0x4057a
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x4c(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x4c(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x50(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x50(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x404e3
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x48(%rsp)
jmp 0x40572
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x40(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x40(%rsp), %eax
movswl %cx, %ecx
subl %ecx, %eax
movl %eax, 0x44(%rsp)
movq 0x130(%rsp), %rdi
movl $0x3, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
movw %ax, %cx
movl 0x44(%rsp), %eax
movswl %cx, %ecx
imull %ecx, %eax
movq 0x130(%rsp), %rcx
cltd
idivl 0x60(%rcx)
movl %eax, 0x48(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0x4016c
cmpl $0x5, 0x12c(%rsp)
jne 0x40774
cmpl $0x1, 0x100(%rsp)
jle 0x4069a
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x4060a
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x3c(%rsp)
jmp 0x40686
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40651
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x38(%rsp)
jmp 0x4067e
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %edi
sarl $0x8, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x406e1
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x34(%rsp)
jmp 0x4075d
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40728
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x30(%rsp)
jmp 0x40755
movq 0x130(%rsp), %rdi
movl $0x4, %esi
callq 0x13390
movq %rax, %rdi
movl 0xe0(%rsp), %esi
movl 0xdc(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
jmp 0x40776
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x4014d
jmp 0x4078e
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x4012e
jmp 0x40ebb
cmpl $0x1, 0x100(%rsp)
jle 0x40a28
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x40a23
movl $0x0, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x40a0b
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x409f3
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40861
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x2c(%rsp)
jmp 0x408e3
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x408ab
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x28(%rsp)
jmp 0x408db
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %edi
sarl $0x8, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40944
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x24(%rsp)
jmp 0x409c6
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x4098e
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x20(%rsp)
jmp 0x409be
movq 0x130(%rsp), %rdi
movslq 0xcc(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xd4(%rsp), %esi
movl 0xd0(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x40800
jmp 0x409f5
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x407e1
jmp 0x40a0d
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x407c2
jmp 0x40eb9
cmpl $0x3, 0x10c(%rsp)
jne 0x40d32
movl $0x0, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x40d2d
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x40d15
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40aba
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x1c(%rsp)
jmp 0x40b34
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40b00
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x40b2c
movq 0x130(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movl $0x1, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40b92
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x14(%rsp)
jmp 0x40c0e
movq 0x130(%rsp), %rdi
movl $0x1, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40bd9
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x40c06
movq 0x130(%rsp), %rdi
movl $0x1, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movq 0x130(%rsp), %rdi
movl $0x2, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40c6c
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x40ce8
movq 0x130(%rsp), %rdi
movl $0x2, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40cb3
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x8(%rsp)
jmp 0x40ce0
movq 0x130(%rsp), %rdi
movl $0x2, %esi
callq 0x13390
movq %rax, %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x40a60
jmp 0x40d17
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x40a41
jmp 0x40eb7
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0x40eb5
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x40e9d
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x40e85
movq 0x130(%rsp), %rdi
movslq 0xb8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xc0(%rsp), %esi
movl 0xbc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x40dd9
movq 0x130(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x4(%rsp)
jmp 0x40e58
movq 0x130(%rsp), %rdi
movslq 0xb8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xc0(%rsp), %esi
movl 0xbc(%rsp), %edx
callq 0x131b0
cwtl
movq 0x130(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x40e22
movq 0x130(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, (%rsp)
jmp 0x40e51
movq 0x130(%rsp), %rdi
movslq 0xb8(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0xc0(%rsp), %esi
movl 0xbc(%rsp), %edx
callq 0x131b0
cwtl
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %edi
andl $0xff, %edi
movq 0x110(%rsp), %rsi
callq 0x34a0
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x40d7b
jmp 0x40e87
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x40d5c
jmp 0x40e9f
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x40d3d
jmp 0x40eb7
jmp 0x40eb9
jmp 0x40ebb
movq 0x110(%rsp), %rdi
callq 0x3350
movb $0x0, 0x147(%rsp)
movb 0x147(%rsp), %al
andb $0x1, %al
addq $0x148, %rsp            # imm = 0x148
retq
nopw %cs:(%rax,%rax)