// File: /workspace/github_c_makefile_star2_O0/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[];

	float tx = (float)resx / (float)stride, ty = 1.0;
	int tsizx = stride, tsizy = resy;

	float winaspect = (float)winx / (float)winy;
	float frameaspect = (float)resx / (float)resy;
	float aspectx, aspecty;

	if (!glinfo.texnpot) {
		for (tsizx = 1; tsizx < stride; tsizx <<= 1) { }
		for (tsizy = 1; tsizy < resy; tsizy <<= 1) { }
		tx = (float)resx / (float)tsizx;
		ty = (float)resy / (float)tsizy;
	}

	// Correct for aspect ratio difference between the window size
	// and the logical 8-bit surface by skewing the quad we draw.
	if (fabs(winaspect - frameaspect) < 0.01) {
		aspectx = aspecty = 1.0;
	} else if (winaspect > frameaspect) {
		// Window is wider than the frame.
		aspectx = frameaspect / winaspect;
		aspecty = 1.0;
	} else {
		// Window is narrower than the frame.
		aspectx = 1.0;
		aspecty = winaspect / frameaspect;
	}

	// Buffer contents: indexes and texcoord/vertex elements.
	GLushort indexes[6] = { 0, 1, 2, 0, 2, 3 };
	GLfloat elements[4][4] = {
		// tx, ty,  vx, vy
		{ 0.0, ty,  -aspectx, -aspecty },
		{ tx,  ty,   aspectx, -aspecty },
		{ tx,  0.0,  aspectx,  aspecty },
		{ 0.0, 0.0, -aspectx,  aspecty },
	};

	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);
	glfunc.glBindBuffer(GL_ARRAY_BUFFER, state->buffer_elements);
	glfunc.glBufferData(GL_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

	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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x62d0
movl $0x0, -0x30(%rbp)
cvtsi2ssl -0x14(%rbp), %xmm0
cvtsi2ssl -0x1c(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x34(%rbp)
movss 0x417de(%rip), %xmm0    # 0xb2e00
movss %xmm0, -0x38(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x40(%rbp)
cvtsi2ssl -0x20(%rbp), %xmm0
cvtsi2ssl -0x24(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
cvtsi2ssl -0x14(%rbp), %xmm0
cvtsi2ssl -0x18(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x48(%rbp)
leaq 0x149750(%rip), %rax    # 0x1badb0
cmpb $0x0, 0x1d(%rax)
jne 0x716c2
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x71681
jmp 0x71677
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
jmp 0x7166d
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x7169c
jmp 0x71692
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x40(%rbp)
jmp 0x71688
cvtsi2ssl -0x14(%rbp), %xmm0
cvtsi2ssl -0x3c(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x34(%rbp)
cvtsi2ssl -0x18(%rbp), %xmm0
cvtsi2ssl -0x40(%rbp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x44(%rbp), %xmm0
subss -0x48(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movaps 0x43f49(%rip), %xmm0    # 0xb5620
pand %xmm0, %xmm1
movsd 0x43f35(%rip), %xmm0    # 0xb5618
ucomisd %xmm1, %xmm0
jbe 0x71705
movss 0x4170f(%rip), %xmm0    # 0xb2e00
movss %xmm0, -0x50(%rbp)
movss 0x41702(%rip), %xmm0    # 0xb2e00
movss %xmm0, -0x4c(%rbp)
jmp 0x7174c
movss -0x44(%rbp), %xmm0
ucomiss -0x48(%rbp), %xmm0
jbe 0x7172e
movss -0x48(%rbp), %xmm0
divss -0x44(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss 0x416d9(%rip), %xmm0    # 0xb2e00
movss %xmm0, -0x50(%rbp)
jmp 0x7174a
movss 0x416ca(%rip), %xmm0    # 0xb2e00
movss %xmm0, -0x4c(%rbp)
movss -0x44(%rbp), %xmm0
divss -0x48(%rbp), %xmm0
movss %xmm0, -0x50(%rbp)
jmp 0x7174c
movq 0x43eb9(%rip), %rax     # 0xb560c
movq %rax, -0x5c(%rbp)
movl 0x43eb7(%rip), %eax     # 0xb5614
movl %eax, -0x54(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp)
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
movss -0x4c(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x98(%rbp)
movss -0x50(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x94(%rbp)
movss -0x34(%rbp), %xmm0
movss %xmm0, -0x90(%rbp)
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x8c(%rbp)
movss -0x4c(%rbp), %xmm0
movss %xmm0, -0x88(%rbp)
movss -0x50(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x84(%rbp)
movss -0x34(%rbp), %xmm0
movss %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x7c(%rbp)
movss -0x4c(%rbp), %xmm0
movss %xmm0, -0x78(%rbp)
movss -0x50(%rbp), %xmm0
movss %xmm0, -0x74(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x70(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
movss -0x4c(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x50(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
leaq 0x149565(%rip), %rax    # 0x1badb0
movsbl 0x19(%rax), %edx
movl $0x2900, %eax           # imm = 0x2900
movl $0x812f, %ecx           # imm = 0x812F
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0xa4(%rbp)
movq 0x57bf5c(%rip), %rax    # 0x5ed7c8
movq -0x10(%rbp), %rsi
addq $0x4, %rsi
movl $0x1, %edi
callq *%rax
movl $0x84c1, %edi           # imm = 0x84C1
callq *0x57bfca(%rip)         # 0x5ed850
movq 0x57bf4b(%rip), %rax    # 0x5ed7d8
movq -0x10(%rbp), %rcx
movl 0x4(%rcx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *%rax
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
movl $0x1908, %edx           # imm = 0x1908
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r8d
xorl %eax, %eax
movl %r9d, %esi
movl $0x1908, (%rsp)         # imm = 0x1908
movl $0x1401, 0x8(%rsp)      # imm = 0x1401
movq $0x0, 0x10(%rsp)
callq *0x57bf0a(%rip)         # 0x5ed7e0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x57bf0d(%rip)         # 0x5ed7f8
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x57bef8(%rip)         # 0x5ed7f8
movq 0x57bef1(%rip), %rax    # 0x5ed7f8
movl -0xa4(%rbp), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
callq *%rax
movq 0x57bed8(%rip), %rax    # 0x5ed7f8
movl -0xa4(%rbp), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
callq *%rax
movl $0x1907, -0xa8(%rbp)    # imm = 0x1907
movl $0x1903, -0xac(%rbp)    # imm = 0x1903
movq 0x57be7b(%rip), %rax    # 0x5ed7c8
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
movl $0x1, %edi
callq *%rax
movl $0x84c0, %edi           # imm = 0x84C0
callq *0x57bee9(%rip)         # 0x5ed850
movq 0x57be6a(%rip), %rax    # 0x5ed7d8
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *%rax
movq 0x57be5d(%rip), %rax    # 0x5ed7e0
movl -0xa8(%rbp), %edx
movl -0x3c(%rbp), %ecx
movl -0x40(%rbp), %r8d
movl -0xac(%rbp), %r10d
movl $0xde1, %edi            # imm = 0xDE1
xorl %r9d, %r9d
xorl %esi, %esi
movl %r9d, %esi
movl %r10d, (%rsp)
movl $0x1401, 0x8(%rsp)      # imm = 0x1401
movq $0x0, 0x10(%rsp)
callq *%rax
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x57be28(%rip)         # 0x5ed7f8
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x57be13(%rip)         # 0x5ed7f8
movq 0x57be0c(%rip), %rax    # 0x5ed7f8
movl -0xa4(%rbp), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
callq *%rax
movq 0x57bdf3(%rip), %rax    # 0x5ed7f8
movl -0xa4(%rbp), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
callq *%rax
movl $0xcf5, %edi            # imm = 0xCF5
movl $0x1, %esi
callq *0x57bd61(%rip)         # 0x5ed788
movl $0x8b31, %edi           # imm = 0x8B31
leaq 0x4488d(%rip), %rsi     # 0xb62c0
callq 0x711f0
movl %eax, -0x2c(%rbp)
movl $0x8b30, %edi           # imm = 0x8B30
leaq 0x445a9(%rip), %rsi     # 0xb5ff0
callq 0x711f0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x71a5b
cmpl $0x0, -0x28(%rbp)
jne 0x71a8b
cmpl $0x0, -0x2c(%rbp)
je 0x71a6d
movq 0x57be18(%rip), %rax    # 0x5ed880
movl -0x2c(%rbp), %edi
callq *%rax
cmpl $0x0, -0x28(%rbp)
je 0x71a7f
movq 0x57be06(%rip), %rax    # 0x5ed880
movl -0x28(%rbp), %edi
callq *%rax
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x71cc8
leaq -0x2c(%rbp), %rsi
movl $0x2, %edi
callq 0x71490
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x71ac6
movq 0x57bdd7(%rip), %rax    # 0x5ed880
movl -0x2c(%rbp), %edi
callq *%rax
movq 0x57bdcb(%rip), %rax    # 0x5ed880
movl -0x28(%rbp), %edi
callq *%rax
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x71cc8
movq 0x57bdb3(%rip), %rax    # 0x5ed880
movl -0x2c(%rbp), %edi
callq *%rax
movq 0x57bda7(%rip), %rax    # 0x5ed880
movl -0x28(%rbp), %edi
callq *%rax
movq 0x57be1b(%rip), %rax    # 0x5ed900
movl -0x30(%rbp), %edi
callq *%rax
movq 0x57bddf(%rip), %rax    # 0x5ed8d0
movq %rax, -0xc0(%rbp)
movq 0x57bdb9(%rip), %rax    # 0x5ed8b8
movl -0x30(%rbp), %edi
leaq 0x440d0(%rip), %rsi     # 0xb5bd9
callq *%rax
movl %eax, %edi
movq -0xc0(%rbp), %rax
movl $0x1, %esi
callq *%rax
movq 0x57bdae(%rip), %rax    # 0x5ed8d0
movq %rax, -0xb8(%rbp)
movq 0x57bd88(%rip), %rax    # 0x5ed8b8
movl -0x30(%rbp), %edi
leaq 0x440a9(%rip), %rsi     # 0xb5be3
callq *%rax
movl %eax, %edi
movq -0xb8(%rbp), %rax
xorl %esi, %esi
callq *%rax
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq 0x57bd37(%rip), %rax    # 0x5ed890
movl -0x30(%rbp), %edi
leaq 0x44088(%rip), %rsi     # 0xb5beb
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x57bd1b(%rip), %rax    # 0x5ed890
movl -0x30(%rbp), %edi
leaq 0x44075(%rip), %rsi     # 0xb5bf4
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq 0x57bd27(%rip), %rax    # 0x5ed8b8
movl -0x30(%rbp), %edi
leaq 0x44064(%rip), %rsi     # 0xb5bff
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x20(%rax)
movq 0x57bc7b(%rip), %rax    # 0x5ed828
movq -0x10(%rbp), %rsi
addq $0x18, %rsi
movl $0x1, %edi
callq *%rax
movq 0x57bc45(%rip), %rax    # 0x5ed808
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %esi
movl $0x8893, %edi           # imm = 0x8893
callq *%rax
movq 0x57bc38(%rip), %rax    # 0x5ed810
leaq -0x5c(%rbp), %rdx
movl $0x8893, %edi           # imm = 0x8893
movl $0xc, %esi
movl $0x88e4, %ecx           # imm = 0x88E4
callq *%rax
movq 0x57bc34(%rip), %rax    # 0x5ed828
movq -0x10(%rbp), %rsi
addq $0x1c, %rsi
movl $0x1, %edi
callq *%rax
movq 0x57bbfe(%rip), %rax    # 0x5ed808
movq -0x10(%rbp), %rcx
movl 0x1c(%rcx), %esi
movl $0x8892, %edi           # imm = 0x8892
callq *%rax
movq 0x57bbf1(%rip), %rax    # 0x5ed810
leaq -0xa0(%rbp), %rdx
movl $0x8892, %edi           # imm = 0x8892
movl $0x40, %esi
movl $0x88e4, %ecx           # imm = 0x88E4
callq *%rax
movq 0x57bbfa(%rip), %rax    # 0x5ed838
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %edi
callq *%rax
movq 0x57bbfa(%rip), %rax    # 0x5ed848
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %edi
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
xorl %r9d, %r9d
callq *%rax
movq 0x57bbc5(%rip), %rax    # 0x5ed838
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %edi
callq *%rax
movq 0x57bbc5(%rip), %rax    # 0x5ed848
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %edi
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
movl $0x8, %r9d
callq *%rax
xorps %xmm3, %xmm3
movaps %xmm3, %xmm0
movaps %xmm3, %xmm1
movaps %xmm3, %xmm2
callq *0x57ba5a(%rip)         # 0x5ed710
movl $0xb71, %edi            # imm = 0xB71
callq *0x57ba97(%rip)         # 0x5ed758
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)