// File: /workspace/github_cpp_makefile_star2_O2/ticky[P]ps2iconsys/src/obj_loader.cpp
// Function: OBJ_FileLoader::WriteFile(char const*) const
void OBJ_FileLoader::WriteFile(char const* fname) const {
	std::ofstream fout(fname, std::ios_base::out);
	if( fout.fail() ) {
		throw( Ghulbus::gbException( Ghulbus::gbException::GB_FAILED,
			                         "Output OBJ file could not be opened" ) );
	}
	//again we need counters since obj doesn't reset indices between meshes:
	int vert_counter=0, normal_counter=0, texture_counter=0;
	int vert_base=0, normal_base=0, texture_base=0;
	int current_smooth_group = 0;
	
	fout.precision(6);
	fout << std::fixed;
	//file header:
	fout << "# OBJ File created by PS2IconSys Viewer" << std::endl
		 << "#  http://www.ghulbus-inc.de/" << std::endl
		 << "#" << std::endl;

	for(int i=0; i<GetNMeshes(); i++) {
		OBJ_Mesh const* mesh = GetMesh(i);
		//object header:
		fout << "# object " << mesh->GetName() << " to come" << std::endl
			 << "#" << std::endl;
		
		//vertex data:
		for(int j=0; j<mesh->GetNVertices(); j++) {
			vert_counter++;
			fout << "v  " << *(mesh->GetVertexX(j)) << " " << *(mesh->GetVertexY(j)) << " "
				<< *(mesh->GetVertexZ(j)) << std::endl;
		}
		fout << "# " << mesh->GetNVertices() << " vertices" << std::endl << std::endl;
		//texture vertex data:
		for(int j=0; j<mesh->GetNTexture(); j++) {
			texture_counter++;
			fout << "vt  " << *(mesh->GetTextureX(j)) << " " << *(mesh->GetTextureY(j)) << " "
				<< *(mesh->GetTextureZ(j)) << std::endl;
		}
		fout << "# " << mesh->GetNTexture() << " texture vertices" << std::endl << std::endl;
		//vertex normal data:
		for(int j=0; j<mesh->GetNNormals(); j++) {
			normal_counter++;
			fout << "vn  " << *(mesh->GetNormalX(j)) << " " << *(mesh->GetNormalY(j)) << " "
				<< *(mesh->GetNormalZ(j)) << std::endl;
		}
		fout << "# " << mesh->GetNNormals() << " vertex normals" << std::endl << std::endl;

		//face list:
		fout << "g " << mesh->GetName() << std::endl;
		current_smooth_group = mesh->GetFace(0)->smoothing_group;
		fout << "s " << current_smooth_group << std::endl;
		for(int j=0; j<mesh->GetNFaces(); j++) {
			OBJ_Mesh::Face const* current_face = mesh->GetFace(j);
			if(current_face->smoothing_group != current_smooth_group) {
				current_smooth_group = current_face->smoothing_group;
				fout << "s " << current_smooth_group << std::endl;
			}
			fout << "f " << (current_face->vert1 + vert_base + 1) << "/" 
				<< (current_face->texture1 + texture_base + 1) << "/" 
				<< (current_face->normal1 + normal_base + 1)   << " " 
				<< (current_face->vert2 + vert_base + 1)       << "/" 
				<< (current_face->texture2 + texture_base + 1) << "/" 
				<< (current_face->normal2 + normal_base + 1)   << " " 
				<< (current_face->vert3 + vert_base + 1)       << "/" 
				<< (current_face->texture3 + texture_base + 1) << "/" 
				<< (current_face->normal3 + normal_base + 1)   << std::endl;
		}
		fout << "# " << mesh->GetNFaces() << " faces" << std::endl << std::endl;

		//adjust base counters:
		vert_base = vert_counter;
		normal_base = normal_counter;
		texture_base = texture_counter;
		fout << "g" << std::endl;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x18(%rsp)
leaq 0x28(%rsp), %rbx
pushq $0x10
popq %rdx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rcx
testb $0x5, 0x48(%rsp,%rcx)
jne 0x320f
movq $0x6, 0x30(%rsp,%rcx)
movq -0x18(%rax), %rax
movl $0xfffffefb, %ecx       # imm = 0xFFFFFEFB
andl 0x40(%rsp,%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x40(%rsp,%rax)
leaq 0x64db(%rip), %rsi      # 0x9065
leaq 0x28(%rsp), %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
leaq 0x64ea(%rip), %rsi      # 0x908d
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
leaq 0x70a4(%rip), %rsi      # 0x9c5e
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
xorl %edx, %edx
movl $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rdx
jge 0x31f3
movq %rdx, 0x20(%rsp)
movq (%rax,%rdx,8), %rbx
leaq 0x28(%rsp), %rdi
leaq 0x6498(%rip), %rsi      # 0x90ab
callq 0x2190
movq 0x60(%rbx), %rsi
testq %rsi, %rsi
leaq 0x71e7(%rip), %rcx      # 0x9e0d
cmoveq %rcx, %rsi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
leaq 0x6479(%rip), %rsi      # 0x90b5
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
leaq 0x700b(%rip), %rsi      # 0x9c5e
callq 0x2190
movq %rax, %rdi
callq 0x20d0
xorl %ecx, %ecx
pushq $0x3
popq %r15
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
divq %r15
movl %ecx, 0x8(%rsp)
cmpl %eax, %ecx
jge 0x2d15
leaq 0x28(%rsp), %rdi
leaq 0x6430(%rip), %rsi      # 0x90be
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x328c
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x70ba(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x32de
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x7090(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x3334
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x20d0
movl 0x8(%rsp), %ecx
incl %ecx
jmp 0x2c66
leaq 0x28(%rsp), %rdi
leaq 0x63a1(%rip), %rsi      # 0x90c2
callq 0x2190
movq %rax, %rdi
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
movl %eax, %esi
callq 0x2300
movq %rax, %rdi
leaq 0x6385(%rip), %rsi      # 0x90d2
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
movl 0x8(%rsp), %eax
addl 0x10(%rsp), %eax
movl %eax, 0x8(%rsp)
xorl %ecx, %ecx
pushq $0x3
popq %r15
movq 0x38(%rbx), %rax
subq 0x30(%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
divq %r15
movl %ecx, 0x4(%rsp)
cmpl %eax, %ecx
jge 0x2e24
leaq 0x28(%rsp), %rdi
leaq 0x6328(%rip), %rsi      # 0x90c5
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x33a0
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x6fab(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x33f2
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x6f81(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x3448
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x20d0
movl 0x4(%rsp), %ecx
incl %ecx
jmp 0x2d74
leaq 0x28(%rsp), %rdi
leaq 0x6292(%rip), %rsi      # 0x90c2
callq 0x2190
movq %rax, %rdi
movq 0x38(%rbx), %rax
subq 0x30(%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
movl %eax, %esi
callq 0x2300
movq %rax, %rdi
leaq 0x626d(%rip), %rsi      # 0x90ca
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
movl 0x4(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
xorl %r15d, %r15d
movq 0x20(%rbx), %rax
subq 0x18(%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
cmpl %eax, %r15d
jge 0x2f2b
leaq 0x28(%rsp), %rdi
leaq 0x6232(%rip), %rsi      # 0x90dc
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl %r15d, %esi
callq 0x34b4
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x6e9f(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3506
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
leaq 0x6e76(%rip), %rsi      # 0x9d72
callq 0x2190
movq %rax, %r14
movq %rbx, %rdi
movl %r15d, %esi
callq 0x355c
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x20d0
incl %r15d
jmp 0x2e81
leaq 0x28(%rsp), %rdi
leaq 0x618b(%rip), %rsi      # 0x90c2
callq 0x2190
movq %rax, %rdi
movq 0x20(%rbx), %rax
subq 0x18(%rbx), %rax
sarq $0x3, %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
movl %eax, %esi
callq 0x2300
movq %rax, %rdi
leaq 0x617d(%rip), %rsi      # 0x90e1
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
leaq 0x28(%rsp), %rdi
leaq 0x616c(%rip), %rsi      # 0x90f1
callq 0x2190
movq 0x60(%rbx), %rsi
testq %rsi, %rsi
leaq 0x6e75(%rip), %rcx      # 0x9e0d
cmoveq %rcx, %rsi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x35b2
movl 0x24(%rax), %eax
movl %eax, 0x14(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x6d1a(%rip), %rsi      # 0x9ce3
callq 0x2190
movq %rax, %rdi
movl 0x14(%rsp), %esi
callq 0x2300
movq %rax, %rdi
callq 0x20d0
addl %r12d, %r15d
incl 0x10(%rsp)
incl 0xc(%rsp)
incl %r12d
xorl %r14d, %r14d
movq 0x50(%rbx), %rax
subq 0x48(%rbx), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpl %eax, %r14d
jge 0x3170
movq %rbx, %rdi
movl %r14d, %esi
callq 0x35b2
movq %rax, %r13
movl 0x24(%rax), %ebp
cmpl 0x14(%rsp), %ebp
je 0x304a
leaq 0x28(%rsp), %rdi
leaq 0x6cb4(%rip), %rsi      # 0x9ce3
callq 0x2190
movq %rax, %rdi
movl %ebp, %esi
callq 0x2300
movq %rax, %rdi
callq 0x20d0
movl %ebp, 0x14(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x609e(%rip), %rsi      # 0x90f4
callq 0x2190
movl (%r13), %esi
addl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x6034(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0x18(%r13), %esi
addl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x6015(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0xc(%r13), %esi
addl %r12d, %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x6cc0(%rip), %rsi      # 0x9d72
callq 0x2190
movl 0x4(%r13), %esi
addl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x5fd8(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0x1c(%r13), %esi
addl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x5fb9(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0x10(%r13), %esi
addl %r12d, %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x6c64(%rip), %rsi      # 0x9d72
callq 0x2190
movl 0x8(%r13), %esi
addl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x5f7c(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0x20(%r13), %esi
addl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
leaq 0x5f5d(%rip), %rsi      # 0x90a9
callq 0x2190
movl 0x14(%r13), %esi
addl %r12d, %esi
movq %rax, %rdi
callq 0x2300
movq %rax, %rdi
callq 0x20d0
incl %r14d
jmp 0x2ff3
leaq 0x28(%rsp), %rdi
leaq 0x5f46(%rip), %rsi      # 0x90c2
callq 0x2190
movq %rax, %rdi
movq 0x50(%rbx), %rax
subq 0x48(%rbx), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movl %eax, %esi
callq 0x2300
movq %rax, %rdi
leaq 0x5f52(%rip), %rsi      # 0x90f7
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
callq 0x20d0
leaq 0x28(%rsp), %rdi
leaq 0x5f38(%rip), %rsi      # 0x90fe
callq 0x2190
movq %rax, %rdi
callq 0x20d0
movq 0x20(%rsp), %rdx
incq %rdx
movl 0x8(%rsp), %eax
movl %eax, 0x10(%rsp)
movl %r15d, %r12d
movl 0x4(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0x2bdf
leaq 0x28(%rsp), %rdi
callq 0x2290
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x18
popq %rdi
callq 0x20e0
movq %rax, %rbx
leaq 0x5e20(%rip), %rdx      # 0x9041
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x7628
leaq 0xbb65(%rip), %rsi      # 0xed98
leaq 0x441a(%rip), %rdx      # 0x7654
movq %rbx, %rdi
callq 0x22f0
jmp 0x324e
jmp 0x324e
jmp 0x324e
jmp 0x324e
jmp 0x324e
jmp 0x324e
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2290
movq %rbx, %rdi
callq 0x2320
nop