// File: /workspace/github_c_makefile_star2_O1/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 $0xa338, %rsp           # imm = 0xA338
movq %r8, -0x40(%rbp)
cmpl $0x400, 0x14(%rdi)      # imm = 0x400
jge 0x9fc0
movq %rcx, %r13
movq %rdx, %r12
movq %rdi, %r14
movl 0x4(%rdi), %eax
movq 0x8(%rcx), %rcx
subq (%r13), %rcx
sarq $0x3, %rcx
movabsq $-0x71c71c71c71c71c7, %rdx # imm = 0x8E38E38E38E38E39
imulq %rcx, %rdx
cmpq %rax, %rdx
jbe 0x9fdf
movq %rsi, %rbx
leaq 0x8f7e(%rip), %rax      # 0x12bb2
movq %rax, -0x30(%rbp)
movb 0xfa4a(%rip), %al       # 0x19688
testb %al, %al
je 0x9ffe
movq %rbx, -0x48(%rbp)
movb 0xfa48(%rip), %al       # 0x19698
testb %al, %al
je 0xa03e
movslq 0x14(%r14), %r15
testq %r15, %r15
jle 0x9c8c
movslq 0x10(%r14), %rax
imulq $0x2c, %rax, %rbx
addq (%r12), %rbx
leaq -0xa360(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9b3e
addq $0x2c, %rbx
addq $0x28, %r12
decq %r15
jne 0x9c74
movl 0x4(%r14), %eax
leaq (%rax,%rax,8), %rsi
shlq $0x3, %rsi
addq (%r13), %rsi
leaq -0x360(%rbp), %rbx
movq %rbx, %rdi
callq 0x21f0
callq 0x697e
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a12
leaq -0x38(%rbp), %rbx
movq %rax, (%rbx)
callq 0xac24
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc9d0
movq %rax, %r12
testq %rax, %rax
jne 0x9db3
movq -0x38(%rbp), %rbx
leaq 0x9339(%rip), %rsi      # 0x13022
movq %rbx, %rdi
callq 0x20a0
testq %rax, %rax
je 0x9d07
leaq 0x932e(%rip), %rdi      # 0x1302b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movl 0x4(%r14), %eax
leaq (%rax,%rax,8), %rdi
shlq $0x3, %rdi
addq (%r13), %rdi
leaq -0x160(%rbp), %rbx
movq %rbx, %rsi
callq 0xa0ce
movq %rbx, %rdi
callq 0x2120
movabsq $0x7265646168732e, %rcx # imm = 0x7265646168732E
movq %rcx, -0x160(%rbp,%rax)
callq 0x697e
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a12
leaq -0x30(%rbp), %rbx
movq %rax, (%rbx)
callq 0xac24
movq %rax, %rdi
movq %rbx, %rsi
callq 0xca28
testb %al, %al
jne 0x9d96
callq 0xac24
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0xc6d8
testb %al, %al
je 0x9d96
callq 0xac24
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0xc9d0
movq %rax, %r12
jmp 0x9d99
xorl %r12d, %r12d
testq %r12, %r12
jne 0x9db3
movq -0x38(%rbp), %rsi
leaq 0x9293(%rip), %rdi      # 0x1303c
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2050
movq 0x10(%rbp), %rbx
movl 0x20(%r14), %r9d
movq -0x38(%rbp), %rdx
testl %r9d, %r9d
js 0x9de5
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq (%rbx), %r8
leaq 0x9281(%rip), %rsi      # 0x13056
leaq -0x160(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
jmp 0x9dfa
leaq 0x9266(%rip), %rsi      # 0x13052
leaq -0x160(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
movq 0x18(%rbp), %r15
callq 0x697e
leaq -0x160(%rbp), %rsi
movq %rax, %rdi
callq 0x6a12
movq %rax, -0x30(%rbp)
movq (%rbx), %rdx
movl 0x20(%r14), %ecx
leaq 0x9237(%rip), %rsi      # 0x1305b
leaq -0x260(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2080
callq 0x697e
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a12
movl 0xc(%r14), %eax
cmpl $0x3, %eax
je 0x9e60
cmpl $0x2, %eax
je 0x9efd
cmpl $0x1, %eax
jne 0x9f94
movslq 0x1c(%r14), %rax
imull $0xaaaaaaab, %eax, %ecx # imm = 0xAAAAAAAB
addl $0x2aaaaaaa, %ecx       # imm = 0x2AAAAAAA
cmpl $0x55555555, %ecx       # imm = 0x55555555
jae 0xa07e
cmpl $0x3, %eax
jl 0x9f94
imulq $0x55555556, %rax, %r13 # imm = 0x55555556
movq %r13, %rax
shrq $0x3f, %rax
shrq $0x20, %r13
addl %eax, %r13d
movslq 0x18(%r14), %r14
addq %r14, %r14
movq -0x48(%rbp), %rax
addq (%rax), %r14
leaq -0x30(%rbp), %rbx
movzwl (%r14), %eax
movzwl 0x2(%r14), %ecx
movzwl 0x4(%r14), %esi
leaq (%rax,%rax,4), %rax
leaq -0xa360(,%rax,8), %rdx
addq %rbp, %rdx
leaq (%rcx,%rcx,4), %rax
leaq -0xa360(%rbp,%rax,8), %rcx
leaq (%rsi,%rsi,4), %rax
leaq -0xa360(,%rax,8), %r8
addq %rbp, %r8
movq %r15, %rdi
movq %rbx, %rsi
callq 0xedd6
addq $0x6, %r14
decl %r13d
jne 0x9eac
jmp 0x9f94
movq %r12, -0x40(%rbp)
movl 0x14(%r14), %edx
movl 0x64(%r14), %ecx
movl 0x68(%r14), %r8d
leaq -0x60(%rbp), %rbx
leaq -0xa360(%rbp), %rsi
movq %rbx, %rdi
callq 0x7d20
movslq (%rbx), %rax
cmpq $0x3, %rax
jl 0x9f87
movq -0x58(%rbp), %r14
imulq $0x55555556, %rax, %r13 # imm = 0x55555556
movq %r13, %rax
shrq $0x3f, %rax
shrq $0x20, %r13
addl %eax, %r13d
movq -0x50(%rbp), %r12
leaq -0x30(%rbp), %rbx
movzwl (%r12), %eax
movzwl 0x2(%r12), %ecx
movzwl 0x4(%r12), %esi
leaq (%rax,%rax,4), %rax
leaq (%r14,%rax,8), %rdx
leaq (%rcx,%rcx,4), %rax
leaq (%r14,%rax,8), %rcx
leaq (%rsi,%rsi,4), %rax
leaq (%r14,%rax,8), %r8
movq %r15, %rdi
movq %rbx, %rsi
callq 0xedd6
addq $0x6, %r12
decl %r13d
jne 0x9f4a
leaq -0x60(%rbp), %rdi
callq 0x81c2
movq -0x40(%rbp), %r12
movq (%r15), %rax
testq %rax, %rax
sete %cl
testq %r12, %r12
sete %dl
orb %cl, %dl
jne 0x9fae
movq %r12, 0x80(%rax)
addq $0xa338, %rsp           # imm = 0xA338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8f6d(%rip), %rdi      # 0x12f34
leaq 0x8c53(%rip), %rsi      # 0x12c21
leaq 0x8f6e(%rip), %rcx      # 0x12f43
movl $0x207, %edx            # imm = 0x207
callq 0x2180
leaq 0x9004(%rip), %rdi      # 0x12fea
leaq 0x8c34(%rip), %rsi      # 0x12c21
leaq 0x8f4f(%rip), %rcx      # 0x12f43
movl $0x208, %edx            # imm = 0x208
callq 0x2180
leaq 0xf683(%rip), %rdi      # 0x19688
callq 0x23e0
testl %eax, %eax
je 0x9c46
callq 0x697e
leaq 0x8ff5(%rip), %rsi      # 0x13013
movq %rax, %rdi
callq 0x6a12
movq %rax, 0xf653(%rip)      # 0x19680
leaq 0xf654(%rip), %rdi      # 0x19688
callq 0x2170
jmp 0x9c46
leaq 0xf653(%rip), %rdi      # 0x19698
callq 0x23e0
testl %eax, %eax
je 0x9c58
callq 0x697e
leaq 0x8fbd(%rip), %rsi      # 0x1301b
movq %rax, %rdi
callq 0x6a12
movq %rax, 0xf623(%rip)      # 0x19690
leaq 0xf624(%rip), %rdi      # 0x19698
callq 0x2170
jmp 0x9c58
leaq 0x8fdf(%rip), %rdi      # 0x13064
leaq 0x8b95(%rip), %rsi      # 0x12c21
leaq 0x8eb0(%rip), %rcx      # 0x12f43
movl $0x262, %edx            # imm = 0x262
callq 0x2180
movq %rax, %rbx
leaq 0xf5f1(%rip), %rdi      # 0x19698
jmp 0xa0b3
movq %rax, %rbx
leaq 0xf5d5(%rip), %rdi      # 0x19688
callq 0x2160
jmp 0xa0c6
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x81c2
movq %rbx, %rdi
callq 0x23c0