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

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

    if (InitGL() != 0) {
        return EXIT_FAILURE;
    }

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

    double elapsedTimeSecs = 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_WINDOWEVENT:
            if (event.window.event == SDL_WINDOWEVENT_RESIZED ||
                event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
                gWidth = event.window.data1;
                gHeight = event.window.data2;
                glViewport(0, 0, gWidth, gHeight);
            }
            break;
        }

        elapsedTimeSecs += targetSecsPerFrame;

        DrawFrame(elapsedTimeSecs);

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

        Uint64 endCounter = SDL_GetPerformanceCounter();

        SDL_GL_SwapWindow(gWindow);

        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;
    }

    DestroyGL();
    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 0x697(%rip), %r14       # 0x3279
movq 0x234f(%rip), %r12      # 0x4f38
pushq $0xa
popq %r15
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21d0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x2c5c
sarl $0x18, %eax
cmpl $0x66, %eax
je 0x2c50
cmpl $0x68, %eax
je 0x2c33
cmpl $0x77, %eax
jne 0x2bed
movq (%r12), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x21a0
movl %eax, 0x2532(%rip)      # 0x515c
testl %eax, %eax
jne 0x2bed
jmp 0x2ea3
movq (%r12), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x21a0
movl %eax, 0x2519(%rip)      # 0x5160
testl %eax, %eax
jne 0x2bed
jmp 0x2eba
movl $0x1, 0x2536(%rip)      # 0x5190
jmp 0x2bed
callq 0x2453
testl %eax, %eax
jne 0x2e85
callq 0x27e4
testl %eax, %eax
je 0x2c7a
pushq $0x1
popq %rax
jmp 0x2e76
movl 0x2524(%rip), %eax      # 0x51a4
testl %eax, %eax
pushq $0x3c
popq %rdx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x3a4(%rip), %xmm0      # 0x3038
divsd %xmm1, %xmm0
leaq 0x638(%rip), %rsi       # 0x32d7
xorl %edi, %edi
movsd %xmm0, 0x8(%rsp)
movb $0x1, %al
callq 0x21b0
callq 0x20c0
movq %rax, %r13
callq 0x20c0
movq %rax, %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x1
popq %r14
leaq 0x20(%rsp), %rbx
testl %r14d, %r14d
je 0x2e6a
movq %r13, %r12
movq %rbx, %rdi
callq 0x2200
movl 0x20(%rsp), %eax
cmpl $0x300, %eax            # imm = 0x300
je 0x2d44
cmpl $0x200, %eax            # imm = 0x200
movsd 0x8(%rsp), %xmm1
je 0x2d0d
pushq $0x1
popq %r14
cmpl $0x100, %eax            # imm = 0x100
jne 0x2d56
xorl %r14d, %r14d
jmp 0x2d56
movb 0x2c(%rsp), %al
addb $-0x5, %al
pushq $0x1
popq %r14
cmpb $0x1, %al
ja 0x2d56
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %ecx
movl %edx, 0x2433(%rip)      # 0x515c
movl %ecx, 0x2431(%rip)      # 0x5160
xorl %edi, %edi
xorl %esi, %esi
callq 0x20f0
movsd 0x8(%rsp), %xmm1
pushq $0x1
popq %r14
jmp 0x2d56
xorl %r14d, %r14d
cmpl $0x1b, 0x34(%rsp)
setne %r14b
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x2aec
callq 0x20c0
movq %r12, %rdi
movq %rax, %rsi
callq 0x237c
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2d6b
callq 0x20c0
movq %r12, %rdi
movq %rax, %rsi
callq 0x237c
ucomisd 0x8(%rsp), %xmm0
jb 0x2edb
callq 0x20c0
movq %rax, %r13
movq 0x23be(%rip), %rdi      # 0x5170
callq 0x20e0
movq %r12, %rdi
movq %r13, %rsi
callq 0x23dd
movsd %xmm0, 0x18(%rsp)
callq 0x2210
movq %rax, %rbp
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0x23dd
ucomisd 0x248(%rip), %xmm0      # 0x3030
jbe 0x2cd1
movq %rbp, %xmm0
movq 0x215(%rip), %xmm2      # 0x3010
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x219(%rip), %xmm3      # 0x3020
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %r13, %rax
subq %r12, %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 0x546(%rip), %rdi       # 0x3387
movsd 0x18(%rsp), %xmm0
movb $0x2, %al
callq 0x2030
movq 0x2103(%rip), %rax      # 0x4f58
movq (%rax), %rdi
callq 0x2120
callq 0x20c0
movq %rax, %r15
jmp 0x2cd1
callq 0x2b67
callq 0x2bb5
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20e4(%rip), %rax      # 0x4f70
movq (%rax), %rbx
callq 0x21f0
leaq 0x420(%rip), %rsi       # 0x32bb
movq %rbx, %rdi
movq %rax, %rdx
jmp 0x2ecf
movq 0x20c6(%rip), %rax      # 0x4f70
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x3c8(%rip), %rsi       # 0x3280
jmp 0x2ecf
movq 0x20af(%rip), %rax      # 0x4f70
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x3ce(%rip), %rsi       # 0x329d
xorl %eax, %eax
callq 0x2080
jmp 0x2c72
leaq 0x427(%rip), %rdi       # 0x3309
leaq 0x473(%rip), %rsi       # 0x335c
leaq 0x480(%rip), %rcx       # 0x3370
movl $0x175, %edx            # imm = 0x175
callq 0x2060