// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x180(%rbp)
movq %rdi, -0x10(%rbp)
callq 0x487510
movq %rax, %rdi
leaq 0x18b1aa(%rip), %rsi    # 0x59059f
callq 0x4ed120
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x40544e
movq $0x602580, %rdi         # imm = 0x602580
leaq 0x185f3d(%rip), %rsi    # 0x58b350
callq 0x47f700
movq -0x180(%rbp), %rsi
movq %rax, %rdi
callq 0x488450
movq %rax, %rdi
leaq 0x185f38(%rip), %rsi    # 0x58b369
callq 0x47f700
movq %rax, %rdi
movq $0x47efe0, %rsi         # imm = 0x47EFE0
callq 0x47e1f0
movb $0x0, -0x1(%rbp)
jmp 0x405822
leaq -0x48(%rbp), %rdi
callq 0x4859c0
movq -0x180(%rbp), %rdi
callq 0x487510
movq %rax, %rdi
callq 0x5011e0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movl $0x2e, %esi
callq 0x401048
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x40548f
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x486b20
jmp 0x40549e
leaq 0x185ec6(%rip), %rsi    # 0x58b36b
leaq -0x48(%rbp), %rdi
callq 0x4872d0
jmp 0x4054b0
movq -0x20(%rbp), %rdi
callq 0x4feeb0
leaq -0x48(%rbp), %rdi
callq 0x487510
movq %rax, %rdi
leaq 0x18a67d(%rip), %rsi    # 0x58fb49
callq 0x4ed120
movq %rax, -0x188(%rbp)
jmp 0x4054da
movq -0x188(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x40558b
movq $0x602580, %rdi         # imm = 0x602580
leaq 0x185e72(%rip), %rsi    # 0x58b370
callq 0x47f700
movq %rax, -0x190(%rbp)
jmp 0x40550c
movq -0x190(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x488450
movq %rax, -0x198(%rbp)
jmp 0x405525
movq -0x198(%rbp), %rdi
leaq 0x185e36(%rip), %rsi    # 0x58b369
callq 0x47f700
movq %rax, -0x1a0(%rbp)
jmp 0x405541
movq -0x1a0(%rbp), %rdi
movq $0x47efe0, %rsi         # imm = 0x47EFE0
callq 0x47e1f0
jmp 0x405556
movq -0x18(%rbp), %rdi
callq 0x4ec890
jmp 0x405561
movb $0x0, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0x405819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x485ad0
jmp 0x405830
movl $0x0, -0x164(%rbp)
movl $0x0, -0x168(%rbp)
movw $0x0, -0x16a(%rbp)
movl $0x0, -0x170(%rbp)
movq 0x1fc09f(%rip), %rax    # 0x601658
movq 0x38(%rax), %rdi
movzwl -0x16a(%rbp), %esi
callq 0x413a30
movq %rax, -0x1a8(%rbp)
jmp 0x4055d2
movq -0x1a8(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, -0x178(%rbp)
jne 0x4055f6
movl $0xf423f, -0x168(%rbp)  # imm = 0xF423F
jmp 0x405636
movq -0x178(%rbp), %rdi
callq 0x405940
movl %eax, -0x1ac(%rbp)
jmp 0x40560a
movl -0x1ac(%rbp), %eax
movl %eax, -0x168(%rbp)
movq -0x178(%rbp), %rdi
callq 0x405960
movl %eax, -0x1b0(%rbp)
jmp 0x40562a
movl -0x1b0(%rbp), %eax
movl %eax, -0x170(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x18d489(%rip), %rsi    # 0x592aca
movb $0x0, %al
callq 0x4de2a0
movq -0x50(%rbp), %rdi
leaq 0x185d4a(%rip), %rsi    # 0x58b39d
leaq 0x185a06(%rip), %rdx    # 0x58b060
leaq 0x1859f3(%rip), %rcx    # 0x58b054
movb $0x0, %al
callq 0x4de2a0
movq -0x180(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0x1b8(%rbp)
callq 0x487510
movq -0x1b8(%rbp), %rdi
movq %rax, %rdx
leaq 0x185d1d(%rip), %rsi    # 0x58b3ad
movb $0x0, %al
callq 0x4de2a0
movq -0x50(%rbp), %rdi
leaq 0x18d428(%rip), %rsi    # 0x592aca
movb $0x0, %al
callq 0x4de2a0
movq -0x50(%rbp), %rdi
leaq 0x185d0a(%rip), %rsi    # 0x58b3be
movb $0x0, %al
callq 0x4de2a0
movq -0x18(%rbp), %rdx
leaq -0x160(%rbp), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x4ece70
movq %rax, -0x1c0(%rbp)
jmp 0x4056d9
movq -0x1c0(%rbp), %rax
cmpq $0x0, %rax
je 0x4057f8
movl -0x164(%rbp), %eax
cmpl -0x168(%rbp), %eax
jne 0x4057c2
movq -0x50(%rbp), %rdi
movl -0x164(%rbp), %edx
incl %edx
movzwl -0x16a(%rbp), %ecx
movl -0x170(%rbp), %r8d
leaq 0x185cc0(%rip), %rsi    # 0x58b3dd
xorl %eax, %eax
leaq -0x160(%rbp), %r9
callq 0x4de2a0
movw -0x16a(%rbp), %ax
incw %ax
movw %ax, -0x16a(%rbp)
movq 0x1fbf15(%rip), %rax    # 0x601658
movq 0x38(%rax), %rdi
movzwl -0x16a(%rbp), %esi
callq 0x413a30
movq %rax, -0x1c8(%rbp)
jmp 0x40575c
movq -0x1c8(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, -0x178(%rbp)
jne 0x405780
movl $0xf423f, -0x168(%rbp)  # imm = 0xF423F
jmp 0x4057c0
movq -0x178(%rbp), %rdi
callq 0x405940
movl %eax, -0x1cc(%rbp)
jmp 0x405794
movl -0x1cc(%rbp), %eax
movl %eax, -0x168(%rbp)
movq -0x178(%rbp), %rdi
callq 0x405960
movl %eax, -0x1d0(%rbp)
jmp 0x4057b4
movl -0x1d0(%rbp), %eax
movl %eax, -0x170(%rbp)
jmp 0x4057e4
movq -0x50(%rbp), %rdi
movl -0x164(%rbp), %edx
addl $0x1, %edx
leaq -0x160(%rbp), %rcx
leaq 0x185c14(%rip), %rsi    # 0x58b3f1
movb $0x0, %al
callq 0x4de2a0
movl -0x164(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x164(%rbp)
jmp 0x4056bb
movq -0x18(%rbp), %rdi
callq 0x4ec890
jmp 0x405803
movq -0x50(%rbp), %rdi
callq 0x4ec890
jmp 0x40580e
movb $0x1, -0x1(%rbp)
movl $0x1, -0x60(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x485ad0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x4b74e0
nopl (%rax)