// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x1422(%rip), %rdx      # 0x4279
callq 0x21d0
movb %al, -0x11(%rbp)
movsbl %al, %eax
cmpl $-0x1, %eax
je 0x2f5b
movsbl -0x11(%rbp), %eax
movl %eax, -0x94(%rbp)
subl $0x66, %eax
je 0x2f4c
jmp 0x2e80
movl -0x94(%rbp), %eax
subl $0x68, %eax
je 0x2ef5
jmp 0x2e8d
movl -0x94(%rbp), %eax
subl $0x77, %eax
jne 0x2f56
jmp 0x2e9e
movq 0x3093(%rip), %rax      # 0x5f38
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x32a0(%rip)      # 0x615c
cmpl $0x0, 0x3299(%rip)      # 0x615c
jne 0x2ef3
movq 0x30a4(%rip), %rax      # 0x5f70
movq (%rax), %rdi
movq 0x3062(%rip), %rax      # 0x5f38
movq (%rax), %rdx
leaq 0x13a0(%rip), %rsi      # 0x4280
movb $0x0, %al
callq 0x2080
movl $0x1, -0x4(%rbp)
jmp 0x3257
jmp 0x2f56
movq 0x303c(%rip), %rax      # 0x5f38
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x21a0
movl %eax, 0x324d(%rip)      # 0x6160
cmpl $0x0, 0x3246(%rip)      # 0x6160
jne 0x2f4a
movq 0x304d(%rip), %rax      # 0x5f70
movq (%rax), %rdi
movq 0x300b(%rip), %rax      # 0x5f38
movq (%rax), %rdx
leaq 0x1366(%rip), %rsi      # 0x429d
movb $0x0, %al
callq 0x2080
movl $0x1, -0x4(%rbp)
jmp 0x3257
jmp 0x2f56
movl $0x1, 0x323a(%rip)      # 0x6190
jmp 0x2e49
callq 0x24d0
cmpl $0x0, %eax
je 0x2f9f
movq 0x3004(%rip), %rax      # 0x5f70
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
callq 0x21f0
movq -0xa0(%rbp), %rdi
movq %rax, %rdx
leaq 0x132f(%rip), %rsi      # 0x42bb
movb $0x0, %al
callq 0x2080
movl $0x1, -0x4(%rbp)
jmp 0x3257
callq 0x2950
cmpl $0x0, %eax
je 0x2fb5
movl $0x1, -0x4(%rbp)
jmp 0x3257
leaq 0x31dc(%rip), %rax      # 0x6198
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x24a0
movl %eax, -0x18(%rbp)
cvtsi2sdl -0x18(%rbp), %xmm1
movsd 0x104e(%rip), %xmm0     # 0x4038
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x18(%rbp), %edx
movsd -0x20(%rbp), %xmm0
xorl %edi, %edi
leaq 0x12d3(%rip), %rsi      # 0x42d7
movb $0x1, %al
callq 0x21b0
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0x20c0
movq %rax, -0x30(%rbp)
callq 0x20c0
movq %rax, -0x38(%rbp)
movl $0x1, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x3246
leaq -0x70(%rbp), %rdi
callq 0x2200
movl -0x70(%rbp), %eax
movl %eax, -0xa4(%rbp)
subl $0x100, %eax            # imm = 0x100
je 0x306f
jmp 0x3051
movl -0xa4(%rbp), %eax
subl $0x200, %eax            # imm = 0x200
je 0x3087
jmp 0x3060
movl -0xa4(%rbp), %eax
subl $0x300, %eax            # imm = 0x300
je 0x3078
jmp 0x30c2
movl $0x0, -0x74(%rbp)
jmp 0x30c2
cmpl $0x1b, -0x5c(%rbp)
jne 0x3085
movl $0x0, -0x74(%rbp)
jmp 0x30c2
movzbl -0x64(%rbp), %eax
cmpl $0x5, %eax
je 0x3099
movzbl -0x64(%rbp), %eax
cmpl $0x6, %eax
jne 0x30c0
movl -0x60(%rbp), %eax
movl %eax, 0x30ba(%rip)      # 0x615c
movl -0x5c(%rbp), %eax
movl %eax, 0x30b5(%rip)      # 0x6160
movl 0x30ab(%rip), %edx      # 0x615c
movl 0x30a9(%rip), %ecx      # 0x6160
xorl %esi, %esi
movl %esi, %edi
callq 0x20f0
jmp 0x30c2
movsd -0x20(%rbp), %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x2d10
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
callq 0x20c0
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x2380
movaps %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x310a
jmp 0x30db
movq -0x30(%rbp), %rax
movq %rax, -0xb8(%rbp)
callq 0x20c0
movq -0xb8(%rbp), %rdi
movq %rax, %rsi
callq 0x2380
ucomisd -0x20(%rbp), %xmm0
jb 0x3132
jmp 0x3151
leaq 0x11d0(%rip), %rdi      # 0x4309
leaq 0x121c(%rip), %rsi      # 0x435c
movl $0x175, %edx            # imm = 0x175
leaq 0x1224(%rip), %rcx      # 0x4370
callq 0x2060
callq 0x20c0
movq %rax, -0x80(%rbp)
movq 0x300f(%rip), %rdi      # 0x6170
callq 0x20e0
movq -0x30(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x2410
movsd %xmm0, -0x88(%rbp)
callq 0x2210
movq %rax, %xmm0
movaps 0xe84(%rip), %xmm3      # 0x4010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0xe88(%rip), %xmm2      # 0x4020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x80(%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, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x20c0
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x2410
movsd 0xe33(%rip), %xmm1      # 0x4030
ucomisd %xmm1, %xmm0
jbe 0x3239
movsd -0x88(%rbp), %xmm0
movsd -0x90(%rbp), %xmm1
leaq 0x116d(%rip), %rdi      # 0x4387
movb $0x2, %al
callq 0x2030
movq 0x2d30(%rip), %rax      # 0x5f58
movq (%rax), %rdi
callq 0x2120
callq 0x20c0
movq %rax, -0x38(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x302c
callq 0x2db0
callq 0x2e10
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq