// File: /workspace/github_c_makefile_star2_O2/vlenhart[P]q3bsp/vformat.cpp
// Function: CheckTexture(char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void CheckTexture(const char *baseName, String &textureFileName)
{

	const char *ext = strrchr(baseName,'.');

	// check if jpg exists
	textureFileName=baseName;
	if (!ext) {
		textureFileName+=".jpg";

		if (ExistsFile(textureFileName.c_str()))
			return;
	}


	// check if png exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".png";

	if (ExistsFile(textureFileName.c_str()))
		return;

	// check if tga exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".tga";

	if (ExistsFile(textureFileName.c_str())) {
		// convert tga to png
		String outFileName=baseName;
		if (ext)  // erase ext 
			outFileName.erase(outFileName.size()-strlen(ext),strlen(ext));

		outFileName+=".png";

		// bmp.TGAToPNG(textureFileName.c_str(),outFileName.c_str());
		int width, height, comp;
		unsigned char *data = stbi_load(textureFileName.c_str(), &width, &height, &comp	, 0);
		stbi_write_png(outFileName.c_str(), width, height, comp, data, width*comp);
		stbi_image_free(data);

		textureFileName = outFileName;
		return;
	
	}

	// check if jpg exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".jpg";

	if (ExistsFile(textureFileName.c_str()))
		return;

	printf("Texture file not found:'%s'\n",baseName);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rsi, %rbx
movq %rdi, %r14
pushq $0x2e
popq %rsi
callq 0x2260
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2430
testq %r15, %r15
je 0xd691
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2430
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2110
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
jmp 0xd6bb
leaq 0x4120(%rip), %rsi      # 0x117b8
movq %rbx, %rdi
callq 0x2420
movq (%rbx), %rdi
callq 0xd608
testb %al, %al
jne 0xd809
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2430
leaq 0x3818(%rip), %rsi      # 0x10eda
movq %rbx, %rdi
callq 0x2420
movq (%rbx), %rdi
callq 0xd608
testb %al, %al
jne 0xd809
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2430
testq %r15, %r15
je 0xd707
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2110
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x40af(%rip), %rsi      # 0x117bd
movq %rbx, %rdi
callq 0x2420
movq (%rbx), %rdi
callq 0xd608
testb %al, %al
je 0xd7c1
leaq -0x50(%rbp), %rdi
leaq -0x24(%rbp), %rdx
movq %r14, %rsi
callq 0x5b0c
testq %r15, %r15
je 0xd75c
leaq -0x50(%rbp), %r14
movq 0x8(%r14), %r12
movq %r15, %rdi
callq 0x2110
subq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x3777(%rip), %rsi      # 0x10eda
leaq -0x50(%rbp), %rdi
callq 0x2420
movq (%rbx), %rdi
leaq -0x24(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x28(%rbp), %rcx
xorl %r8d, %r8d
callq 0x270f
movq %rax, %r14
movq -0x50(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x2c(%rbp), %edx
movl -0x28(%rbp), %ecx
movl %ecx, %r9d
imull %esi, %r9d
movq %rax, %r8
callq 0x5067
movq %r14, %rdi
callq 0x26f9
leaq -0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x2140
leaq -0x50(%rbp), %rdi
callq 0x2120
jmp 0xd809
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2430
testq %r15, %r15
je 0xd7ee
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2110
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x3fc3(%rip), %rsi      # 0x117b8
movq %rbx, %rdi
callq 0x2420
movq (%rbx), %rdi
callq 0xd608
testb %al, %al
je 0xd816
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3fa5(%rip), %rdi      # 0x117c2
movq %r14, %rsi
xorl %eax, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2050
jmp 0xd835
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x23e0
nop