// File: /workspace/github_c_makefile_star2_O2/adelciotto[P]plasma/src/rgb_plasma.c
// Function: main
int main(int argc, char *argv[]) {
    char opt;
    while ((opt = getopt(argc, argv, ":w:h:s:fi")) != -1) {
        switch (opt) {
        case 'w':
            // Obviously not proper use of strtol, but, thats fine
            // for this simple program.
            width = strtol(optarg, (char **)NULL, 10);
            if (width == 0) {
                fprintf(stderr, "invalid value for width: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 'h':
            height = strtol(optarg, (char **)NULL, 10);
            if (height == 0) {
                fprintf(stderr, "invalid value for height: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 's':
            scale = strtol(optarg, (char **)NULL, 10);
            if (scale == 0) {
                fprintf(stderr, "invalid value for scale: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 'f':
            fullscreen = 1;
            break;
        case 'i':
            interactive = 1;
            break;
        }
    }

    if (InitSDL() != 0) {
        fprintf(stderr, "error initializing SDL, %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    int refreshRate = GetDisplayRefreshRate(displayMode);
    const double targetSecsPerFrame = 1.0 / (double)refreshRate;
    LogInfo("display refresh rate %d, target secs per frame %f", refreshRate,
            targetSecsPerFrame);

    pixelBuffer = calloc(width * height, sizeof(*pixelBuffer));
    if (pixelBuffer == NULL) {
        LogError("failed to calloc pixel buffer %dx%d", width, height);
        return EXIT_FAILURE;
    }

    double elapsedTimeMs = 0.0;
    Uint64 lastCounter = SDL_GetPerformanceCounter();
    Uint64 metricsPrintCounter = SDL_GetPerformanceCounter();
    SDL_Event event;
    int isRunning = 1;

    while (isRunning) {
        SDL_PollEvent(&event);
        switch (event.type) {
        case SDL_QUIT:
            isRunning = 0;
            break;
        case SDL_KEYDOWN:
            if (event.key.keysym.sym == SDLK_ESCAPE) {
                isRunning = 0;
            }
            break;
        case SDL_MOUSEMOTION: {
            int x, y;
            SDL_GetMouseState(&x, &y);
            mouseX = 0.5 + x / (double)width - 1.0;
            mouseY = 0.5 + y / (double)height - 1.0;
            break;
        }
        }

        elapsedTimeMs += targetSecsPerFrame;

        DrawFrame(elapsedTimeMs);

        // Manually cap the frame rate
        while (GetElapsedTimeSecs(lastCounter, SDL_GetPerformanceCounter()) <
               targetSecsPerFrame) {
        }
        assert(GetElapsedTimeSecs(lastCounter, SDL_GetPerformanceCounter()) >=
               targetSecsPerFrame);

        Uint64 endCounter = SDL_GetPerformanceCounter();

        SDL_UpdateTexture(texture, NULL, pixelBuffer,
                          width * sizeof(*pixelBuffer));
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);

        double msPerFrame = GetElapsedTimeMs(lastCounter, endCounter);
        double fps = (double)SDL_GetPerformanceFrequency() /
                     (double)(endCounter - lastCounter);

        if (GetElapsedTimeMs(metricsPrintCounter, SDL_GetPerformanceCounter()) >
            1000.0) {
            printf("ms/f: %f, fps: %f\r", msPerFrame, fps);
            fflush(stdout);
            metricsPrintCounter = SDL_GetPerformanceCounter();
        }

        lastCounter = endCounter;
    }

    free(pixelBuffer);
    DestroySDL();

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x7ca(%rip), %r14       # 0x20f3
leaq 0x2848(%rip), %r13      # 0x4178
movq 0x2699(%rip), %r12      # 0x3fd0
pushq $0xa
popq %r15
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10a0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x19e2
sarl $0x18, %eax
movq %r13, %rcx
cmpl $0x66, %eax
je 0x19d7
cmpl $0x68, %eax
je 0x1992
cmpl $0x69, %eax
je 0x19d0
cmpl $0x73, %eax
je 0x19af
cmpl $0x77, %eax
jne 0x193b
movq (%r12), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x11c0
movl %eax, 0x27a7(%rip)      # 0x4130
testl %eax, %eax
jne 0x193b
jmp 0x1cd9
movq (%r12), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x11c0
movl %eax, 0x278e(%rip)      # 0x4134
testl %eax, %eax
jne 0x193b
jmp 0x1cf0
movq (%r12), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x11c0
movl %eax, 0x2775(%rip)      # 0x4138
testl %eax, %eax
jne 0x193b
jmp 0x1d07
leaq 0x27a5(%rip), %rcx      # 0x417c
movl $0x1, (%rcx)
jmp 0x193b
callq 0x142b
testl %eax, %eax
jne 0x1ca4
movl 0x2797(%rip), %eax      # 0x418c
testl %eax, %eax
pushq $0x3c
popq %rdx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x66f(%rip), %xmm0      # 0x2078
divsd %xmm1, %xmm0
leaq 0x75d(%rip), %rsi       # 0x2171
xorl %edi, %edi
movsd %xmm0, (%rsp)
movb $0x1, %al
callq 0x1200
movl 0x2708(%rip), %ebx      # 0x4130
movl 0x2706(%rip), %ebp      # 0x4134
movl %ebp, %eax
imull %ebx, %eax
movslq %eax, %rdi
pushq $0x4
popq %rsi
callq 0x10f0
movq %rax, 0x272b(%rip)      # 0x4170
testq %rax, %rax
je 0x1c8e
callq 0x11b0
movq %rax, %r12
callq 0x11b0
movq %rax, %r13
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x1
popq %r15
leaq 0x20(%rsp), %rbx
testl %r15d, %r15d
je 0x1c79
movq %r12, %rbp
movq %rbx, %rdi
callq 0x10d0
movl 0x20(%rsp), %eax
cmpl $0x400, %eax            # imm = 0x400
je 0x1aba
cmpl $0x300, %eax            # imm = 0x300
je 0x1aac
pushq $0x1
popq %r15
cmpl $0x100, %eax            # imm = 0x100
movsd (%rsp), %xmm1
jne 0x1b26
xorl %r15d, %r15d
jmp 0x1b26
xorl %r15d, %r15d
cmpl $0x1b, 0x34(%rsp)
setne %r15b
jmp 0x1b21
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x11e0
cvtsi2sdl 0xc(%rsp), %xmm0
cvtsi2sdl 0x2659(%rip), %xmm1 # 0x4130
divsd %xmm1, %xmm0
movsd 0x56d(%rip), %xmm2      # 0x2050
addsd %xmm2, %xmm0
movsd 0x569(%rip), %xmm3      # 0x2058
addsd %xmm3, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm1
movsd %xmm0, 0x263f(%rip)     # 0x4140
cvtsi2sdl 0x262b(%rip), %xmm0 # 0x4134
divsd %xmm0, %xmm1
addsd %xmm2, %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, 0x262b(%rip)     # 0x4148
pushq $0x1
popq %r15
movsd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x155f
callq 0x11b0
movq %rbp, %rdi
movq %rax, %rsi
callq 0x1354
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1b3b
callq 0x11b0
movq %rbp, %rdi
movq %rax, %rsi
callq 0x1354
ucomisd (%rsp), %xmm0
jb 0x1d1e
callq 0x11b0
movq %rax, %r12
movq 0x25e8(%rip), %rdi      # 0x4168
movq 0x25e9(%rip), %rdx      # 0x4170
movl 0x25a3(%rip), %ecx      # 0x4130
shll $0x2, %ecx
xorl %esi, %esi
callq 0x1120
movq 0x25c2(%rip), %rdi      # 0x4160
callq 0x1150
movq 0x25b6(%rip), %rdi      # 0x4160
movq 0x25b7(%rip), %rsi      # 0x4168
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1160
movq 0x259f(%rip), %rdi      # 0x4160
callq 0x1080
movq %rbp, %rdi
movq %r12, %rsi
callq 0x13b5
movsd %xmm0, 0x18(%rsp)
callq 0x1130
movq %rax, %r14
callq 0x11b0
movq %r13, %rdi
movq %rax, %rsi
callq 0x13b5
ucomisd 0x449(%rip), %xmm0      # 0x2040
jbe 0x1a71
movq %r14, %xmm0
movq 0x406(%rip), %xmm2      # 0x2010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x40a(%rip), %xmm3      # 0x2020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %r12, %rax
subq %rbp, %rax
movq %rax, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
leaq 0x5f2(%rip), %rdi       # 0x2242
movsd 0x18(%rsp), %xmm0
movb $0x2, %al
callq 0x1050
movq 0x235c(%rip), %rax      # 0x3fc0
movq (%rax), %rdi
callq 0x1180
callq 0x11b0
movq %rax, %r13
jmp 0x1a71
movq 0x24f0(%rip), %rdi      # 0x4170
callq 0x1190
callq 0x18e4
xorl %eax, %eax
jmp 0x1cca
leaq 0x50e(%rip), %rsi       # 0x21a3
xorl %edi, %edi
movl %ebx, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x11a0
jmp 0x1cc7
movq 0x2335(%rip), %rax      # 0x3fe0
movq (%rax), %rbx
callq 0x1110
leaq 0x49b(%rip), %rsi       # 0x2155
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x11d0
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2300(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x40f(%rip), %rsi       # 0x20fd
jmp 0x1cc0
movq 0x22e9(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x415(%rip), %rsi       # 0x211a
jmp 0x1cc0
movq 0x22d2(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x41c(%rip), %rsi       # 0x2138
jmp 0x1cc0
leaq 0x4a2(%rip), %rdi       # 0x21c7
leaq 0x4ee(%rip), %rsi       # 0x221a
leaq 0x4f8(%rip), %rcx       # 0x222b
movl $0xf4, %edx
callq 0x10e0