// File: /workspace/github_c_makefile_star2_O2/vlenhart[P]q3bsp/q3bsp.cpp
// Function: QuakeFace::Build(std::vector<unsigned short, std::allocator<unsigned short>> const&, std::vector<QuakeVertex, std::allocator<QuakeVertex>> const&, std::vector<ShaderReference, std::allocator<ShaderReference>>&, StringRef const&, StringRef const&, StringRef const&, VertexMesh&)
void QuakeFace::Build(const UShortVector &elements,
                      const QuakeVertexVector &vertices,
                      ShaderReferenceVector &shaders,
                      const StringRef &lmPrefix,
                      const StringRef &sourcename,
                      const StringRef &code,
                      VertexMesh &mesh)
{
  assert( mVcount < 1024 );
  assert( mShader >= 0 && mShader < shaders.size() );
  StringRef mat;


  static StringRef oname = StringDict::gStringDict().Get("outside");
  static StringRef inside  = StringDict::gStringDict().Get("inside");

  StringRef name = inside;


  int type = 0;

  LightMapVertex verts[1024];

  for (int i=0; i<mVcount; i++)
  {
    vertices[ i+mFirstVertice ].Get(verts[i]);
  }

  char scratch[256];
  char texname[256];

  //shaders[ mShader ].GetTextureName(texname);
  shaders[ mShader ].GetTextureFullName(texname); // get full name with path

/* hg why ?
  if ( strcmp("toxicskytim_ctf1",texname) == 0 ) 
	  return;
*/

#if 0
  if ( mLightmap < 0 ) 
	  return;
#endif

  StringRef basetexture = StringDict::gStringDict().Get(texname);

  QuakeShader *shader = QuakeShaderFactory::gQuakeShaderFactory().Locate(basetexture);

  if ( shader )
  {
    //shader->GetBaseTexture(basetexture);
    //printf("Shader uses texture: %s\n",basetexture);
  }
  else {

	  if ( strcmp("textures/liquids/lavahell_1000",basetexture) == 0 )  {
	  }
	  if ( strstr(basetexture,"lavahell") )  {
	    printf("shader for : %s\n",basetexture.Get());
	  }

	// try to load a matching shader from disk 
    shaders[mShader].GetShaderFileName(scratch); 
	strcat(scratch,".shader");
    mat = StringDict::gStringDict().Get(scratch);

	if (!QuakeShaderFactory::gQuakeShaderFactory().ShaderFileLoaded(mat)
		&& 	QuakeShaderFactory::gQuakeShaderFactory().AddShader(mat)) 
	{
	     shader = QuakeShaderFactory::gQuakeShaderFactory().Locate(basetexture);
	}

	if (!shader) {
	    printf("No shader for : '%s'\n",basetexture.Get());

	}
  }	
  
  // geometry sorted by shader  string

  if ( mLightmap < 0 ) // no lightmap
	  sprintf(scratch,"%s+",basetexture.Get());
  else	
	sprintf(scratch,"%s+%slm%s%02d",basetexture.Get(),lmPrefix.Get(),
          code.Get(),
          mLightmap);

  mat = StringDict::gStringDict().Get(scratch);

  char lmapspace[256];
  sprintf(lmapspace,"lm%s%02d",code.Get(),mLightmap);
  StringRef lmap = SGET(lmapspace);

  switch ( mType )
  {
    case FACETYPE_NORMAL:
    case FACETYPE_TRISURF:
          if ( 1 )
          {
            assert( (mEcount%3) == 0 );
            const unsigned short *idx = &elements[mFirstElement];
            int tcount = mEcount/3;
            for (int j=0; j<tcount; j++)
            {
              int i1 = idx[0];
              int i2 = idx[1];
              int i3 = idx[2];

              mesh.AddTri(mat,verts[i1], verts[i2], verts[i3] );

              idx+=3;
            }
          }
          break;
    case FACETYPE_MESH:
        if ( 1 )
        {
          PatchSurface surface(verts,mVcount,mControlX,mControlY);
          const LightMapVertex *vlist = surface.GetVerts();
          const unsigned short *indices = surface.GetIndices();
          int tcount = surface.GetIndiceCount()/3;

          for (int j=0; j<tcount; j++)
          {

            int i1 = *indices++;
            int i2 = *indices++;
            int i3 = *indices++;

            mesh.AddTri(mat,vlist[i1], vlist[i2], vlist[i3] );

          }
        }
        break;
    case FACETYPE_FLARE:
      break;
    default:
//      assert( 0 );
      break;
  }
  if (mesh.mLastSection && shader) 
	  mesh.mLastSection->SetShader(shader);


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa348, %rsp           # imm = 0xA348
movq %r8, -0x50(%rbp)
cmpl $0x400, 0x14(%rdi)      # imm = 0x400
jge 0x8ce1
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r15
movl 0x4(%rdi), %ecx
movq 0x8(%r14), %rax
subq (%r14), %rax
pushq $0x48
popq %rsi
cqto
idivq %rsi
cmpq %rcx, %rax
jbe 0x8d00
leaq 0x8245(%rip), %rax      # 0x10bb2
movq %rax, -0x30(%rbp)
movb 0x10d31(%rip), %al      # 0x196a8
testb %al, %al
je 0x8d1f
movq %r12, -0x58(%rbp)
movq %r14, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movb 0x10d27(%rip), %al      # 0x196b8
testb %al, %al
je 0x8d5f
movq 0x18(%rbp), %rbx
movl 0x14(%r15), %eax
movslq 0x10(%r15), %rcx
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
imulq $0x2c, %rcx, %r12
movq -0x58(%rbp), %rcx
addq (%rcx), %r12
imulq $0x28, %rax, %r14
cmpq %r13, %r14
je 0x89df
leaq -0xa370(,%r13), %rsi
addq %rbp, %rsi
movq %r12, %rdi
callq 0x8888
addq $0x2c, %r12
addq $0x28, %r13
jmp 0x89bd
movl 0x4(%r15), %eax
imulq $0x48, %rax, %rsi
movq -0x40(%rbp), %r14
addq (%r14), %rsi
leaq -0x370(%rbp), %r12
movq %r12, %rdi
callq 0x21f0
callq 0x5aca
movq %rax, %rdi
movq %r12, %rsi
callq 0x5bae
leaq -0x38(%rbp), %r12
movq %rax, (%r12)
callq 0x9906
movq %rax, %rdi
movq %r12, %rsi
callq 0xb806
testq %rax, %rax
je 0x8a32
movq %rax, %r13
jmp 0x8afe
movq -0x38(%rbp), %r12
leaq 0x85d5(%rip), %rsi      # 0x11012
movq %r12, %rdi
callq 0x20a0
testq %rax, %rax
je 0x8a5b
leaq 0x85ca(%rip), %rdi      # 0x1101b
movq %r12, %rsi
xorl %eax, %eax
callq 0x2050
movl 0x4(%r15), %eax
imulq $0x48, %rax, %rdi
addq (%r14), %rdi
leaq -0x170(%rbp), %r14
movq %r14, %rsi
callq 0x8df0
movq %r14, %rdi
callq 0x2110
movabsq $0x7265646168732e, %rcx # imm = 0x7265646168732E
movq %rcx, -0x170(%rbp,%rax)
callq 0x5aca
movq %rax, %rdi
movq %r14, %rsi
callq 0x5bae
leaq -0x30(%rbp), %r14
movq %rax, (%r14)
callq 0x9906
movq %rax, %rdi
movq %r14, %rsi
callq 0xb830
testb %al, %al
jne 0x8ae9
callq 0x9906
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0xb602
testb %al, %al
je 0x8ae9
callq 0x9906
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0xb806
testq %rax, %rax
jne 0x8a2a
movq -0x38(%rbp), %rsi
leaq 0x8538(%rip), %rdi      # 0x1102c
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x2050
movq 0x10(%rbp), %r14
movl 0x20(%r15), %r9d
movq -0x38(%rbp), %rdx
testl %r9d, %r9d
js 0x8b30
movq -0x50(%rbp), %rax
movq (%rax), %rcx
movq (%r14), %r8
leaq 0x8526(%rip), %rsi      # 0x11046
leaq -0x170(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
jmp 0x8b45
leaq 0x850b(%rip), %rsi      # 0x11042
leaq -0x170(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
callq 0x5aca
leaq -0x170(%rbp), %rsi
movq %rax, %rdi
callq 0x5bae
movq %rax, -0x30(%rbp)
movq (%r14), %rdx
movl 0x20(%r15), %ecx
leaq 0x84e0(%rip), %rsi      # 0x1104b
leaq -0x270(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2080
callq 0x5aca
movq %rax, %rdi
movq %r14, %rsi
callq 0x5bae
movl 0xc(%r15), %eax
cmpl $0x3, %eax
je 0x8ba7
cmpl $0x2, %eax
je 0x8c2a
cmpl $0x1, %eax
jne 0x8cb5
movl 0x1c(%r15), %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
testl %edx, %edx
jne 0x8d9f
movslq 0x18(%r15), %r15
addq %r15, %r15
movq -0x48(%rbp), %rcx
addq (%rcx), %r15
xorl %r12d, %r12d
testl %eax, %eax
cmovgl %eax, %r12d
leaq -0x30(%rbp), %r14
subl $0x1, %r12d
jb 0x8cb5
movzwl (%r15), %eax
movzwl 0x2(%r15), %ecx
movzwl 0x4(%r15), %esi
imulq $0x28, %rax, %rax
leaq (%rax,%rbp), %rdx
addq $-0xa370, %rdx          # imm = 0xFFFF5C90
imulq $0x28, %rcx, %rax
leaq (%rax,%rbp), %rcx
addq $-0xa370, %rcx          # imm = 0xFFFF5C90
imulq $0x28, %rsi, %rax
leaq (%rax,%rbp), %r8
addq $-0xa370, %r8           # imm = 0xFFFF5C90
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd96a
addq $0x6, %r15
jmp 0x8bd4
movq %r13, -0x40(%rbp)
movl 0x14(%r15), %edx
movl 0x64(%r15), %ecx
movl 0x68(%r15), %r8d
leaq -0x70(%rbp), %r14
leaq -0xa370(%rbp), %rsi
movq %r14, %rdi
callq 0x6d74
movq 0x8(%r14), %r15
movq 0x10(%r14), %r12
movl (%r14), %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
xorl %r13d, %r13d
testl %eax, %eax
cmovgl %eax, %r13d
leaq -0x30(%rbp), %r14
subl $0x1, %r13d
jb 0x8ca8
movzwl (%r12), %eax
movzwl 0x2(%r12), %ecx
movzwl 0x4(%r12), %esi
imulq $0x28, %rax, %rdx
addq %r15, %rdx
imulq $0x28, %rcx, %rcx
addq %r15, %rcx
imulq $0x28, %rsi, %r8
addq %r15, %r8
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd96a
addq $0x6, %r12
jmp 0x8c6b
leaq -0x70(%rbp), %rdi
callq 0x719c
movq -0x40(%rbp), %r13
movq (%rbx), %rax
testq %rax, %rax
sete %cl
testq %r13, %r13
sete %dl
orb %cl, %dl
jne 0x8ccf
movq %r13, 0x80(%rax)
addq $0xa348, %rsp           # imm = 0xA348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x823c(%rip), %rdi      # 0x10f24
leaq 0x7f22(%rip), %rsi      # 0x10c11
leaq 0x823d(%rip), %rcx      # 0x10f33
movl $0x207, %edx            # imm = 0x207
callq 0x2180
leaq 0x82d3(%rip), %rdi      # 0x10fda
leaq 0x7f03(%rip), %rsi      # 0x10c11
leaq 0x821e(%rip), %rcx      # 0x10f33
movl $0x208, %edx            # imm = 0x208
callq 0x2180
leaq 0x10982(%rip), %rdi     # 0x196a8
callq 0x2400
testl %eax, %eax
je 0x897f
callq 0x5aca
leaq 0x82c4(%rip), %rsi      # 0x11003
movq %rax, %rdi
callq 0x5bae
movq %rax, 0x10952(%rip)     # 0x196a0
leaq 0x10953(%rip), %rdi     # 0x196a8
callq 0x2170
jmp 0x897f
leaq 0x10952(%rip), %rdi     # 0x196b8
callq 0x2400
testl %eax, %eax
je 0x8999
callq 0x5aca
leaq 0x828c(%rip), %rsi      # 0x1100b
movq %rax, %rdi
callq 0x5bae
movq %rax, 0x10922(%rip)     # 0x196b0
leaq 0x10923(%rip), %rdi     # 0x196b8
callq 0x2170
jmp 0x8999
leaq 0x82ae(%rip), %rdi      # 0x11054
leaq 0x7e64(%rip), %rsi      # 0x10c11
leaq 0x817f(%rip), %rcx      # 0x10f33
movl $0x262, %edx            # imm = 0x262
callq 0x2180
movq %rax, %rbx
leaq 0x108f0(%rip), %rdi     # 0x196b8
jmp 0x8dd4
movq %rax, %rbx
leaq 0x108d4(%rip), %rdi     # 0x196a8
callq 0x2160
jmp 0x8de7
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x719c
movq %rbx, %rdi
callq 0x23e0
nop