// File: /workspace/github_c_makefile_star2_O1/adelciotto[P]plasma/src/cube_plasma.c
// Function: InitGL
int InitGL(void) {
    gProgramId = glCreateProgram();

    GLuint vertexShader;
    char *vertexShaderSource = ReadFile(VERTEX_SHADER_PATH);
    if (vertexShaderSource == NULL) {
        LogError("could not read file %s", VERTEX_SHADER_PATH);
    }
    if (compileShader(GL_VERTEX_SHADER, (const char **)&vertexShaderSource,
                      &vertexShader) != GL_TRUE) {
        LogShaderError(vertexShader);
        return -1;
    }

    GLuint fragmentShader;
    char *fragmentShaderSource = ReadFile(FRAGMENT_SHADER_PATH);
    if (fragmentShaderSource == NULL) {
        LogError("could not read file %s", FRAGMENT_SHADER_PATH);
    }
    if (compileShader(GL_FRAGMENT_SHADER, (const char **)&fragmentShaderSource,
                      &fragmentShader) != GL_TRUE) {
        LogShaderError(fragmentShader);
        return -1;
    }

    free(fragmentShaderSource);
    free(vertexShaderSource);

    glAttachShader(gProgramId, vertexShader);
    glAttachShader(gProgramId, fragmentShader);

    glLinkProgram(gProgramId);
    GLint programSuccess = GL_TRUE;
    glGetProgramiv(gProgramId, GL_LINK_STATUS, &programSuccess);
    if (programSuccess != GL_TRUE) {
        LogProgramError(gProgramId);
        return -1;
    }

    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);

    gUniformTimeLocation = glGetUniformLocation(gProgramId, "uTime");
    if (gUniformTimeLocation == -1) {
        LogError("could not get uniform location for uTime");
        return -1;
    }
    gUniformScaleLocation = glGetUniformLocation(gProgramId, "uScale");
    if (gUniformScaleLocation == -1) {
        LogError("could not get uniform location for uScale");
        return -1;
    }
    gUniformModelLocation = glGetUniformLocation(gProgramId, "uModel");
    if (gUniformModelLocation == -1) {
        LogError("could not get uniform location for uModel");
        return -1;
    }
    gUniformViewLocation = glGetUniformLocation(gProgramId, "uView");
    if (gUniformViewLocation == -1) {
        LogError("could not get uniform location for uView");
        return -1;
    }
    gUniformProjectionLocation =
        glGetUniformLocation(gProgramId, "uProjection");
    if (gUniformProjectionLocation == -1) {
        LogError("could not get uniform location for uProjection");
        return -1;
    }
    gUniformViewPositionLocation =
        glGetUniformLocation(gProgramId, "uViewPosition");
    if (gUniformViewPositionLocation == -1) {
        LogError("could not get uniform location for uViewPosition");
        return -1;
    }

    glViewport(0, 0, gWidth, gHeight);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_MULTISAMPLE);

    GLfloat vertexData[] = {
        -0.5f, -0.5f, -0.5f, 0.0f,  0.0f,  -1.0f, 0.5f,  -0.5f, -0.5f,
        0.0f,  0.0f,  -1.0f, 0.5f,  0.5f,  -0.5f, 0.0f,  0.0f,  -1.0f,
        0.5f,  0.5f,  -0.5f, 0.0f,  0.0f,  -1.0f, -0.5f, 0.5f,  -0.5f,
        0.0f,  0.0f,  -1.0f, -0.5f, -0.5f, -0.5f, 0.0f,  0.0f,  -1.0f,

        -0.5f, -0.5f, 0.5f,  0.0f,  0.0f,  1.0f,  0.5f,  -0.5f, 0.5f,
        0.0f,  0.0f,  1.0f,  0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
        0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,  -0.5f, 0.5f,  0.5f,
        0.0f,  0.0f,  1.0f,  -0.5f, -0.5f, 0.5f,  0.0f,  0.0f,  1.0f,

        -0.5f, 0.5f,  0.5f,  -1.0f, 0.0f,  0.0f,  -0.5f, 0.5f,  -0.5f,
        -1.0f, 0.0f,  0.0f,  -0.5f, -0.5f, -0.5f, -1.0f, 0.0f,  0.0f,
        -0.5f, -0.5f, -0.5f, -1.0f, 0.0f,  0.0f,  -0.5f, -0.5f, 0.5f,
        -1.0f, 0.0f,  0.0f,  -0.5f, 0.5f,  0.5f,  -1.0f, 0.0f,  0.0f,

        0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,  0.5f,  0.5f,  -0.5f,
        1.0f,  0.0f,  0.0f,  0.5f,  -0.5f, -0.5f, 1.0f,  0.0f,  0.0f,
        0.5f,  -0.5f, -0.5f, 1.0f,  0.0f,  0.0f,  0.5f,  -0.5f, 0.5f,
        1.0f,  0.0f,  0.0f,  0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

        -0.5f, -0.5f, -0.5f, 0.0f,  -1.0f, 0.0f,  0.5f,  -0.5f, -0.5f,
        0.0f,  -1.0f, 0.0f,  0.5f,  -0.5f, 0.5f,  0.0f,  -1.0f, 0.0f,
        0.5f,  -0.5f, 0.5f,  0.0f,  -1.0f, 0.0f,  -0.5f, -0.5f, 0.5f,
        0.0f,  -1.0f, 0.0f,  -0.5f, -0.5f, -0.5f, 0.0f,  -1.0f, 0.0f,

        -0.5f, 0.5f,  -0.5f, 0.0f,  1.0f,  0.0f,  0.5f,  0.5f,  -0.5f,
        0.0f,  1.0f,  0.0f,  0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
        0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  -0.5f, 0.5f,  0.5f,
        0.0f,  1.0f,  0.0f,  -0.5f, 0.5f,  -0.5f, 0.0f,  1.0f,  0.0f};

    glGenVertexArrays(1, &gVAO);
    glBindVertexArray(gVAO);

    glGenBuffers(1, &gVBO);
    glBindBuffer(GL_ARRAY_BUFFER, gVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData,
                 GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
                          (void *)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
                          (void *)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

    Mat4Perspective(0.785398, (float)gWidth / (float)gHeight, 1.0f, 10.0f,
                    gProj);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movq 0x363f(%rip), %rax      # 0x5f38
callq *(%rax)
movl %eax, 0x390f(%rip)      # 0x6210
leaq 0x18e0(%rip), %rdi      # 0x41e8
callq 0x27eb
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x292e
leaq 0x18e7(%rip), %rsi      # 0x4205
leaq 0x18c3(%rip), %rdx      # 0x41e8
xorl %edi, %edi
xorl %eax, %eax
callq 0x22a0
movq 0x36a3(%rip), %r12      # 0x5fd8
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%r12)
movl %eax, %ebx
movq 0x3671(%rip), %rbp      # 0x5fb8
leaq 0x10(%rsp), %rdx
movl %eax, %edi
movl $0x1, %esi
xorl %ecx, %ecx
callq *(%rbp)
movq 0x3611(%rip), %r15      # 0x5f70
movl %ebx, %edi
callq *(%r15)
leaq 0x20(%rsp), %r14
movl $0x0, (%r14)
movq 0x35c9(%rip), %r13      # 0x5f40
movl %ebx, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r13)
cmpl $0x1, (%r14)
jne 0x2c8d
movq %rbp, %r14
leaq 0x1883(%rip), %rdi      # 0x421c
callq 0x27eb
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x29bf
leaq 0x1856(%rip), %rsi      # 0x4205
leaq 0x1866(%rip), %rdx      # 0x421c
xorl %edi, %edi
xorl %eax, %eax
callq 0x22a0
movl $0x8b30, %edi           # imm = 0x8B30
callq *(%r12)
movl %eax, %ebp
leaq 0x8(%rsp), %rdx
movl %eax, %edi
movl $0x1, %esi
xorl %ecx, %ecx
callq *(%r14)
movl %ebp, %edi
callq *(%r15)
leaq 0x20(%rsp), %r14
movl $0x0, (%r14)
movl %ebp, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r13)
cmpl $0x1, (%r14)
jne 0x2c91
movq 0x8(%rsp), %rdi
callq 0x2090
movq 0x10(%rsp), %rdi
callq 0x2090
movq 0x3579(%rip), %r14      # 0x5f98
movl 0x37eb(%rip), %edi      # 0x6210
movl %ebx, %esi
callq *(%r14)
movl 0x37e0(%rip), %edi      # 0x6210
movl %ebp, %esi
callq *(%r14)
movq 0x356c(%rip), %rax      # 0x5fa8
movl 0x37ce(%rip), %edi      # 0x6210
callq *(%rax)
leaq 0x1c(%rsp), %r14
movl $0x1, (%r14)
movq 0x34b1(%rip), %rax      # 0x5f08
movl 0x37b3(%rip), %edi      # 0x6210
movl $0x8b82, %esi           # imm = 0x8B82
movq %r14, %rdx
callq *(%rax)
cmpl $0x1, (%r14)
jne 0x2cb1
movq 0x3548(%rip), %r14      # 0x5fc0
movl %ebp, %edi
callq *(%r14)
movl %ebx, %edi
callq *(%r14)
movq 0x345f(%rip), %rbx      # 0x5ee8
movl 0x3781(%rip), %edi      # 0x6210
leaq 0x17c6(%rip), %rsi      # 0x425c
callq *(%rbx)
movl %eax, 0x36e2(%rip)      # 0x6180
cmpl $-0x1, %eax
je 0x2cbe
movl 0x3763(%rip), %edi      # 0x6210
leaq 0x17d1(%rip), %rsi      # 0x4285
callq *(%rbx)
movl %eax, 0x36c8(%rip)      # 0x6184
cmpl $-0x1, %eax
je 0x2cc7
movl 0x3745(%rip), %edi      # 0x6210
leaq 0x17dd(%rip), %rsi      # 0x42af
callq *(%rbx)
movl %eax, 0x36ae(%rip)      # 0x6188
cmpl $-0x1, %eax
je 0x2cd0
movl 0x3727(%rip), %edi      # 0x6210
leaq 0x17e9(%rip), %rsi      # 0x42d9
callq *(%rbx)
movl %eax, 0x3694(%rip)      # 0x618c
cmpl $-0x1, %eax
je 0x2cd9
movl 0x3709(%rip), %edi      # 0x6210
leaq 0x17f4(%rip), %rsi      # 0x4302
callq *(%rbx)
movl %eax, 0x367a(%rip)      # 0x6190
cmpl $-0x1, %eax
je 0x2ce2
movl 0x36eb(%rip), %edi      # 0x6210
leaq 0x1805(%rip), %rsi      # 0x4331
callq *(%rbx)
movl %eax, 0x3660(%rip)      # 0x6194
cmpl $-0x1, %eax
je 0x2ceb
movl 0x369d(%rip), %edx      # 0x61e0
movl 0x369b(%rip), %ecx      # 0x61e4
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x2100
movl $0xb71, %edi            # imm = 0xB71
callq 0x2170
movl $0x809d, %edi           # imm = 0x809D
callq 0x2170
leaq 0x18f1(%rip), %rsi      # 0x4460
leaq 0x20(%rsp), %r14
movl $0x360, %edx            # imm = 0x360
movq %r14, %rdi
callq 0x2050
movq 0x3418(%rip), %rax      # 0x5fa0
leaq 0x3685(%rip), %rsi      # 0x6214
movl $0x1, %edi
callq *(%rax)
movq 0x344b(%rip), %rax      # 0x5fe8
movl 0x3671(%rip), %edi      # 0x6214
callq *(%rax)
movq 0x336c(%rip), %rax      # 0x5f18
leaq 0x3665(%rip), %rsi      # 0x6218
movl $0x1, %edi
callq *(%rax)
movq 0x331f(%rip), %rax      # 0x5ee0
movl 0x3651(%rip), %esi      # 0x6218
movl $0x8892, %edi           # imm = 0x8892
callq *(%rax)
movq 0x333b(%rip), %rax      # 0x5f10
movl $0x360, %esi            # imm = 0x360
movl $0x8892, %edi           # imm = 0x8892
movq %r14, %rdx
movl $0x88e4, %ecx           # imm = 0x88E4
callq *(%rax)
movq 0x33f0(%rip), %r14      # 0x5fe0
xorl %edi, %edi
movl $0x3, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x18, %r8d
xorl %r9d, %r9d
callq *(%r14)
movq 0x33bf(%rip), %r15      # 0x5fd0
xorl %edi, %edi
callq *(%r15)
movl $0xc, %r9d
movl $0x1, %edi
movl $0x3, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x18, %r8d
callq *(%r14)
movl $0x1, %edi
callq *(%r15)
cvtsi2ssl 0x359a(%rip), %xmm0 # 0x61e0
cvtsi2ssl 0x3596(%rip), %xmm1 # 0x61e4
divss %xmm1, %xmm0
movss 0x1426(%rip), %xmm1     # 0x4080
divss %xmm0, %xmm1
movss %xmm1, 0x35ba(%rip)     # 0x6220
movl $0x401a827c, 0x35c4(%rip) # imm = 0x401A827C
movabsq $-0x407fffff40638e39, %rax # imm = 0xBF800000BF9C71C7
movq %rax, 0x35c7(%rip)      # 0x6248
movl $0xc00e38e4, 0x35cd(%rip) # imm = 0xC00E38E4
jmp 0x2c9d
movl %ebx, %edi
jmp 0x2c93
movl %ebp, %edi
callq 0x2665
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x3559(%rip), %edi      # 0x6210
callq 0x2728
jmp 0x2c98
leaq 0x1574(%rip), %rsi      # 0x4239
jmp 0x2cf2
leaq 0x1594(%rip), %rsi      # 0x4262
jmp 0x2cf2
leaq 0x15b5(%rip), %rsi      # 0x428c
jmp 0x2cf2
leaq 0x15d6(%rip), %rsi      # 0x42b6
jmp 0x2cf2
leaq 0x15f6(%rip), %rsi      # 0x42df
jmp 0x2cf2
leaq 0x161c(%rip), %rsi      # 0x430e
xorl %edi, %edi
xorl %eax, %eax
callq 0x22a0
jmp 0x2c98