// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/engine/renderer/opengl_renderer.cpp
// Function: Renderer::Impl::compile_shader_program_from_source(char const*)
static Program compile_shader_program_from_source(const char *source) {
#define SHADER_ERROR_CHECK(SHDR)                             \
    do {                                                     \
        GLint success = 0;                                   \
        glGetShaderiv(SHDR, GL_COMPILE_STATUS, &success);    \
        if (success == GL_FALSE) {                           \
            char buffer[512];                                \
            glGetShaderInfoLog(SHDR, 500, NULL, &buffer[0]); \
            ERR("%s: %s\n", "\"" #SHDR "\"", buffer);        \
            glDeleteShader(frag);                            \
            glDeleteShader(vert);                            \
            return Program::invalid_prog();                         \
        }                                                    \
    } while (false);

    u32 frag = glCreateShader(GL_FRAGMENT_SHADER);
    u32 vert = glCreateShader(GL_VERTEX_SHADER);

    const char *complete_source[] = {
        "#version 330\n",
        "#define VERT\n",
        "struct Camera\n"
        "{\n"
        "    vec2  pos;\n"
        "    vec2  offset;\n"
        "    float zoom;\n"
        "    float aspect_ratio;\n"
        "};"
        "struct Window\n"
        "{\n"
        "    float width;\n"
        "    float height;\n"
        "    float aspect_ratio;\n"
        "};"
        "layout (std140) uniform Global\n"
        "{\n"
        "   Camera cam[" STR(OPENGL_NUM_CAMERAS) "];\n"
        "   Window win;\n"
        "};\n"
        "uniform uint current_cam;\n"
        ,
        source};
    glShaderSource(vert, LEN(complete_source), complete_source, NULL);
    glCompileShader(vert);
    SHADER_ERROR_CHECK(vert);

    complete_source[1] = "#define FRAG\n";
    glShaderSource(frag, LEN(complete_source), complete_source, NULL);
    glCompileShader(frag);
    SHADER_ERROR_CHECK(frag);

    Program shader = {(s32) glCreateProgram()};
    glAttachShader(shader.id, vert);
    glAttachShader(shader.id, frag);
    glLinkProgram(shader.id);

    glDeleteShader(frag);
    glDeleteShader(vert);

    GLint success = 0;
    glGetProgramiv(shader.id, GL_LINK_STATUS, &success);
    if (success == GL_FALSE) {
        char buffer[512];
        glGetProgramInfoLog(shader.id, 500, NULL, &buffer[0]);
        ERR("Program: %s\n", buffer);
        return Program::invalid_prog();
    }

    unsigned int block = glGetUniformBlockIndex(shader, "Camera");
    glUniformBlockBinding(shader, block, GLSL_GLOBAL_BLOCK);

    return shader;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdi, %r15
leaq 0x43e16(%rip), %rbx     # 0x64d18
movl $0x8b30, %edi           # imm = 0x8B30
callq *(%rbx)
movl %eax, %r14d
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%rbx)
movl %eax, %ebx
leaq 0x246dc(%rip), %rax     # 0x455f8
leaq -0x50(%rbp), %rdx
movq %rax, (%rdx)
leaq 0x246dc(%rip), %rax     # 0x45606
movq %rax, 0x8(%rdx)
leaq 0x246df(%rip), %rax     # 0x45614
movq %rax, 0x10(%rdx)
movq %r15, 0x18(%rdx)
leaq 0x44934(%rip), %rax     # 0x65878
movl %ebx, %edi
movl $0x4, %esi
xorl %ecx, %ecx
callq *(%rax)
leaq 0x43d4a(%rip), %r12     # 0x64ca0
movl %ebx, %edi
callq *(%r12)
leaq -0x2c(%rbp), %r15
movl $0x0, (%r15)
leaq 0x441c2(%rip), %r13     # 0x65130
movl %ebx, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r15, %rdx
callq *(%r13)
cmpl $0x0, (%r15)
je 0x2106a
leaq 0x247c1(%rip), %rax     # 0x4574e
leaq -0x50(%rbp), %rdx
movq %rax, 0x8(%rdx)
movl %r14d, %edi
movl $0x4, %esi
xorl %ecx, %ecx
leaq 0x448d2(%rip), %rax     # 0x65878
callq *(%rax)
movl %r14d, %edi
callq *(%r12)
movl $0x0, (%r15)
movl %r14d, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r15, %rdx
callq *(%r13)
cmpl $0x0, (%r15)
je 0x210ba
leaq 0x43d3a(%rip), %rax     # 0x64d10
callq *(%rax)
movl %eax, %r15d
leaq 0x43a5e(%rip), %r12     # 0x64a40
movl %eax, %edi
movl %ebx, %esi
callq *(%r12)
movl %r15d, %edi
movl %r14d, %esi
callq *(%r12)
leaq 0x44345(%rip), %rax     # 0x65340
movl %r15d, %edi
callq *(%rax)
leaq 0x43d51(%rip), %r12     # 0x64d58
movl %r14d, %edi
callq *(%r12)
movl %ebx, %edi
callq *(%r12)
leaq -0x2c(%rbp), %rbx
movl $0x0, (%rbx)
leaq 0x440d3(%rip), %rax     # 0x650f8
movl %r15d, %edi
movl $0x8b82, %esi           # imm = 0x8B82
movq %rbx, %rdx
callq *(%rax)
cmpl $0x0, (%rbx)
je 0x21145
leaq 0x44176(%rip), %rax     # 0x651b8
xorl %ebx, %ebx
cmpl $-0x1, %r15d
setne %bl
leaq 0x2471e(%rip), %rsi     # 0x45770
movl %ebx, %edi
callq *(%rax)
leaq 0x44afb(%rip), %rcx     # 0x65b58
movl %ebx, %edi
movl %eax, %esi
xorl %edx, %edx
callq *(%rcx)
jmp 0x21130
leaq 0x440af(%rip), %rax     # 0x65120
leaq -0x250(%rbp), %r15
movl %ebx, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %r15, %rcx
callq *(%rax)
movq 0x2df43(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x246b0(%rip), %r10     # 0x45747
leaq 0xfc01(%rip), %rdi      # 0x30c9f
leaq 0xffd0(%rip), %rdx      # 0x31075
leaq 0x24670(%rip), %r8      # 0x4571c
leaq 0x2468c(%rip), %r9      # 0x4573f
movl $0x34, %ecx
jmp 0x21109
leaq 0x4405f(%rip), %rax     # 0x65120
leaq -0x250(%rbp), %r15
movl %r14d, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %r15, %rcx
callq *(%rax)
movq 0x2def2(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x24674(%rip), %r10     # 0x4575c
leaq 0xfbb0(%rip), %rdi      # 0x30c9f
leaq 0xff7f(%rip), %rdx      # 0x31075
leaq 0x2461f(%rip), %r8      # 0x4571c
leaq 0x2463b(%rip), %r9      # 0x4573f
movl $0x39, %ecx
xorl %eax, %eax
pushq %r15
pushq %r10
callq 0x718c
addq $0x10, %rsp
leaq 0x43c39(%rip), %r15     # 0x64d58
movl %r14d, %edi
callq *(%r15)
movl %ebx, %edi
callq *(%r15)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x43fa4(%rip), %rax     # 0x650f0
leaq -0x250(%rbp), %rbx
movl %r15d, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %rbx, %rcx
callq *(%rax)
movq 0x2de67(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
subq $0x8, %rsp
leaq 0xfb28(%rip), %rdi      # 0x30c9f
leaq 0xfef7(%rip), %rdx      # 0x31075
leaq 0x24597(%rip), %r8      # 0x4571c
leaq 0x245d7(%rip), %r9      # 0x45763
movl $0x48, %ecx
xorl %eax, %eax
pushq %rbx
callq 0x718c
addq $0x10, %rsp
jmp 0x2112a