// File: /workspace/github_cpp_makefile_star2_O1/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 0x181474(%rip), %rsi    # 0x58655f
callq 0x4e35a0
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x4051be
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%r13), %rdi
callq 0x4f7800
movq %rax, %r14
movq %rax, %rdi
movl $0x2e, %esi
callq 0x401048
testq %rax, %rax
je 0x405131
movb $0x0, (%rax)
leaq -0x60(%rbp), %r15
movq 0x8(%r15), %r12
movq %r14, %rdi
callq 0x401118
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x47d700
leaq 0x17c086(%rip), %rsi    # 0x5811e1
leaq -0x60(%rbp), %rdi
callq 0x47e260
movq %r14, %rdi
callq 0x4f54d0
movq -0x60(%rbp), %rdi
leaq 0x180992(%rip), %rsi    # 0x585b09
callq 0x4e35a0
testq %rax, %rax
setne %bl
je 0x40522e
movq %rax, %r15
movq 0x1ee4c6(%rip), %rax    # 0x5f3658
movq 0x38(%rax), %rdi
movl $0x0, -0x2c(%rbp)
xorl %esi, %esi
callq 0x40c62e
movl %ebx, -0x3c(%rbp)
testq %rax, %rax
je 0x4052a6
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %ebx
jmp 0x4052ab
movq $0x5f4580, %rbx         # imm = 0x5F4580
leaq 0x17bffa(%rip), %rsi    # 0x5811c6
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x4761b0
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %rbx, %rdi
callq 0x4761b0
movq %rax, %rbx
leaq 0x17bfec(%rip), %rsi    # 0x5811df
movl $0x1, %edx
movq %rax, %rdi
callq 0x4761b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x475cb0
movq %rax, %rdi
callq 0x475280
xorl %ebx, %ebx
jmp 0x4053f3
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17bfaa(%rip), %rsi    # 0x5811e6
movl $0x2c, %edx
callq 0x4761b0
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x4761b0
movq %rax, %r14
leaq 0x17bf7b(%rip), %rsi    # 0x5811df
movl $0x1, %edx
movq %rax, %rdi
callq 0x4761b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq %r14, %rdi
callq 0x475cb0
movq %rax, %rdi
callq 0x475280
movq -0x38(%rbp), %rdi
callq 0x4e2d10
jmp 0x4053da
movl $0xf423f, %ebx          # imm = 0xF423F
movl $0xa, %edi
movq %r15, %rsi
callq 0x4e7cc0
leaq 0x17bf54(%rip), %rsi    # 0x581213
leaq 0x17c0fa(%rip), %rdx    # 0x5813c0
leaq 0x17c0e3(%rip), %rcx    # 0x5813b0
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4720
movq (%r13), %rdx
leaq 0x17bf41(%rip), %rsi    # 0x581223
movq %r15, %rdi
xorl %eax, %eax
callq 0x4d4720
movl $0xa, %edi
movq %r15, %rsi
callq 0x4e7cc0
leaq 0x17bf34(%rip), %rdi    # 0x581234
movl $0x1e, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4e38d0
leaq -0x160(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
movq -0x38(%rbp), %rdx
callq 0x4e32f0
testq %rax, %rax
je 0x4053c6
leaq -0x160(%rbp), %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %r13d, %eax
leal 0x1(%rax), %r13d
cmpl %ebx, %eax
jne 0x40538e
movzwl %r12w, %ecx
movq %r15, %rdi
leaq 0x17befd(%rip), %rsi    # 0x581253
movl %r13d, %edx
movl -0x2c(%rbp), %r8d
movq %r14, %r9
xorl %eax, %eax
callq 0x4d4720
incl %r12d
movq 0x1ee2e7(%rip), %rax    # 0x5f3658
movq 0x38(%rax), %rdi
movzwl %r12w, %esi
callq 0x40c62e
testq %rax, %rax
je 0x4053a7
movl 0x1c(%rax), %ecx
movl %ecx, -0x2c(%rbp)
movl 0x20(%rax), %ebx
jmp 0x4053ac
movq %r15, %rdi
leaq 0x17becf(%rip), %rsi    # 0x581267
movl %r13d, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x4d4720
jmp 0x4053ac
movl $0xf423f, %ebx          # imm = 0xF423F
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
movq -0x38(%rbp), %rdx
callq 0x4e32f0
testq %rax, %rax
jne 0x40533d
movq -0x38(%rbp), %rdi
callq 0x4e2d10
movq %r15, %rdi
callq 0x4e2d10
movl -0x3c(%rbp), %ebx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x4053f3
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movl %ebx, %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 0x40d0c0
movq %rbx, %rdi
callq 0x4ad960
nop