// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/glbuild.c
// Function: glbuild_prepare_8bit_shader
int glbuild_prepare_8bit_shader(glbuild8bit *state, int resx, int resy, int stride, int winx, int winy)
{
	GLuint shaders[2] = {0,0}, prog = 0;

	extern const char default_glbuild_fs_glsl[];
	extern const char default_glbuild_vs_glsl[];

	int tsizx = stride, tsizy = resy;

	state->resx = resx;
	state->resy = resy;
	state->winx = state->winy = 0;
	state->tx = (float)resx / (float)stride;
	state->ty = 1.0;
	if (!glinfo.texnpot) {
		for (tsizx = 1; tsizx < stride; tsizx <<= 1) { }
		for (tsizy = 1; tsizy < resy; tsizy <<= 1) { }
		state->tx = (float)resx / (float)tsizx;
		state->ty = (float)resy / (float)tsizy;
	}

	GLushort indexes[6] = { 0, 1, 2, 0, 2, 3 };
	GLint clamp = glinfo.clamptoedge ? GL_CLAMP_TO_EDGE : GL_CLAMP;
	GLenum intfmt, extfmt;

	// Initialise texture objects for the palette and framebuffer.
	// The textures will be uploaded on the first rendered frame.
	glfunc.glGenTextures(1, &state->paltex);
	glfunc.glActiveTexture(GL_TEXTURE1);
	glfunc.glBindTexture(GL_TEXTURE_2D, state->paltex);
	glfunc.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);	// Allocates memory.
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);

#if (USE_OPENGL == USE_GLES2)
	intfmt = GL_LUMINANCE;
	extfmt = GL_LUMINANCE;
#else
	intfmt = GL_RGB;
	extfmt = GL_RED;
#endif

	glfunc.glGenTextures(1, &state->frametex);
	glfunc.glActiveTexture(GL_TEXTURE0);
	glfunc.glBindTexture(GL_TEXTURE_2D, state->frametex);
	glfunc.glTexImage2D(GL_TEXTURE_2D, 0, intfmt, tsizx, tsizy, 0, extfmt, GL_UNSIGNED_BYTE, NULL);	// Allocates memory.
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);

	glfunc.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Prepare the vertex and fragment shaders.
	shaders[0] = glbuild_compile_shader(GL_VERTEX_SHADER, default_glbuild_vs_glsl);
	shaders[1] = glbuild_compile_shader(GL_FRAGMENT_SHADER, default_glbuild_fs_glsl);
	if (!shaders[0] || !shaders[1]) {
		if (shaders[0]) glfunc.glDeleteShader(shaders[0]);
		if (shaders[1]) glfunc.glDeleteShader(shaders[1]);
		return -1;
	}

	// Prepare the shader program.
	prog = glbuild_link_program(2, shaders);
	if (!prog) {
		glfunc.glDeleteShader(shaders[0]);
		glfunc.glDeleteShader(shaders[1]);
		return -1;
	}

	// Shaders were detached in glbuild_link_program.
	glfunc.glDeleteShader(shaders[0]);
	glfunc.glDeleteShader(shaders[1]);

	// Connect the textures to the program.
	glfunc.glUseProgram(prog);
	glfunc.glUniform1i(glfunc.glGetUniformLocation(prog, "u_palette"), 1);
	glfunc.glUniform1i(glfunc.glGetUniformLocation(prog, "u_frame"), 0);

	state->program = prog;
	state->attrib_vertex = glfunc.glGetAttribLocation(prog, "a_vertex");
	state->attrib_texcoord = glfunc.glGetAttribLocation(prog, "a_texcoord");
	state->unif_gamma = glfunc.glGetUniformLocation(prog, "u_gamma");

#if (USE_OPENGL == USE_GL3)
	glfunc.glGenVertexArrays(1, &state->vao);
	glfunc.glBindVertexArray(state->vao);
