// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: ptm_uploadtexture
static void ptm_uploadtexture(PTMHead * ptm, unsigned short flags, PTTexture * tex, PTCacheTile * tdef)
{
	GLint mipmap;
	GLint intexfmt;
	int compress = PTCOMPRESS_NONE;
	unsigned char * comprdata = 0;
	int tdefmip = 0, comprsize = 0;
	int starttime;

	detect_texture_size();

#if USE_OPENGL == USE_GLES2
	// GLES permits BGRA as an internal format.
    intexfmt = tex->rawfmt;
#else
    intexfmt = GL_RGBA;
#endif
	if (!(flags & PTH_NOCOMPRESS) && glusetexcompr) {
#if GL_EXT_texture_compression_dxt1 || GL_EXT_texture_compression_s3tc
		if (!compress && !tex->hasalpha && glinfo.texcomprdxt1) {
			intexfmt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
			compress = PTCOMPRESS_DXT1;
		}
#endif
#if GL_OES_compressed_ETC1_RGB8_texture
		if (!compress && !tex->hasalpha && glinfo.texcompretc1) {
			intexfmt = GL_ETC1_RGB8_OES;
			compress = PTCOMPRESS_ETC1;
		}
#endif
#if GL_EXT_texture_compression_s3tc
		if (!compress && tex->hasalpha && glinfo.texcomprdxt5) {
			intexfmt = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
			compress = PTCOMPRESS_DXT5;
		}
#endif
	}

	if (compress && tdef) {
		tdef->format = intexfmt;
		tdef->tsizx  = tex->tsizx;
		tdef->tsizy  = tex->tsizy;
	}

	if (ptm->glpic == 0) {
		glfunc.glGenTextures(1, &ptm->glpic);
	}
	glfunc.glBindTexture(GL_TEXTURE_2D, ptm->glpic);

	ptm_fixtransparency(tex, (flags & PTH_CLAMPED));

	mipmap = 0;
	if (! (flags & PTH_NOMIPLEVEL)) {
		// if we aren't instructed to preserve all mipmap levels,
		// immediately throw away gltexmiplevel mipmaps
		mipmap = max(0, gltexmiplevel);
	}
	while ((tex->sizx >> mipmap) > (1 << gltexmaxsize) ||
	       (tex->sizy >> mipmap) > (1 << gltexmaxsize)) {
		// throw away additional mipmaps until the texture fits within
		// the maximum size permitted by the GL driver
		mipmap++;
	}

	for ( ;
	     mipmap > 0 && (tex->sizx > 1 || tex->sizy > 1);
	     mipmap--) {
		if (compress && tdef) {
			comprsize = ptcompress_getstorage(tex->sizx, tex->sizy, compress);
			comprdata = (unsigned char *) malloc(comprsize);

			starttime = getticks();
			ptcompress_compress(tex->pic, tex->sizx, tex->sizy, comprdata, compress);
			if (polymosttexverbosity >= 2) {
				buildprintf("PolymostTex: ptcompress_compress (%dx%d, %s) took %f sec\n",
					   tex->sizx, tex->sizy, compressfourcc[compress],
					   (float)(getticks() - starttime) / 1000.f);
			}

			tdef->mipmap[tdefmip].sizx = tex->sizx;
			tdef->mipmap[tdefmip].sizy = tex->sizy;
			tdef->mipmap[tdefmip].length = comprsize;
			tdef->mipmap[tdefmip].data = comprdata;
			tdefmip++;

			comprdata = 0;
		}

		ptm_mipscale(tex);
		ptm_fixtransparency(tex, (flags & PTH_CLAMPED));
	}

	if (compress) {
		comprsize = ptcompress_getstorage(tex->sizx, tex->sizy, compress);
		comprdata = (unsigned char *) malloc(comprsize);

		starttime = getticks();
		ptcompress_compress(tex->pic, tex->sizx, tex->sizy, comprdata, compress);
		if (polymosttexverbosity >= 2) {
			buildprintf("PolymostTex: ptcompress_compress (%dx%d, %s) took %f sec\n",
				   tex->sizx, tex->sizy, compressfourcc[compress],
				   (float)(getticks() - starttime) / 1000.f);
		}

		if (tdef) {
			tdef->mipmap[tdefmip].sizx = tex->sizx;
			tdef->mipmap[tdefmip].sizy = tex->sizy;
			tdef->mipmap[tdefmip].length = comprsize;
			tdef->mipmap[tdefmip].data = comprdata;
			tdefmip++;
		}

		glfunc.glCompressedTexImage2D(GL_TEXTURE_2D, 0,
			intexfmt, tex->sizx, tex->sizy, 0,
			comprsize, (const GLvoid *) comprdata);

		if (tdef) {
			// we need to retain each mipmap for the tdef struct, so
			// force each mipmap to be allocated afresh in the loop below
			comprdata = 0;
		}
	} else {
		glfunc.glTexImage2D(GL_TEXTURE_2D, 0,
			intexfmt, tex->sizx, tex->sizy, 0, tex->rawfmt,
			GL_UNSIGNED_BYTE, (const GLvoid *) tex->pic);
	}

	for (mipmap = 1; tex->sizx > 1 || tex->sizy > 1; mipmap++) {
		ptm_mipscale(tex);
		ptm_fixtransparency(tex, (flags & PTH_CLAMPED));

		if (compress) {
			comprsize = ptcompress_getstorage(tex->sizx, tex->sizy, compress);
			if (tdef) {
				comprdata = (unsigned char *) malloc(comprsize);
			}

			starttime = getticks();
			ptcompress_compress(tex->pic, tex->sizx, tex->sizy, comprdata, compress);
			if (polymosttexverbosity >= 2) {
				buildprintf("PolymostTex: ptcompress_compress (%dx%d, %s) took %f sec\n",
					   tex->sizx, tex->sizy, compressfourcc[compress],
					   (float)(getticks() - starttime) / 1000.f);
			}

			if (tdef) {
				tdef->mipmap[tdefmip].sizx = tex->sizx;
				tdef->mipmap[tdefmip].sizy = tex->sizy;
				tdef->mipmap[tdefmip].length = comprsize;
				tdef->mipmap[tdefmip].data = comprdata;
				tdefmip++;
			}

			glfunc.glCompressedTexImage2D(GL_TEXTURE_2D, mipmap,
						intexfmt, tex->sizx, tex->sizy, 0,
						comprsize, (const GLvoid *) comprdata);

			if (tdef) {
				// we need to retain each mipmap for the tdef struct, so
				// force each mipmap to be allocated afresh in this loop
				comprdata = 0;
			}
		} else {
			glfunc.glTexImage2D(GL_TEXTURE_2D, mipmap,
				intexfmt, tex->sizx, tex->sizy, 0, tex->rawfmt,
				GL_UNSIGNED_BYTE, (const GLvoid *) tex->pic);
		}
	}

	ptm->flags = 0;
	ptm->flags |= (tex->hasalpha ? PTH_HASALPHA : 0);

	if (comprdata) {
		free(comprdata);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r12
movq %rdx, %r14
movq %rdi, 0x30(%rsp)
leaq 0x593f0c(%rip), %r15    # 0x5fa998
cmpl $0x0, (%r15)
jg 0x66ac5
leaq 0x1177b7(%rip), %rax    # 0x17e250
movl 0x28(%rax), %eax
testl %eax, %eax
je 0x66abd
movl $0x0, (%r15)
cmpl $0x2, %eax
jl 0x66ac5
xorl %ecx, %ecx
movl %eax, %edx
incl %ecx
shrl %edx
cmpl $0x3, %eax
movl %edx, %eax
ja 0x66ab0
jmp 0x66ac2
movl $0x6, %ecx
movl %ecx, (%r15)
movzwl %si, %ebp
testb $0x10, %bpl
jne 0x66b4d
leaq 0x3eaab(%rip), %rax     # 0xa5580
cmpl $0x0, (%rax)
je 0x66b4d
movl 0x1c(%r14), %ecx
leaq 0x11776b(%rip), %rax    # 0x17e250
testl %ecx, %ecx
jne 0x66b00
cmpb $0x0, 0x1a(%rax)
je 0x66b00
movl $0x1, 0x4(%rsp)
movl $0x83f1, (%rsp)         # imm = 0x83F1
jmp 0x66b19
testl %ecx, %ecx
je 0x66b4d
cmpb $0x0, 0x1b(%rax)
je 0x66b4d
movl $0x2, 0x4(%rsp)
movl $0x83f3, (%rsp)         # imm = 0x83F3
testq %r12, %r12
je 0x67070
movl (%rsp), %eax
movl %eax, 0x10(%r12)
movl 0x10(%r14), %eax
movl %eax, 0x14(%r12)
movl 0x14(%r14), %eax
movl %eax, 0x18(%r12)
movb $0x1, %r13b
movb $0x1, %al
movl %eax, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x66b71
testq %r12, %r12
setne %al
movl %eax, 0x8(%rsp)
movl $0x1908, (%rsp)         # imm = 0x1908
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x30(%rsp), %rax
movl (%rax), %esi
testl %esi, %esi
jne 0x66b98
leaq 0x593bbd(%rip), %rax    # 0x5fa740
movl $0x1, %edi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq *0xb8(%rax)
movl (%rbx), %esi
leaq 0x593ba1(%rip), %rax    # 0x5fa740
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%rax)
movl %ebp, %esi
andl $0x1, %esi
movq %r14, %rdi
movl %esi, 0x24(%rsp)
callq 0x67660
leaq 0x593dda(%rip), %rax    # 0x5fa99c
movl (%rax), %ebx
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
testb $0x20, %bpl
cmovnel %eax, %ebx
movl 0x8(%r14), %eax
movb (%r15), %cl
movl $0x1, %edx
shll %cl, %edx
movl 0xc(%r14), %esi
movl %eax, %edi
movl %ebx, %ecx
sarl %cl, %edi
cmpl %edx, %edi
jg 0x66bf8
movl %esi, %edi
movl %ebx, %ecx
sarl %cl, %edi
cmpl %edx, %edi
jle 0x66bfc
incl %ebx
jmp 0x66be4
movl 0x4(%rsp), %eax
movq %rax, 0x38(%rsp)
testl %ebx, %ebx
movq %r12, 0x28(%rsp)
je 0x66d3b
incl %ebx
leaq 0x20(%r12), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movl %r13d, 0x18(%rsp)
movl 0x8(%r14), %edi
cmpl $0x1, %edi
jg 0x66c40
cmpl $0x2, 0xc(%r14)
jl 0x66d44
testb %r13b, %r13b
je 0x66d1a
movl 0xc(%r14), %esi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
callq 0x68f20
movl %eax, %ebp
movslq %eax, %rdi
callq 0x69a0
movq %rax, %r15
callq 0x6e428
movl %eax, %r12d
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %r15, %rcx
movl %r13d, %r8d
callq 0x68f8e
cmpl $0x2, 0x3e900(%rip)     # 0xa558c
jl 0x66ce5
movl 0x8(%r14), %r13d
movl 0xc(%r14), %eax
movl %eax, 0x4c(%rsp)
movq 0x38(%rsp), %rax
leaq 0x2beba(%rip), %rcx     # 0x92b60
movq (%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp)
callq 0x6e428
subl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x15531(%rip), %xmm0    # 0x7c1f8
cvtss2sd %xmm0, %xmm0
leaq 0x156f7(%rip), %rdi     # 0x7c3c9
movl %r13d, %esi
movl 0x4c(%rsp), %edx
movq 0x50(%rsp), %rcx
movb $0x1, %al
callq 0x25221
movl 0x8(%r14), %eax
movq 0x10(%rsp), %rdx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rcx
movq 0x40(%rsp), %rsi
movl %eax, (%rsi,%rcx,8)
movl 0xc(%r14), %eax
movl %eax, 0x4(%rsi,%rcx,8)
movl %ebp, 0x8(%rsi,%rcx,8)
movq %r15, 0x10(%rsi,%rcx,8)
incl %edx
movq %rdx, 0x10(%rsp)
movl 0x18(%rsp), %r13d
movq %r14, %rdi
callq 0x678f6
movq %r14, %rdi
movl 0x24(%rsp), %esi
callq 0x67660
decl %ebx
cmpl $0x1, %ebx
jg 0x66c2c
jmp 0x66d44
movq $0x0, 0x10(%rsp)
cmpb $0x0, 0xc(%rsp)
je 0x66e26
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0x4(%rsp), %r12d
movl %r12d, %edx
callq 0x68f20
movl %eax, %r15d
movslq %eax, %rdi
callq 0x69a0
movq %rax, %rbp
callq 0x6e428
movl %eax, %ebx
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %rbp, %rcx
movl %r12d, %r8d
callq 0x68f8e
cmpl $0x2, 0x3e7f6(%rip)     # 0xa558c
jl 0x66de7
movl 0x8(%r14), %eax
movl %eax, 0x18(%rsp)
movl 0xc(%r14), %r13d
leaq 0x2bdb5(%rip), %rax     # 0x92b60
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %r12
callq 0x6e428
subl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x1542d(%rip), %xmm0    # 0x7c1f8
cvtss2sd %xmm0, %xmm0
leaq 0x155f3(%rip), %rdi     # 0x7c3c9
movl 0x18(%rsp), %esi
movl %r13d, %edx
movq %r12, %rcx
movb $0x1, %al
callq 0x25221
cmpb $0x0, 0x8(%rsp)
movq 0x28(%rsp), %r13
je 0x66e67
movl 0x8(%r14), %eax
movq 0x10(%rsp), %rdx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rcx
movl %eax, 0x20(%r13,%rcx,8)
movl 0xc(%r14), %eax
movl %eax, 0x24(%r13,%rcx,8)
movl %r15d, 0x28(%r13,%rcx,8)
movq %rbp, 0x30(%r13,%rcx,8)
incl %edx
movq %rdx, 0x10(%rsp)
xorl %ebx, %ebx
jmp 0x66e6a
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl 0x18(%r14), %eax
subq $0x8, %rsp
xorl %ebx, %ebx
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl 0x8(%rsp), %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x5938ea(%rip), %rax    # 0x5fa740
callq *0xd0(%rax)
addq $0x20, %rsp
movq 0x28(%rsp), %r13
jmp 0x66e93
movq %rbp, %rbx
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl (%rsp), %edx
xorl %r9d, %r9d
pushq %rbp
pushq %r15
leaq 0x5938b7(%rip), %rax    # 0x5fa740
callq *0xf0(%rax)
addq $0x10, %rsp
addq $0x20, %r13
movl $0x1, %r15d
movq %r13, 0x28(%rsp)
cmpl $0x1, 0x8(%r14)
jg 0x66eb4
cmpl $0x2, 0xc(%r14)
jl 0x6702a
movq %r14, %rdi
callq 0x678f6
movq %r14, %rdi
movl 0x24(%rsp), %esi
callq 0x67660
cmpb $0x0, 0xc(%rsp)
je 0x66fbb
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0x4(%rsp), %edx
callq 0x68f20
movl %eax, %ebp
cmpb $0x0, 0x8(%rsp)
je 0x66ef8
movslq %ebp, %rdi
callq 0x69a0
movq %rax, %rbx
callq 0x6e428
movl %eax, %r12d
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %rbx, %rcx
movl 0x4(%rsp), %r8d
callq 0x68f8e
cmpl $0x2, 0x3e66d(%rip)     # 0xa558c
jl 0x66f7e
movl 0x8(%r14), %r13d
movq %rbx, 0x18(%rsp)
movl 0xc(%r14), %ebx
movq 0x38(%rsp), %rax
leaq 0x2bc26(%rip), %rcx     # 0x92b60
movq (%rcx,%rax,8), %rax
movq %rax, 0x40(%rsp)
callq 0x6e428
subl %r12d, %eax
cvtsi2ss %rax, %xmm0
divss 0x152a0(%rip), %xmm0    # 0x7c1f8
cvtss2sd %xmm0, %xmm0
leaq 0x15466(%rip), %rdi     # 0x7c3c9
movl %r13d, %esi
movq 0x28(%rsp), %r13
movl %ebx, %edx
movq 0x18(%rsp), %rbx
movq 0x40(%rsp), %rcx
movb $0x1, %al
callq 0x25221
cmpb $0x0, 0x8(%rsp)
je 0x66ff6
movl 0x8(%r14), %eax
movq 0x10(%rsp), %rdx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rcx
movl %eax, (%r13,%rcx,8)
movl 0xc(%r14), %eax
movl %eax, 0x4(%r13,%rcx,8)
movl %ebp, 0x8(%r13,%rcx,8)
movq %rbx, 0x10(%r13,%rcx,8)
incl %edx
movq %rdx, 0x10(%rsp)
movq %rbx, %rax
xorl %ebx, %ebx
jmp 0x66ff9
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl 0x18(%r14), %eax
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl 0x8(%rsp), %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x593756(%rip), %rax    # 0x5fa740
callq *0xd0(%rax)
addq $0x20, %rsp
jmp 0x67022
movq %rbx, %rax
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl (%rsp), %edx
xorl %r9d, %r9d
pushq %rax
pushq %rbp
leaq 0x593728(%rip), %rax    # 0x5fa740
callq *0xf0(%rax)
addq $0x10, %rsp
incl %r15d
jmp 0x66ea2
movq 0x30(%rsp), %rcx
movl $0x0, 0x4(%rcx)
xorl %eax, %eax
cmpl $0x0, 0x1c(%r14)
setne %al
shll $0x3, %eax
movl %eax, 0x4(%rcx)
testq %rbx, %rbx
je 0x67061
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x61c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
jmp 0x66b71