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

  int nb_channels = image.channel.size();
  if (nb_channels != 3 || image.transform.size() != 2 || image.transform[0].ID != TRANSFORM_YCbCr || image.transform[1].ID != TRANSFORM_ChromaSubsample
      || image.channel[1].hshift != 1 || image.channel[2].hshift != 1) {
    e_printf("Image is not YCbCr 4:2:0, not saving as .yuv.\n");
    return false;
  }

  FILE *fp = fopen(output_image,"wb");
  if (!fp) {
    return false;
  }
  if (image.colormodel != 0) {
    v_printf(1,"Warning: saving as .yuv, even though the image is not sRGB.\n");
  }

  for (int c = 0; c < 3; c++) {
    for (int y = 0; y < image.channel[c].h; y++) {
     for (int x = 0; x < image.channel[c].w; x++) {
        fputc(CLAMP(image.channel[c].value(y,x),image.minval,image.maxval) & 0xFF,fp);
        if (image.maxval > 255)
            fputc((CLAMP(image.channel[c].value(y,x),image.minval,image.maxval)>>8) & 0xFF,fp);
     }
    }
  }

  fclose(fp);
  return 0;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x40(%rsp), %rdi
callq 0x4b80
movl %eax, 0x3c(%rsp)
cmpl $0x3, 0x3c(%rsp)
jne 0x40f80
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
callq 0x4a70
cmpq $0x2, %rax
jne 0x40f80
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20d00
cmpl $0x0, (%rax)
jne 0x40f80
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movl $0x1, %esi
callq 0x20d00
cmpl $0x3, (%rax)
jne 0x40f80
movq 0x40(%rsp), %rdi
movl $0x1, %esi
callq 0x13390
cmpl $0x1, 0x2c(%rax)
jne 0x40f80
movq 0x40(%rsp), %rdi
movl $0x2, %esi
callq 0x13390
cmpl $0x1, 0x2c(%rax)
je 0x40f98
leaq 0x7776(%rip), %rdi      # 0x486fd
movb $0x0, %al
callq 0x3a670
movb $0x0, 0x57(%rsp)
jmp 0x41209
movq 0x48(%rsp), %rdi
leaq 0x69b3(%rip), %rsi      # 0x47957
callq 0x3460
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x40fc0
movb $0x0, 0x57(%rsp)
jmp 0x41209
movq 0x40(%rsp), %rax
cmpl $0x0, 0x70(%rax)
je 0x40fde
movl $0x1, %edi
leaq 0x7755(%rip), %rsi      # 0x4872c
movb $0x0, %al
callq 0x3a770
movl $0x0, 0x2c(%rsp)
cmpl $0x3, 0x2c(%rsp)
jge 0x411fa
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rcx
movl 0x20(%rsp), %eax
cmpl 0x1c(%rcx), %eax
jge 0x411e8
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl 0x18(%rcx), %eax
jge 0x411d6
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movq 0x40(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x41087
movq 0x40(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x18(%rsp)
jmp 0x410eb
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movq 0x40(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x410bf
movq 0x40(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x14(%rsp)
jmp 0x410e3
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %edi
andl $0xff, %edi
movq 0x30(%rsp), %rsi
callq 0x34a0
movq 0x40(%rsp), %rax
cmpl $0xff, 0x60(%rax)
jle 0x411c4
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movq 0x40(%rsp), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x41149
movq 0x40(%rsp), %rax
movl 0x5c(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x411ad
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movq 0x40(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x41181
movq 0x40(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x411a5
movq 0x40(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0x13390
movq %rax, %rdi
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %edx
callq 0x12ba0
cwtl
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %edi
sarl $0x8, %edi
andl $0xff, %edi
movq 0x30(%rsp), %rsi
callq 0x34a0
jmp 0x411c6
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x41028
jmp 0x411d8
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x40ff9
jmp 0x411ea
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x40fe6
movq 0x30(%rsp), %rdi
callq 0x3350
movb $0x0, 0x57(%rsp)
movb 0x57(%rsp), %al
andb $0x1, %al
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)