// File: /workspace/github_c_makefile_star2_O3/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 0x364b(%rip), %rax      # 0x5f38
callq *(%rax)
movl %eax, 0x390b(%rip)      # 0x6200
leaq 0x1900(%rip), %rdi      # 0x41fc
callq 0x27df
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x2922
leaq 0x1907(%rip), %rsi      # 0x4219
leaq 0x18e3(%rip), %rdx      # 0x41fc
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
movq 0x36af(%rip), %r12      # 0x5fd8
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%r12)
movl %eax, %ebx
movq 0x367d(%rip), %rbp      # 0x5fb8
leaq 0x10(%rsp), %rdx
movl %eax, %edi
movl $0x1, %esi
xorl %ecx, %ecx
callq *(%rbp)
movq 0x361d(%rip), %r15      # 0x5f70
movl %ebx, %edi
callq *(%r15)
leaq 0x20(%rsp), %r14
movl $0x0, (%r14)
movq 0x35d5(%rip), %r13      # 0x5f40
movl %ebx, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r14, %rdx
callq *(%r13)
cmpl $0x1, (%r14)
jne 0x2c80
movq %rbp, %r14
leaq 0x18a3(%rip), %rdi      # 0x4230
callq 0x27df
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x29b3
leaq 0x1876(%rip), %rsi      # 0x4219
leaq 0x1886(%rip), %rdx      # 0x4230
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
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 0x2c84
movq 0x8(%rsp), %rdi
callq 0x2090
movq 0x10(%rsp), %rdi
callq 0x2090
movq 0x3585(%rip), %r14      # 0x5f98
movl 0x37e7(%rip), %edi      # 0x6200
movl %ebx, %esi
callq *(%r14)
movl 0x37dc(%rip), %edi      # 0x6200
movl %ebp, %esi
callq *(%r14)
movq 0x3578(%rip), %rax      # 0x5fa8
movl 0x37ca(%rip), %edi      # 0x6200
callq *(%rax)
leaq 0x1c(%rsp), %r14
movl $0x1, (%r14)
movq 0x34bd(%rip), %rax      # 0x5f08
movl 0x37af(%rip), %edi      # 0x6200
movl $0x8b82, %esi           # imm = 0x8B82
movq %r14, %rdx
callq *(%rax)
cmpl $0x1, (%r14)
jne 0x2ca4
movq 0x3554(%rip), %r14      # 0x5fc0
movl %ebp, %edi
callq *(%r14)
movl %ebx, %edi
callq *(%r14)
movq 0x346b(%rip), %rbx      # 0x5ee8
movl 0x377d(%rip), %edi      # 0x6200
leaq 0x17e6(%rip), %rsi      # 0x4270
callq *(%rbx)
movl %eax, 0x36de(%rip)      # 0x6170
cmpl $-0x1, %eax
je 0x2cb1
movl 0x375f(%rip), %edi      # 0x6200
leaq 0x17f1(%rip), %rsi      # 0x4299
callq *(%rbx)
movl %eax, 0x36c4(%rip)      # 0x6174
cmpl $-0x1, %eax
je 0x2cba
movl 0x3741(%rip), %edi      # 0x6200
leaq 0x17fd(%rip), %rsi      # 0x42c3
callq *(%rbx)
movl %eax, 0x36aa(%rip)      # 0x6178
cmpl $-0x1, %eax
je 0x2cc3
movl 0x3723(%rip), %edi      # 0x6200
leaq 0x1809(%rip), %rsi      # 0x42ed
callq *(%rbx)
movl %eax, 0x3690(%rip)      # 0x617c
cmpl $-0x1, %eax
je 0x2ccc
movl 0x3705(%rip), %edi      # 0x6200
leaq 0x1814(%rip), %rsi      # 0x4316
callq *(%rbx)
movl %eax, 0x3676(%rip)      # 0x6180
cmpl $-0x1, %eax
je 0x2cd5
movl 0x36e7(%rip), %edi      # 0x6200
leaq 0x1825(%rip), %rsi      # 0x4345
callq *(%rbx)
movl %eax, 0x365c(%rip)      # 0x6184
cmpl $-0x1, %eax
je 0x2cde
movl 0x3699(%rip), %edx      # 0x61d0
movl 0x3697(%rip), %ecx      # 0x61d4
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 0x191d(%rip), %rsi      # 0x4480
leaq 0x20(%rsp), %r14
movl $0x360, %edx            # imm = 0x360
movq %r14, %rdi
callq 0x2050
movq 0x3424(%rip), %rax      # 0x5fa0
leaq 0x3681(%rip), %rsi      # 0x6204
movl $0x1, %edi
callq *(%rax)
movq 0x3457(%rip), %rax      # 0x5fe8
movl 0x366d(%rip), %edi      # 0x6204
callq *(%rax)
movq 0x3378(%rip), %rax      # 0x5f18
leaq 0x3661(%rip), %rsi      # 0x6208
movl $0x1, %edi
callq *(%rax)
movq 0x332b(%rip), %rax      # 0x5ee0
movl 0x364d(%rip), %esi      # 0x6208
movl $0x8892, %edi           # imm = 0x8892
callq *(%rax)
movq 0x3347(%rip), %rax      # 0x5f10
movl $0x360, %esi            # imm = 0x360
movl $0x8892, %edi           # imm = 0x8892
movq %r14, %rdx
movl $0x88e4, %ecx           # imm = 0x88E4
callq *(%rax)
movq 0x33fc(%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 0x33cb(%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 0x3596(%rip), %xmm0 # 0x61d0
cvtsi2ssl 0x3592(%rip), %xmm1 # 0x61d4
divss %xmm1, %xmm0
movss 0x1442(%rip), %xmm1     # 0x4090
divss %xmm0, %xmm1
movss %xmm1, 0x35b6(%rip)     # 0x6210
movl $0x401a827c, 0x35c0(%rip) # imm = 0x401A827C
movsd 0x13c4(%rip), %xmm0     # 0x4030
movsd %xmm0, 0x35c4(%rip)     # 0x6238
movl $0xc00e38e4, 0x35ca(%rip) # imm = 0xC00E38E4
jmp 0x2c90
movl %ebx, %edi
jmp 0x2c86
movl %ebp, %edi
callq 0x2655
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 0x3556(%rip), %edi      # 0x6200
callq 0x271a
jmp 0x2c8b
leaq 0x1595(%rip), %rsi      # 0x424d
jmp 0x2ce5
leaq 0x15b5(%rip), %rsi      # 0x4276
jmp 0x2ce5
leaq 0x15d6(%rip), %rsi      # 0x42a0
jmp 0x2ce5
leaq 0x15f7(%rip), %rsi      # 0x42ca
jmp 0x2ce5
leaq 0x1617(%rip), %rsi      # 0x42f3
jmp 0x2ce5
leaq 0x163d(%rip), %rsi      # 0x4322
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
jmp 0x2c8b