// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x155c(%rip), %rdx      # 0x3123
callq 0x10a0
movb %al, -0x11(%rbp)
movsbl %al, %eax
cmpl $-0x1, %eax
je 0x1d2e
movsbl -0x11(%rbp), %eax
addl $-0x66, %eax
movl %eax, %ecx
movq %rcx, -0xa0(%rbp)
subl $0x11, %eax
ja 0x1d29
movq -0xa0(%rbp), %rax
leaq 0x1486(%rip), %rcx      # 0x3088
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x33be(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x350f(%rip)      # 0x5138
cmpl $0x0, 0x3508(%rip)      # 0x5138
jne 0x1c60
movq 0x33a7(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq 0x338d(%rip), %rax      # 0x4fd0
movq (%rax), %rdx
leaq 0x14e0(%rip), %rsi      # 0x312d
movb $0x0, %al
callq 0x11e0
movl $0x1, -0x4(%rbp)
jmp 0x2101
jmp 0x1d29
movq 0x3364(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x34b9(%rip)      # 0x513c
cmpl $0x0, 0x34b2(%rip)      # 0x513c
jne 0x1cba
movq 0x334d(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq 0x3333(%rip), %rax      # 0x4fd0
movq (%rax), %rdx
leaq 0x14a3(%rip), %rsi      # 0x314a
movb $0x0, %al
callq 0x11e0
movl $0x1, -0x4(%rbp)
jmp 0x2101
jmp 0x1d29
movq 0x330d(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x3466(%rip)      # 0x5140
cmpl $0x0, 0x345f(%rip)      # 0x5140
jne 0x1d11
movq 0x32f6(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq 0x32dc(%rip), %rax      # 0x4fd0
movq (%rax), %rdx
leaq 0x146a(%rip), %rsi      # 0x3168
movb $0x0, %al
callq 0x11e0
movl $0x1, -0x4(%rbp)
jmp 0x2101
jmp 0x1d29
movl $0x1, 0x3463(%rip)      # 0x5180
jmp 0x1d29
movl $0x1, 0x345b(%rip)      # 0x5184
jmp 0x1bb9
callq 0x1530
cmpl $0x0, %eax
je 0x1d72
movq 0x32a1(%rip), %rax      # 0x4fe0
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
callq 0x1110
movq -0xa8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1426(%rip), %rsi      # 0x3185
movb $0x0, %al
callq 0x11e0
movl $0x1, -0x4(%rbp)
jmp 0x2101
leaq 0x340f(%rip), %rax      # 0x5188
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x1500
movl %eax, -0x18(%rbp)
cvtsi2sdl -0x18(%rbp), %xmm1
movsd 0x12a1(%rip), %xmm0     # 0x3048
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x18(%rbp), %edx
movsd -0x20(%rbp), %xmm0
xorl %edi, %edi
leaq 0x13e0(%rip), %rsi      # 0x31a1
movb $0x1, %al
callq 0x1210
movl 0x336a(%rip), %eax      # 0x5138
imull 0x3367(%rip), %eax      # 0x513c
movslq %eax, %rdi
movl $0x4, %esi
callq 0x10f0
movq %rax, 0x338f(%rip)      # 0x5178
cmpq $0x0, 0x3387(%rip)      # 0x5178
jne 0x1e1b
movl 0x333f(%rip), %edx      # 0x5138
movl 0x333d(%rip), %ecx      # 0x513c
xorl %edi, %edi
leaq 0x13cb(%rip), %rsi      # 0x31d3
movb $0x0, %al
callq 0x11b0
movl $0x1, -0x4(%rbp)
jmp 0x2101
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0x11c0
movq %rax, -0x30(%rbp)
callq 0x11c0
movq %rax, -0x38(%rbp)
movl $0x1, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x20e9
leaq -0x70(%rbp), %rdi
callq 0x10d0
movl -0x70(%rbp), %eax
movl %eax, -0xac(%rbp)
subl $0x100, %eax            # imm = 0x100
je 0x1e82
jmp 0x1e61
movl -0xac(%rbp), %eax
subl $0x300, %eax            # imm = 0x300
je 0x1e8b
jmp 0x1e70
movl -0xac(%rbp), %eax
subl $0x400, %eax            # imm = 0x400
je 0x1e9a
jmp 0x1f09
movl $0x0, -0x74(%rbp)
jmp 0x1f09
cmpl $0x1b, -0x5c(%rbp)
jne 0x1e98
movl $0x0, -0x74(%rbp)
jmp 0x1f09
leaq -0x78(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x11f0
cvtsi2sdl -0x78(%rbp), %xmm1
cvtsi2sdl 0x3284(%rip), %xmm0 # 0x5138
divsd %xmm0, %xmm1
movsd 0x1190(%rip), %xmm0     # 0x3050
addsd %xmm1, %xmm0
movsd 0x117c(%rip), %xmm1     # 0x3048
subsd %xmm1, %xmm0
movsd %xmm0, 0x3270(%rip)     # 0x5148
cvtsi2sdl -0x7c(%rbp), %xmm1
cvtsi2sdl 0x3257(%rip), %xmm0 # 0x513c
divsd %xmm0, %xmm1
movsd 0x115f(%rip), %xmm0     # 0x3050
addsd %xmm1, %xmm0
movsd 0x114b(%rip), %xmm1     # 0x3048
subsd %xmm1, %xmm0
movsd %xmm0, 0x3247(%rip)     # 0x5150
movsd -0x20(%rbp), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x16b0
movq -0x30(%rbp), %rax
movq %rax, -0xb8(%rbp)
callq 0x11c0
movq -0xb8(%rbp), %rdi
movq %rax, %rsi
callq 0x13e0
movaps %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1f51
jmp 0x1f22
movq -0x30(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x11c0
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x13e0
ucomisd -0x20(%rbp), %xmm0
jb 0x1f79
jmp 0x1f98
leaq 0x1277(%rip), %rdi      # 0x31f7
leaq 0x12c3(%rip), %rsi      # 0x324a
movl $0xf4, %edx
leaq 0x12c8(%rip), %rcx      # 0x325b
callq 0x10e0
callq 0x11c0
movq %rax, -0x88(%rbp)
movq 0x31c5(%rip), %rdi      # 0x5170
movq 0x31c6(%rip), %rdx      # 0x5178
movl 0x3180(%rip), %ecx      # 0x5138
shll $0x2, %ecx
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0xd0(%rbp)
callq 0x1120
movq 0x3196(%rip), %rdi      # 0x5168
callq 0x1160
movq -0xd0(%rbp), %rcx
movq 0x3183(%rip), %rdi      # 0x5168
movq 0x3184(%rip), %rsi      # 0x5170
movq %rcx, %rdx
callq 0x1170
movq 0x316d(%rip), %rdi      # 0x5168
callq 0x1080
movq -0x30(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x1470
movsd %xmm0, -0x90(%rbp)
callq 0x1140
movq %rax, %xmm0
movaps 0xfe7(%rip), %xmm3      # 0x3010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0xfeb(%rip), %xmm2      # 0x3020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
callq 0x11c0
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
callq 0x1470
movsd 0xf93(%rip), %xmm1      # 0x3030
ucomisd %xmm1, %xmm0
jbe 0x20d9
movsd -0x90(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
leaq 0x11b8(%rip), %rdi      # 0x3272
movb $0x2, %al
callq 0x1050
movq 0x2ef8(%rip), %rax      # 0x4fc0
movq (%rax), %rdi
callq 0x1190
callq 0x11c0
movq %rax, -0x38(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x1e3c
movq 0x3088(%rip), %rdi      # 0x5178
callq 0x11a0
callq 0x1b70
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq