// File: /workspace/github_c_makefile_star2_O2/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 0x36e0(%rip), %rax      # 0x5f38
callq *(%rax)
movl %eax, 0x39a0(%rip)      # 0x6200
leaq 0x1991(%rip), %rdi      # 0x41f8
callq 0x2753
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x288d
leaq 0x1998(%rip), %rsi      # 0x4215
leaq 0x1974(%rip), %rdx      # 0x41f8
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
leaq 0x10(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movl $0x8b31, %edi           # imm = 0x8B31
callq 0x27dd
testb %al, %al
je 0x2b76
leaq 0x197c(%rip), %rdi      # 0x422c
callq 0x2753
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x28d6
leaq 0x194f(%rip), %rsi      # 0x4215
leaq 0x195f(%rip), %rdx      # 0x422c
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movl $0x8b30, %edi           # imm = 0x8B30
callq 0x27dd
testb %al, %al
je 0x2b7c
movq 0x8(%rsp), %rdi
callq 0x2090
movq 0x10(%rsp), %rdi
callq 0x2090
movq 0x368d(%rip), %r14      # 0x5f98
movl 0x38ef(%rip), %edi      # 0x6200
movl 0x4(%rsp), %ebx
movl %ebx, %esi
callq *(%r14)
movl 0x38e0(%rip), %edi      # 0x6200
movl (%rsp), %ebp
movl %ebp, %esi
callq *(%r14)
movq 0x3679(%rip), %rax      # 0x5fa8
movl 0x38cb(%rip), %edi      # 0x6200
callq *(%rax)
leaq 0x1c(%rsp), %r14
movl $0x1, (%r14)
movq 0x35be(%rip), %rax      # 0x5f08
movl 0x38b0(%rip), %edi      # 0x6200
movl $0x8b82, %esi           # imm = 0x8B82
movq %r14, %rdx
callq *(%rax)
cmpl $0x1, (%r14)
jne 0x2b86
movq 0x3655(%rip), %r14      # 0x5fc0
movl %ebp, %edi
callq *(%r14)
movl %ebx, %edi
callq *(%r14)
movq 0x356c(%rip), %rbx      # 0x5ee8
movl 0x387e(%rip), %edi      # 0x6200
leaq 0x18e3(%rip), %rsi      # 0x426c
callq *(%rbx)
movl %eax, 0x37df(%rip)      # 0x6170
cmpl $-0x1, %eax
je 0x2b93
movl 0x3860(%rip), %edi      # 0x6200
leaq 0x18ee(%rip), %rsi      # 0x4295
callq *(%rbx)
movl %eax, 0x37c5(%rip)      # 0x6174
cmpl $-0x1, %eax
je 0x2b9c
movl 0x3842(%rip), %edi      # 0x6200
leaq 0x18fa(%rip), %rsi      # 0x42bf
callq *(%rbx)
movl %eax, 0x37ab(%rip)      # 0x6178
cmpl $-0x1, %eax
je 0x2ba5
movl 0x3824(%rip), %edi      # 0x6200
leaq 0x1906(%rip), %rsi      # 0x42e9
callq *(%rbx)
movl %eax, 0x3791(%rip)      # 0x617c
cmpl $-0x1, %eax
je 0x2bae
movl 0x3806(%rip), %edi      # 0x6200
leaq 0x1911(%rip), %rsi      # 0x4312
callq *(%rbx)
movl %eax, 0x3777(%rip)      # 0x6180
cmpl $-0x1, %eax
je 0x2bb7
movl 0x37e8(%rip), %edi      # 0x6200
leaq 0x1922(%rip), %rsi      # 0x4341
callq *(%rbx)
movl %eax, 0x375d(%rip)      # 0x6184
cmpl $-0x1, %eax
je 0x2bc0
movl 0x379a(%rip), %edx      # 0x61d0
movl 0x3798(%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 0x1a0e(%rip), %rsi      # 0x4470
leaq 0x20(%rsp), %r14
movl $0x360, %edx            # imm = 0x360
movq %r14, %rdi
callq 0x2050
movq 0x3525(%rip), %rax      # 0x5fa0
leaq 0x3782(%rip), %rsi      # 0x6204
pushq $0x1
popq %rbp
movl %ebp, %edi
callq *(%rax)
movq 0x3558(%rip), %rax      # 0x5fe8
movl 0x376e(%rip), %edi      # 0x6204
callq *(%rax)
movq 0x3479(%rip), %rax      # 0x5f18
leaq 0x3762(%rip), %rsi      # 0x6208
movl %ebp, %edi
callq *(%rax)
movq 0x342f(%rip), %rax      # 0x5ee0
movl 0x3751(%rip), %esi      # 0x6208
movl $0x8892, %edi           # imm = 0x8892
callq *(%rax)
movq 0x344b(%rip), %rax      # 0x5f10
movl $0x360, %esi            # imm = 0x360
movl $0x8892, %edi           # imm = 0x8892
movq %r14, %rdx
movl $0x88e4, %ecx           # imm = 0x88E4
callq *(%rax)
movq 0x3500(%rip), %r12      # 0x5fe0
pushq $0x3
popq %r14
pushq $0x18
popq %r15
xorl %edi, %edi
movl %r14d, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl %r15d, %r8d
xorl %r9d, %r9d
callq *(%r12)
movq 0x34cb(%rip), %r13      # 0x5fd0
xorl %edi, %edi
callq *(%r13)
pushq $0xc
popq %r9
movl %ebp, %edi
movl %r14d, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl %r15d, %r8d
callq *(%r12)
movl %ebp, %edi
callq *(%r13)
cvtsi2ssl 0x36a0(%rip), %xmm0 # 0x61d0
cvtsi2ssl 0x369c(%rip), %xmm1 # 0x61d4
divss %xmm1, %xmm0
movss 0x154c(%rip), %xmm1     # 0x4090
divss %xmm0, %xmm1
movss %xmm1, 0x36c0(%rip)     # 0x6210
movl $0x401a827c, 0x36ca(%rip) # imm = 0x401A827C
movsd 0x14ce(%rip), %xmm0     # 0x4030
movsd %xmm0, 0x36ce(%rip)     # 0x6238
movl $0xc00e38e4, 0x36d4(%rip) # imm = 0xC00E38E4
jmp 0x2bd3
movl 0x4(%rsp), %edi
jmp 0x2b7f
movl (%rsp), %edi
callq 0x25c9
jmp 0x2bd0
movl 0x3674(%rip), %edi      # 0x6200
callq 0x268e
jmp 0x2bd0
leaq 0x16af(%rip), %rsi      # 0x4249
jmp 0x2bc7
leaq 0x16cf(%rip), %rsi      # 0x4272
jmp 0x2bc7
leaq 0x16f0(%rip), %rsi      # 0x429c
jmp 0x2bc7
leaq 0x1711(%rip), %rsi      # 0x42c6
jmp 0x2bc7
leaq 0x1731(%rip), %rsi      # 0x42ef
jmp 0x2bc7
leaq 0x1757(%rip), %rsi      # 0x431e
xorl %edi, %edi
xorl %eax, %eax
callq 0x2290
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq