// File: /workspace/github_c_makefile_star2_O1/adelciotto[P]plasma/src/palette_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.
            width = strtol(optarg, (char **)NULL, 10);
            if (width == 0) {
                fprintf(stderr, "invalid value for w: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 'h':
            height = strtol(optarg, (char **)NULL, 10);
            if (height == 0) {
                fprintf(stderr, "invalid value for h: %s\n", optarg);
                return EXIT_FAILURE;
            }
            break;
        case 'f':
            fullscreen = 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;
    }
    plasmaBuffer = calloc(width * height, sizeof(*plasmaBuffer));
    if (plasmaBuffer == NULL) {
        LogError("failed to calloc plasma buffer %dx%d", width, height);
        return EXIT_FAILURE;
    }

    InitPalette();
    InitPlasma();

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

        elapsedTimeMs += targetSecsPerFrame * 1000.0;

        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(plasmaBuffer);
    free(pixelBuffer);
    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 0x7b4(%rip), %rdx       # 0x20df
callq 0x10a0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x19b7
leaq 0x79e(%rip), %r14       # 0x20df
movq 0x2688(%rip), %r15      # 0x3fd0
sarl $0x18, %eax
cmpl $0x66, %eax
je 0x1996
cmpl $0x68, %eax
je 0x1978
cmpl $0x77, %eax
jne 0x19a0
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x27b9(%rip)      # 0x4128
testl %eax, %eax
jne 0x19a0
jmp 0x1d82
movq (%r15), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x11d0
movl %eax, 0x279f(%rip)      # 0x412c
testl %eax, %eax
jne 0x19a0
jmp 0x1d89
movl $0x1, 0x27c8(%rip)      # 0x4168
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10a0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x1948
callq 0x148d
testl %eax, %eax
jne 0x1d7b
movl 0x27b2(%rip), %eax      # 0x417c
testl %eax, %eax
movl $0x3c, %edx
cmovnel %eax, %edx
cvtsi2sd %edx, %xmm1
movsd 0x6a0(%rip), %xmm0      # 0x2080
divsd %xmm1, %xmm0
leaq 0x749(%rip), %rsi       # 0x2134
xorl %edi, %edi
movsd %xmm0, 0x8(%rsp)
movb $0x1, %al
callq 0x1200
movl 0x2728(%rip), %ebp      # 0x4128
movl 0x2726(%rip), %ebx      # 0x412c
movl %ebx, %eax
imull %ebp, %eax
movslq %eax, %r14
movl $0x4, %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, 0x2736(%rip)      # 0x4158
testq %rax, %rax
je 0x1d4a
movl $0x4, %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, 0x2721(%rip)      # 0x4160
testq %rax, %rax
je 0x1d53
callq 0x15c4
callq 0x1678
callq 0x11c0
movq %rax, %r12
callq 0x11c0
movq %rax, %r15
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movsd 0x5bd(%rip), %xmm0      # 0x2030
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x10d0
movl 0x30(%rsp), %eax
cmpl $0x300, %eax            # imm = 0x300
je 0x1a9f
cmpl $0x100, %eax            # imm = 0x100
jne 0x1aa9
movb $0x1, %bl
jmp 0x1aab
cmpl $0x1b, 0x44(%rsp)
sete %bl
jmp 0x1aab
xorl %ebx, %ebx
movsd (%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rsp)
callq 0x1874
callq 0x11c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x53b(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x53f(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
punpckldq 0x507(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x50f(%rip), %xmm0      # 0x2020
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 0x1ac0
callq 0x11c0
subq %r12, %rax
movq %rax, %xmm0
movq 0x4c8(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x4cc(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
punpckldq 0x494(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x49c(%rip), %xmm0      # 0x2020
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 0x1d90
callq 0x11c0
movq %rax, %r13
movq 0x259b(%rip), %rdi      # 0x4150
movq 0x259c(%rip), %rdx      # 0x4158
movl 0x2566(%rip), %ecx      # 0x4128
shll $0x2, %ecx
xorl %esi, %esi
callq 0x1120
movq 0x2575(%rip), %rdi      # 0x4148
callq 0x1160
movq 0x2569(%rip), %rdi      # 0x4148
movq 0x256a(%rip), %rsi      # 0x4150
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1170
movq 0x2552(%rip), %rdi      # 0x4148
callq 0x1080
callq 0x1140
movq %rax, %rbp
callq 0x1140
movq %rax, %r14
callq 0x11c0
subq %r15, %rax
movq %rax, %xmm0
movq 0x3f0(%rip), %xmm1      # 0x2010
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x3f4(%rip), %xmm1      # 0x2020
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x3ec(%rip), %xmm0      # 0x2030
mulsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
callq 0x1140
movq %rax, %xmm0
movq 0x3b0(%rip), %xmm3      # 0x2010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x3b4(%rip), %xmm4      # 0x2020
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 0x3a2(%rip), %xmm0      # 0x2030
jbe 0x1d1e
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 0x354(%rip), %xmm0      # 0x2030
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 0x52e(%rip), %rdi       # 0x222e
movb $0x2, %al
callq 0x1050
movq 0x22b2(%rip), %rax      # 0x3fc0
movq (%rax), %rdi
callq 0x1190
callq 0x11c0
movq %rax, %r15
movq %r13, %r12
testb %bl, %bl
je 0x1a7f
movq 0x2430(%rip), %rdi      # 0x4160
callq 0x11a0
movq 0x241c(%rip), %rdi      # 0x4158
callq 0x11a0
callq 0x18e6
xorl %eax, %eax
jmp 0x1d6c
leaq 0x415(%rip), %rsi       # 0x2166
jmp 0x1d5a
leaq 0x430(%rip), %rsi       # 0x218a
xorl %edi, %edi
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x11b0
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1284
jmp 0x1d67
callq 0x1262
jmp 0x1d67
callq 0x1240
jmp 0x1d67
leaq 0x418(%rip), %rdi       # 0x21af
leaq 0x464(%rip), %rsi       # 0x2202
leaq 0x472(%rip), %rcx       # 0x2217
movl $0xe5, %edx
callq 0x10e0