// File: miklhh[P]mandelbrot/src/main.cpp
// Function: main
int main (int argc, char *argv[])
{
    /* Get rid of the 'argv', 'argc' warnings. */
    (void)argc;
    (void)argv;

    /* Init SDL. */
    if (SDL_Init(SDL_INIT_VIDEO) != 0) 
    {
        std::cout << "Could not init SDL." << std::endl;
        return -1;
    }

    SDL_Event event;
    if (SDL_CreateWindowAndRenderer(scr_width, scr_height, 0, &window, &renderer) != 0)
    {
        std::cerr << "Could not create window and renderer." << std::endl;
        return -1;
    }

    /* Initialize the mandelbrot renderer. */

    //complex<double> ul = std::complex<double>(-0.74510, 0.11868);
    //complex<double> lr = std::complex<double>(-0.74490, 0.11848);

    /* Fractal. */
    //complex<double> ul = std::complex<double>(-0.7455, 0.1190);
    //complex<double> lr = std::complex<double>(-0.7445, 0.1180);

    /* Fractal zoomed out. */
    //complex<double> ul = std::complex<double>(-0.746, 0.120);
    //complex<double> lr = std::complex<double>(-0.745, 0.119);

    /* Original fractal. */
    std::complex<double> ul = std::complex<double>(-2, 1);
    std::complex<double> lr = std::complex<double>(1, -1);


    render_init(4);
    render_mandelbrot(ul, lr);
    //render_mandelbrot(std::complex<double>(0.3994999999988, 0.195303), scale_t{ 0.00000001, 0.00000001 });

    /* Draw mandelbrot to the renderer and renderer to the screen. */
    bool once = true;
    while (window_is_open)
    {
        if (render_test_complete() && once)
        {
            render_create_bmp("hello.bmp");
            once = false;
        }

        render_draw_to_SDL_Renderer(renderer);
        SDL_RenderPresent(renderer);

        /* Handle events. */
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT) { window_is_open = false; }
        }
        SDL_Delay(1);
    }
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    shutdown("Successful exit.", 0);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x20, %edi
callq 0x3230
cmpl $0x0, %eax
je 0x3796
movq 0x11861(%rip), %rdi     # 0x14fd0
leaq 0xa8ea(%rip), %rsi      # 0xe060
callq 0x3260
movq %rax, %rdi
movq 0x11833(%rip), %rsi     # 0x14fb8
callq 0x3290
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x395b
movl $0x780, %edi            # imm = 0x780
movl $0x438, %esi            # imm = 0x438
xorl %edx, %edx
leaq 0x11aa7(%rip), %rcx     # 0x15250
leaq 0x11aa8(%rip), %r8      # 0x15258
callq 0x30a0
cmpl $0x0, %eax
je 0x37e8
movq 0x1182f(%rip), %rdi     # 0x14ff0
leaq 0xa8ac(%rip), %rsi      # 0xe074
callq 0x3260
movq %rax, %rdi
movq 0x117e1(%rip), %rsi     # 0x14fb8
callq 0x3290
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x395b
leaq -0x58(%rbp), %rdi
movsd 0xa824(%rip), %xmm0     # 0xe018
movsd 0xa80c(%rip), %xmm1     # 0xe008
callq 0x3980
leaq -0x68(%rbp), %rdi
movsd 0xa7fb(%rip), %xmm0     # 0xe008
movsd 0xa7fb(%rip), %xmm1     # 0xe010
callq 0x3980
movl $0x4, %edi
callq 0x3d00
leaq -0x58(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4040
movb $0x1, -0x69(%rbp)
testb $0x1, 0x119f8(%rip)     # 0x15234
je 0x38a3
callq 0x44d0
testb $0x1, %al
jne 0x3849
jmp 0x385f
testb $0x1, -0x69(%rbp)
je 0x385f
leaq 0xa844(%rip), %rdi      # 0xe09a
callq 0x44f0
movb $0x0, -0x69(%rbp)
movq 0x119f2(%rip), %rdi     # 0x15258
callq 0x4390
movq 0x119e6(%rip), %rdi     # 0x15258
callq 0x3050
leaq -0x48(%rbp), %rdi
callq 0x3160
cmpl $0x0, %eax
je 0x3897
cmpl $0x100, -0x48(%rbp)     # imm = 0x100
jne 0x3895
movb $0x0, 0x1199f(%rip)     # 0x15234
jmp 0x3877
movl $0x1, %edi
callq 0x31b0
jmp 0x3835
movq 0x119ae(%rip), %rdi     # 0x15258
callq 0x30e0
movq 0x1199a(%rip), %rdi     # 0x15250
callq 0x3030
leaq -0x91(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x33f0
movq -0xb0(%rbp), %rdx
leaq 0xa7c8(%rip), %rsi      # 0xe0a4
leaq -0x90(%rbp), %rdi
callq 0x39b0
jmp 0x38ea
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
callq 0x36b0
jmp 0x38fa
leaq -0x90(%rbp), %rdi
callq 0x3170
leaq -0x91(%rbp), %rdi
callq 0x32b0
movl $0x0, -0x4(%rbp)
jmp 0x395b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x394d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3170
leaq -0x91(%rbp), %rdi
callq 0x32b0
jmp 0x3967
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x33e0
nopw %cs:(%rax,%rax)
nopl (%rax)