// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
movl %eax, %edi
callq 0x10a0
leaq 0x8acd(%rip), %rax      # 0xea01
movq %rax, -0x20(%rbp)
leaq 0x8acc(%rip), %rax      # 0xea0b
movq %rax, -0x28(%rbp)
movl $0x5, -0x2c(%rbp)
movl $0x64, -0x30(%rbp)
cmpl $0x1, -0x8(%rbp)
jle 0x5f63
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x5f75
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
cmpl $0x3, -0x8(%rbp)
jle 0x5fb4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1180
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x5f93
jmp 0x5fb2
leaq 0x8a7c(%rip), %rdi      # 0xea16
leaq 0x8a85(%rip), %rsi      # 0xea26
movl $0x70, %edx
leaq 0x8a82(%rip), %rcx      # 0xea2f
callq 0x1080
jmp 0x5fb4
cmpl $0x4, -0x8(%rbp)
jle 0x5ff3
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1180
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x5fd2
jmp 0x5ff1
leaq 0x8a6d(%rip), %rdi      # 0xea46
leaq 0x8a46(%rip), %rsi      # 0xea26
movl $0x74, %edx
leaq 0x8a43(%rip), %rcx      # 0xea2f
callq 0x1080
jmp 0x5ff3
cmpl $0x5, -0x8(%rbp)
jle 0x5ffd
movb $0x1, -0x11(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x44(%rbp), %rsi
leaq -0x48(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
movl $0x3, %r8d
callq 0x4940
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x605e
movq 0xbfab(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x89fb(%rip), %rsi      # 0xea52
movb $0x0, %al
callq 0x10d0
cmpq $0x0, -0x60(%rbp)
je 0x6067
jmp 0x6086
leaq 0x8a19(%rip), %rdi      # 0xea87
leaq 0x89b1(%rip), %rsi      # 0xea26
movl $0x84, %edx
leaq 0x89ae(%rip), %rcx      # 0xea2f
callq 0x1080
movl -0x44(%rbp), %eax
imull -0x48(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x1120
movq %rax, -0x68(%rbp)
movl -0x44(%rbp), %eax
imull -0x48(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x1120
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x638f
callq 0x11c0
cltd
idivl -0x44(%rbp)
movl %edx, -0x78(%rbp)
callq 0x11c0
cltd
idivl -0x48(%rbp)
movl %edx, -0x7c(%rbp)
movl -0x78(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x7c(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x78(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x7c(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movl -0x2c(%rbp), %ecx
leaq -0x8c(%rbp), %rdi
callq 0x5c60
movq -0x68(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x60(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
callq 0x5e30
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl -0x44(%rbp), %r8d
movl -0x2c(%rbp), %r9d
movq -0x8c(%rbp), %rsi
movq -0x84(%rbp), %rdx
callq 0x5d30
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl -0x44(%rbp), %r8d
movq -0x8c(%rbp), %rdx
movq -0x84(%rbp), %rcx
callq 0x5ba0
movsd %xmm0, -0x98(%rbp)
movq -0x60(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x44(%rbp), %r8d
movq -0x8c(%rbp), %rdx
movq -0x84(%rbp), %rcx
callq 0x5ba0
movsd %xmm0, -0xa0(%rbp)
movsd -0x98(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x61eb
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x44(%rbp), %edx
movq -0x8c(%rbp), %rcx
movq -0x84(%rbp), %r8
callq 0x5e80
jmp 0x6209
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl -0x44(%rbp), %edx
movq -0x8c(%rbp), %rcx
movq -0x84(%rbp), %r8
callq 0x5e80
movl -0x74(%rbp), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x637f
testb $0x1, -0x11(%rbp)
je 0x62dc
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edx
leaq -0xa8(%rbp), %rdi
leaq 0x884d(%rip), %rsi      # 0xea8e
movb $0x0, %al
callq 0x1150
cmpq $0x0, -0xa8(%rbp)
je 0x6254
jmp 0x6273
leaq 0x883f(%rip), %rdi      # 0xea9a
leaq 0x87c4(%rip), %rsi      # 0xea26
movl $0xa3, %edx
leaq 0x87c1(%rip), %rcx      # 0xea2f
callq 0x1080
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpb $0x0, (%rax)
je 0x62bc
movq -0xb0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x62a6
movq -0xb0(%rbp), %rax
movb $0x30, (%rax)
jmp 0x62a8
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x6281
movq -0xa8(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x70(%rbp), %r8
imull $0x3, -0x44(%rbp), %r9d
movl $0x3, %ecx
callq 0x3d10
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
cmpq -0x40(%rbp), %rax
je 0x637d
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0xb8(%rbp)
cvtsi2sdl -0x74(%rbp), %xmm0
cvtsi2sdl -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
cvtsi2sdq -0xb8(%rbp), %xmm0
divsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
cvtsi2sdq -0xb8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd 0x84f1(%rip), %xmm0     # 0xe850
mulsd -0xc0(%rbp), %xmm0
movsd -0xd0(%rbp), %xmm1
leaq 0x8729(%rip), %rdi      # 0xea9f
movb $0x2, %al
callq 0x1070
jmp 0x637f
jmp 0x6381
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x60b9
movq -0x60(%rbp), %rdi
callq 0x4900
movq -0x28(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x70(%rbp), %r8
imull $0x3, -0x44(%rbp), %r9d
movl $0x3, %ecx
callq 0x3d10
movq -0x68(%rbp), %rdi
callq 0x1030
movq -0x70(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rax
movq %rax, -0xe0(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1110
movq -0xe0(%rbp), %rsi
movq %rax, %rdx
subq -0x38(%rbp), %rdx
movl -0x50(%rbp), %ecx
movl -0x30(%rbp), %r8d
subl -0x50(%rbp), %r8d
leaq 0x86cb(%rip), %rdi      # 0xeac6
movb $0x0, %al
callq 0x1070
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nop