// File: /workspace/github_cpp_makefile_star2_O3/jpbalarini[P]otsus/./framework/CImage.cpp
// Function: CImage::load_LUM(char*)
void CImage::load_LUM(char *filename) {
  unsigned char lumWidthArray[4];
  unsigned char lumHeightArray[4];
  size_t dummy;

  FILE *infile = fopen(filename, "rb");
  if (!infile) {
    PRINT_ERROR("CImage: error opening LUM file %s\n!", filename);
    exit(-1);
  }

  int num_channels = 1;
  this->bits = 16;

  PRINT_VERBOSE("CImage load RGB (%dx%d), %d channels, in I_%d\n",
  this->Nx, this->Ny, num_channels, this->id);

  dummy = fread(&lumWidthArray, sizeof(char), 4, infile);
  this->Nx = lumWidthArray[0] + 256*lumWidthArray[1] +
              65536*lumWidthArray[2] + 16777216*lumWidthArray[3];
  dummy += fread(&lumHeightArray, sizeof(char), 4, infile);
  dummy = dummy; // To prevent the warning
  this->Ny = lumHeightArray[0] + 256*lumHeightArray[1] +
           65536*lumHeightArray[2] + 16777216*lumHeightArray[3];

  // Identify file format
  char lumFormat[5];
  dummy = fread(lumFormat, 1, 5, infile);
  if (strcmp(lumFormat, "12LI") != 0 &&
      strcmp(lumFormat, "16LI") != 0 &&
      strcmp(lumFormat, "16LU") != 0) {
    PRINT_ERROR("Unknown LUM format (not 12LI, 16LI, 16LU): %s\n", lumFormat);
    exit(-1);
  }
  
  // Skip offset
  fseek(infile, -2 * this->Nx * this->Ny, SEEK_END);
  
  // Create data channels
  create_channels(num_channels, Nx, Ny);

  // Read data and put it in the channels
  for (int ch = 0; ch < num_channels; ch++) {    
    float *m = this->get_channel(ch);

    unsigned char luminanceArray[2];

    int ptr = 0;
    for (int y = 0; y < this->Ny; y++)
      for (int x = 0; x < this->Nx; x++) {
        dummy = fread(&luminanceArray, 1, 2, infile);
        m[ptr++] = luminanceArray[0] + 256*luminanceArray[1];
      }
  }

  fclose(infile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2bdb(%rip), %rsi      # 0x8110
movq %r15, %rdi
callq 0x32f0
testq %rax, %rax
je 0x56cd
movq %rax, %r14
movl $0x10, 0x8(%rbx)
leaq 0x6d6a(%rip), %rax      # 0xc2c1
cmpb $0x1, (%rax)
jne 0x5578
movl (%rbx), %esi
movl 0x4(%rbx), %edx
movl 0xc(%rbx), %r8d
leaq 0x2e42(%rip), %rdi      # 0x83ae
movl $0x1, %ecx
xorl %eax, %eax
callq 0x3070
leaq 0x24(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x33d0
movl (%r15), %eax
movl %eax, (%rbx)
leaq 0x20(%rsp), %r15
movl $0x1, %esi
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x33d0
movl (%r15), %eax
movl %eax, 0x4(%rbx)
leaq 0xd(%rsp), %r15
movl $0x1, %esi
movl $0x5, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x33d0
movl $0x494c3231, %eax       # imm = 0x494C3231
xorl (%r15), %eax
movzbl 0x4(%r15), %ecx
orl %eax, %ecx
je 0x560a
movl $0x494c3631, %eax       # imm = 0x494C3631
xorl 0xd(%rsp), %eax
movzbl 0x11(%rsp), %ecx
orl %eax, %ecx
je 0x560a
movl $0x554c3631, %eax       # imm = 0x554C3631
xorl 0xd(%rsp), %eax
movzbl 0x11(%rsp), %ecx
orl %eax, %ecx
jne 0x56d5
movslq (%rbx), %rax
movslq 0x4(%rbx), %rsi
imulq %rax, %rsi
addq %rsi, %rsi
negq %rsi
movq %r14, %rdi
movl $0x2, %edx
callq 0x32b0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x4d78
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5bd6
movq %rax, 0x18(%rsp)
movl 0x4(%rbx), %ecx
testl %ecx, %ecx
jle 0x56b7
movl (%rbx), %eax
leaq 0x12(%rsp), %r12
xorl %edx, %edx
testl %eax, %eax
jle 0x56af
movl %edx, 0x14(%rsp)
movslq %edx, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,4), %rbp
xorl %r15d, %r15d
movl $0x1, %esi
movl $0x2, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x33d0
movzwl 0x12(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, (%rbp,%r15,4)
movl (%rbx), %eax
incq %r15
cmpl %eax, %r15d
jl 0x5673
movl 0x14(%rsp), %edx
addl %r15d, %edx
movl 0x4(%rbx), %ecx
incl %r13d
cmpl %ecx, %r13d
jl 0x565c
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3240
movq %r15, %rdi
callq 0x38db
leaq 0xd(%rsp), %rdi
callq 0x3894
nop