// File: /workspace/github_c_makefile_star2_O3/vlenhart[P]q3bsp/q3bsp.cpp
// Function: Quake3BSP::ReadLightmaps(void const*)
void Quake3BSP::ReadLightmaps(const void *mem)
{
  assert( mOk );

  int lsize;
  int lcount;
  const unsigned char *maps = (const unsigned char *) mHeader.LumpInfo(Q3_LIGHTMAPS,mem,lsize,lcount);
  unsigned char *lmaps = new unsigned char[lcount];
  memcpy(lmaps,maps,lcount);

  for (int j=0; j<lcount; j++)
  {
    unsigned int c = lmaps[j];
    lmaps[j] = (unsigned char) c;
  }

  #define LMWID 128
  #define LMHIT 128
  #define LMSIZE (LMWID*LMHIT*3)

  int texcount = lcount / LMSIZE;

  unsigned char *map = lmaps;

  for (int i=0; i<texcount; i++)
  {
    char scratch[256];
    sprintf(scratch,"%slm%s%02d",mLmPrefix.Get(),mCodeName.Get(),i);
    String sname = scratch;
    if (mUsePng) {
      String bname = sname+".png";
      stbi_write_png(bname.c_str(), LMWID, LMHIT, 3, map, LMWID*3);
    } else {
      String bname = sname+".bmp";
      stbi_write_bmp(bname.c_str(), LMWID, LMHIT, 3, map);
    }
    map+=LMSIZE;
  }


  delete lmaps;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
cmpb $0x0, (%rdi)
je 0x8f15
movq %rdi, %rbx
movslq 0x94(%rdi), %r14
testq %r14, %r14
je 0x8f34
movq %rsi, %r15
movslq 0x90(%rbx), %rax
addq %rax, %r15
movq %r14, %rdi
callq 0x2040
movq %rax, -0x70(%rbp)
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x21c0
cmpl $0xc000, %r14d          # imm = 0xC000
jl 0x8ef5
imulq $0x2aaaaaab, %r14, %rcx # imm = 0x2AAAAAAB
movq %rcx, %rax
shrq $0x3f, %rax
sarq $0x2d, %rcx
addl %eax, %ecx
movq %rcx, -0x78(%rbp)
leaq -0x180(%rbp), %r12
xorl %r15d, %r15d
movq -0x70(%rbp), %r13
movq 0x10(%rbx), %rcx
movq 0x1f0(%rbx), %rdx
movq %r12, %rdi
leaq 0xa26e(%rip), %rsi      # 0x13049
movl %r15d, %r8d
xorl %eax, %eax
callq 0x2080
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %r12, %rdi
callq 0x2120
leaq (%rax,%rbp), %rdx
addq $-0x180, %rdx           # imm = 0xFE80
leaq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x6d58
cmpb $0x1, 0x1e8(%rbx)
jne 0x8e66
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x6cba
movq %r14, %rdi
leaq 0xa09c(%rip), %rsi      # 0x12eda
callq 0x2400
movq -0x48(%rbp), %rdi
movl $0x80, %esi
movl $0x80, %edx
movl $0x3, %ecx
movq %r13, %r8
movl $0x180, %r9d            # imm = 0x180
callq 0x5d3c
jmp 0x8eaf
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x6cba
movq %r14, %rdi
leaq 0xa050(%rip), %rsi      # 0x12edf
callq 0x2400
movq -0x48(%rbp), %rdi
movl $0x80, %esi
movl $0x80, %edx
movl $0x3, %ecx
movq %r13, %r8
callq 0x43cd
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x8ec8
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x8ee1
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
addq $0xc000, %r13           # imm = 0xC000
incl %r15d
cmpl %r15d, -0x78(%rbp)
jne 0x8dc6
movl $0x1, %esi
movq -0x70(%rbp), %rdi
callq 0x2230
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9cf1(%rip), %rdi      # 0x12c0d
leaq 0x9cee(%rip), %rsi      # 0x12c11
leaq 0x9f84(%rip), %rcx      # 0x12eae
movl $0x1a3, %edx            # imm = 0x1A3
callq 0x2180
leaq 0x9d54(%rip), %rdi      # 0x12c8f
leaq 0x9ccf(%rip), %rsi      # 0x12c11
leaq 0x9cfc(%rip), %rcx      # 0x12c45
movl $0xab, %edx
callq 0x2180
jmp 0x8f5b
jmp 0x8f60
jmp 0x8f60
jmp 0x8f60
movq %rax, %rbx
jmp 0x8f7c
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x8f7c
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x8f95
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop