// File: /workspace/github_c_makefile_star2_O2/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 0xaae6(%rip), %eax      # 0x15ae8
movq %rax, 0x8(%rsp)
leaq 0xc652(%rip), %r13      # 0x17660
movq (%r13), %rcx
leaq 0x4b1e(%rip), %rdi      # 0xfb37
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
leaq 0xc665(%rip), %rbx      # 0x17690
leaq 0x28(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x97be
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
testq %rax, %rax
je 0xb07c
movq (%rax), %rdi
callq 0x20e0
movq %rax, %rbp
movq 0x8(%rsp), %rax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9789
incq 0x10(%rsp)
addq $0x8, %r15
movq %rbp, 0x8(%rsp)
jmp 0xb047
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq 0xc602(%rip), %r12      # 0x17690
leaq 0x28(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x97be
movq %r15, %rbx
testq %rax, %rax
je 0xb0ba
movq %rax, (%rbx)
addq $0x8, %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x9789
jmp 0xb0a1
leaq 0x1de(%rip), %rcx       # 0xb29f
pushq $0x8
popq %rdx
movq %r15, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x20b0
movq 0x8(%rsp), %rax
leal 0x12(%rax), %ecx
movl $0x84, %eax
xorl %edx, %edx
idivl %ecx
movl %eax, %ecx
leal (%rcx,%rbx), %eax
decl %eax
cltd
idivl %ecx
movslq %eax, %rbp
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, 0x18(%rsp)
pushq $0x20
popq %r12
xorl %ebx, %ebx
cmpq 0x18(%rsp), %rbx
je 0xb254
movq %rbx, 0x20(%rsp)
cmpq 0x10(%rsp), %rbx
jge 0xb23b
movq (%r15,%rbx,8), %r14
movq (%r13), %rdi
movq (%r14), %rcx
leaq 0x4a14(%rip), %rsi      # 0xfb48
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x21e0
movq 0x18(%r14), %rax
testb $0x20, 0xc(%rax)
pushq $0x3d
popq %rdi
je 0xb150
movl %r12d, %edi
movq (%r13), %rsi
callq 0x2170
testb $0x8, 0x10(%r14)
movq (%r13), %rcx
jne 0xb178
leaq 0x49e2(%rip), %rdi      # 0xfb4d
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
jmp 0xb18e
movzwl 0x8(%r14), %edx
movq %rcx, %rdi
leaq 0x49cd(%rip), %rsi      # 0xfb54
xorl %eax, %eax
callq 0x21e0
movq (%r13), %rsi
movq 0x18(%r14), %rax
testb $0x20, 0xc(%rax)
movl %r12d, %edi
je 0xb1a2
pushq $0x52
popq %rdi
callq 0x2170
movq (%r13), %rsi
testb $0x2, 0x10(%r14)
movl %r12d, %edi
je 0xb1b8
pushq $0x47
popq %rdi
callq 0x2170
movq (%r13), %rsi
testb $0x40, 0x10(%r14)
movl %r12d, %edi
je 0xb1ce
pushq $0x58
popq %rdi
callq 0x2170
movq (%r13), %rsi
testb $0x20, 0x10(%r14)
movl %r12d, %edi
je 0xb1e4
pushq $0x4c
popq %rdi
callq 0x2170
movq (%r13), %rsi
testb $0x4, 0x10(%r14)
movl %r12d, %edi
je 0xb1fa
pushq $0x57
popq %rdi
callq 0x2170
movq 0x18(%r14), %rax
movl 0x18(%rax), %edx
movq (%r13), %rcx
testl %edx, %edx
je 0xb221
movq %rcx, %rdi
leaq 0x4941(%rip), %rsi      # 0xfb59
xorl %eax, %eax
callq 0x21e0
jmp 0xb233
leaq 0x4939(%rip), %rdi      # 0xfb61
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
addq %rbp, %rbx
jmp 0xb117
movq (%r13), %rsi
pushq $0xa
popq %rdi
callq 0x2170
movq 0x20(%rsp), %rbx
incq %rbx
jmp 0xb107
movq (%r13), %rcx
leaq 0x4909(%rip), %rdi      # 0xfb68
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x22d0
xorl %ebx, %ebx
leaq 0xa7fd(%rip), %r14      # 0x15a70
leaq 0x9ff6(%rip), %r15      # 0x15270
movslq (%r14), %rax
cmpq %rax, %rbx
jge 0xb290
movq (%r15,%rbx,8), %rdi
callq 0xb2b0
incq %rbx
jmp 0xb27a
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq