// File: /workspace/github_cpp_makefile_star2_O2/stacksmith[P]picoasm/main.cpp
// Function: printListing(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool printListing(string strFileName){

  FILE *f ;

  f = fopen( strFileName.c_str(), "r" ) ;

  if ( f == NULL ) {
    cout << "ERR: Unable to load file '" << strFileName << "'" << endl;
    return (false);
  }

  char *cpTemp;
  char *cpExt;
  string strAsmListing;
  FILE *fListing;

  // build assembler listing file name from source file name 
  cpTemp = strdup(strFileName.c_str());
  cpExt = strrchr(cpTemp, '.');
  if (cpExt != NULL){
    *cpExt = '\0';
  }
  strAsmListing = cpTemp;
  strAsmListing += LA_PICOASM_LISTING_EXT;
  free(cpTemp);
  fListing = fopen(strAsmListing.c_str(), "w") ;

  if ( fListing == NULL ) {
    cout << "ERR: Unable to open assembler listing file '" 
	 << strAsmListing << "'" << endl;
    fclose(f);
    return (false);
  }

  char buf[ 256 ] ;
  int linenr = 0 ;
  int iCodeLine = 0;
  uint16_t uiAddr = 0;
  uint32_t uiHexCode = 0;

  CInstruction *instr = m_picoBlaze->code->getInstruction( uiAddr ) ;

  if (instr == NULL){
    iCodeLine = 999999;  // no code in file - use a giant line number
  } else {
    iCodeLine = instr->getSourceLine();
    uiHexCode = instr->getHexCode();
  }

  fprintf(fListing, "\n");
  fprintf(fListing, "%s  Version %s\n", AppDescription, version);   // heading
  fprintf(fListing, "Source File: %s\n", strFileName.c_str());
  fprintf(fListing, "\n");
  fprintf(fListing, "Line  Addr Instr  Source Code\n");   
  //                "llll  AAA  HHHHH  SSSSSSSSS...", 

  while( fgets( buf, sizeof( buf ), f ) ) {
    // if this is the next code line 
    if (linenr == iCodeLine){
      fprintf(fListing, 
	      "%4d  %03x  %05x  %s", 
	      linenr + 1, uiAddr, uiHexCode, buf);  // one-base line number
      // get next instruction
      uiAddr++;
      instr = m_picoBlaze->code->getInstruction( uiAddr ) ;
      if (instr == NULL){
	// no more code in file
	iCodeLine = 999999;  // no code in file - use a giant line number
      } else {
	iCodeLine = instr->getSourceLine();
	uiHexCode = instr->getHexCode();
      }
    } else {
      fprintf(fListing, "%4d              %s", 
	      linenr + 1, buf);                    // one-based line number
    }
    linenr++;
  }

  fclose (f);
  fclose(fListing);
		
  return (true);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdi, %r13
movq (%rdi), %rdi
leaq 0x1816c1(%rip), %rsi    # 0x58655f
callq 0x4e3010
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x404f51
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%r13), %rdi
callq 0x4f7270
movq %rax, %r14
pushq $0x2e
popq %rsi
movq %rax, %rdi
callq 0x401048
testq %rax, %rax
je 0x404edf
movb $0x0, (%rax)
leaq -0x68(%rbp), %rdi
movq %r14, %rsi
callq 0x47c970
leaq 0x17c2ef(%rip), %rsi    # 0x5811e1
leaq -0x68(%rbp), %rdi
callq 0x47d170
movq %r14, %rdi
callq 0x4f4f40
movq -0x68(%rbp), %rdi
leaq 0x180bfb(%rip), %rsi    # 0x585b09
callq 0x4e3010
testq %rax, %rax
setne %bl
je 0x404f8d
movq %rax, %r15
movq 0x1ef733(%rip), %rax    # 0x5f4658
movq 0x38(%rax), %rdi
movl $0x0, -0x2c(%rbp)
xorl %esi, %esi
callq 0x40ae7e
testq %rax, %rax
movl %ebx, -0x3c(%rbp)
je 0x404fd1
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %ebx
jmp 0x404fd6
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c267(%rip), %rsi    # 0x5811c6
callq 0x4755f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x47e340
leaq 0x17c269(%rip), %rsi    # 0x5811df
movq %rax, %rdi
callq 0x4755f0
movq %rax, %rdi
callq 0x474ed0
xorl %ebx, %ebx
jmp 0x405102
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c24b(%rip), %rsi    # 0x5811e6
callq 0x4755f0
leaq -0x68(%rbp), %rsi
movq %rax, %rdi
callq 0x47e340
leaq 0x17c22c(%rip), %rsi    # 0x5811df
movq %rax, %rdi
callq 0x4755f0
movq %rax, %rdi
callq 0x474ed0
movq -0x38(%rbp), %rdi
callq 0x4e2780
jmp 0x4050f9
movl $0xf423f, %ebx          # imm = 0xF423F
pushq $0xa
popq %r14
movl %r14d, %edi
movq %r15, %rsi
callq 0x4e7730
leaq 0x17c227(%rip), %rsi    # 0x581213
leaq 0x17c3cd(%rip), %rdx    # 0x5813c0
leaq 0x17c3b6(%rip), %rcx    # 0x5813b0
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4190
movq (%r13), %rdx
leaq 0x17c214(%rip), %rsi    # 0x581223
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4190
movl %r14d, %edi
movq %r15, %rsi
callq 0x4e7730
leaq 0x17c209(%rip), %rdi    # 0x581234
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x4e3340
leaq -0x170(%rbp), %r13
leaq 0x17c220(%rip), %r12    # 0x581267
xorl %r14d, %r14d
movq $0x0, -0x48(%rbp)
incl %r14d
negl %ebx
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq -0x38(%rbp), %rdx
callq 0x4e2d60
testq %rax, %rax
je 0x4050e5
leal (%rbx,%r14), %eax
cmpl $0x1, %eax
je 0x40508e
movq %r15, %rdi
movq %r12, %rsi
movl %r14d, %edx
movq %r13, %rcx
xorl %eax, %eax
callq 0x4d4190
incl %r14d
jmp 0x405057
movq -0x48(%rbp), %rbx
movzwl %bx, %ecx
movq %r15, %rdi
leaq 0x17c1b4(%rip), %rsi    # 0x581253
movl %r14d, %edx
movl -0x2c(%rbp), %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x4d4190
incl %ebx
movq 0x1ef59f(%rip), %rax    # 0x5f4658
movq 0x38(%rax), %rdi
movq %rbx, -0x48(%rbp)
movzwl %bx, %esi
callq 0x40ae7e
movl $0xf423f, %ebx          # imm = 0xF423F
testq %rax, %rax
je 0x405052
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %ebx
jmp 0x405052
movq -0x38(%rbp), %rdi
callq 0x4e2780
movq %r15, %rdi
callq 0x4e2780
movl -0x3c(%rbp), %ebx
leaq -0x68(%rbp), %rdi
callq 0x47b9c0
movl %ebx, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40511a
jmp 0x40511a
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x47b9c0
movq %rbx, %rdi
callq 0x4ad3d0