#endif

	// Prepare buffer objects for the vertex/texcoords and indexes.
	glfunc.glGenBuffers(1, &state->buffer_indexes);
	glfunc.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->buffer_indexes);
	glfunc.glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);

	glfunc.glGenBuffers(1, &state->buffer_elements);
	glbuild_update_window_size(state, winx, winy);

	glfunc.glEnableVertexAttribArray(state->attrib_texcoord);
	glfunc.glVertexAttribPointer(state->attrib_texcoord, 2, GL_FLOAT, GL_FALSE,
		sizeof(GLfloat)*4, (const GLvoid *)0);

    glfunc.glEnableVertexAttribArray(state->attrib_vertex);
	glfunc.glVertexAttribPointer(state->attrib_vertex, 2, GL_FLOAT, GL_FALSE,
		sizeof(GLfloat)*4, (const GLvoid *)(sizeof(GLfloat)*2));

#if (USE_OPENGL == USE_GL3)
    glfunc.glBindVertexArray(0);
#endif

	// Set some persistent GL state.
	glfunc.glClearColor(0.0, 0.0, 0.0, 0.0);
	glfunc.glDisable(GL_DEPTH_TEST);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, 0x8(%rsp)
movq %rdi, %rbx
movl %esi, 0x24(%rdi)
cvtsi2ss %esi, %xmm0
movl %edx, 0x28(%rdi)
cvtsi2ss %ecx, %xmm1
movq $0x0, 0x2c(%rdi)
movaps %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, 0x34(%rdi)
movl $0x3f800000, 0x38(%rdi) # imm = 0x3F800000
leaq 0x12eb4f(%rip), %rax    # 0x17e250
cmpb $0x0, 0x1d(%rax)
je 0x4f70f
movl %edx, %r12d
movl %ecx, %r15d
jmp 0x4f753
movl $0x1, %esi
movl %esi, %r15d
leal (%r15,%r15), %esi
cmpl %ecx, %r15d
jl 0x4f714
movl $0x1, %ecx
movl %ecx, %r12d
leal (%r12,%r12), %ecx
cmpl %edx, %r12d
jl 0x4f725
movd %r12d, %xmm1
movd %r15d, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
cvtdq2ps %xmm2, %xmm2
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
divps %xmm2, %xmm0
movlps %xmm0, 0x34(%rbx)
movabsq $0x200010000, %rcx      # imm = 0x200010000
movq %rcx, 0x18(%rsp)
movl $0x30002, 0x20(%rsp)    # imm = 0x30002
cmpb $0x0, 0x19(%rax)
movl $0x2900, %eax           # imm = 0x2900
movl $0x812f, %r13d          # imm = 0x812F
cmovel %eax, %r13d
leaq 0x4(%rbx), %rsi
movl $0x1, %edi
callq *0x5ab06c(%rip)         # 0x5fa7f8
movl $0x84c1, %edi           # imm = 0x84C1
callq *0x5ab0e9(%rip)         # 0x5fa880
movl 0x4(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5ab063(%rip)         # 0x5fa808
subq $0x8, %rsp
xorl %r14d, %r14d
movl $0x1401, %ebp           # imm = 0x1401
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1908, %edx           # imm = 0x1908
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %rbp
pushq $0x1908                 # imm = 0x1908
callq *0x5ab037(%rip)         # 0x5fa810
addq $0x20, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ab036(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ab021(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r13d, %edx
callq *0x5ab00e(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r13d, %edx
callq *0x5aaffb(%rip)         # 0x5fa828
leaq 0x8(%rbx), %rsi
movl $0x1, %edi
callq *0x5aafbc(%rip)         # 0x5fa7f8
movl $0x84c0, %edi           # imm = 0x84C0
callq *0x5ab039(%rip)         # 0x5fa880
movl 0x8(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5aafb3(%rip)         # 0x5fa808
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1907, %edx           # imm = 0x1907
movl %r15d, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %rbp
pushq $0x1903                 # imm = 0x1903
callq *0x5aaf94(%rip)         # 0x5fa810
addq $0x20, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5aaf93(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5aaf7e(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r13d, %edx
callq *0x5aaf6b(%rip)         # 0x5fa828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r13d, %edx
callq *0x5aaf58(%rip)         # 0x5fa828
movl $0xcf5, %edi            # imm = 0xCF5
movl $0x1, %esi
callq *0x5aaed8(%rip)         # 0x5fa7b8
leaq 0x2ab19(%rip), %rsi     # 0x7a400
movl $0x8b31, %edi           # imm = 0x8B31
callq 0x4f41f
movl %eax, %r15d
movl %eax, 0x10(%rsp)
leaq 0x2a831(%rip), %rsi     # 0x7a130
movl $0x8b30, %edi           # imm = 0x8B30
callq 0x4f41f
movl %eax, %r13d
movl %eax, 0x14(%rsp)
testl %r15d, %r15d
setne %al
testl %r13d, %r13d
setne %cl
testb %cl, %al
je 0x4fa9e
leaq 0x10(%rsp), %rsi
movl $0x2, %edi
callq 0x4f5c7
movl %eax, %r12d
movl %r15d, %edi
callq *0x5aaf71(%rip)         # 0x5fa8b0
movl %r13d, %edi
callq *0x5aaf68(%rip)         # 0x5fa8b0
testl %r12d, %r12d
je 0x4fac2
movl %r12d, %edi
callq *0x5aafd6(%rip)         # 0x5fa930
movq 0x5aaf9f(%rip), %r14    # 0x5fa900
leaq 0x2a3a9(%rip), %rsi     # 0x79d11
movl %r12d, %edi
callq *0x5aaf77(%rip)         # 0x5fa8e8
movl %eax, %edi
movl $0x1, %esi
callq *%r14
movq 0x5aaf7e(%rip), %r14    # 0x5fa900
leaq 0x2a392(%rip), %rsi     # 0x79d1b
movl %r12d, %edi
callq *0x5aaf56(%rip)         # 0x5fa8e8
xorl %r15d, %r15d
movl %eax, %edi
xorl %esi, %esi
callq *%r14
movl %r12d, (%rbx)
leaq 0x2a37d(%rip), %rsi     # 0x79d23
movl %r12d, %edi
callq *0x5aaf11(%rip)         # 0x5fa8c0
movl %eax, 0x10(%rbx)
leaq 0x2a373(%rip), %rsi     # 0x79d2c
movl %r12d, %edi
callq *0x5aaefe(%rip)         # 0x5fa8c0
movl %eax, 0x14(%rbx)
leaq 0x2a36b(%rip), %rsi     # 0x79d37
movl %r12d, %edi
callq *0x5aaf13(%rip)         # 0x5fa8e8
movl %eax, 0x20(%rbx)
leaq 0x18(%rbx), %rsi
movl $0x1, %edi
callq *0x5aae71(%rip)         # 0x5fa858
movl 0x18(%rbx), %esi
movl $0x8893, %edi           # imm = 0x8893
callq *0x5aae43(%rip)         # 0x5fa838
leaq 0x18(%rsp), %rdx
movl $0xc, %esi
movl $0x8893, %edi           # imm = 0x8893
movl $0x88e4, %ecx           # imm = 0x88E4
callq *0x5aae31(%rip)         # 0x5fa840
leaq 0x1c(%rbx), %rsi
movl $0x1, %edi
callq *0x5aae3a(%rip)         # 0x5fa858
movq %rbx, %rdi
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
callq 0x4fada
movl 0x14(%rbx), %edi
callq *0x5aae31(%rip)         # 0x5fa868
movl 0x14(%rbx), %edi
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
xorl %r9d, %r9d
callq *0x5aae23(%rip)         # 0x5fa878
movl 0x10(%rbx), %edi
callq *0x5aae0a(%rip)         # 0x5fa868
movl 0x10(%rbx), %edi
movl $0x8, %r9d
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
callq *0x5aadf9(%rip)         # 0x5fa878
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
callq *0x5aacaf(%rip)         # 0x5fa740
movl $0xb71, %edi            # imm = 0xB71
callq *0x5aacec(%rip)         # 0x5fa788
jmp 0x4fac8
testl %r15d, %r15d
je 0x4faac
movl %r15d, %edi
callq *0x5aae04(%rip)         # 0x5fa8b0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
je 0x4fac8
movl %r13d, %edi
callq *0x5aadf0(%rip)         # 0x5fa8b0
jmp 0x4fac8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq