// File: /workspace/github_cpp_makefile_star2_O3/matthewarcus[P]mmps/combine.cpp
// Function: main
int main(int argc, char* argv[])
{
  Image image1;
  Image image2;
  const char* file1;
  const char* file2;
  const char* outfilename = NULL;
  Rgb background = black;
  int width;
  int height;
  double fuzz = 2.0;

  char *progname = argv[0];
  argc--;argv++;
  OptSpec *optspecs [] = {
    // General options
    new RgbSpec ("-bg", background),
    new DoubleSpec ("-fuzz", fuzz),
    new StringSpec ("-out", outfilename),
    NULL // Finish up
  };

  while (argc > 0) {
    int result = OptSpec::ReadOpts(argc, argv, optspecs);
    if (result != 1) {
      break;
    }
    argc--; argv++;
  }
  
  if (argc < 2) {
    error(usage, progname);
  }
  file1 = argv[0];
  file2 = argv[1];
  image1.Read(file1);
  image2.Read(file2);

  width = image1.Width();
  height = image1.Height();
  if (image2.Width() != width || image2.Height() != height) {
    error("Images must have same dimensions");
  }
  Image outImage(width, height);
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      Rgb rgb1;
      Rgb rgb2;
      Rgb out;
      image1.GetRgb(col,row,rgb1);
      image2.GetRgb(col,row,rgb2);
      if (rgb1.Match(background,fuzz)) {
	out = rgb2;
      } else if (rgb2.Match(background, fuzz)) {
	out = rgb1;
      } else {
#if 0
	// Average the rgb values, this produces alarming discontinuities
	out.r = (rgb1.r + rgb2.r)/2;
	out.g = (rgb1.g + rgb2.g)/2;
	out.b = (rgb1.b + rgb2.b)/2;
#endif
	// But taking the maximum of the rgb values is smooth
	out.r = max(rgb1.r,rgb2.r);
	out.g = max(rgb1.g,rgb2.g);
	out.b = max(rgb1.b,rgb2.b);

      }
      outImage.SetRgb(col,row,out);
    }
 }
  outImage.Write(outfilename);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movq %rsi, %r14
