// File: /workspace/github_cpp_makefile_star2_O1/cloudinary[P]fuif/./export/write_png.h
// Function: write_PNG_file(char const*, Image const&)
bool write_PNG_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 PNG.\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;}

  if (w > 0x7fffffffL || h > 0x7fffffffL) {
    printf("Image too large to be saved as PNG.\n");
    return false;
  }

  FILE *fp = fopen(output_image,"wb");
  if (!fp) {
    return false;
  }
  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,(png_voidp) NULL,NULL,NULL);
  if (!png_ptr) {
    fclose(fp);
    return false;
  }

  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_write_struct(&png_ptr,(png_infopp) NULL);
    fclose(fp);
    return false;
  }

  png_init_io(png_ptr,fp);
  png_set_user_limits(png_ptr, 0x7fffffffL, 0x7fffffffL);
//  png_set_filter(png_ptr,0,PNG_FILTER_PAETH);
//  png_set_compression_level(png_ptr,Z_BEST_COMPRESSION);


  int colortype=PNG_COLOR_TYPE_RGB;
  bool cmyk = false;
  if ((image.colormodel & 48) == 0) { // RGB
    if (nb_channels == 4) colortype=PNG_COLOR_TYPE_RGB_ALPHA;
  } else if ((image.colormodel & 48) == 16) { // CMYK
    if (nb_channels == 5) colortype=PNG_COLOR_TYPE_RGB_ALPHA;
    if (nb_channels > 3) cmyk = true;
  }

  if (nb_channels == 1) colortype=PNG_COLOR_TYPE_GRAY;
  if (nb_channels == 2) colortype=PNG_COLOR_TYPE_GRAY_ALPHA;
  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("Cannot save as PNG since bit depth is higher than 16-bit.\n");    return false; }

  if (image.channel[0].maxval > 255 && debugstuff && nb_channels == 1) {colortype=PNG_COLOR_TYPE_RGB; nb_channels=3;}

  int shift = 0;
