// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rdx, %r14
movq %rdi, 0x40(%rsp)
leaq 0x597a4a(%rip), %rbx    # 0x5f3998
cmpl $0x0, (%rbx)
jle 0x5bf94
movzwl %si, %r15d
testb $0x10, %r15b
jne 0x5c000
leaq 0x42618(%rip), %rax     # 0x9e580
cmpl $0x0, (%rax)
je 0x5c000
movl 0x1c(%r14), %ecx
leaq 0x11b2d4(%rip), %rax    # 0x177250
testl %ecx, %ecx
jne 0x5bfb9
cmpb $0x0, 0x1a(%rax)
je 0x5bfb9
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movl $0x83f1, %ecx           # imm = 0x83F1
jmp 0x5bfcf
leaq 0x11b2b5(%rip), %rax    # 0x177250
movl 0x28(%rax), %eax
testl %eax, %eax
je 0x5c500
andl $0x0, (%rbx)
pushq $0x1
popq %rcx
cmpl $0x2, %eax
jl 0x5bf53
movl %ecx, (%rbx)
shrl %eax
incl %ecx
jmp 0x5bfac
testl %ecx, %ecx
je 0x5c000
cmpb $0x0, 0x1b(%rax)
je 0x5c000
pushq $0x2
popq %rax
movl %eax, 0xc(%rsp)
movl $0x83f3, %ecx           # imm = 0x83F3
testq %r13, %r13
je 0x5c50b
movl %ecx, 0x10(%r13)
movl 0x10(%r14), %eax
movl %eax, 0x14(%r13)
movl 0x14(%r14), %eax
movl %eax, 0x18(%r13)
movb $0x1, %al
movl %eax, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x10(%rsp)
movb $0x1, %al
movl %eax, 0x14(%rsp)
jmp 0x5c027
testq %r13, %r13
setne %al
movl %eax, 0x10(%rsp)
movl $0x1908, %ecx           # imm = 0x1908
movl $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x14(%rsp)
movl %ecx, 0x28(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %esi
testl %esi, %esi
jne 0x5c052
leaq 0x597703(%rip), %rax    # 0x5f3740
pushq $0x1
popq %rdi
movq 0x40(%rsp), %r12
movq %r12, %rsi
callq *0xb8(%rax)
movl (%r12), %esi
leaq 0x5976e7(%rip), %rax    # 0x5f3740
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%rax)
movl %r15d, %r12d
andl $0x1, %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x5ca8b
leaq 0x59791f(%rip), %rax    # 0x5f399c
movl (%rax), %ebp
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
testb $0x20, %r15b
cmovnel %eax, %ebp
movl 0x8(%r14), %eax
movb (%rbx), %cl
pushq $0x1
popq %r15
movl %r15d, %edx
shll %cl, %edx
movl 0xc(%r14), %esi
movl %eax, %edi
movl %ebp, %ecx
sarl %cl, %edi
cmpl %edx, %edi
jg 0x5c0b4
movl %esi, %edi
movl %ebp, %ecx
sarl %cl, %edi
cmpl %edx, %edi
jle 0x5c0b8
incl %ebp
jmp 0x5c0a0
movl 0xc(%rsp), %eax
movq %rax, 0x48(%rsp)
addq $0x20, %r13
movq %r13, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
movl %r12d, 0x2c(%rsp)
movslq 0x20(%rsp), %r13
testl %ebp, %ebp
jle 0x5c1ea
movl 0x8(%r14), %edi
cmpl $0x1, %edi
jg 0x5c0f9
cmpl $0x2, 0xc(%r14)
jl 0x5c1ea
cmpb $0x0, 0x18(%rsp)
je 0x5c1d0
movl 0xc(%r14), %esi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
callq 0x5e23c
movl %eax, %r12d
movslq %eax, %rdi
callq 0x69a0
movq %rax, %r15
callq 0x62cd4
movl %eax, 0x20(%rsp)
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %r15, %rcx
movl %ebx, %r8d
callq 0x5e2c3
cmpl $0x2, 0x42445(%rip)     # 0x9e58c
jl 0x5c19d
movl 0x8(%r14), %ebx
movl 0xc(%r14), %eax
movl %eax, 0x30(%rsp)
movq 0x48(%rsp), %rax
leaq 0x2f9df(%rip), %rcx     # 0x8bb40
movq (%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp)
callq 0x62cd4
subl 0x20(%rsp), %eax
cvtsi2ss %rax, %xmm0
divss 0x14db8(%rip), %xmm0    # 0x70f38
cvtss2sd %xmm0, %xmm0
leaq 0x14f6a(%rip), %rdi     # 0x710f5
movl %ebx, %esi
movl 0x30(%rsp), %edx
movq 0x50(%rsp), %rcx
movb $0x1, %al
callq 0x22427
movl 0x8(%r14), %eax
imulq $0x18, %r13, %rcx
movq 0x38(%rsp), %rdx
movl %eax, (%rdx,%rcx)
movl 0xc(%r14), %eax
movl %eax, 0x4(%rdx,%rcx)
movl %r12d, 0x8(%rdx,%rcx)
movq %r15, 0x10(%rdx,%rcx)
incl %r13d
movq %r13, 0x20(%rsp)
pushq $0x1
popq %r15
movl 0x2c(%rsp), %r12d
movq %r14, %rdi
callq 0x5ccfa
movq %r14, %rdi
movl %r12d, %esi
callq 0x5ca8b
decl %ebp
jmp 0x5c0d8
cmpb $0x0, 0x14(%rsp)
je 0x5c2fe
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
callq 0x5e23c
movq %rax, 0x18(%rsp)
movslq %eax, %rdi
callq 0x69a0
movq %rax, %r12
callq 0x62cd4
movl %eax, %ebp
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x5e2c3
cmpl $0x2, 0x42350(%rip)     # 0x9e58c
jl 0x5c28f
movl 0x8(%r14), %ebx
movl 0xc(%r14), %r15d
movq 0x48(%rsp), %rax
leaq 0x2f8ee(%rip), %rcx     # 0x8bb40
movq (%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
callq 0x62cd4
subl %ebp, %eax
cvtsi2ss %rax, %xmm0
divss 0x14cc9(%rip), %xmm0    # 0x70f38
cvtss2sd %xmm0, %xmm0
leaq 0x14e7b(%rip), %rdi     # 0x710f5
movl %ebx, %esi
movl %r15d, %edx
pushq $0x1
popq %r15
movq 0x30(%rsp), %rcx
movb $0x1, %al
callq 0x22427
movq %r12, %rbx
cmpb $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r10
je 0x5c2ca
movl 0x8(%r14), %eax
imulq $0x18, %r13, %rcx
movq 0x38(%rsp), %rdx
movl %eax, (%rdx,%rcx)
movl 0xc(%r14), %eax
movl %eax, 0x4(%rdx,%rcx)
movl %r10d, 0x8(%rdx,%rcx)
movq %r12, 0x10(%rdx,%rcx)
incl %r13d
xorl %ebx, %ebx
movq %r13, 0x20(%rsp)
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl 0x28(%rsp), %ebp
movl %ebp, %edx
xorl %r9d, %r9d
pushq %r12
pushq %r10
leaq 0x597453(%rip), %rax    # 0x5f3740
callq *0xf0(%rax)
addq $0x10, %rsp
movl 0x2c(%rsp), %r12d
jmp 0x5c33a
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 0x30(%rsp), %ebp
movl %ebp, %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x597410(%rip), %rax    # 0x5f3740
callq *0xd0(%rax)
addq $0x20, %rsp
movq 0x38(%rsp), %r13
cmpl $0x1, 0x8(%r14)
jg 0x5c351
cmpl $0x2, 0xc(%r14)
jl 0x5c4d1
movq %r14, %rdi
callq 0x5ccfa
movq %r14, %rdi
movl %r12d, %esi
callq 0x5ca8b
cmpb $0x0, 0x14(%rsp)
je 0x5c45f
movl %r15d, 0x18(%rsp)
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0xc(%rsp), %edx
callq 0x5e23c
movl %eax, %r12d
cmpb $0x0, 0x10(%rsp)
je 0x5c39a
movslq %r12d, %rdi
callq 0x69a0
movq %rax, %rbx
callq 0x62cd4
movl %eax, %r15d
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %rbx, %rcx
movl 0xc(%rsp), %r8d
callq 0x5e2c3
cmpl $0x2, 0x421cb(%rip)     # 0x9e58c
jl 0x5c41d
movq %rbx, 0x30(%rsp)
movl 0x8(%r14), %ebx
movl 0xc(%r14), %r13d
movq 0x48(%rsp), %rax
leaq 0x2f764(%rip), %rcx     # 0x8bb40
movq (%rcx,%rax,8), %rbp
callq 0x62cd4
subl %r15d, %eax
cvtsi2ss %rax, %xmm0
divss 0x14b43(%rip), %xmm0    # 0x70f38
cvtss2sd %xmm0, %xmm0
leaq 0x14cf5(%rip), %rdi     # 0x710f5
movl %ebx, %esi
movq 0x30(%rsp), %rbx
movl %r13d, %edx
movq 0x38(%rsp), %r13
movq %rbp, %rcx
movl 0x28(%rsp), %ebp
movb $0x1, %al
callq 0x22427
cmpb $0x0, 0x10(%rsp)
movl 0x18(%rsp), %r15d
je 0x5c498
movl 0x8(%r14), %eax
movq 0x20(%rsp), %rdx
movslq %edx, %rdx
imulq $0x18, %rdx, %rcx
movl %eax, (%r13,%rcx)
movl 0xc(%r14), %eax
movl %eax, 0x4(%r13,%rcx)
movl %r12d, 0x8(%r13,%rcx)
movq %rbx, 0x10(%r13,%rcx)
incl %edx
movq %rdx, 0x20(%rsp)
movq %rbx, %rax
xorl %ebx, %ebx
jmp 0x5c49b
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl 0x18(%r14), %eax
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl %ebp, %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x5972b4(%rip), %rax    # 0x5f3740
callq *0xd0(%rax)
addq $0x20, %rsp
jmp 0x5c4c9
movq %rbx, %rax
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl %ebp, %edx
xorl %r9d, %r9d
pushq %rax
pushq %r12
leaq 0x597286(%rip), %rax    # 0x5f3740
callq *0xf0(%rax)
addq $0x10, %rsp
movl 0x2c(%rsp), %r12d
incl %r15d
jmp 0x5c33f
movq 0x40(%rsp), %rcx
andl $0x0, 0x4(%rcx)
xorl %eax, %eax
cmpl $0x0, 0x1c(%r14)
setne %al
shll $0x3, %eax
movl %eax, 0x4(%rcx)
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x61c0
movl $0x6, (%rbx)
jmp 0x5bf53
movb $0x1, %al
movl %eax, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
jmp 0x5c027