// File: /workspace/github_cpp_makefile_star2_O3/Jiro-iwai[P]yuv2psnr/yuv2psnr.cpp
// Function: main
int main(int argc, char* argv[])
{
  Option opt;
  // CIF
  opt.width = 352;
  opt.height = 288;
  // HD
  //int width  = 1920;
  //int height = 1080;
  opt.file0 = "";
  opt.file1 = "";
  opt.showFrame = false;
  parse_option(argc, argv, opt);

  const unsigned int area = opt.width*opt.height;
  //const unsigned int min_read_unit = 16;
  //const unsigned int min_read_unit = 8;
  unsigned int min_read_unit = area;
  const unsigned char MAX = 255;

  ifstream fin0( opt.file0.c_str(), ios::in | ios::binary );
  ifstream fin1( opt.file1.c_str(), ios::in | ios::binary );
    
  if( !fin0 ){
    cout << "Error: cannot open file `" << opt.file0 << "'" << endl;
    return 1;
  }

  if( !fin1 ){
    cout << "Error: cannot open file `" << opt.file1 << "'" << endl;
    return 1;
  }

  int count = 0;    
  int nframe = 0;
  unsigned char* c0 = new unsigned char[min_read_unit];
  unsigned char* c1 = new unsigned char[min_read_unit];

  double totalYMSE = 0.0;
  double totalUMSE = 0.0;
  double totalVMSE = 0.0;

  double frameYMSE = 0.0;
  double frameUMSE = 0.0;
  double frameVMSE = 0.0;

  cout << "Frame       Y-PSNR[dB]  U-PSNR[dB]  V-PSNR[dB]" << endl;

  while( !fin0.eof() ){

    fin0.read( (char*)c0, min_read_unit ); 
    fin1.read( (char*)c1, min_read_unit ); 
    count += min_read_unit;

    // extract Y
    if( count <= area ) { 
      MSE(c0, c1, min_read_unit, frameYMSE);
      min_read_unit = area/4;

    // extract U
    } else if( area < count && count <= area/4*5 ) { // YUV420
      MSE(c0, c1, min_read_unit, frameUMSE);

    // extract V
    } else if( area/4*5 < count ) { // YUV420
      MSE(c0, c1, min_read_unit, frameVMSE);
    }

    // end of 1 frame
    if( count == area/2*3 ) {

      totalYMSE += frameYMSE;
      totalUMSE += frameUMSE;
      totalVMSE += frameVMSE;

      frameYMSE /= area;
      frameUMSE /= area/4;
      frameVMSE /= area/4;

      if( opt.showFrame ) {

        double y = MSE2PSNR(frameYMSE, MAX);
        double u = MSE2PSNR(frameUMSE, MAX);
        double v = MSE2PSNR(frameVMSE, MAX);
        cout << boost::format("%-12d%-12.4f%-12.4f%-12.4f") % nframe % y % u % v << endl;
      }

      frameYMSE = 0.0;
      frameUMSE = 0.0;
      frameVMSE = 0.0;

      nframe++;
      count = 0;
      min_read_unit = area;
    }
  }

  totalYMSE /= area*nframe;
  totalUMSE /= area/4*nframe;
  totalVMSE /= area/4*nframe;

  double y = MSE2PSNR(totalYMSE, MAX);
  double u = MSE2PSNR(totalUMSE, MAX);
  double v = MSE2PSNR(totalVMSE, MAX);
  cout << boost::format("Total       %-12.4f%-12.4f%-12.4f") % y % u % v << endl;

  delete [] c0;
  delete [] c1;

  fin0.close();
  fin1.close();
    
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
xorl %eax, %eax
movq %rax, -0x8(%r15)
movb %al, (%r15)
leaq 0xc0(%rsp), %r13
movq %r13, -0x10(%r13)
movq %rax, -0x8(%r13)
movb %al, (%r13)
movabsq $0x12000000160, %rax    # imm = 0x12000000160
movq %rax, -0x38(%r13)
leaq 0x687b(%rip), %rcx      # 0xb170
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x4340
leaq 0xb0(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x685c(%rip), %rcx      # 0xb170
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x4340
leaq 0x88(%rsp), %rdx
movb $0x0, 0x48(%rdx)
movl %ebp, %edi
movq %rbx, %rsi
callq 0x45f4
movl 0x8c(%rsp), %ebp
imull 0x88(%rsp), %ebp
movq 0x90(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
movl $0xc, %edx
callq 0x43f0
movq 0xb0(%rsp), %rsi
leaq 0x3c0(%rsp), %rdi
movl $0xc, %edx
callq 0x43f0
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x1d8(%rsp,%rax)
je 0x49ee
movq 0xb63c(%rip), %rdi      # 0xffd0
leaq 0x67ba(%rip), %rsi      # 0xb155
movl $0x19, %edx
callq 0x4280
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movq 0xb614(%rip), %rdi      # 0xffd0
callq 0x4280
movq %rax, %rbx
leaq 0x67a4(%rip), %rsi      # 0xb16f
movl $0x1, %edx
movq %rax, %rdi
callq 0x4280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4240
jmp 0x4a67
movq 0x3c0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x3e0(%rsp,%rax)
je 0x4ad1
movq 0xb5c1(%rip), %rdi      # 0xffd0
leaq 0x673f(%rip), %rsi      # 0xb155
movl $0x19, %edx
callq 0x4280
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movq 0xb599(%rip), %rdi      # 0xffd0
callq 0x4280
movq %rax, %rbx
leaq 0x6729(%rip), %rsi      # 0xb16f
movl $0x1, %edx
movq %rax, %rdi
callq 0x4280
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movl $0x1, %ebx
movq %rax, %rdi
callq 0x41d0
leaq 0x3c0(%rsp), %rdi
callq 0x4080
leaq 0x1b8(%rsp), %rdi
callq 0x4080
movq 0xb0(%rsp), %rdi
cmpq %r13, %rdi
je 0x4aab
callq 0x4230
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x4abd
callq 0x4230
movl %ebx, %eax
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0xc(%rsp)
movl %ebp, %r15d
movq %r15, %rdi
callq 0x4030
movq %rax, %rbx
movq %r15, %rdi
callq 0x4030
movq %rax, %r14
movq 0xb4d7(%rip), %r12      # 0xffd0
leaq 0x6671(%rip), %rsi      # 0xb171
movl $0x2e, %edx
movq %r12, %rdi
callq 0x4280
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq 0xb4a4(%rip), %rdi      # 0xffd0
callq 0x4040
movq %rax, %rdi
callq 0x41d0
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
movl %ebp, %ecx
shrl $0x2, %ecx
movq %rcx, 0x38(%rsp)
testb $0x2, 0x1d8(%rsp,%rax)
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
jne 0x4f99
movq 0x38(%rsp), %rcx
leal (%rcx,%rcx,4), %eax
movl %eax, 0x68(%rsp)
movl %ebp, %eax
shrl %eax
leal (%rax,%rax,2), %eax
movl %eax, 0x6c(%rsp)
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x70(%rsp)
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
xorl %r15d, %r15d
movl %ebp, %r13d
movsd %xmm6, 0x48(%rsp)
movsd %xmm5, 0x18(%rsp)
movsd %xmm4, (%rsp)
movsd %xmm3, 0x78(%rsp)
movsd %xmm2, 0x40(%rsp)
movsd %xmm1, 0x10(%rsp)
movl %r13d, %r12d
leaq 0x1b8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x42b0
leaq 0x3c0(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x42b0
addl %r13d, %r15d
cmpl %ebp, %r15d
movsd 0x10(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x78(%rsp), %xmm3
jbe 0x4c59
cmpl 0x68(%rsp), %r15d
movsd (%rsp), %xmm4
movsd 0x18(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
jbe 0x4c9c
testl %r13d, %r13d
je 0x4cc6
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm4
incq %rax
cmpq %rax, %r12
jne 0x4c36
jmp 0x4cc9
testl %r13d, %r13d
movsd (%rsp), %xmm4
movsd 0x18(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
je 0x4c92
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm6
incq %rax
cmpq %rax, %r12
jne 0x4c71
movq 0x38(%rsp), %rax
movl %eax, %r13d
jmp 0x4cc9
testl %r13d, %r13d
je 0x4cc6
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
movzbl (%r14,%rax), %edx
subl %edx, %ecx
imull %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
addsd %xmm0, %xmm5
incq %rax
cmpq %rax, %r12
jne 0x4ca3
jmp 0x4cc9
xorl %r13d, %r13d
cmpl 0x6c(%rsp), %r15d
jne 0x4f7f
cmpb $0x1, 0xd0(%rsp)
jne 0x4f5d
movsd %xmm6, 0x48(%rsp)
movapd %xmm6, %xmm1
divsd 0x80(%rsp), %xmm1
ucomisd 0x6313(%rip), %xmm1     # 0xb010
movsd 0x6303(%rip), %xmm0     # 0xb008
movsd %xmm4, (%rsp)
movsd %xmm5, 0x18(%rsp)
jbe 0x4d36
movsd 0x6306(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movsd 0x18(%rsp), %xmm5
movsd (%rsp), %xmm4
mulsd 0x62e2(%rip), %xmm0     # 0xb018
movapd %xmm5, %xmm1
divsd 0x70(%rsp), %xmm1
movsd %xmm0, 0x60(%rsp)
ucomisd 0x62c2(%rip), %xmm1     # 0xb010
movsd 0x62b2(%rip), %xmm0     # 0xb008
leaq 0xd8(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq 0x509f(%rip), %r13      # 0x9e09
jbe 0x4d8a
movsd 0x62ac(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movsd (%rsp), %xmm4
mulsd 0x628e(%rip), %xmm0     # 0xb018
movapd %xmm4, %xmm1
divsd 0x70(%rsp), %xmm1
movsd %xmm0, 0x58(%rsp)
ucomisd 0x626e(%rip), %xmm1     # 0xb010
movsd 0x625e(%rip), %xmm0     # 0xb008
jbe 0x4dc5
movsd 0x626c(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
mulsd 0x6253(%rip), %xmm0     # 0xb018
movsd %xmm0, 0x50(%rsp)
movq %r15, %rdi
leaq 0x63cb(%rip), %rsi      # 0xb1a0
callq 0x53d4
leaq 0xc(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x5016(%rip), %rax      # 0x9e01
movq %rax, 0x28(%rsp)
leaq 0x500b(%rip), %rax      # 0x9e02
movq %rax, 0x30(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8d6a
leaq 0x60(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %r13, 0x28(%rsp)
leaq 0x4fed(%rip), %rcx      # 0x9e0a
movq %rcx, 0x30(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8d6a
leaq 0x58(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %r13, 0x28(%rsp)
leaq 0x4fc7(%rip), %rcx      # 0x9e0a
movq %rcx, 0x30(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8d6a
leaq 0x50(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %r13, 0x28(%rsp)
leaq 0x4fa1(%rip), %rcx      # 0x9e0a
movq %rcx, 0x30(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x8d6a
movq 0xb150(%rip), %rdi      # 0xffd0
movq %rax, %rsi
callq 0x5275
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41d0
cmpb $0x1, 0x1a8(%rsp)
jne 0x4ed1
leaq 0x1b0(%rsp), %rdi
callq 0x4310
movb $0x0, 0x1a8(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x55be
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x4ef8
callq 0x4230
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x4f32
callq 0x4230
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
movl %eax, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
movl %eax, 0x108(%rsp)
movq %rax, 0x110(%rsp)
movq %r15, %rdi
callq 0x5612
movsd 0x10(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movsd 0x78(%rsp), %xmm3
movsd (%rsp), %xmm4
movsd 0x18(%rsp), %xmm5
movsd 0x48(%rsp), %xmm6
addsd %xmm6, %xmm3
addsd %xmm5, %xmm2
addsd %xmm4, %xmm1
incl 0xc(%rsp)
xorl %r15d, %r15d
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
movl %ebp, %r13d
movq 0x1b8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x1d8(%rsp,%rax)
je 0x4bad
movl 0xc(%rsp), %eax
imull %eax, %ebp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
divsd %xmm0, %xmm3
movq 0x38(%rsp), %rcx
imull %eax, %ecx
xorps %xmm5, %xmm5
cvtsi2sd %rcx, %xmm5
divsd %xmm5, %xmm2
movsd 0x6040(%rip), %xmm4     # 0xb008
ucomisd 0x6040(%rip), %xmm3     # 0xb010
movapd %xmm4, %xmm0
jbe 0x5019
movsd 0x6042(%rip), %xmm0     # 0xb020
divsd %xmm3, %xmm0
movsd %xmm1, 0x10(%rsp)
movsd %xmm2, 0x40(%rsp)
movsd %xmm5, (%rsp)
callq 0x4170
movsd (%rsp), %xmm5
movsd 0x6003(%rip), %xmm4     # 0xb008
movsd 0x40(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
mulsd 0x5fff(%rip), %xmm0     # 0xb018
divsd %xmm5, %xmm1
movsd %xmm0, 0x60(%rsp)
ucomisd 0x5fe5(%rip), %xmm2     # 0xb010
movapd %xmm4, %xmm0
jbe 0x505e
movsd 0x5fe7(%rip), %xmm0     # 0xb020
divsd %xmm2, %xmm0
movsd %xmm1, 0x10(%rsp)
callq 0x4170
movsd 0x5fb8(%rip), %xmm4     # 0xb008
movsd 0x10(%rsp), %xmm1
mulsd 0x5fba(%rip), %xmm0     # 0xb018
movsd %xmm0, 0x58(%rsp)
ucomisd 0x5fa4(%rip), %xmm1     # 0xb010
jbe 0x508b
movsd 0x5faa(%rip), %xmm0     # 0xb020
divsd %xmm1, %xmm0
callq 0x4170
movapd %xmm0, %xmm4
mulsd 0x5f8d(%rip), %xmm4     # 0xb018
movsd %xmm4, 0x50(%rsp)
leaq 0x6123(%rip), %rsi      # 0xb1bb
leaq 0xd8(%rsp), %rdi
callq 0x53d4
leaq 0xc0(%rsp), %r13
leaq 0x60(%rsp), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x4d48(%rip), %r15      # 0x9e09
movq %r15, 0x8(%rsi)
leaq 0x4d3e(%rip), %r12      # 0x9e0a
movq %r12, 0x10(%rsi)
leaq 0xd8(%rsp), %rdi
callq 0x8d6a
leaq 0x58(%rsp), %rcx
leaq 0x20(%rsp), %rsi
movq %rcx, (%rsi)
movq %r15, 0x8(%rsi)
movq %r12, 0x10(%rsi)
movq %rax, %rdi
callq 0x8d6a
leaq 0x50(%rsp), %rcx
leaq 0x20(%rsp), %rsi
movq %rcx, (%rsi)
movq %r15, 0x8(%rsi)
movq %r12, 0x10(%rsi)
movq %rax, %rdi
callq 0x8d6a
movq 0xaeb2(%rip), %rdi      # 0xffd0
movq %rax, %rsi
callq 0x5275
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4240
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41d0
cmpb $0x1, 0x1a8(%rsp)
jne 0x516f
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
callq 0x4310
movb $0x0, -0x8(%r15)
leaq 0x150(%rsp), %r15
movq %r15, %rdi
callq 0x55be
movq -0x28(%r15), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x5195
callq 0x4230
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
leaq 0xa0(%rsp), %r15
je 0x51d7
callq 0x4230
xorl %eax, %eax
movq %rax, 0xf0(%rsp)
movl %eax, 0xf8(%rsp)
movq %rax, 0x100(%rsp)
movl %eax, 0x108(%rsp)
movq %rax, 0x110(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x5612
movq %rbx, %rdi
callq 0x42c0
movq %r14, %rdi
callq 0x42c0
leaq 0x1b8(%rsp), %rdi
callq 0x4060
leaq 0x3c0(%rsp), %rdi
callq 0x4060
xorl %ebx, %ebx
jmp 0x4a7f
jmp 0x5243
jmp 0x5243
jmp 0x5243
jmp 0x5243
jmp 0x5243
jmp 0x5243
jmp 0x5231
movq %rax, %rbx
jmp 0x5253
jmp 0x522a
movq %rax, %rbx
jmp 0x5260
jmp 0x5243
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x5532
jmp 0x5246
movq %rax, %rbx
leaq 0x3c0(%rsp), %rdi
callq 0x4080
leaq 0x1b8(%rsp), %rdi
callq 0x4080
leaq 0x88(%rsp), %rdi
callq 0x5588
movq %rbx, %rdi
callq 0x43d0