// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x1593(%rip), %rdx      # 0x4279
callq 0x21d0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x2d72
leaq 0x157d(%rip), %r14      # 0x4279
movq 0x3235(%rip), %r15      # 0x5f38
sarl $0x18, %eax
cmpl $0x66, %eax
je 0x2d51
cmpl $0x68, %eax
je 0x2d33
cmpl $0x77, %eax
jne 0x2d5b
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x3432(%rip)      # 0x615c
testl %eax, %eax
jne 0x2d5b
jmp 0x30ff
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x3418(%rip)      # 0x6160
testl %eax, %eax
jne 0x2d5b
jmp 0x3106
movl $0x1, 0x3435(%rip)      # 0x6190
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21d0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x2d03
callq 0x24c5
testl %eax, %eax
jne 0x30f3
callq 0x286e
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x30e4
movl 0x340b(%rip), %eax      # 0x61a4
testl %eax, %eax
movl $0x3c, %edx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x1289(%rip), %xmm0     # 0x4038
divsd %xmm1, %xmm0
leaq 0x151d(%rip), %rsi      # 0x42d7
xorl %edi, %edi
movsd %xmm0, 0x8(%rsp)
movb $0x1, %al
callq 0x21b0
callq 0x20c0
movq %rax, %r12
callq 0x20c0
movq %rax, %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x2200
movl 0x30(%rsp), %eax
cmpl $0x300, %eax            # imm = 0x300
je 0x2e3f
cmpl $0x200, %eax            # imm = 0x200
movsd 0x8(%rsp), %xmm1
je 0x2e10
cmpl $0x100, %eax            # imm = 0x100
jne 0x2e3b
movb $0x1, %bl
jmp 0x2e4d
movb 0x3c(%rsp), %al
addb $-0x5, %al
cmpb $0x1, %al
ja 0x2e3b
movl 0x40(%rsp), %edx
movl 0x44(%rsp), %ecx
movl %edx, 0x3334(%rip)      # 0x615c
movl %ecx, 0x3332(%rip)      # 0x6160
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x20f0
jmp 0x2e47
xorl %ebx, %ebx
jmp 0x2e4d
cmpl $0x1b, 0x44(%rsp)
sete %bl
movsd 0x8(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x2bee
callq 0x20c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x1199(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x119d(%rip), %xmm1     # 0x4020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x2210
movq %rax, %xmm0
punpckldq 0x1165(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x116d(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
ja 0x2e62
callq 0x20c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x1126(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x112a(%rip), %xmm1     # 0x4020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x2210
movq %rax, %xmm0
punpckldq 0x10f2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x10fa(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
ucomisd 0x8(%rsp), %xmm0
jb 0x310d
callq 0x20c0
movq %rax, %r13
movq 0x3219(%rip), %rdi      # 0x6170
callq 0x20e0
callq 0x2210
movq %rax, %rbp
callq 0x2210
movq %rax, %r14
callq 0x20c0
subq %r15, %rax
movq %rax, %xmm0
movq 0x108f(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1093(%rip), %xmm1     # 0x4020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x108b(%rip), %xmm0     # 0x4030
mulsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x2210
movq %rax, %xmm0
movq 0x104f(%rip), %xmm3     # 0x4010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x1053(%rip), %xmm4     # 0x4020
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 0x1041(%rip), %xmm0     # 0x4030
jbe 0x307f
movq %r13, %rax
subq %r12, %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 %r14, %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 0xff3(%rip), %xmm0      # 0x4030
movq %rbp, %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 0x1326(%rip), %rdi      # 0x4387
movb $0x2, %al
callq 0x2030
movq 0x2ee9(%rip), %rax      # 0x5f58
movq (%rax), %rdi
callq 0x2120
callq 0x20c0
movq %rax, %r15
movq %r13, %r12
testb %bl, %bl
je 0x2de3
movq 0x2ed7(%rip), %rbx      # 0x5f68
leaq 0x30f4(%rip), %rsi      # 0x618c
movl $0x1, %edi
callq *(%rbx)
leaq 0x30e2(%rip), %rsi      # 0x6188
movl $0x1, %edi
callq *(%rbx)
movq 0x2efc(%rip), %rax      # 0x5fb0
leaq 0x30c9(%rip), %rsi      # 0x6184
movl $0x1, %edi
callq *(%rax)
movq 0x2ebf(%rip), %rax      # 0x5f88
movl 0x30b1(%rip), %edi      # 0x6180
callq *(%rax)
movq 0x3098(%rip), %rdi      # 0x6170
callq 0x2170
callq 0x20d0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22d4
movl $0x1, %eax
jmp 0x30e4
callq 0x22b2
jmp 0x30f8
callq 0x2290
jmp 0x30f8
leaq 0x11f5(%rip), %rdi      # 0x4309
leaq 0x1241(%rip), %rsi      # 0x435c
leaq 0x124e(%rip), %rcx      # 0x4370
movl $0x175, %edx            # imm = 0x175
callq 0x2060