movl %edi, %ebp
movl %edi, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xaf78
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xaf78
movq $0x0, 0x30(%rsp)
movb $0x0, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x20(%rsp)
movq (%r14), %rbx
decl %ebp
movl %ebp, 0x8(%rsp)
addq $0x8, %r14
movq %r14, 0x18(%rsp)
movl $0x18, %edi
callq 0x4200
movq %rax, %r14
leaq 0x7995(%rip), %rsi      # 0xc026
movq %rax, %rdi
callq 0xa132
leaq 0xd6e0(%rip), %rax      # 0x11d80
addq $0x10, %rax
movq %rax, (%r14)
leaq 0xc(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r14, 0xa0(%rsp)
movl $0x20, %edi
callq 0x4200
movq %rax, %r14
leaq 0x795e(%rip), %rsi      # 0xc02a
leaq 0x20(%rsp), %rdx
movsd 0x792f(%rip), %xmm0     # 0xc008
movq %rax, %rdi
callq 0xa2ac
movq %r14, 0xa8(%rsp)
movl $0x18, %edi
callq 0x4200
movq %rax, %r14
leaq 0x7933(%rip), %rsi      # 0xc030
leaq 0x30(%rsp), %rdx
movq %rax, %rdi
callq 0xa1b4
movq %r14, 0xb0(%rsp)
movq $0x0, 0xb8(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x4767
leaq 0x8(%rsp), %r14
leaq 0x18(%rsp), %r15
leaq 0xa0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xa142
movl 0x8(%rsp), %ecx
cmpl $0x1, %eax
jne 0x4762
leal -0x1(%rcx), %eax
movl %eax, 0x8(%rsp)
addq $0x8, 0x18(%rsp)
cmpl $0x1, %ecx
jg 0x4737
jmp 0x4767
cmpl $0x1, %ecx
jg 0x4790
movq 0xda5a(%rip), %rdx      # 0x121c8
leaq 0xc0(%rsp), %r14
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4290
movq %r14, %rdi
callq 0xa0dc
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rbx
leaq 0x70(%rsp), %rdi
xorl %edx, %edx
callq 0xb166
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xb166
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x28(%rsp)
movslq %eax, %rbx
cmpq %rbx, 0x40(%rsp)
jne 0x47e1
movslq 0x10(%rsp), %rdx
cmpq %rdx, 0x48(%rsp)
je 0x47f2
leaq 0x784d(%rip), %rdi      # 0xc035
callq 0xa0dc
movslq 0x10(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0xaf78
cmpl $0x0, 0x10(%rsp)
jle 0x496c
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rbp
andq %rax, %rbp
xorl %r12d, %r12d
leaq 0xc(%rsp), %r14
xorl %r13d, %r13d
xorl %eax, %eax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x28(%rsp)
jle 0x4955
movl $0x1, %r15d
xorl %ebx, %ebx
movw %r12w, 0x5(%rsp)
movb %r12b, 0x7(%rsp)
movw %r12w, 0x2(%rsp)
movb %r12b, 0x4(%rsp)
movq 0x70(%rsp), %rax
imulq %r13, %rax
addq 0x80(%rsp), %rax
movb -0x1(%r15,%rax), %cl
movb %cl, 0x5(%rsp)
movb (%r15,%rax), %cl
movb %cl, 0x6(%rsp)
movb 0x1(%r15,%rax), %al
movq 0x40(%rsp), %rcx
imulq %r13, %rcx
addq 0x50(%rsp), %rcx
movb %al, 0x7(%rsp)
movb -0x1(%r15,%rcx), %al
movb %al, 0x2(%rsp)
movb (%r15,%rcx), %al
movb %al, 0x3(%rsp)
movb 0x1(%r15,%rcx), %al
movb %al, 0x4(%rsp)
movsd 0x20(%rsp), %xmm0
leaq 0x5(%rsp), %rdi
movq %r14, %rsi
callq 0xaea4
testb %al, %al
je 0x48d2
movb 0x2(%rsp), %cl
movb 0x3(%rsp), %al
movb 0x4(%rsp), %dl
jmp 0x4923
movsd 0x20(%rsp), %xmm0
leaq 0x2(%rsp), %rdi
movq %r14, %rsi
callq 0xaea4
movb 0x5(%rsp), %cl
testb %al, %al
je 0x48f7
movb 0x6(%rsp), %al
movb 0x7(%rsp), %dl
jmp 0x4923
movzbl %cl, %eax
movzbl 0x2(%rsp), %ecx
cmpb %cl, %al
cmoval %eax, %ecx
movzbl 0x6(%rsp), %edx
movzbl 0x3(%rsp), %eax
cmpb %al, %dl
cmoval %edx, %eax
movzbl 0x7(%rsp), %esi
movzbl 0x4(%rsp), %edx
cmpb %dl, %sil
cmoval %esi, %edx
movq 0xc0(%rsp), %rsi
imulq %r13, %rsi
addq 0xd0(%rsp), %rsi
movb %cl, -0x1(%r15,%rsi)
movb %al, (%r15,%rsi)
movb %dl, 0x1(%r15,%rsi)
incq %rbx
addq $0x3, %r15
cmpq %rbx, %rbp
jne 0x4844
movq 0x38(%rsp), %rax
incq %rax
addq $0x3, %r13
cmpq 0x10(%rsp), %rax
jne 0x482c
movq 0x30(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb38a
leaq 0xc0(%rsp), %rdi
callq 0xafcc
leaq 0x40(%rsp), %rdi
callq 0xafcc
leaq 0x70(%rsp), %rdi
callq 0xafcc
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x49e1
jmp 0x49dc
jmp 0x49c3
movq %rax, %rbx
movl $0x20, %esi
jmp 0x49cb
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x4210
jmp 0x49f1
movq %rax, %rbx
jmp 0x49fb
jmp 0x49dc
movq %rax, %rbx
jmp 0x49f1
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0xafcc
leaq 0x40(%rsp), %rdi
callq 0xafcc
leaq 0x70(%rsp), %rdi
callq 0xafcc
movq %rbx, %rdi
callq 0x4300
nopl (%rax)