// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rsi, %r14
movl %edi, %ebx
xorl %edi, %edi
callq 0x10f0
movl %eax, %edi
callq 0x10a0
movl %ebx, 0xc(%rsp)
cmpl $0x2, %ebx
jl 0x4f22
movq 0x8(%r14), %rbx
jmp 0x4f29
leaq 0x61cb(%rip), %rbx      # 0xb0f4
cmpl $0x3, 0xc(%rsp)
jl 0x4f36
movq 0x10(%r14), %rax
jmp 0x4f3d
leaq 0x61c1(%rip), %rax      # 0xb0fe
movq %rax, 0x68(%rsp)
movl $0x64, 0x8(%rsp)
cmpl $0x4, 0xc(%rsp)
jl 0x4f66
movq 0x18(%r14), %rdi
callq 0x1170
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x5456
movl $0x5, %eax
movq %rax, 0x50(%rsp)
cmpl $0x5, 0xc(%rsp)
jl 0x4f8d
movq 0x20(%r14), %rdi
callq 0x1170
movq %rax, 0x50(%rsp)
testl %eax, %eax
je 0x5475
movq $0x0, 0x48(%rsp)
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x70(%rsp)
leaq 0x64(%rsp), %rsi
leaq 0x60(%rsp), %rdx
leaq 0xb4(%rsp), %rcx
movq %rbx, %rdi
movl $0x3, %r8d
callq 0x36aa
movq %rax, 0x88(%rsp)
testq %rax, %rax
je 0x5451
movslq 0x64(%rsp), %rax
movl 0x60(%rsp), %ecx
movq %rax, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
imull %ecx, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %r14
movq %r14, %rdi
callq 0x1100
movq %rax, %r12
movq %r14, %rdi
callq 0x1100
movq %rax, %r13
cmpl $0x0, 0x8(%rsp)
jle 0x53b2
movq 0x50(%rsp), %rax
addl %eax, %eax
movl %eax, 0x5c(%rsp)
movq 0x18(%rsp), %rax
leal (%rax,%rax,2), %ecx
movl %ecx, 0x58(%rsp)
cvtsi2sdl 0x8(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq (%rax,%rax,2), %rbp
movq $0x0, 0x80(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %ebx, %ebx
movq %r12, 0x40(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 0x78(%rsp)
movl %ebx, 0x10(%rsp)
callq 0x11b0
cltd
movq 0x18(%rsp), %rbp
idivl %ebp
movl %edx, %r14d
callq 0x11b0
cltd
movl 0x14(%rsp), %ecx
idivl %ecx
movl %ecx, %r8d
movl %r14d, %esi
movq 0x50(%rsp), %r15
subl %r15d, %esi
movl %edx, %eax
subl %r15d, %eax
movq %rax, %rcx
shlq $0x20, %rcx
addl %r15d, %r14d
addl %r15d, %edx
movl %esi, %edi
sarl $0x1f, %edi
andl %esi, %edi
subl %edi, %r14d
testl %esi, %esi
movl $0x0, %edi
cmovlel %edi, %esi
testl %eax, %eax
cmovnsl %edi, %eax
movl $0x0, %edi
cmovnsq %rcx, %rdi
subl %eax, %edx
movq %r12, %r15
movq %rdi, %r12
shrq $0x20, %r12
movl 0x5c(%rsp), %ecx
subl %ecx, %r12d
shlq $0x20, %r12
cmpl %r8d, %edx
movl $0x0, %eax
cmovgl %ecx, %eax
cmovleq %rdi, %r12
movl %edx, 0x38(%rsp)
movl %eax, 0x30(%rsp)
subl %eax, %edx
movq %rdx, 0x90(%rsp)
movq %rdx, %rax
shlq $0x20, %rax
cmpl %ebp, %r14d
movl $0x0, %edx
cmovgl %ecx, %edx
subl %edx, %r14d
movl %esi, 0x3c(%rsp)
movl %esi, %r13d
movl %edx, 0x34(%rsp)
subl %edx, %r13d
orq %r12, %r13
orq %rax, %r14
callq 0x11b0
cltd
idivl %ebp
movl %edx, %ebx
callq 0x11b0
cltd
idivl 0x14(%rsp)
imull %ebp, %edx
addl %ebx, %edx
leal (%rdx,%rdx,2), %eax
movslq %eax, %rcx
movq 0x88(%rsp), %rbx
addq %rbx, %rcx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl %ebp, %r8d
movq 0x50(%rsp), %r9
callq 0x4ce2
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r14, %rcx
movl %ebp, %r8d
callq 0x4b5e
movsd %xmm0, 0xa8(%rsp)
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movl %ebp, %r8d
callq 0x4b5e
ucomisd 0xa8(%rsp), %xmm0
jbe 0x523a
movq 0x48(%rsp), %rax
incl %eax
movq %rax, 0x48(%rsp)
movq %r12, %rax
shrq $0x20, %rax
cmpl %eax, 0x90(%rsp)
jle 0x52b5
subq %r13, %r14
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %rax, %r14
sarq $0x20, %r14
sarq $0x20, %r12
movslq 0x30(%rsp), %rbx
addq %r12, %rbx
movslq 0x38(%rsp), %rax
subq %rax, %rbx
imulq 0x18(%rsp), %r12
movl 0x3c(%rsp), %eax
addq %r12, %rax
movslq 0x34(%rsp), %rcx
subq %rcx, %rax
leaq (%rax,%rax,2), %r15
movq 0x40(%rsp), %r12
movq 0x20(%rsp), %r13
movq 0x78(%rsp), %rbp
leaq (%r15,%r13), %rdi
leaq (%r12,%r15), %rsi
movq %r14, %rdx
callq 0x10e0
addq %rbp, %r15
incq %rbx
jne 0x521d
jmp 0x52bf
movq %r12, %rax
shrq $0x20, %rax
cmpl %eax, 0x90(%rsp)
jle 0x52b5
subq %r13, %r14
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %rax, %r14
sarq $0x20, %r14
sarq $0x20, %r12
movslq 0x30(%rsp), %rbx
addq %r12, %rbx
movslq 0x38(%rsp), %rax
subq %rax, %rbx
imulq 0x18(%rsp), %r12
movl 0x3c(%rsp), %eax
addq %r12, %rax
movslq 0x34(%rsp), %rcx
subq %rcx, %rax
leaq (%rax,%rax,2), %r15
movq 0x40(%rsp), %r12
movq 0x20(%rsp), %r13
movq 0x78(%rsp), %rbp
leaq (%r12,%r15), %rdi
leaq (%r15,%r13), %rsi
movq %r14, %rdx
callq 0x10e0
addq %rbp, %r15
incq %rbx
jne 0x529b
jmp 0x52bf
movq 0x40(%rsp), %r12
movq 0x20(%rsp), %r13
movl 0x10(%rsp), %ebx
imull $0x3afb7e91, %ebx, %eax # imm = 0x3AFB7E91
addl $0xc504816f, %eax       # imm = 0xC504816F
rorl $0x4, %eax
cmpl $0x68db8, %eax          # imm = 0x68DB8
ja 0x53a6
cmpl $0x6, 0xc(%rsp)
jl 0x534a
movl 0x2c(%rsp), %edx
incl %edx
leaq 0x98(%rsp), %rdi
leaq 0x5e89(%rip), %rsi      # 0xb181
movl %edx, 0x2c(%rsp)
xorl %eax, %eax
callq 0x1140
movq 0x98(%rsp), %rax
testq %rax, %rax
jne 0x531b
jmp 0x5432
movb $0x30, (%rax)
incq %rax
movzbl (%rax), %ecx
cmpl $0x20, %ecx
je 0x5315
testl %ecx, %ecx
jne 0x5318
movq 0x98(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x14(%rsp), %edx
movl $0x3, %ecx
movq %r13, %r8
movl 0x58(%rsp), %r9d
callq 0x2d78
xorl %edi, %edi
callq 0x10f0
cmpq 0x80(%rsp), %rax
je 0x53a6
xorl %edi, %edi
callq 0x10f0
movq %rax, 0x80(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
subq 0x70(%rsp), %rax
divsd 0xa0(%rsp), %xmm0
cvtsi2sd %rax, %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
subsd %xmm2, %xmm1
mulsd 0x5ba8(%rip), %xmm0     # 0xaf40
leaq 0x5df3(%rip), %rdi      # 0xb192
movb $0x2, %al
callq 0x1070
incl %ebx
cmpl 0x8(%rsp), %ebx
jne 0x5067
movq 0x88(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rsi
leal (%rsi,%rsi,2), %r9d
movq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl 0x14(%rsp), %edx
movl $0x3, %ecx
movq %r13, %r8
callq 0x2d78
movq %r12, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
xorl %edi, %edi
callq 0x10f0
subq 0x70(%rsp), %rax
movl 0x8(%rsp), %r8d
movq 0x48(%rsp), %rcx
subl %ecx, %r8d
leaq 0x5da8(%rip), %rdi      # 0xb1b9
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1070
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d54(%rip), %rdi      # 0xb18d
leaq 0x5cd9(%rip), %rsi      # 0xb119
leaq 0x5cdb(%rip), %rcx      # 0xb122
movl $0xa3, %edx
callq 0x1080
callq 0x11d0
leaq 0x5cac(%rip), %rdi      # 0xb109
leaq 0x5cb5(%rip), %rsi      # 0xb119
leaq 0x5cb7(%rip), %rcx      # 0xb122
movl $0x70, %edx
callq 0x1080
leaq 0x5cbd(%rip), %rdi      # 0xb139
leaq 0x5c96(%rip), %rsi      # 0xb119
leaq 0x5c98(%rip), %rcx      # 0xb122
movl $0x74, %edx
callq 0x1080