// File: matthewarcus[P]mmps/testimage.cpp
// Function: main
int main(int argc, char* argv[])
{
  int width = 360;
  int height = 180;
  int equalarea = false;

  char *progname = argv[0];
  argc--;argv++;
  while (argc > 0) {
    if (strcmp(argv[0],"-w") == 0) {
      argc--; argv++;
      if (argc == 0) { error(usage, progname); };
      width = atoi(argv[0]);
    } else if (strcmp(argv[0], "-h") == 0) {
      argc--; argv++;
      if (argc == 0) { error(usage, progname); };
      height = atoi(argv[0]);
    } else if (strcmp(argv[0], "-bg") == 0) {
      argc--; argv++;
      if (argc == 0) { error(usage, progname); };
      int r,g,b;
      sscanf(argv[0],"%d:%d:%d",&r,&g,&b);
      background.r = r;
      background.g = g;
      background.b = b;
    } else if (strcmp(argv[0], "-fg") == 0) {
      argc--; argv++;
      if (argc == 0) { error(usage, progname); };
      int r,g,b;
      sscanf(argv[0],"%d:%d:%d",&r,&g,&b);
      foreground.r = r;
      foreground.g = g;
      foreground.b = b;
    } else if (strcmp(argv[0], "-equalarea") == 0) {
      equalarea = true;
    } else {
      error(usage, progname);
    }
    argc--; argv++;
  }

  Image outImage;
  outImage.Create(width, height);
  outImage.Fill(0); // Set to all black

#if 0
  for (int i = 0; i < 62; i+=1) {
    double x0 = cos(i * 2 * pi/64);
    double y0 = sin(i * 2 * pi/64);
    double x1 = cos((i + 1) * 2 * pi/64);
    double y1 = sin((i + 1) * 2 * pi/64);
    x0 *= 200;
    y0 *= 200;
    x1 *= 200;
    y1 *= 200;

    if (i%2 == 1) {
      x0 += 200;
      y0 += 200;
      x1 += 200;
      y1 += 200;
    } else {
      x0 += 196;
      y0 += 196;
      x1 += 196;
      y1 += 196;
    }

    outImage.DrawLine(x0,y0,x1,y1,Rgb(255,0,0));
  }
#endif
#if 0
  SpiralPlotter plotter;
  CirclePlotter plotter2;
  TanPlotter plotter3;
  ThingPlotter plotter4(10,11);
  ThingPlotter plotter5(9,17);
  outImage.PlotLine(0, pi/2, plotter, Rgb(0,0,255));
  outImage.PlotLine(0, 2 * pi, plotter2, Rgb(0,255,0));
  outImage.PlotLine(-2*pi, 2*pi, plotter3, Rgb(255,0,0));
  outImage.PlotLine(0, 2 * pi, plotter4, Rgb(0,255,255), 197);
  outImage.PlotLine(0, 2 * pi, plotter5, Rgb(255,0,255), 197);
#endif
#if 0
  for (int i = 0; i < 20; i++) {
    outImage.DrawLine(0, 0, 200, i * 10, Rgb(255,0,0));
    outImage.DrawLine(i * 10, 0, 0, 200, Rgb(0,255,0));
  }
#endif

#if 1
  if (equalarea) {
    double xsize = width / 36.0;
    int ysize = height / 18;
    int index, x, y;
    for (index = 0, y = 0; y < height; y++) {
      double k = cos (pi * (0.5 * height - y)/height);
      for (x = 0; x < width; x++, index++) {
	double realx = k * (0.5 * width - x);
	bool neg = (realx < 0.0);
	realx = fabs(realx);
	int i = int(floor(realx/xsize))%2;
	if (neg) i = 1 - i;
	if (i == (y/ysize)%2){
	  outImage.SetRgb(index,foreground);
	} else {
	  outImage.SetRgb(index,background);
	}
    }
    }
  } else {
    int xsize = width / 36;
    int ysize = height / 18;
    int index = 0;
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++, index++) {
	if ((x/xsize)%2 == (y/ysize)%2) {
	  outImage.SetRgb(index,foreground);
	} else {
	  outImage.SetRgb(index,background);
	}
      }
    }
  }
#endif
  outImage.Write(NULL);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x168, -0x14(%rbp)     # imm = 0x168