//  if (image.channel[0].maxval == 2048) shift = 5;  // could use something like this to debug DCT values

  png_set_IHDR(png_ptr,info_ptr,w,h,bit_depth,colortype,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

  png_write_info(png_ptr,info_ptr);

  png_bytep row = (png_bytep) png_malloc(png_ptr,nb_channels * bytes_per_value * w);

  v_printf(10,"Writing PNG file (range %i..%i)\n",image.minval,image.maxval);
  if (image.channel[0].maxval > 255 && debugstuff) {
    v_printf(10,"Writing PNG file with false colors for debug purposes\n");
    for (size_t r = 0; r < (size_t) h; r++) {
     for (size_t c = 0; c < (size_t) w; c++) {
        row[c * 3 + 0] = (png_byte) (CLAMP((image.channel[0].value(r,c)%10)*26,0,255));
        row[c * 3 + 1] = (png_byte) (CLAMP(image.channel[0].value(r,c)/10,0,255));
        row[c * 3 + 2] = (png_byte) (CLAMP(image.channel[0].value(r,c)/500,0,255));
     }
     png_write_row(png_ptr,row);
    }
  } else if (false && image.channel[0].minval < 0) {
    v_printf(10,"Writing PNG file with negative pixels\n");
   // visualize the negative pixels somehow, for debugging.
   for (size_t r = 0; r < (size_t) h; r++) {
     for (size_t c = 0; c < (size_t) w; c++) {
      for (int p=0; p<nb_channels; p++) {
        row[c * nb_channels + p] = (png_byte) (CLAMP(127+(image.channel[p].value(r,c)),0,255));
      }
     }
    png_write_row(png_ptr,row);
    }
  } else if (cmyk) {
   v_printf(1,"Warning: converting CMYK image to RGB in order to save it as PNG.\n");
   for (size_t r = 0; r < (size_t) h; r++) {
     for (size_t c = 0; c < (size_t) w; c++) {
      for (int p=0; p<3; p++) {
        row[c * (nb_channels-1) + p] = (png_byte) (CLAMP( (255-image.channel[p].value(r,c))*(image.channel[3].value(r,c))/255,0,255));
      }
      if (nb_channels == 5) row[c * (nb_channels-1) + 3] = (png_byte) (CLAMP(image.channel[4].value(r,c),0,255)); // cmyk+alpha

     }
    png_write_row(png_ptr,row);
    }
  } else for (size_t r = 0; r < (size_t) h; r++) {
    if (bytes_per_value == 1) {
     for (size_t c = 0; c < (size_t) w; c++) {
      for (int p=0; p<nb_channels; p++) {
        row[c * nb_channels + p] = (png_byte) (CLAMP(image.channel[p].value(r,c),image.minval,image.maxval));
      }
     }
    } else {
     if (!debugstuff) {
     for (size_t c = 0; c < (size_t) w; c++) {
      for (int p=0; p<nb_channels; p++) {
        row[c * nb_channels * 2 + 2*p] = (png_byte) ((CLAMP(image.channel[p].value(r,c),image.minval,image.maxval)<<shift) >> 8);
        row[c * nb_channels * 2 + 2*p + 1] = (png_byte) ((CLAMP(image.channel[p].value(r,c),image.minval,image.maxval)<<shift) & 0xff);
      }
     }
     } else {
     for (size_t c = 0; c < (size_t) w; c++) {
      for (int p=0; p<nb_channels; p++) {
        row[c * nb_channels * 2 + 2*p] = (png_byte) ((image.channel[p].value(r,c)) >> 8);
        row[c * nb_channels * 2 + 2*p + 1] = (png_byte) ((image.channel[p].value(r,c)) & 0xff);
      }
     }
     }
    }
    png_write_row(png_ptr,row);
  }

  png_free(png_ptr,row);
  png_write_end(png_ptr,info_ptr);
  png_destroy_write_struct(&png_ptr,&info_ptr);
  fclose(fp);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r15
movq 0x8(%rsi), %rbx
subq (%rsi), %rbx
shrq $0x6, %rbx
movl 0x70(%rsi), %eax
andl $0x30, %eax
xorl %r14d, %r14d
cmpl $0x10, %eax
sete %r14b
orl $0x4, %r14d
movl %ebx, %ebp
cmpl %ebx, %r14d
movq %rsi, 0x10(%rsp)
jge 0x20002
leaq 0x741c(%rip), %rdi      # 0x2740b
movl %ebx, %esi
xorl %eax, %eax
callq 0x3796
movq 0x10(%rsp), %rsi
movl $0x3, %ebp
testl %ebp, %ebp
je 0x20143
movq %r15, 0x8(%rsp)
movl %r14d, 0x24(%rsp)
movq %rbx, 0x28(%rsp)
movq (%rsi), %rax
movl 0x18(%rax), %edi
movl 0x1c(%rax), %r12d
movl 0x30(%rsi), %eax
cmpl %edi, %eax
cmovll %eax, %edi
movl 0x34(%rsi), %eax
cmpl %r12d, %eax
cmovll %eax, %r12d
cmpl $0x2, %ebp
movl %edi, 0x4(%rsp)
jl 0x2009f
movl $0x1, %r13d
movl $0x5c, %ebx
movl $0x1, %r15d
leaq 0x7426(%rip), %r14      # 0x2747c
movq (%rsi), %rax
movl -0x4(%rax,%rbx), %edx
movl (%rax,%rbx), %ecx
cmpl %edi, %edx
cmovll %r15d, %ebp
cmpl %r12d, %ecx
cmovll %r15d, %ebp
cmpl $0x1, %ebp
jne 0x20090
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x3796
movl 0x4(%rsp), %edi
movq 0x10(%rsp), %rsi
incq %r13
movslq %ebp, %rax
addq $0x40, %rbx
cmpq %rax, %r13
jl 0x20056
movl 0x64(%rsi), %r13d
cmpl %r13d, %ebp
movl $0x1, %ebx
cmovlel %ebp, %ebx
leaq 0x69a6(%rip), %rsi      # 0x26a5b
movq 0x8(%rsp), %rdi
callq 0x3370
testq %rax, %rax
je 0x208a5
movq %rax, %r15
leaq 0x708e(%rip), %rdi      # 0x27160
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3100
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x20162
movq %rax, %rdi
callq 0x3590
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x20156
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x35d0
movq 0x18(%rsp), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
callq 0x34a0
movq 0x10(%rsp), %rdi
movl 0x70(%rdi), %eax
andl $0x30, %eax
cmpl $0x10, %eax
je 0x2016a
testl %eax, %eax
movl 0x4(%rsp), %edx
jne 0x20185
xorl %eax, %eax
cmpl $0x4, %ebx
sete %al
leal 0x2(,%rax,4), %eax
jmp 0x2018a
leaq 0x7306(%rip), %rdi      # 0x27450
xorl %eax, %eax
callq 0x3796
jmp 0x208a5
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x3230
movq %r15, %rdi
jmp 0x208a0
xorl %eax, %eax
cmpl $0x5, %ebx
sete %al
leal 0x2(,%rax,4), %eax
cmpl $0x4, %ebx
setge %cl
movl 0x4(%rsp), %edx
jmp 0x2018c
movl $0x2, %eax
xorl %ecx, %ecx
movl 0x60(%rdi), %esi
cmpl $0x10000, %esi          # imm = 0x10000
jl 0x201a0
leaq 0x7343(%rip), %rdi      # 0x274e1
jmp 0x2014a
movl %ecx, 0x30(%rsp)
movslq %edx, %r14
movslq %r12d, %rcx
movq %rcx, 0x38(%rsp)
xorl %r8d, %r8d
movl %esi, 0x4c(%rsp)
cmpl $0x100, %esi            # imm = 0x100
setge %r8b
movq %r8, 0x8(%rsp)
cmpl %r13d, %ebp
setg %cl
movl 0x24(%rsp), %esi
cmpl 0x28(%rsp), %esi
movl %ebx, %r11d
setl %bl
movl %edx, %ebp
leal 0x8(,%r8,8), %r8d
xorl %r10d, %r10d
cmpl $0x1, %r11d
sete %dl
cmovel %r10d, %eax
cmpl $0x2, %r11d
movl $0x4, %esi
cmovnel %eax, %esi
movq (%rdi), %rax
movw $0x100, %di             # imm = 0x100
cmpw %di, 0x22(%rax)
setge %al
orb %cl, %bl
andb %bl, %dl
testb %dl, %al
movl $0x2, %r9d
cmovel %esi, %r9d
movl $0x3, %r13d
cmovel %r11d, %r13d
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %rsi
subq $0x8, %rsp
movl %ebp, %edx
movl %r12d, %ecx
pushq %r10
pushq %r10
pushq %r10
callq 0x3540
addq $0x20, %rsp
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x31c0
movl %r13d, %eax
movq 0x8(%rsp), %rcx
shll %cl, %eax
movq 0x18(%rsp), %rdi
imull %ebp, %eax
movslq %eax, %rsi
callq 0x3550
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movl 0x5c(%rax), %edx
movq 0x10(%rsp), %rax
movl 0x60(%rax), %ecx
leaq 0x7290(%rip), %rsi      # 0x2751c
movl $0xa, %edi
xorl %eax, %eax
callq 0x1d0d6
movq 0x10(%rsp), %rax
movq (%rax), %rax
movw $0x100, %cx             # imm = 0x100
cmpw %cx, 0x22(%rax)
movq %r15, 0x50(%rsp)
jl 0x2044a
testb %bl, %bl
je 0x2044a
leaq 0x727b(%rip), %rsi      # 0x2753d
xorl %ebx, %ebx
movl $0xa, %edi
xorl %eax, %eax
callq 0x1d0d6
testl %r12d, %r12d
movq 0x38(%rsp), %r13
je 0x2086e
cmpq $0x1, %r14
adcq $0x0, %r14
movq 0x8(%rsp), %r15
addq $0x2, %r15
xorl %r12d, %r12d
cmpl $0x0, 0x4(%rsp)
je 0x2042a
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq 0x24(%rax), %rcx
movq %r15, %rdx
xorl %esi, %esi
movl 0x18(%rax), %edi
imull %r12d, %edi
addl %esi, %edi
movslq %edi, %rdi
movq (%rax), %r8
movq 0x8(%rax), %r9
subq %r8, %r9
sarq %r9
cmpq %rdi, %r9
leaq (%r8,%rdi,2), %rdi
cmovbeq %rcx, %rdi
movswq (%rdi), %r8
imulq $0x66666667, %r8, %rdi  # imm = 0x66666667
movq %rdi, %r9
shrq $0x3f, %r9
sarq $0x22, %rdi
addl %r9d, %edi
addl %edi, %edi
leal (%rdi,%rdi,4), %r9d
xorl %edi, %edi
subl %r9d, %r8d
movl $0x0, %r9d
js 0x20369
leal (%r8,%r8,4), %r9d
leal (%r9,%r9,4), %r9d
addl %r8d, %r9d
movb %r9b, -0x2(%rdx)
movl 0x18(%rax), %r8d
imull %r12d, %r8d
addl %esi, %r8d
movslq %r8d, %r8
movq (%rax), %r9
movq 0x8(%rax), %r10
subq %r9, %r10
sarq %r10
cmpq %r8, %r10
leaq (%r9,%r8,2), %r8
cmovbeq %rcx, %r8
movzwl (%r8), %r8d
cmpw $-0x9, %r8w
jl 0x203c2
movswl %r8w, %r8d
movb $-0x1, %dil
cmpl $0x9ff, %r8d            # imm = 0x9FF
jg 0x203c2
imull $0x6667, %r8d, %edi     # imm = 0x6667
movl %edi, %r8d
shrl $0x1f, %r8d
shrl $0x12, %edi
addl %r8d, %edi
movb %dil, -0x1(%rdx)
movl 0x18(%rax), %edi
imull %r12d, %edi
addl %esi, %edi
movslq %edi, %rdi
movq (%rax), %r8
movq 0x8(%rax), %r9
subq %r8, %r9
sarq %r9
cmpq %rdi, %r9
leaq (%r8,%rdi,2), %rdi
cmovbeq %rcx, %rdi
movswl (%rdi), %edi
imull $0xffff8313, %edi, %r8d # imm = 0xFFFF8313
shrl $0x10, %r8d
addl %edi, %r8d
movzwl %r8w, %r8d
movl %r8d, %r9d
shrl $0xf, %r9d
shrl $0x8, %r8d
addl %r9d, %r8d
cmpl $0xfffffe0d, %edi       # imm = 0xFFFFFE0D
cmovll %ebx, %r8d
movb %r8b, (%rdx)
incq %rsi
addq $0x3, %rdx
cmpq %rsi, %r14
jne 0x2030e
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3160
incq %r12
cmpq %r13, %r12
jne 0x202f2
jmp 0x2086e
cmpb $0x0, 0x30(%rsp)
je 0x205ff
leaq 0x7118(%rip), %rsi      # 0x27574
movl $0x1, %edi
xorl %eax, %eax
callq 0x1d0d6
testl %r12d, %r12d
je 0x2086e
leal -0x1(%r13), %eax
movslq %eax, %r15
cmpq $0x1, %r14
adcq $0x0, %r14
xorl %r12d, %r12d
cmpl $0x0, 0x4(%rsp)
je 0x205dd
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq 0xe4(%rax), %rcx
leaq 0x124(%rax), %rdx
movq %rdx, 0x28(%rsp)
leaq 0x24(%rax), %rsi
movq 0x8(%rsp), %rdi
xorl %r8d, %r8d
movq $-0x3, %r9
movq %rsi, %r10
movl -0xc(%r10), %r11d
imull %r12d, %r11d
addl %r8d, %r11d
movslq %r11d, %r11
movq -0x24(%r10), %rbx
movq -0x1c(%r10), %rbp
subq %rbx, %rbp
sarq %rbp
cmpq %r11, %rbp
leaq (%rbx,%r11,2), %r11
cmovbeq %r10, %r11
movswl (%r11), %r11d
movl $0xff, %ebp
subl %r11d, %ebp
movl 0xd8(%rax), %r11d
imull %r12d, %r11d
addl %r8d, %r11d
movslq %r11d, %r11
movq 0xc0(%rax), %rbx
movq 0xc8(%rax), %rdx
subq %rbx, %rdx
sarq %rdx
cmpq %r11, %rdx
leaq (%rbx,%r11,2), %rdx
cmovbeq %rcx, %rdx
movswl (%rdx), %ebx
imull %ebp, %ebx
cmpl $0xffffff02, %ebx       # imm = 0xFFFFFF02
jge 0x20535
xorl %r11d, %r11d
jmp 0x2055e
movb $-0x1, %r11b
cmpl $0xfeff, %ebx           # imm = 0xFEFF
jg 0x2055e
movslq %ebx, %rdx
imulq $-0x7f7f7f7f, %rdx, %r11 # imm = 0x80808081
shrq $0x20, %r11
addl %ebx, %r11d
movl %r11d, %edx
shrl $0x1f, %edx
shrl $0x7, %r11d
addl %edx, %r11d
movb %r11b, 0x3(%rdi,%r9)
addq $0x40, %r10
incq %r9
jne 0x204bf
cmpl $0x5, %r13d
jne 0x205ce
movl 0x118(%rax), %edx
imull %r12d, %edx
addl %r8d, %edx
movslq %edx, %rdx
movq 0x100(%rax), %r9
movq 0x108(%rax), %r10
subq %r9, %r10
sarq %r10
cmpq %rdx, %r10
leaq (%r9,%rdx,2), %rdx
cmovbeq 0x28(%rsp), %rdx
movswl (%rdx), %edx
movl $0xff, %r9d
cmpl %r9d, %edx
cmovgel %r9d, %edx
testw %dx, %dx
movl $0x0, %r9d
cmovlel %r9d, %edx
movq 0x8(%rsp), %r9
movb %dl, 0x3(%r9,%r8,4)
incq %r8
addq %r15, %rdi
cmpq %r14, %r8
jne 0x204b5
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3160
incq %r12
cmpq 0x38(%rsp), %r12
jne 0x20483
jmp 0x2086e
movb %bl, 0x24(%rsp)
testl %r12d, %r12d
je 0x2086e
movslq %r13d, %rax
movq %rax, %rbp
addq %rax, %rax
movq %rax, 0x28(%rsp)
cmpq $0x1, %r14
adcq $0x0, %r14
movl %r13d, %r15d
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
cmpl $0xff, 0x4c(%rsp)
jle 0x206f7
cmpb $0x0, 0x24(%rsp)
movl 0x4(%rsp), %eax
je 0x20783
testl %eax, %eax
je 0x20851
movq 0x10(%rsp), %rax
movq (%rax), %rax
addq $0x24, %rax
movq 0x30(%rsp), %rcx
xorl %edx, %edx
testl %r13d, %r13d
jle 0x206e1
movq %rax, %rsi
xorl %edi, %edi
movl -0xc(%rsi), %r8d
imull %r12d, %r8d
addl %edx, %r8d
movslq %r8d, %r8
movq -0x24(%rsi), %r9
movq -0x1c(%rsi), %r10
subq %r9, %r10
sarq %r10
cmpq %r8, %r10
leaq (%r9,%r8,2), %r8
cmovbeq %rsi, %r8
movb 0x1(%r8), %r8b
movb %r8b, -0x1(%rcx,%rdi,2)
movl -0xc(%rsi), %r8d
imull %r12d, %r8d
addl %edx, %r8d
movslq %r8d, %r8
movq -0x24(%rsi), %r9
movq -0x1c(%rsi), %r10
subq %r9, %r10
sarq %r10
cmpq %r8, %r10
leaq (%r9,%r8,2), %r8
cmovbeq %rsi, %r8
movb (%r8), %r8b
movb %r8b, (%rcx,%rdi,2)
incq %rdi
addq $0x40, %rsi
cmpq %rdi, %r15
jne 0x20677
incq %rdx
addq 0x28(%rsp), %rcx
cmpq %r14, %rdx
jne 0x2066d
jmp 0x20851
cmpl $0x0, 0x4(%rsp)
je 0x20851
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movl 0x5c(%rdx), %ecx
movl 0x60(%rdx), %edx
addq $0x24, %rax
movq 0x8(%rsp), %rsi
xorl %edi, %edi
testl %r13d, %r13d
jle 0x20773
movq %rax, %r8
xorl %r9d, %r9d
movl -0xc(%r8), %r10d
imull %r12d, %r10d
addl %edi, %r10d
movslq %r10d, %r10
movq -0x24(%r8), %r11
movq -0x1c(%r8), %rbx
subq %r11, %rbx
sarq %rbx
cmpq %r10, %rbx
leaq (%r11,%r10,2), %r10
cmovbeq %r8, %r10
movswl (%r10), %r11d
movl %edx, %r10d
cmpl %r11d, %edx
jl 0x2075c
movl %r11d, %r10d
cmpl %r11d, %ecx
cmovgl %ecx, %r10d
movb %r10b, (%rsi,%r9)
incq %r9
addq $0x40, %r8
cmpq %r9, %r15
jne 0x20726
incq %rdi
addq %rbp, %rsi
cmpq %r14, %rdi
jne 0x2071b
jmp 0x20851
testl %eax, %eax
je 0x20851
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movl 0x5c(%rdx), %ecx
movl 0x60(%rdx), %r9d
addq $0x24, %rax
movq 0x30(%rsp), %rsi
xorl %edi, %edi
testl %r13d, %r13d
jle 0x20840
movq %rax, %r8
xorl %ebx, %ebx
movl -0xc(%r8), %edx
imull %r12d, %edx
addl %edi, %edx
movslq %edx, %rdx
movq -0x24(%r8), %r10
movq -0x1c(%r8), %r11
subq %r10, %r11
sarq %r11
cmpq %rdx, %r11
leaq (%r10,%rdx,2), %rdx
cmovbeq %r8, %rdx
movswl (%rdx), %r10d
movl %r9d, %edx
cmpl %r10d, %r9d
jl 0x207e8
movl %r10d, %edx
cmpl %r10d, %ecx
cmovgl %ecx, %edx
movb %dh, -0x1(%rsi,%rbx,2)
movl -0xc(%r8), %edx
imull %r12d, %edx
addl %edi, %edx
movslq %edx, %rdx
movq -0x24(%r8), %r10
movq -0x1c(%r8), %r11
subq %r10, %r11
sarq %r11
cmpq %rdx, %r11
leaq (%r10,%rdx,2), %rdx
cmovbeq %r8, %rdx
movswl (%rdx), %r10d
movl %r9d, %edx
cmpl %r10d, %r9d
jl 0x20827
movl %r10d, %edx
cmpl %r10d, %ecx
cmovgl %ecx, %edx
movb %dl, (%rsi,%rbx,2)
incq %rbx
addq $0x40, %r8
cmpq %rbx, %r15
jne 0x207b3
incq %rdi
addq 0x28(%rsp), %rsi
cmpq %r14, %rdi
jne 0x207a5
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3160
incq %r12
cmpq 0x38(%rsp), %r12
jne 0x20635
leaq 0x18(%rsp), %rbx
movq (%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0x31d0
movq (%rbx), %rdi
leaq 0x40(%rsp), %r15
movq (%r15), %rsi
callq 0x32d0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3230
movq 0x50(%rsp), %rdi
callq 0x32a0
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq