// File: /workspace/github_c_makefile_star2_O2/nilesr[P]dotter/dotter.c
// Function: main
int main(int argc, char** argv) {
   // Defaults
   bool make_slices = false;
   srand(time(NULL));
   char* source_filename = "input.png";
   char* output_filename = "output.png";
   int radius = 5;
   int iterations = 100;
   // Override defaults with command line arguments, if specified
   if (argc > 1) {
      source_filename = argv[1];
   }
   if (argc > 2) {
      output_filename = argv[2];
   }
   if (argc > 3) {
      iterations = atoi(argv[3]);
      assert(iterations != 0);
   }
   if (argc > 4) {
      radius = atoi(argv[4]);
      assert(radius != 0);
   }
   if (argc > 5) {
      make_slices = true;
   }
   // helper variables
   time_t start = time(NULL);
   time_t now = 0;
   int width, height, n;
   int kept = 0;
   int slice = 0;
   // Read in the source image
   uint8_t* source = stbi_load(source_filename, &width, &height, &n, 3);
   if (!source) {
      fprintf(stderr, "Unable to open/read image, are you sure it's a png?\n");
   }
   assert(source);
   // Create our two images
   uint8_t* img1 = malloc(width * height * 3);
   uint8_t* img2 = malloc(width * height * 3);
   for (int i = 0; i < iterations; i++) {
      // Pick a random location do draw the circle at
      int x = rand() % width;
      int y = rand() % height;
      box_t box = (box_t) {.x1 = x - radius, .y1 = y - radius, .x2 = x + radius, .y2 = y + radius};
      // Make sure the circle is inside the boundries of the image
      correct(&box, width, height, radius);
      // Draw the dot in img1
      draw(img1, box, random_color(source, width, height), width, radius);
      // Take the color distance between source and img1, and source and img2
      double newdist = dist(source, img1, box, width);
      double olddist = dist(source, img2, box, width);
      // If img1 is better, copy img1 to img2 (only copies the changed pixels)
      // otherwise, copy img2 back to img1 (only copies the changed pixels)
      if (newdist < olddist) {
         kept++;
         copy(img1, img2, width, box);
      } else {
         copy(img2, img1, width, box);
      }
      // Every 10000 iterations (max 1 per second), print a status update
      // Also every 10000 iterations, if we're making slices, output a slice
      if (i % 10000 == 1) {
         if (make_slices) {
            char* name;
            slice++;
            asprintf(&name, "out-%4d.png", slice);
            assert(name);
            // change the padding spaces to zeroes in the filename
            for (char* c = name; *c; c++) { if (*c == ' ') *c = '0'; }
            // save the slice
            stbi_write_png(name, width, height, 3, img2, width * 3);
         }
         if (time(NULL) != now) {
            // Estimate time remaining
            now = time(NULL);
            time_t elapsed = now - start;
            double percent = ((double) i) / iterations;
            double total = elapsed / percent;
            double eta = total - elapsed;
            printf("%3.3f%% complete, eta = %3.3f seconds\n", 100 * percent, eta);
         }
      }
   }
   // Free the source image
   stbi_image_free(source);
   // Write the output
   stbi_write_png(output_filename, width, height, 3, img2, width * 3);
   // Free img1 and img2
   free(img1);
   free(img2);
   // Print a final message and exit
   printf("Written to %s. Took %d seconds, kept %d cirlces and discarded %d circles\n", output_filename, time(NULL) - start, kept, iterations - kept);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movl %edi, %ebx
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x10a0
pushq $0x5
popq %rax
movl %eax, 0x1c(%rsp)
leaq 0x55dd(%rip), %r12      # 0xa0fe
pushq $0x64
popq %rax
movl %eax, 0x4(%rsp)
movl %ebx, 0x8(%rsp)
cmpl $0x2, %ebx
jl 0x4b8b
movq 0x8(%r14), %rbx
je 0x4b92
movq 0x10(%r14), %r12
cmpl $0x4, 0x8(%rsp)
jb 0x4b92
movq 0x18(%r14), %rdi
callq 0x1170
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x4ecd
cmpl $0x4, 0x8(%rsp)
je 0x4b92
movq 0x20(%r14), %rdi
callq 0x1170
movl %eax, 0x1c(%rsp)
testl %eax, %eax
jne 0x4b92
leaq 0x55c3(%rip), %rdi      # 0xa139
leaq 0x559c(%rip), %rsi      # 0xa119
leaq 0x559e(%rip), %rcx      # 0xa122
pushq $0x74
jmp 0x4ee4
leaq 0x5562(%rip), %rbx      # 0xa0f4
movq %r12, 0x60(%rsp)
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x40(%rsp)
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x84(%rsp), %rcx
pushq $0x3
popq %r8
movq %rbx, %rdi
callq 0x356b
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x4e92
movl 0x2c(%rsp), %r12d
movl 0x28(%rsp), %ecx
leal (%r12,%r12,2), %eax
movl %eax, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
imull %ecx, %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x1100
movq %rax, %r15
movq %rbx, %rdi
callq 0x1100
movl 0x4(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x68(%rsp)
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movl %edx, 0x20(%rsp)
movq %rax, %r14
xorl %ebx, %ebx
movl $0x0, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x48(%rsp)
movq %rax, 0x70(%rsp)
movq 0x50(%rsp), %rbp
cmpl 0x20(%rsp), %ebx
je 0x4dfa
movl %ebx, 0x24(%rsp)
callq 0x11a0
cltd
idivl %r12d
movl %edx, %ebx
callq 0x11a0
cltd
movl 0xc(%rsp), %r13d
idivl %r13d
movl %ebx, %eax
movl 0x1c(%rsp), %r14d
subl %r14d, %eax
movl %eax, 0x30(%rsp)
movl %edx, %eax
subl %r14d, %eax
movl %eax, 0x34(%rsp)
addl %r14d, %ebx
movl %ebx, 0x38(%rsp)
addl %r14d, %edx
movl %edx, 0x3c(%rsp)
leaq 0x30(%rsp), %rdi
movl %r12d, %esi
movl %r13d, %edx
movl %r14d, %ecx
callq 0x4926
movq %rbp, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x4a37
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rax, %rcx
movl %r12d, %r8d
movl %r14d, %r9d
movq 0x70(%rsp), %r14
callq 0x4978
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movl %r12d, %r8d
callq 0x4835
movsd %xmm0, 0x78(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movl %r12d, %r8d
callq 0x4835
ucomisd 0x78(%rsp), %xmm0
jbe 0x4d16
incl 0x18(%rsp)
movq %r15, %rdi
movq %r14, %rsi
jmp 0x4d1c
movq %r14, %rdi
movq %r15, %rsi
movl %r12d, %edx
movq %rbx, %rcx
movq %r13, %r8
callq 0x4a71
movl 0x24(%rsp), %ebx
movl %ebx, %eax
xorl %edx, %edx
movl $0x2710, %ecx           # imm = 0x2710
divl %ecx
cmpl $0x1, %edx
jne 0x4df3
cmpl $0x6, 0x8(%rsp)
jl 0x4da3
movl 0x14(%rsp), %edx
incl %edx
leaq 0x58(%rsp), %rdi
leaq 0x5426(%rip), %rsi      # 0xa181
movl %edx, 0x14(%rsp)
xorl %eax, %eax
callq 0x1140
movq 0x58(%rsp), %rax
testq %rax, %rax
jne 0x4d7b
jmp 0x4e73
movb $0x30, (%rax)
incq %rax
movzbl (%rax), %ecx
cmpl $0x20, %ecx
je 0x4d75
testl %ecx, %ecx
jne 0x4d78
movq 0x58(%rsp), %rdi
movl %r12d, %esi
movl 0xc(%rsp), %edx
pushq $0x3
popq %rcx
movq %r14, %r8
movl 0x10(%rsp), %r9d
callq 0x2c45
xorl %edi, %edi
callq 0x10f0
cmpq 0x48(%rsp), %rax
je 0x4df3
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x48(%rsp)
cvtsi2sd %ebx, %xmm0
subq 0x40(%rsp), %rax
divsd 0x68(%rsp), %xmm0
cvtsi2sd %rax, %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
subsd %xmm2, %xmm1
mulsd 0x515b(%rip), %xmm0     # 0x9f40
leaq 0x53a6(%rip), %rdi      # 0xa192
movb $0x2, %al
callq 0x1070
incl %ebx
jmp 0x4c41
movq 0x50(%rsp), %rdi
callq 0x1030
pushq $0x3
popq %rcx
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
movl 0xc(%rsp), %edx
movq %r14, %r8
movl 0x10(%rsp), %r9d
callq 0x2c45
movq %r15, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %edi, %edi
callq 0x10f0
subq 0x40(%rsp), %rax
movl 0x4(%rsp), %r8d
movl 0x18(%rsp), %ecx
subl %ecx, %r8d
leaq 0x5367(%rip), %rdi      # 0xa1b9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5313(%rip), %rdi      # 0xa18d
leaq 0x5298(%rip), %rsi      # 0xa119
leaq 0x529a(%rip), %rcx      # 0xa122
movl $0xa3, %edx
callq 0x1080
movq 0x915f(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x52a2(%rip), %rdi      # 0xa145
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
leaq 0x52c5(%rip), %rdi      # 0xa17a
leaq 0x525d(%rip), %rsi      # 0xa119
leaq 0x525f(%rip), %rcx      # 0xa122
movl $0x84, %edx
callq 0x1080
leaq 0x5235(%rip), %rdi      # 0xa109
leaq 0x523e(%rip), %rsi      # 0xa119
leaq 0x5240(%rip), %rcx      # 0xa122
pushq $0x70
popq %rdx
callq 0x1080