movl $0xb4, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
jle 0x5dbe
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe483(%rip), %rsi      # 0x14049
callq 0x5340
cmpl $0x0, %eax
jne 0x5c0f
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x5bfb
movq 0x1665e(%rip), %rdi     # 0x1c250
movq -0x28(%rbp), %rsi
callq 0x61d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5380
movl %eax, -0x14(%rbp)
jmp 0x5da4
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe42f(%rip), %rsi      # 0x1404c
callq 0x5340
cmpl $0x0, %eax
jne 0x5c66
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x5c52
movq 0x16607(%rip), %rdi     # 0x1c250
movq -0x28(%rbp), %rsi
callq 0x61d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x5380
movl %eax, -0x18(%rbp)
jmp 0x5da2
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe3db(%rip), %rsi      # 0x1404f
callq 0x5340
cmpl $0x0, %eax
jne 0x5cea
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x5ca9
movq 0x165b0(%rip), %rdi     # 0x1c250
movq -0x28(%rbp), %rsi
callq 0x61d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe39c(%rip), %rsi      # 0x14053
leaq -0x2c(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq -0x34(%rbp), %r8
movb $0x0, %al
callq 0x5290
movl -0x2c(%rbp), %eax
movb %al, 0x165ac(%rip)      # 0x1c27f
movl -0x30(%rbp), %eax
movb %al, 0x165a4(%rip)      # 0x1c280
movl -0x34(%rbp), %eax
movb %al, 0x1659c(%rip)      # 0x1c281
jmp 0x5da0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe364(%rip), %rsi      # 0x1405c
callq 0x5340
cmpl $0x0, %eax
jne 0x5d6b
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x5d2d
movq 0x1652c(%rip), %rdi     # 0x1c250
movq -0x28(%rbp), %rsi
callq 0x61d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe318(%rip), %rsi      # 0x14053
leaq -0x38(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
leaq -0x40(%rbp), %r8
movb $0x0, %al
callq 0x5290
movl -0x38(%rbp), %eax
movb %al, 0x16525(%rip)      # 0x1c27c
movl -0x3c(%rbp), %eax
movb %al, 0x1651d(%rip)      # 0x1c27d
movl -0x40(%rbp), %eax
movb %al, 0x16515(%rip)      # 0x1c27e
jmp 0x5d9e
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xe2e7(%rip), %rsi      # 0x14060
callq 0x5340
cmpl $0x0, %eax
jne 0x5d8c
movl $0x1, -0x1c(%rbp)
jmp 0x5d9c
movq 0x164bd(%rip), %rdi     # 0x1c250
movq -0x28(%rbp), %rsi
callq 0x61d0
jmp 0x5d9e
jmp 0x5da0
jmp 0x5da2
jmp 0x5da4
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x5bae
xorl %eax, %eax
movl %eax, %edx
leaq -0x70(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
movq %rdx, %rsi
callq 0x11be0
movq -0xe0(%rbp), %rdi
movslq -0x14(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x11c50
jmp 0x5deb
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x6220
jmp 0x5df8
cmpl $0x0, -0x1c(%rbp)
je 0x6050
cvtsi2sdl -0x14(%rbp), %xmm0
movsd 0xe1f9(%rip), %xmm1     # 0x14008
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movl -0x18(%rbp), %eax
movl $0x12, %ecx
cltd
idivl %ecx
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x604b
movl -0x18(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x98(%rbp), %xmm1
movsd 0xe1aa(%rip), %xmm2     # 0x14010
mulsd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
fldl -0xd0(%rbp)
fldl 0xe196(%rip)            # 0x14018
fmulp %st, %st(1)
movl %eax, -0xc8(%rbp)
fildl -0xc8(%rbp)
fdivrp %st, %st(1)
movq %rsp, %rax
fstpt (%rax)
callq 0x6260
fstpt -0xec(%rbp)
jmp 0x5ea4
fldt -0xec(%rbp)
fstpl -0xd8(%rbp)
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6035
movsd -0xa0(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
cvtsi2sdl -0x94(%rbp), %xmm2
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
movsd 0xe103(%rip), %xmm3     # 0x14010
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
xorps %xmm0, %xmm0
ucomisd -0xa8(%rbp), %xmm0
seta %al
andb $0x1, %al
movb %al, -0xa9(%rbp)
movsd -0xa8(%rbp), %xmm0
movaps 0xe0da(%rip), %xmm1     # 0x14020
pand %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
divsd -0x88(%rbp), %xmm0
callq 0x53e0
cvttsd2si %xmm0, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0xb0(%rbp)
testb $0x1, -0xa9(%rbp)
je 0x5faf
movl $0x1, %eax
subl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x5faf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x11ca0
jmp 0x6188
movl -0xb0(%rbp), %eax
movl %eax, -0xf0(%rbp)
movl -0x98(%rbp), %eax
cltd
idivl -0x8c(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl -0xf0(%rbp), %eax
cmpl %edx, %eax
jne 0x5ff5
movslq -0x90(%rbp), %rsi
leaq 0x16294(%rip), %rdx     # 0x1c27c
leaq -0x70(%rbp), %rdi
callq 0x6290
jmp 0x5ff3
jmp 0x6010
movslq -0x90(%rbp), %rsi
leaq 0x1627c(%rip), %rdx     # 0x1c27f
leaq -0x70(%rbp), %rdi
callq 0x6290
jmp 0x600e
jmp 0x6010
jmp 0x6012
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x5eca
jmp 0x6037
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x5e40
jmp 0x615d
movl -0x14(%rbp), %eax
movl $0x24, %ecx
cltd
idivl %ecx
movl %eax, -0xb4(%rbp)
movl -0x18(%rbp), %eax
movl $0x12, %ecx
cltd
idivl %ecx
movl %eax, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x615b
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x6145
movl -0xc4(%rbp), %eax
cltd
idivl -0xb4(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, -0xf4(%rbp)
movl -0xc0(%rbp), %eax
cltd
idivl -0xb8(%rbp)
movl $0x2, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl -0xf4(%rbp), %edx
cmpl %eax, %edx
jne 0x6105
movslq -0xbc(%rbp), %rsi
leaq 0x16184(%rip), %rdx     # 0x1c27c
leaq -0x70(%rbp), %rdi
callq 0x6290
jmp 0x6103
jmp 0x6120
movslq -0xbc(%rbp), %rsi
leaq 0x1616c(%rip), %rdx     # 0x1c27f
leaq -0x70(%rbp), %rdi
callq 0x6290
jmp 0x611e
jmp 0x6120
jmp 0x6122
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x609f
jmp 0x6147
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x6086
jmp 0x615d
xorl %eax, %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x122d0
jmp 0x616c
movl $0x0, -0x4(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x11ca0
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x5400
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)