// File: /workspace/github_c_makefile_star2_O1/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 0x8ff8
movq %rsi, %rdx
movq %rdi, %rbx
addq $0x18, %rdi
leaq -0x80(%rbp), %rcx
leaq -0x7c(%rbp), %r14
movl $0xe, %esi
movq %r14, %r8
callq 0x970a
movq %rax, %r15
movslq (%r14), %r14
movq %r14, %rdi
callq 0x2040
movq %rax, -0x70(%rbp)
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x21c0
cmpq $0xc000, %r14           # imm = 0xC000
jl 0x8fd8
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 0xa19b(%rip), %rsi      # 0x13059
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 0x6d42
cmpb $0x1, 0x1e8(%rbx)
jne 0x8f49
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 0x6ca8
movq %r14, %rdi
leaq 0x9fc9(%rip), %rsi      # 0x12eea
callq 0x2400
movq -0x48(%rbp), %rdi
movl $0x80, %esi
movl $0x80, %edx
movl $0x3, %ecx
movq %r13, %r8
movl $0x180, %r9d            # imm = 0x180
callq 0x5ccc
jmp 0x8f92
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 0x6ca8
movq %r14, %rdi
leaq 0x9f7d(%rip), %rsi      # 0x12eef
callq 0x2400
movq -0x48(%rbp), %rdi
movl $0x80, %esi
movl $0x80, %edx
movl $0x3, %ecx
movq %r13, %r8
callq 0x42dd
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x8fab
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x8fc4
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
addq $0xc000, %r13           # imm = 0xC000
incl %r15d
cmpl %r15d, -0x78(%rbp)
jne 0x8ea9
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 0x9c1e(%rip), %rdi      # 0x12c1d
leaq 0x9c1b(%rip), %rsi      # 0x12c21
leaq 0x9eb1(%rip), %rcx      # 0x12ebe
movl $0x1a3, %edx            # imm = 0x1A3
callq 0x2180
jmp 0x901f
jmp 0x9024
jmp 0x9024
jmp 0x9024
movq %rax, %rbx
jmp 0x9040
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x9040
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2230
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x9059
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop