// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/crossassemblers/macro11/symbols.c
// Function: list_symbol_table
void list_symbol_table(
    void)
{
    SYMBOL_ITER iter;
    SYMBOL *sym;
    int skip_locals = 0;
    int longest_symbol = symbol_len;

    fprintf(lstfile,"\n\nSymbol table\n\n");

    /* Count the symbols in the table */
    int nsyms = 0;
    for (sym = first_sym(&symbol_st, &iter); sym != NULL; sym = next_sym(&symbol_st, &iter)) {
        if (skip_locals && sym->flags & SYMBOLFLAG_LOCAL) {
            continue;
        }
        nsyms++;
        int len = strlen(sym->label);
        if (len > longest_symbol) {
            longest_symbol = len;
        }
    }

    /* Sort them by name */
    SYMBOL **symbols = malloc(nsyms * sizeof (SYMBOL *));
    SYMBOL **symbolp = symbols;

    for (sym = first_sym(&symbol_st, &iter); sym != NULL; sym = next_sym(&symbol_st, &iter)) {
        if (skip_locals && sym->flags & SYMBOLFLAG_LOCAL) {
            continue;
        }
        *symbolp++ = sym;
    }

    qsort(symbols, nsyms, sizeof(SYMBOL *), symbol_compar);

    symbolp = symbols;

    /* Print the listing in NCOLS columns. */
    int ncols = (132 / (longest_symbol + 18));
    int nlines = (nsyms + ncols - 1) / ncols;
    int line;
    /*
     * DIRER$  004562RGX    006
     * ^       ^     ^      ^-- for R symbols: program segment number
     * |       |     +-- Flags: R = relocatable
     * |       |                G = global
     * |       |                X = implicit global
     * |       |                L = local
     * |       |                W = weak
     * |       +- value, ****** for if it was not a definition
     * +- label name
     */

    for (line = 0; line < nlines; line++) {
        int i;
        for (i = line; i < nsyms; i += nlines) {
            sym = symbols[i];

            fprintf(lstfile,"%-*s", longest_symbol, sym->label);
            fprintf(lstfile,"%c", (sym->section->flags & PSECT_REL) ? ' ' : '=');
            if (!(sym->flags & SYMBOLFLAG_DEFINITION)) {
                fprintf(lstfile,"******");
            } else {
                fprintf(lstfile,"%06o", sym->value & 0177777);
            }
            fprintf(lstfile,"%c", (sym->section->flags & PSECT_REL) ? 'R' : ' ');
            fprintf(lstfile,"%c", (sym->flags & SYMBOLFLAG_GLOBAL) ?  'G' : ' ');
            fprintf(lstfile,"%c", (sym->flags & SYMBOLFLAG_IMPLICIT_GLOBAL) ? 'X' : ' ');
            fprintf(lstfile,"%c", (sym->flags & SYMBOLFLAG_LOCAL) ?   'L' : ' ');
            fprintf(lstfile,"%c", (sym->flags & SYMBOLFLAG_WEAK) ?    'W' : ' ');
            if (sym->section->sector != 0) {
                fprintf(lstfile,"  %03d ", sym->section->sector);
            } else {
                fprintf(lstfile,"      ");
            }
        }
        fprintf(lstfile,"\n");
    }

    /* List sections */

    fprintf(lstfile,"\n\nProgram sections:\n\n");

    int i;
    for (i = 0; i < sector; i++) {
        list_section(sections[i]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0xa2b5(%rip), %ebx      # 0x15ae8
leaq 0xbe26(%rip), %r13      # 0x17660
movq (%r13), %rcx
leaq 0x52a2(%rip), %rdi      # 0x10ae7
movl $0x1, %r14d
movl $0x10, %esi
movl $0x1, %edx
callq 0x22d0
leaq 0xbe2f(%rip), %rax      # 0x17690
movq (%rax), %r12
testq %r12, %r12
jne 0xb87d
addq $0x8, %rax
incq %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0xb861
xorl %ebp, %ebp
jmp 0xb8ea
xorl %ebp, %ebp
movl %ebx, %r15d
incl %ebp
movq (%r12), %rdi
callq 0x20e0
movq %rax, %rbx
cmpl %ebx, %r15d
cmovgl %r15d, %ebx
movq 0x20(%r12), %r12
testq %r12, %r12
jne 0xb87f
cmpl $0x400, %r14d           # imm = 0x400
movl $0x3ff, %eax            # imm = 0x3FF
cmovgel %r14d, %eax
cmpl $0x3fe, %r14d           # imm = 0x3FE
jg 0xb8ea
movslq %r14d, %r14
leaq 0xbdcc(%rip), %rcx      # 0x17690
leaq (%rcx,%r14,8), %rcx
incq %r14
negq %rax
movq (%rcx), %r12
testq %r12, %r12
jne 0xb87f
addq $0x8, %rcx
leaq (%rax,%r14), %rdx
incq %rdx
incq %r14
cmpq $0x1, %rdx
jne 0xb8ce
movl %ebp, %eax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rdi
callq 0x2230
movq %rax, 0x10(%rsp)
leaq 0xbd86(%rip), %rcx      # 0x17690
movl $0x1, %eax
movq (%rcx), %rsi
testq %rsi, %rsi
jne 0xb928
addq $0x8, %rcx
incq %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0xb90f
jmp 0xb980
leaq 0xbd61(%rip), %rcx      # 0x17690
movq 0x10(%rsp), %rdx
movq %rsi, (%rdx)
addq $0x8, %rdx
movq 0x20(%rsi), %rsi
testq %rsi, %rsi
jne 0xb934
cmpl $0x400, %eax            # imm = 0x400
movl $0x3ff, %edi            # imm = 0x3FF
cmovgel %eax, %edi
cmpl $0x3fe, %eax            # imm = 0x3FE
jg 0xb980
cltq
leaq (%rcx,%rax,8), %r8
incq %rax
negq %rdi
movq (%r8), %rsi
testq %rsi, %rsi
jne 0xb934
addq $0x8, %r8
leaq (%rdi,%rax), %rsi
incq %rsi
incq %rax
cmpq $0x1, %rsi
jne 0xb964
leaq 0x23a(%rip), %rcx       # 0xbbc1
movl $0x8, %edx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x20b0
leal 0x12(%rbx), %ecx
movl $0x84, %eax
xorl %edx, %edx
idivl %ecx
movl %eax, %ecx
leal (%r14,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %eax, 0xc(%rsp)
testl %eax, %eax
jle 0xbb6e
movl 0xc(%rsp), %eax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
cmpl %eax, 0x18(%rsp)
jle 0xbb47
movq 0x20(%rsp), %rax
movl %eax, %r14d
movq 0x10(%rsp), %rbp
movq (%rbp), %r12
movq (%r13), %rdi
movq (%r12), %rcx
leaq 0x50f0(%rip), %rsi      # 0x10af8
movl %ebx, %edx
xorl %eax, %eax
callq 0x21e0
movq 0x18(%r12), %rax
testb $0x20, 0xc(%rax)
movl $0x3d, %edi
je 0xba26
movl $0x20, %edi
movq (%r13), %rsi
callq 0x2170
testb $0x8, 0x10(%r12)
movq (%r13), %rcx
jne 0xba53
movl $0x6, %esi
movl $0x1, %edx
leaq 0x50b1(%rip), %rdi      # 0x10afd
callq 0x22d0
jmp 0xba6a
movzwl 0x8(%r12), %edx
movq %rcx, %rdi
leaq 0x50a1(%rip), %rsi      # 0x10b04
xorl %eax, %eax
callq 0x21e0
movq (%r13), %rsi
movq 0x18(%r12), %rax
testb $0x20, 0xc(%rax)
movl $0x20, %r15d
movl $0x20, %edi
je 0xba89
movl $0x52, %edi
callq 0x2170
movq (%r13), %rsi
testb $0x2, 0x10(%r12)
movl $0x20, %edi
je 0xbaa4
movl $0x47, %edi
callq 0x2170
movq (%r13), %rsi
testb $0x40, 0x10(%r12)
movl $0x20, %edi
je 0xbabf
movl $0x58, %edi
callq 0x2170
movq (%r13), %rsi
testb $0x20, 0x10(%r12)
movl $0x20, %edi
je 0xbada
movl $0x4c, %edi
callq 0x2170
movq (%r13), %rsi
testb $0x4, 0x10(%r12)
je 0xbaf1
movl $0x57, %r15d
movl %r15d, %edi
callq 0x2170
movq 0x18(%r12), %rax
movl 0x18(%rax), %edx
movq (%r13), %rcx
testl %edx, %edx
je 0xbb1c
movq %rcx, %rdi
leaq 0x4ff6(%rip), %rsi      # 0x10b09
xorl %eax, %eax
callq 0x21e0
jmp 0xbb32
movl $0x6, %esi
movl $0x1, %edx
leaq 0x4fe4(%rip), %rdi      # 0x10b11
callq 0x22d0
addq 0x30(%rsp), %rbp
addl 0xc(%rsp), %r14d
cmpl %r14d, 0x18(%rsp)
jg 0xb9f5
movq (%r13), %rsi
movl $0xa, %edi
callq 0x2170
movq 0x20(%rsp), %rax
incq %rax
addq $0x8, 0x10(%rsp)
cmpq 0x28(%rsp), %rax
jne 0xb9d9
movq (%r13), %rcx
leaq 0x4f9f(%rip), %rdi      # 0x10b18
movl $0x15, %esi
movl $0x1, %edx
callq 0x22d0
leaq 0x9ee1(%rip), %rbx      # 0x15a70
cmpl $0x0, (%rbx)
jle 0xbbb2
xorl %r14d, %r14d
leaq 0x96d2(%rip), %r15      # 0x15270
movq (%r15,%r14,8), %rdi
callq 0xbbd2
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0xbb9e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq