// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movq %rdx, %r14
movq %rdi, %r12
leaq 0x594c3b(%rip), %r15    # 0x5f9998
cmpl $0x0, (%r15)
jg 0x64d96
leaq 0x1184e6(%rip), %rax    # 0x17d250
movl 0x28(%rax), %eax
testl %eax, %eax
je 0x64d8e
movl $0x0, (%r15)
cmpl $0x2, %eax
jl 0x64d96
xorl %ecx, %ecx
movl %eax, %edx
incl %ecx
shrl %edx
cmpl $0x3, %eax
movl %edx, %eax
ja 0x64d81
jmp 0x64d93
movl $0x6, %ecx
movl %ecx, (%r15)
movzwl %si, %ebp
xorl %edx, %edx
movl $0x1908, 0x8(%rsp)      # imm = 0x1908
testb $0x10, %bpl
jne 0x64e21
leaq 0x3f7d0(%rip), %rax     # 0xa4580
cmpl $0x0, (%rax)
movl $0x0, 0x4(%rsp)
je 0x64e29
movl 0x1c(%r14), %ecx
testl %ecx, %ecx
setne %dl
leaq 0x118483(%rip), %rax    # 0x17d250
cmpb $0x0, 0x1a(%rax)
sete %sil
orb %dl, %sil
movl $0x1908, %edx           # imm = 0x1908
movl $0x83f1, %edi           # imm = 0x83F1
cmovnel %edx, %edi
movl %edi, 0x8(%rsp)
movl $0x1, 0x4(%rsp)
movb $0x1, %dl
cmpb $0x1, %sil
jne 0x64e29
xorl %edx, %edx
testl %ecx, %ecx
je 0x64e21
cmpb $0x0, 0x1b(%rax)
movl $0x0, 0x4(%rsp)
je 0x64e29
movl $0x2, 0x4(%rsp)
movl $0x83f3, 0x8(%rsp)      # imm = 0x83F3
movb $0x1, %dl
jmp 0x64e29
movl $0x0, 0x4(%rsp)
cmpq $0x0, 0x10(%rsp)
setne %r13b
andb %dl, %r13b
cmpb $0x1, %r13b
jne 0x64e56
movq 0x10(%rsp), %rcx
movl 0x8(%rsp), %eax
movl %eax, 0x10(%rcx)
movl 0x10(%r14), %eax
movl %eax, 0x14(%rcx)
movl 0x14(%r14), %eax
movl %eax, 0x18(%rcx)
movl %edx, 0xc(%rsp)
cmpl $0x0, (%r12)
jne 0x64e76
leaq 0x5948d8(%rip), %rax    # 0x5f9740
movl $0x1, %edi
movq %r12, %rsi
callq *0xb8(%rax)
leaq 0x5948c3(%rip), %rax    # 0x5f9740
movl (%r12), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%rax)
movl %ebp, %esi
andl $0x1, %esi
movq %r14, %rdi
movl %esi, 0x2c(%rsp)
callq 0x65968
leaq 0x594af8(%rip), %rax    # 0x5f999c
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 0x64eda
movl %esi, %edi
movl %ebx, %ecx
sarl %cl, %edi
cmpl %edx, %edi
jle 0x64ede
incl %ebx
jmp 0x64ec6
movq %r12, 0x48(%rsp)
movl 0x4(%rsp), %eax
movq %rax, 0x30(%rsp)
testl %ebx, %ebx
je 0x65027
incl %ebx
movq 0x10(%rsp), %r12
leaq 0x20(%r12), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
movb %r13b, 0x20(%rsp)
movl 0x8(%r14), %edi
cmpl $0x1, %edi
jg 0x64f27
cmpl $0x2, 0xc(%r14)
jl 0x65035
testb %r13b, %r13b
je 0x65006
movl 0xc(%r14), %esi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
callq 0x67224
movl %eax, %ebp
movslq %eax, %rdi
callq 0x69a0
movq %rax, %r15
callq 0x6c6fa
movl %eax, %r12d
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %r15, %rcx
movl %r13d, %r8d
callq 0x67296
cmpl $0x2, 0x3f619(%rip)     # 0xa458c
jl 0x64fcc
movl 0x8(%r14), %r13d
movl 0xc(%r14), %eax
movl %eax, 0x38(%rsp)
movq 0x30(%rsp), %rax
leaq 0x2cba3(%rip), %rcx     # 0x91b30
movq (%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp)
callq 0x6c6fa
subl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x15d8a(%rip), %xmm0    # 0x7ad38
cvtss2sd %xmm0, %xmm0
leaq 0x15f24(%rip), %rdi     # 0x7aedd
movl %r13d, %esi
movl 0x38(%rsp), %edx
movq 0x50(%rsp), %rcx
movb $0x1, %al
callq 0x24cb4
movl 0x8(%r14), %eax
movq 0x18(%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, 0x18(%rsp)
movq 0x10(%rsp), %r12
movb 0x20(%rsp), %r13b
movq %r14, %rdi
callq 0x65bdc
movq %r14, %rdi
movl 0x2c(%rsp), %esi
callq 0x65968
decl %ebx
cmpl $0x1, %ebx
jg 0x64f13
jmp 0x65035
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r12
movl 0xc(%rsp), %ebx
testb %bl, %bl
je 0x65117
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0x4(%rsp), %r13d
movl %r13d, %edx
callq 0x67224
movl %eax, %r15d
movslq %eax, %rdi
callq 0x69a0
movq %rax, %rbp
callq 0x6c6fa
movl %eax, %ebx
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %rbp, %rcx
movl %r13d, %r8d
callq 0x67296
cmpl $0x2, 0x3f504(%rip)     # 0xa458c
jl 0x650de
movl 0x8(%r14), %eax
movl %eax, 0x20(%rsp)
movl 0xc(%r14), %r13d
leaq 0x2ca93(%rip), %rax     # 0x91b30
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %r12
callq 0x6c6fa
subl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x15c7b(%rip), %xmm0    # 0x7ad38
cvtss2sd %xmm0, %xmm0
leaq 0x15e15(%rip), %rdi     # 0x7aedd
movl 0x20(%rsp), %esi
movl %r13d, %edx
movq %r12, %rcx
movq 0x10(%rsp), %r12
movb $0x1, %al
callq 0x24cb4
testq %r12, %r12
je 0x65154
movl 0x8(%r14), %eax
movq 0x18(%rsp), %rdx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rcx
movl %eax, 0x20(%r12,%rcx,8)
movl 0xc(%r14), %eax
movl %eax, 0x24(%r12,%rcx,8)
movl %r15d, 0x28(%r12,%rcx,8)
movq %rbp, 0x30(%r12,%rcx,8)
incl %edx
movq %rdx, 0x18(%rsp)
xorl %r13d, %r13d
jmp 0x65157
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl 0x18(%r14), %eax
subq $0x8, %rsp
xorl %r13d, %r13d
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl 0x10(%rsp), %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x5945f8(%rip), %rax    # 0x5f9740
callq *0xd0(%rax)
addq $0x20, %rsp
jmp 0x65185
movq %rbp, %r13
movl 0xc(%rsp), %ebx
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl 0x8(%rsp), %edx
xorl %r9d, %r9d
pushq %rbp
pushq %r15
leaq 0x5945c5(%rip), %rax    # 0x5f9740
callq *0xf0(%rax)
addq $0x10, %rsp
leaq 0x20(%r12), %rax
movq %rax, 0x38(%rsp)
movl $0x1, %r15d
cmpl $0x1, 0x8(%r14)
jg 0x651a7
cmpl $0x2, 0xc(%r14)
jl 0x6531d
movq %r14, %rdi
callq 0x65bdc
movq %r14, %rdi
movl 0x2c(%rsp), %esi
callq 0x65968
testb %bl, %bl
je 0x652a9
movl 0x8(%r14), %edi
movl 0xc(%r14), %esi
movl 0x4(%rsp), %edx
callq 0x67224
movl %eax, %ebp
testq %r12, %r12
movq %r13, %rbx
je 0x651e9
movslq %ebp, %rdi
callq 0x69a0
movq %rax, %rbx
callq 0x6c6fa
movl %eax, %r12d
movq (%r14), %rdi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movq %rbx, %rcx
movl 0x4(%rsp), %r8d
callq 0x67296
cmpl $0x2, 0x3f37c(%rip)     # 0xa458c
jl 0x6526a
movl 0x8(%r14), %r13d
movq %rbx, 0x20(%rsp)
movl 0xc(%r14), %ebx
movq 0x30(%rsp), %rax
leaq 0x2c905(%rip), %rcx     # 0x91b30
movq (%rcx,%rax,8), %rax
movq %rax, 0x40(%rsp)
callq 0x6c6fa
subl %r12d, %eax
cvtsi2ss %rax, %xmm0
divss 0x15aef(%rip), %xmm0    # 0x7ad38
cvtss2sd %xmm0, %xmm0
leaq 0x15c89(%rip), %rdi     # 0x7aedd
movl %r13d, %esi
movl %ebx, %edx
movq 0x20(%rsp), %rbx
movq 0x40(%rsp), %rcx
movb $0x1, %al
callq 0x24cb4
movq 0x10(%rsp), %r12
testq %r12, %r12
je 0x652e4
movl 0x8(%r14), %eax
movq 0x18(%rsp), %rdx
movslq %edx, %rdx
leaq (%rdx,%rdx,2), %rcx
movq 0x38(%rsp), %rsi
movl %eax, (%rsi,%rcx,8)
movl 0xc(%r14), %eax
movl %eax, 0x4(%rsi,%rcx,8)
movl %ebp, 0x8(%rsi,%rcx,8)
movq %rbx, 0x10(%rsi,%rcx,8)
incl %edx
movq %rdx, 0x18(%rsp)
xorl %r13d, %r13d
jmp 0x652e7
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl 0x18(%r14), %eax
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl 0x10(%rsp), %edx
xorl %r9d, %r9d
pushq (%r14)
pushq $0x1401                 # imm = 0x1401
pushq %rax
leaq 0x594468(%rip), %rax    # 0x5f9740
callq *0xd0(%rax)
addq $0x20, %rsp
jmp 0x65315
movq %rbx, %r13
movl 0x8(%r14), %ecx
movl 0xc(%r14), %r8d
movl $0xde1, %edi            # imm = 0xDE1
movl %r15d, %esi
movl 0x8(%rsp), %edx
xorl %r9d, %r9d
pushq %rbx
pushq %rbp
leaq 0x594439(%rip), %rax    # 0x5f9740
callq *0xf0(%rax)
addq $0x10, %rsp
movl 0xc(%rsp), %ebx
incl %r15d
jmp 0x65195
movq 0x48(%rsp), %rcx
movl $0x0, 0x4(%rcx)
xorl %eax, %eax
cmpl $0x0, 0x1c(%r14)
setne %al
shll $0x3, %eax
movl %eax, 0x4(%rcx)
testq %r13, %r13
je 0x65354
movq %r13, %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