// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq %rsi, %r14
movl %edi, %ebx
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x10a0
movl $0x5, %eax
movq %rax, 0x48(%rsp)
leaq 0x6243(%rip), %r15      # 0xb15e
movl $0x64, 0xc(%rsp)
movl %ebx, 0x24(%rsp)
cmpl $0x2, %ebx
jl 0x4f8a
movq 0x8(%r14), %rbx
je 0x4f91
movq 0x10(%r14), %r15
cmpl $0x4, 0x24(%rsp)
jb 0x4f91
movq 0x18(%r14), %rdi
callq 0x1170
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x541e
cmpl $0x4, 0x24(%rsp)
je 0x4f91
movq 0x20(%r14), %rdi
callq 0x1170
movq %rax, 0x48(%rsp)
testl %eax, %eax
jne 0x4f91
leaq 0x6227(%rip), %rdi      # 0xb199
leaq 0x6200(%rip), %rsi      # 0xb179
leaq 0x6202(%rip), %rcx      # 0xb182
movl $0x74, %edx
callq 0x1080
leaq 0x61c3(%rip), %rbx      # 0xb154
movq %r15, 0x80(%rsp)
movq $0x0, 0x40(%rsp)
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x58(%rsp)
leaq 0x54(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0xa4(%rsp), %rcx
movq %rbx, %rdi
movl $0x3, %r8d
callq 0x363d
movq %rax, 0x70(%rsp)
testq %rax, %rax
je 0x5419
movslq 0x54(%rsp), %rax
movl 0x50(%rsp), %ecx
movq %rax, 0x18(%rsp)
leal (%rax,%rax,2), %eax
movl %eax, 0x34(%rsp)
movl %ecx, 0x14(%rsp)
imull %ecx, %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x1100
movq %rax, %rbp
movq %rbx, %rdi
callq 0x1100
movq %rax, %r12
cmpl $0x0, 0xc(%rsp)
jle 0x5379
cvtsi2sdl 0xc(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x48(%rsp), %rax
addl %eax, %eax
movl %eax, 0x3c(%rsp)
movq 0x18(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq $0x0, 0x68(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x0, 0x38(%rsp)
xorl %r13d, %r13d
movq %rbp, 0x28(%rsp)
movq %r12, 0x90(%rsp)
movq %rax, 0x60(%rsp)
callq 0x11a0
cltd
idivl 0x18(%rsp)
movl %edx, %ebx
callq 0x11a0
cltd
movl 0x14(%rsp), %edi
idivl %edi
movl %edx, %r14d
movl %ebx, %ecx
movq 0x48(%rsp), %rax
subl %eax, %ecx
subl %eax, %edx
movq %rdx, %rsi
shlq $0x20, %rsi
addl %eax, %r14d
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
testl %edx, %edx
movl $0x0, %r15d
cmovnsq %rsi, %r15
movl $0x0, %esi
cmovsl %edx, %esi
subl %esi, %r14d
cmpl %edi, %r14d
movl %r13d, 0x10(%rsp)
jle 0x50e5
testl %edx, %edx
movl $0x0, %esi
cmovlel %esi, %edx
movl 0x3c(%rsp), %esi
subl %esi, %r14d
subl %esi, %edx
shlq $0x20, %rdx
movq %rdx, %r15
jmp 0x50e9
movl 0x3c(%rsp), %esi
movq 0x48(%rsp), %r13
addl %r13d, %ebx
movl %ecx, %edx
sarl $0x1f, %edx
andl %ecx, %edx
subl %edx, %ebx
movq %rax, %rcx
orq %r15, %rcx
movq %r14, %rdx
shlq $0x20, %rdx
subl %esi, %eax
movabsq $-0x100000000, %rdi     # imm = 0xFFFFFFFF00000000
andq %rdi, %r15
orq %rax, %r15
movq 0x18(%rsp), %rbp
cmpl %ebp, %ebx
movl $0x0, %eax
cmovgl %esi, %eax
cmovleq %rcx, %r15
subl %eax, %ebx
orq %rdx, %rbx
callq 0x11a0
cltd
idivl %ebp
movl %edx, %r12d
callq 0x11a0
cltd
idivl 0x14(%rsp)
imull %ebp, %edx
addl %r12d, %edx
movq %r13, %r9
leal (%rdx,%rdx,2), %eax
movslq %eax, %rcx
movq 0x70(%rsp), %r13
addq %r13, %rcx
movq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movl %ebp, %r8d
callq 0x4d4a
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x4c08
movsd %xmm0, 0x98(%rsp)
movq %r13, %rdi
movq 0x90(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x4c08
ucomisd 0x98(%rsp), %xmm0
jbe 0x5229
movq 0x40(%rsp), %rax
incl %eax
movq %rax, 0x40(%rsp)
movq %r15, %rax
shrq $0x20, %rax
cmpl %eax, %r14d
jle 0x528b
subq %r15, %rbx
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %rax, %rbx
sarq $0x20, %rbx
movq %r15, %rax
sarq $0x20, %rax
movslq %r15d, %rcx
movslq %r14d, %r14
subq %rax, %r14
imulq 0x18(%rsp), %rax
addq %rcx, %rax
leaq (%rax,%rax,2), %r15
movq 0x28(%rsp), %rbp
movq 0x60(%rsp), %r13
leaq (%r12,%r15), %rdi
leaq (%r15,%rbp), %rsi
movq %rbx, %rdx
callq 0x10e0
addq %r13, %r15
decq %r14
jne 0x520f
jmp 0x5290
movq %r15, %rax
shrq $0x20, %rax
cmpl %eax, %r14d
jle 0x528b
subq %r15, %rbx
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %rax, %rbx
sarq $0x20, %rbx
movq %r15, %rax
sarq $0x20, %rax
movslq %r15d, %rcx
movslq %r14d, %r14
subq %rax, %r14
imulq 0x18(%rsp), %rax
addq %rcx, %rax
leaq (%rax,%rax,2), %r15
movq 0x28(%rsp), %rbp
movq 0x60(%rsp), %r13
leaq (%r15,%rbp), %rdi
leaq (%r12,%r15), %rsi
movq %rbx, %rdx
callq 0x10e0
addq %r13, %r15
decq %r14
jne 0x5271
jmp 0x5290
movq 0x28(%rsp), %rbp
movl 0x10(%rsp), %r13d
imull $0x3afb7e91, %r13d, %eax # imm = 0x3AFB7E91
addl $0xc504816f, %eax       # imm = 0xC504816F
rorl $0x4, %eax
cmpl $0x68db8, %eax          # imm = 0x68DB8
ja 0x536b
cmpl $0x6, 0x24(%rsp)
jl 0x5314
movl 0x38(%rsp), %edx
incl %edx
leaq 0x78(%rsp), %rdi
leaq 0x5f19(%rip), %rsi      # 0xb1e1
movl %edx, 0x38(%rsp)
xorl %eax, %eax
callq 0x1140
movq 0x78(%rsp), %rax
testq %rax, %rax
jne 0x52e8
jmp 0x53fa
movb $0x30, (%rax)
incq %rax
movzbl (%rax), %ecx
cmpl $0x20, %ecx
je 0x52e2
testl %ecx, %ecx
jne 0x52e5
movq 0x78(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x14(%rsp), %edx
movl $0x3, %ecx
movq %r12, %r8
movl 0x34(%rsp), %r9d
callq 0x2c5a
xorl %edi, %edi
callq 0x10f0
cmpq 0x68(%rsp), %rax
je 0x536b
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x68(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
subq 0x58(%rsp), %rax
divsd 0x88(%rsp), %xmm0
cvtsi2sd %rax, %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
subsd %xmm2, %xmm1
mulsd 0x5c43(%rip), %xmm0     # 0xafa0
leaq 0x5e8e(%rip), %rdi      # 0xb1f2
movb $0x2, %al
callq 0x1070
incl %r13d
cmpl 0xc(%rsp), %r13d
jne 0x506e
movq 0x70(%rsp), %rdi
callq 0x1030
movq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl 0x14(%rsp), %edx
movl $0x3, %ecx
movq %r12, %r8
movl 0x34(%rsp), %r9d
callq 0x2c5a
movq %rbp, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
xorl %edi, %edi
callq 0x10f0
subq 0x58(%rsp), %rax
movl 0xc(%rsp), %r8d
movq 0x40(%rsp), %rcx
subl %ecx, %r8d
leaq 0x5e40(%rip), %rdi      # 0xb219
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5dec(%rip), %rdi      # 0xb1ed
leaq 0x5d71(%rip), %rsi      # 0xb179
leaq 0x5d73(%rip), %rcx      # 0xb182
movl $0xa3, %edx
callq 0x1080
callq 0x11c0
leaq 0x5d44(%rip), %rdi      # 0xb169
leaq 0x5d4d(%rip), %rsi      # 0xb179
leaq 0x5d4f(%rip), %rcx      # 0xb182
movl $0x70, %edx
callq 0x1080