// File: /workspace/github_cpp_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movq %rdi, %r13
movq (%rdi), %rdi
leaq 0x181472(%rip), %rsi    # 0x58655f
callq 0x4e3500
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x405135
leaq -0x50(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movq (%r13), %rdi
callq 0x4f7760
movq %rax, %r14
movq %rax, %rdi
movl $0x2e, %esi
callq 0x401048
testq %rax, %rax
je 0x4051a6
movb $0x0, (%rax)
movq -0x58(%rbp), %r15
jmp 0x4051a9
movq $0x5f4580, %rbx         # imm = 0x5F4580
leaq 0x17c083(%rip), %rsi    # 0x5811c6
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x476110
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x476110
movq %rax, %rbx
leaq 0x17c075(%rip), %rsi    # 0x5811df
movl $0x1, %edx
movq %rax, %rdi
callq 0x476110
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x475c10
movq %rax, %rdi
callq 0x4751e0
xorl %r12d, %r12d
jmp 0x4053f2
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x401118
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x47d660
leaq 0x17c015(%rip), %rsi    # 0x5811e1
leaq -0x60(%rbp), %rdi
callq 0x47e1c0
movq %r14, %rdi
callq 0x4f5430
movq -0x60(%rbp), %rdi
leaq 0x180921(%rip), %rsi    # 0x585b09
callq 0x4e3500
testq %rax, %rax
setne %r12b
je 0x40522b
movq %rax, %r15
movq 0x1ee458(%rip), %rax    # 0x5f3658
movq 0x38(%rax), %rdi
movl $0x0, -0x2c(%rbp)
xorl %esi, %esi
callq 0x40c5bc
testq %rax, %rax
movl %r12d, -0x3c(%rbp)
je 0x4052a3
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %r14d
jmp 0x4052a9
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17bfad(%rip), %rsi    # 0x5811e6
movl $0x2c, %edx
callq 0x476110
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x476110
movq %rax, %r14
leaq 0x17bf7e(%rip), %rsi    # 0x5811df
movl $0x1, %edx
movq %rax, %rdi
callq 0x476110
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq %r14, %rdi
callq 0x475c10
movq %rax, %rdi
callq 0x4751e0
movq -0x38(%rbp), %rdi
callq 0x4e2c70
jmp 0x4053dd
movl $0xf423f, %r14d         # imm = 0xF423F
movl $0xa, %edi
movq %r15, %rsi
callq 0x4e7c20
leaq 0x17bf56(%rip), %rsi    # 0x581213
leaq 0x17c0fc(%rip), %rdx    # 0x5813c0
leaq 0x17c0e5(%rip), %rcx    # 0x5813b0
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4680
movq (%r13), %rdx
leaq 0x17bf43(%rip), %rsi    # 0x581223
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4680
movl $0xa, %edi
movq %r15, %rsi
callq 0x4e7c20
leaq 0x17bf36(%rip), %rdi    # 0x581234
movl $0x1e, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4e3830
leaq -0x160(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
movq -0x38(%rbp), %rdx
callq 0x4e3250
testq %rax, %rax
je 0x4053c4
leaq -0x160(%rbp), %r13
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl %r14d, %r12d
jne 0x405386
incl %r14d
movzwl %bx, %ecx
movq %r15, %rdi
leaq 0x17bf04(%rip), %rsi    # 0x581253
movl %r14d, %edx
movl -0x2c(%rbp), %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0x4d4680
incl %ebx
movq 0x1ee2ef(%rip), %rax    # 0x5f3658
movq 0x38(%rax), %rdi
movzwl %bx, %esi
callq 0x40c5bc
testq %rax, %rax
je 0x4053a1
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %r14d
jmp 0x4053a7
leal 0x1(%r12), %edx
movq %r15, %rdi
leaq 0x17bed2(%rip), %rsi    # 0x581267
movq %r13, %rcx
xorl %eax, %eax
callq 0x4d4680
jmp 0x4053a7
movl $0xf423f, %r14d         # imm = 0xF423F
incl %r12d
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq -0x38(%rbp), %rdx
callq 0x4e3250
testq %rax, %rax
jne 0x40533a
movq -0x38(%rbp), %rdi
callq 0x4e2c70
movq %r15, %rdi
callq 0x4e2c70
leaq -0x50(%rbp), %rbx
movl -0x3c(%rbp), %r12d
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
je 0x4053f2
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x40d020
movl %r12d, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40540b
jmp 0x40540b
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x405427
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x40d020
movq %rbx, %rdi
callq 0x4ad8c0
nop