// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x6ae(%rip), %rdx       # 0x20eb
callq 0x10a0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x1b12
leaq 0x2729(%rip), %r15      # 0x4180
movq 0x2572(%rip), %r12      # 0x3fd0
leaq 0x686(%rip), %r14       # 0x20eb
leaq 0x2718(%rip), %r13      # 0x4184
sarl $0x18, %eax
cmpl $0x68, %eax
jle 0x1aa2
cmpl $0x69, %eax
je 0x1ace
cmpl $0x73, %eax
je 0x1ad9
cmpl $0x77, %eax
jne 0x1af7
movq (%r12), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x269f(%rip)      # 0x4138
testl %eax, %eax
jne 0x1af7
jmp 0x1f21
movq %r15, %rcx
cmpl $0x66, %eax
je 0x1ad1
cmpl $0x68, %eax
jne 0x1af7
movq (%r12), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x2677(%rip)      # 0x413c
testl %eax, %eax
jne 0x1af7
jmp 0x1f1a
movq %r13, %rcx
movl $0x1, (%rcx)
jmp 0x1af7
movq (%r12), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x2651(%rip)      # 0x4140
testl %eax, %eax
je 0x1f13
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10a0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x1a6c
callq 0x14cd
testl %eax, %eax
jne 0x1f0c
movl 0x266f(%rip), %eax      # 0x4194
testl %eax, %eax
movl $0x3c, %edx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x52d(%rip), %xmm0      # 0x2068
divsd %xmm1, %xmm0
leaq 0x623(%rip), %rsi       # 0x2169
xorl %edi, %edi
movsd %xmm0, (%rsp)
movb $0x1, %al
callq 0x1210
movl 0x25de(%rip), %ebx      # 0x4138
movl 0x25dc(%rip), %ebp      # 0x413c
movl %ebp, %eax
imull %ebx, %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x10f0
movq %rax, 0x25ff(%rip)      # 0x4178
testq %rax, %rax
je 0x1ee4
callq 0x11c0
movq %rax, %rbp
callq 0x11c0
movq %rax, %r13
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x10d0
movl 0x30(%rsp), %eax
cmpl $0x400, %eax            # imm = 0x400
je 0x1bd9
cmpl $0x300, %eax            # imm = 0x300
je 0x1bcf
cmpl $0x100, %eax            # imm = 0x100
movsd (%rsp), %xmm1
jne 0x1c51
movb $0x1, %bl
jmp 0x1c53
cmpl $0x1b, 0x44(%rsp)
sete %bl
jmp 0x1c4a
leaq 0xc(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x11f0
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x253c(%rip), %xmm1 # 0x4138
divsd %xmm1, %xmm0
movsd 0x438(%rip), %xmm2      # 0x2040
addsd %xmm2, %xmm0
movsd 0x434(%rip), %xmm3      # 0x2048
addsd %xmm3, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x8(%rsp), %xmm1
movsd %xmm0, 0x251f(%rip)     # 0x4148
xorps %xmm0, %xmm0
cvtsi2sdl 0x2508(%rip), %xmm0 # 0x413c
divsd %xmm0, %xmm1
addsd %xmm2, %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, 0x2508(%rip)     # 0x4150
xorl %ebx, %ebx
movsd (%rsp), %xmm1
jmp 0x1c53
xorl %ebx, %ebx
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x1610
callq 0x11c0
subq %rbp, %rax
movq %rax, %xmm0
movq 0x393(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x397(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
punpckldq 0x35f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x367(%rip), %xmm0      # 0x2020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd (%rsp), %xmm0
ucomisd %xmm2, %xmm0
ja 0x1c68
callq 0x11c0
subq %rbp, %rax
movq %rax, %xmm0
movq 0x321(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x325(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
punpckldq 0x2ed(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2f5(%rip), %xmm0      # 0x2020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
ucomisd (%rsp), %xmm0
jb 0x1f28
callq 0x11c0
movq %rax, %r14
movq 0x2415(%rip), %rdi      # 0x4170
movq 0x2416(%rip), %rdx      # 0x4178
movl 0x23d0(%rip), %ecx      # 0x4138
shll $0x2, %ecx
xorl %esi, %esi
callq 0x1120
movq 0x23ef(%rip), %rdi      # 0x4168
callq 0x1160
movq 0x23e3(%rip), %rdi      # 0x4168
movq 0x23e4(%rip), %rsi      # 0x4170
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1170
movq 0x23cc(%rip), %rdi      # 0x4168
callq 0x1080
callq 0x1140
movq %rax, %r15
callq 0x1140
movq %rax, %r12
callq 0x11c0
subq %r13, %rax
movq %rax, %xmm0
movq 0x24a(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x24e(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x246(%rip), %xmm0      # 0x2030
mulsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
movq 0x20a(%rip), %xmm3      # 0x2010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x20e(%rip), %xmm4      # 0x2020
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
ucomisd 0x1fc(%rip), %xmm0      # 0x2030
jbe 0x1ec4
movq %r14, %rax
subq %rbp, %rax
movq %rax, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
movq %r12, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm2, %xmm1
movapd %xmm2, %xmm0
mulsd 0x1ae(%rip), %xmm0      # 0x2030
movq %r15, %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm4, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
divsd %xmm3, %xmm0
leaq 0x394(%rip), %rdi       # 0x223a
movb $0x2, %al
callq 0x1050
movq 0x210c(%rip), %rax      # 0x3fc0
movq (%rax), %rdi
callq 0x1190
callq 0x11c0
movq %rax, %r13
movq %r14, %rbp
testb %bl, %bl
je 0x1b9c
movq 0x22a2(%rip), %rdi      # 0x4178
callq 0x11a0
callq 0x19f8
xorl %eax, %eax
jmp 0x1efd
leaq 0x2b0(%rip), %rsi       # 0x219b
xorl %edi, %edi
movl %ebx, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x11b0
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12c6
jmp 0x1ef8
callq 0x1260
jmp 0x1ef8
callq 0x1282
jmp 0x1ef8
callq 0x12a4
jmp 0x1ef8
leaq 0x290(%rip), %rdi       # 0x21bf
leaq 0x2dc(%rip), %rsi       # 0x2212
leaq 0x2e6(%rip), %rcx       # 0x2223
movl $0xf4, %edx
callq 0x10e0