// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/mdsprite.c
// Function: gloadtex
unsigned gloadtex (int *picbuf, int xsiz, int ysiz, int is8bit, int dapal)
{
	unsigned rtexid;
	coltype *pic, *pic2;
	unsigned char *cptr;
	int i;

	pic = (coltype *)picbuf; //Correct for GL's RGB order; also apply gamma here..
	pic2 = (coltype *)malloc(xsiz*ysiz*sizeof(coltype)); if (!pic2) return((unsigned)-1);
	cptr = (unsigned char*)&britable[usegammabrightness ? 0 : curbrightness][0];
	if (!is8bit)
	{
		for(i=xsiz*ysiz-1;i>=0;i--)
		{
			pic2[i].b = cptr[pic[i].r];
			pic2[i].g = cptr[pic[i].g];
			pic2[i].r = cptr[pic[i].b];
			pic2[i].a = 255;
		}
	}
	else
	{
		if (palookup[dapal] == 0) dapal = 0;
		for(i=xsiz*ysiz-1;i>=0;i--)
		{
			pic2[i].b = cptr[palette[(int)palookup[dapal][pic[i].a]*3+2]*4];
			pic2[i].g = cptr[palette[(int)palookup[dapal][pic[i].a]*3+1]*4];
			pic2[i].r = cptr[palette[(int)palookup[dapal][pic[i].a]*3+0]*4];
			pic2[i].a = 255;
		}
	}

	glfunc.glGenTextures(1,(GLuint*)&rtexid);
	glfunc.glBindTexture(GL_TEXTURE_2D,rtexid);
	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glfunc.glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,xsiz,ysiz,0,GL_RGBA,GL_UNSIGNED_BYTE,(unsigned char *)pic2);
	free(pic2);
	return(rtexid);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %r12d
movl %ecx, %r13d
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r15
movl %edx, (%rsp)
imull %esi, %ebx
movslq %ebx, %rdi
shlq $0x2, %rdi
callq 0x69a0
testq %rax, %rax
je 0x613a5
movq %rax, %r14
leaq 0x43484(%rip), %rcx     # 0xa478c
xorl %eax, %eax
cmpl $0x0, (%rcx)
jne 0x61318
leaq 0x1576ee(%rip), %rax    # 0x1b8a04
movl (%rax), %eax
cltq
shlq $0x8, %rax
addq 0x31cb3(%rip), %rax     # 0x92fd8
testl %r13d, %r13d
je 0x613af
testl %ebx, %ebx
jle 0x613f1
movslq %r12d, %rcx
leaq 0x3377a0(%rip), %rdx    # 0x398ae0
movq (%rdx,%rcx,8), %rsi
testq %rsi, %rsi
cmoveq %rsi, %rcx
movq (%rdx,%rcx,8), %rcx
movl %ebx, %edx
incq %rdx
leaq 0x21b255(%rip), %rsi    # 0x27c5b0
movzbl -0x5(%r15,%rdx,4), %edi
movzbl (%rcx,%rdi), %edi
leaq (%rdi,%rdi,2), %rdi
movzbl 0x2(%rsi,%rdi), %r8d
movb (%rax,%r8,4), %r8b
movb %r8b, -0x6(%r14,%rdx,4)
movzbl 0x1(%rsi,%rdi), %r8d
movb (%rax,%r8,4), %r8b
movb %r8b, -0x7(%r14,%rdx,4)
movzbl (%rsi,%rdi), %edi
movb (%rax,%rdi,4), %dil
movb %dil, -0x8(%r14,%rdx,4)
movb $-0x1, -0x5(%r14,%rdx,4)
decq %rdx
cmpq $0x1, %rdx
ja 0x6135b
jmp 0x613f1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x6147e
testl %ebx, %ebx
jle 0x613f1
movl %ebx, %ecx
incq %rcx
movzbl -0x8(%r15,%rcx,4), %edx
movb (%rax,%rdx), %dl
movb %dl, -0x6(%r14,%rcx,4)
movzbl -0x7(%r15,%rcx,4), %edx
movb (%rax,%rdx), %dl
movb %dl, -0x7(%r14,%rcx,4)
movzbl -0x6(%r15,%rcx,4), %edx
movb (%rax,%rdx), %dl
movb %dl, -0x8(%r14,%rcx,4)
movb $-0x1, -0x5(%r14,%rcx,4)
decq %rcx
cmpq $0x1, %rcx
ja 0x613b8
leaq 0x599348(%rip), %rbx    # 0x5fa740
leaq 0x4(%rsp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq *0xb8(%rbx)
movl (%r15), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%rbx)
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0xe8(%rbx)
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0xe8(%rbx)
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1908, %edx           # imm = 0x1908
movl %ebp, %ecx
movl 0x8(%rsp), %r8d
xorl %r9d, %r9d
pushq %r14
pushq $0x1401                 # imm = 0x1401
pushq $0x1908                 # imm = 0x1908
callq *0xd0(%rbx)
addq $0x20, %rsp
movq %r14, %rdi
callq 0x61c0
movl (%r15), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq