// File: /workspace/github_c_makefile_star2_O3/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 0x158f(%rip), %rdx      # 0x4279
callq 0x21d0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x2d76
leaq 0x1579(%rip), %r14      # 0x4279
movq 0x3231(%rip), %r15      # 0x5f38
sarl $0x18, %eax
cmpl $0x66, %eax
je 0x2d55
cmpl $0x68, %eax
je 0x2d37
cmpl $0x77, %eax
jne 0x2d5f
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x342e(%rip)      # 0x615c
testl %eax, %eax
jne 0x2d5f
jmp 0x3103
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x3414(%rip)      # 0x6160
testl %eax, %eax
jne 0x2d5f
jmp 0x310a
movl $0x1, 0x3431(%rip)      # 0x6190
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21d0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x2d07
callq 0x24c5
testl %eax, %eax
jne 0x30f7
callq 0x2872
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x30e8
movl 0x3407(%rip), %eax      # 0x61a4
testl %eax, %eax
movl $0x3c, %edx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x1285(%rip), %xmm0     # 0x4038
divsd %xmm1, %xmm0
leaq 0x1519(%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 0x2e43
cmpl $0x200, %eax            # imm = 0x200
movsd 0x8(%rsp), %xmm1
je 0x2e14
cmpl $0x100, %eax            # imm = 0x100
jne 0x2e3f
movb $0x1, %bl
jmp 0x2e51
movb 0x3c(%rsp), %al
addb $-0x5, %al
cmpb $0x1, %al
ja 0x2e3f
movl 0x40(%rsp), %edx
movl 0x44(%rsp), %ecx
movl %edx, 0x3330(%rip)      # 0x615c
movl %ecx, 0x332e(%rip)      # 0x6160
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x20f0
jmp 0x2e4b
xorl %ebx, %ebx
jmp 0x2e51
cmpl $0x1b, 0x44(%rsp)
sete %bl
movsd 0x8(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x2bf2
callq 0x20c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x1195(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1199(%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 0x1161(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1169(%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 0x2e66
callq 0x20c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x1122(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1126(%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 0x10ee(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x10f6(%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 0x3111
callq 0x20c0
movq %rax, %r13
movq 0x3215(%rip), %rdi      # 0x6170
callq 0x20e0
callq 0x2210
movq %rax, %rbp
callq 0x2210
movq %rax, %r14
callq 0x20c0
subq %r15, %rax
movq %rax, %xmm0
movq 0x108b(%rip), %xmm1     # 0x4010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x108f(%rip), %xmm1     # 0x4020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x1087(%rip), %xmm0     # 0x4030
mulsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x2210
movq %rax, %xmm0
movq 0x104b(%rip), %xmm3     # 0x4010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x104f(%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 0x103d(%rip), %xmm0     # 0x4030
jbe 0x3083
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 0xfef(%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 0x1322(%rip), %rdi      # 0x4387
movb $0x2, %al
callq 0x2030
movq 0x2ee5(%rip), %rax      # 0x5f58
movq (%rax), %rdi
callq 0x2120
callq 0x20c0
movq %rax, %r15
movq %r13, %r12
testb %bl, %bl
je 0x2de7
movq 0x2ed3(%rip), %rbx      # 0x5f68
leaq 0x30f0(%rip), %rsi      # 0x618c
movl $0x1, %edi
callq *(%rbx)
leaq 0x30de(%rip), %rsi      # 0x6188
movl $0x1, %edi
callq *(%rbx)
movq 0x2ef8(%rip), %rax      # 0x5fb0
leaq 0x30c5(%rip), %rsi      # 0x6184
movl $0x1, %edi
callq *(%rax)
movq 0x2ebb(%rip), %rax      # 0x5f88
movl 0x30ad(%rip), %edi      # 0x6180
callq *(%rax)
movq 0x3094(%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 0x30e8
callq 0x22b2
jmp 0x30fc
callq 0x2290
jmp 0x30fc
leaq 0x11f1(%rip), %rdi      # 0x4309
leaq 0x123d(%rip), %rsi      # 0x435c
leaq 0x124a(%rip), %rcx      # 0x4370
movl $0x175, %edx            # imm = 0x175
callq 0x2060