// File: /workspace/github_c_makefile_star2_O2/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 0x404a4(%rip), %rbx     # 0x5fd28
movl $0x8b30, %edi           # imm = 0x8B30
callq *(%rbx)
movl %eax, %r14d
movl $0x8b31, %edi           # imm = 0x8B31
callq *(%rbx)
movl %eax, %ebx
leaq 0x20d32(%rip), %rax     # 0x405d0
leaq -0x50(%rbp), %rdx
movq %rax, (%rdx)
leaq 0x20d32(%rip), %rax     # 0x405de
movq %rax, 0x8(%rdx)
leaq 0x20d35(%rip), %rax     # 0x405ec
movq %rax, 0x10(%rdx)
movq %r15, 0x18(%rdx)
leaq 0x40fc2(%rip), %rax     # 0x60888
pushq $0x4
popq %rsi
movl %ebx, %edi
xorl %ecx, %ecx
callq *(%rax)
leaq 0x403da(%rip), %r12     # 0x5fcb0
movl %ebx, %edi
callq *(%r12)
leaq -0x2c(%rbp), %r15
andl $0x0, (%r15)
leaq 0x40855(%rip), %r13     # 0x60140
movl %ebx, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r15, %rdx
callq *(%r13)
cmpl $0x0, (%r15)
je 0x1f9df
leaq 0x20e1c(%rip), %rax     # 0x40726
leaq -0x50(%rbp), %rdx
movq %rax, 0x8(%rdx)
pushq $0x4
popq %rsi
movl %r14d, %edi
xorl %ecx, %ecx
leaq 0x40f67(%rip), %rax     # 0x60888
callq *(%rax)
movl %r14d, %edi
callq *(%r12)
andl $0x0, (%r15)
movl %r14d, %edi
movl $0x8b81, %esi           # imm = 0x8B81
movq %r15, %rdx
callq *(%r13)
cmpl $0x0, (%r15)
je 0x1fa2c
leaq 0x403d2(%rip), %rax     # 0x5fd20
callq *(%rax)
movl %eax, %r15d
leaq 0x400f6(%rip), %r12     # 0x5fa50
movl %eax, %edi
movl %ebx, %esi
callq *(%r12)
movl %r15d, %edi
movl %r14d, %esi
callq *(%r12)
leaq 0x409dd(%rip), %rax     # 0x60350
movl %r15d, %edi
callq *(%rax)
leaq 0x403e9(%rip), %r12     # 0x5fd68
movl %r14d, %edi
callq *(%r12)
movl %ebx, %edi
callq *(%r12)
leaq -0x2c(%rbp), %rbx
andl $0x0, (%rbx)
leaq 0x4076e(%rip), %rax     # 0x60108
movl %r15d, %edi
movl $0x8b82, %esi           # imm = 0x8B82
movq %rbx, %rdx
callq *(%rax)
cmpl $0x0, (%rbx)
je 0x1fab1
leaq 0x40811(%rip), %rax     # 0x601c8
xorl %ebx, %ebx
cmpl $-0x1, %r15d
setne %bl
leaq 0x20d81(%rip), %rsi     # 0x40748
movl %ebx, %edi
callq *(%rax)
leaq 0x41196(%rip), %rcx     # 0x60b68
movl %ebx, %edi
movl %eax, %esi
xorl %edx, %edx
callq *(%rcx)
jmp 0x1fa9c
leaq 0x4074a(%rip), %rax     # 0x60130
leaq -0x250(%rbp), %r15
movl %ebx, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %r15, %rcx
callq *(%rax)
movq 0x2a5ce(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
leaq 0x20d13(%rip), %r10     # 0x4071f
leaq 0xc264(%rip), %rdi      # 0x2bc77
leaq 0xc633(%rip), %rdx      # 0x2c04d
leaq 0x20cd3(%rip), %r8      # 0x406f4
leaq 0x20cef(%rip), %r9      # 0x40717
pushq $0x34
jmp 0x1fa78
leaq 0x406fd(%rip), %rax     # 0x60130
leaq -0x250(%rbp), %r15
movl %r14d, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %r15, %rcx
callq *(%rax)
movq 0x2a580(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
leaq 0x20cda(%rip), %r10     # 0x40734
leaq 0xc216(%rip), %rdi      # 0x2bc77
leaq 0xc5e5(%rip), %rdx      # 0x2c04d
leaq 0x20c85(%rip), %r8      # 0x406f4
leaq 0x20ca1(%rip), %r9      # 0x40717
pushq $0x39
popq %rcx
xorl %eax, %eax
pushq %r15
pushq %r10
callq 0x683a
popq %rax
popq %rcx
leaq 0x402db(%rip), %r15     # 0x5fd68
movl %r14d, %edi
callq *(%r15)
movl %ebx, %edi
callq *(%r15)
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40648(%rip), %rax     # 0x60100
leaq -0x250(%rbp), %rbx
movl %r15d, %edi
movl $0x1f4, %esi            # imm = 0x1F4
xorl %edx, %edx
movq %rbx, %rcx
callq *(%rax)
movq 0x2a4fb(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
subq $0x8, %rsp
leaq 0xc194(%rip), %rdi      # 0x2bc77
leaq 0xc563(%rip), %rdx      # 0x2c04d
leaq 0x20c03(%rip), %r8      # 0x406f4
leaq 0x20c43(%rip), %r9      # 0x4073b
pushq $0x48
popq %rcx
xorl %eax, %eax
pushq %rbx
callq 0x683a
popq %rax
popq %rcx
jmp 0x1fa98