// File: /workspace/github_c_makefile_star2_O3/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 0x9ed9
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 0x9ef8
movq %rsi, %rbx
leaq 0x9062(%rip), %rax      # 0x12bb2
movq %rax, -0x30(%rbp)
movb 0xfb1e(%rip), %al       # 0x19678
testb %al, %al
je 0x9f17
movq %rbx, -0x48(%rbp)
movb 0xfb1c(%rip), %al       # 0x19688
testb %al, %al
je 0x9f57
movslq 0x14(%r14), %r15
testq %r15, %r15
jle 0x9ba8
movslq 0x10(%r14), %rax
imulq $0x2c, %rax, %rbx
addq (%r12), %rbx
leaq -0xa360(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9a5a
addq $0x2c, %rbx
addq $0x28, %r12
decq %r15
jne 0x9b90
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 0x69ac
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a3e
leaq -0x38(%rbp), %rbx
movq %rax, (%rbx)
callq 0xab4a
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc92a
testq %rax, %rax
je 0x9bfb
movq %rax, %r12
jmp 0x9ccc
movq -0x38(%rbp), %rbx
leaq 0x940c(%rip), %rsi      # 0x13012
movq %rbx, %rdi
callq 0x20a0
testq %rax, %rax
je 0x9c24
leaq 0x9401(%rip), %rdi      # 0x1301b
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 0x9fe8
movq %rbx, %rdi
callq 0x2120
movabsq $0x7265646168732e, %rcx # imm = 0x7265646168732E
movq %rcx, -0x160(%rbp,%rax)
callq 0x69ac
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a3e
leaq -0x30(%rbp), %rbx
movq %rax, (%rbx)
callq 0xab4a
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc972
testb %al, %al
jne 0x9cb7
callq 0xab4a
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0xc618
testb %al, %al
je 0x9cb7
callq 0xab4a
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
callq 0xc92a
testq %rax, %rax
jne 0x9bf3
movq -0x38(%rbp), %rsi
leaq 0x936a(%rip), %rdi      # 0x1302c
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x2050
movq 0x10(%rbp), %rbx
movl 0x20(%r14), %r9d
movq -0x38(%rbp), %rdx
testl %r9d, %r9d
js 0x9cfe
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq (%rbx), %r8
leaq 0x9358(%rip), %rsi      # 0x13046
leaq -0x160(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
jmp 0x9d13
leaq 0x933d(%rip), %rsi      # 0x13042
leaq -0x160(%rbp), %rdi
xorl %eax, %eax
callq 0x2080
movq 0x18(%rbp), %r15
callq 0x69ac
leaq -0x160(%rbp), %rsi
movq %rax, %rdi
callq 0x6a3e
movq %rax, -0x30(%rbp)
movq (%rbx), %rdx
movl 0x20(%r14), %ecx
leaq 0x930e(%rip), %rsi      # 0x1304b
leaq -0x260(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2080
callq 0x69ac
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6a3e
movl 0xc(%r14), %eax
cmpl $0x3, %eax
je 0x9d79
cmpl $0x2, %eax
je 0x9e16
cmpl $0x1, %eax
jne 0x9ead
movslq 0x1c(%r14), %rax
imull $0xaaaaaaab, %eax, %ecx # imm = 0xAAAAAAAB
addl $0x2aaaaaaa, %ecx       # imm = 0x2AAAAAAA
cmpl $0x55555555, %ecx       # imm = 0x55555555
jae 0x9f97
cmpl $0x3, %eax
jl 0x9ead
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 0xece6
addq $0x6, %r14
decl %r13d
jne 0x9dc5
jmp 0x9ead
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 0x7cfc
movslq (%rbx), %rax
cmpq $0x3, %rax
jl 0x9ea0
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 0xece6
addq $0x6, %r12
decl %r13d
jne 0x9e63
leaq -0x60(%rbp), %rdi
callq 0x810c
movq -0x40(%rbp), %r12
movq (%r15), %rax
testq %rax, %rax
sete %cl
testq %r12, %r12
sete %dl
orb %cl, %dl
jne 0x9ec7
movq %r12, 0x80(%rax)
addq $0xa338, %rsp           # imm = 0xA338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9044(%rip), %rdi      # 0x12f24
leaq 0x8d2a(%rip), %rsi      # 0x12c11
leaq 0x9045(%rip), %rcx      # 0x12f33
movl $0x207, %edx            # imm = 0x207
callq 0x2180
leaq 0x90db(%rip), %rdi      # 0x12fda
leaq 0x8d0b(%rip), %rsi      # 0x12c11
leaq 0x9026(%rip), %rcx      # 0x12f33
movl $0x208, %edx            # imm = 0x208
callq 0x2180
leaq 0xf75a(%rip), %rdi      # 0x19678
callq 0x23e0
testl %eax, %eax
je 0x9b62
callq 0x69ac
leaq 0x90cc(%rip), %rsi      # 0x13003
movq %rax, %rdi
callq 0x6a3e
movq %rax, 0xf72a(%rip)      # 0x19670
leaq 0xf72b(%rip), %rdi      # 0x19678
callq 0x2170
jmp 0x9b62
leaq 0xf72a(%rip), %rdi      # 0x19688
callq 0x23e0
testl %eax, %eax
je 0x9b74
callq 0x69ac
leaq 0x9094(%rip), %rsi      # 0x1300b
movq %rax, %rdi
callq 0x6a3e
movq %rax, 0xf6fa(%rip)      # 0x19680
leaq 0xf6fb(%rip), %rdi      # 0x19688
callq 0x2170
jmp 0x9b74
leaq 0x90b6(%rip), %rdi      # 0x13054
leaq 0x8c6c(%rip), %rsi      # 0x12c11
leaq 0x8f87(%rip), %rcx      # 0x12f33
movl $0x262, %edx            # imm = 0x262
callq 0x2180
movq %rax, %rbx
leaq 0xf6c8(%rip), %rdi      # 0x19688
jmp 0x9fcc
movq %rax, %rbx
leaq 0xf6ac(%rip), %rdi      # 0x19678
callq 0x2160
jmp 0x9fdf
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x810c
movq %rbx, %rdi
callq 0x23c0
nop