// File: /workspace/github_c_makefile_star2_O1/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 0xa0f2(%rip), %eax      # 0x15ae8
movq %rax, 0x18(%rsp)
leaq 0xbc5e(%rip), %r15      # 0x17660
movq (%r15), %rcx
leaq 0x50cf(%rip), %rdi      # 0x10adb
movl $0x10, %esi
movl $0x1, %edx
callq 0x22d0
movl $0x1, %ebx
xorl %eax, %eax
leaq 0xbc67(%rip), %r12      # 0x17690
xorl %ecx, %ecx
movq (%rcx,%r12), %r14
testq %r14, %r14
jne 0xba4a
addq $0x8, %rcx
incl %ebx
cmpq $0x1ff8, %rcx           # imm = 0x1FF8
jne 0xba2b
movl $0x3ff, %ebx            # imm = 0x3FF
jmp 0xba4d
movq %r14, %rax
xorl %r13d, %r13d
testq %rax, %rax
je 0xbaca
movq 0x18(%rsp), %rcx
movl %ecx, %ebp
movq (%rax), %rdi
callq 0x20e0
cmpl %eax, %ebp
cmovgl %ebp, %eax
movq %rax, 0x18(%rsp)
testq %r14, %r14
je 0xba77
movq 0x20(%r14), %r14
testq %r14, %r14
je 0xba81
movq %r14, %rax
jmp 0xbabf
cmpl $0x400, %ebx            # imm = 0x400
movl $0x3ff, %eax            # imm = 0x3FF
cmovgel %ebx, %eax
cmpl $0x3fe, %ebx            # imm = 0x3FE
jg 0xbabd
movslq %ebx, %rcx
movq %rax, %rdx
subq %rcx, %rdx
shlq $0x3, %rcx
xorl %esi, %esi
leaq (%rcx,%rsi,8), %rdi
movq (%r12,%rdi), %r14
testq %r14, %r14
jne 0xbac4
incq %rsi
cmpq %rsi, %rdx
jne 0xbaa6
movl %eax, %ebx
xorl %eax, %eax
incl %r13d
jmp 0xba50
addl %esi, %ebx
incl %ebx
jmp 0xba7c
movl %r13d, %eax
movq %rax, 0x20(%rsp)
leaq (,%rax,8), %rdi
callq 0x2230
movq %rax, 0x10(%rsp)
movl $0x1, %eax
xorl %esi, %esi
xorl %edx, %edx
movq (%rdx,%r12), %rcx
testq %rcx, %rcx
jne 0xbb0c
addq $0x8, %rdx
incl %eax
cmpq $0x1ff8, %rdx           # imm = 0x1FF8
jne 0xbaed
movl $0x3ff, %eax            # imm = 0x3FF
jmp 0xbb0f
movq %rcx, %rsi
testq %rsi, %rsi
je 0xbb7e
movq 0x10(%rsp), %rdx
movq %rsi, (%rdx)
testq %rcx, %rcx
je 0xbb25
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
je 0xbb2f
movq %rcx, %rsi
jmp 0xbb6c
cmpl $0x400, %eax            # imm = 0x400
movl $0x3ff, %esi            # imm = 0x3FF
cmovgel %eax, %esi
cmpl $0x3fe, %eax            # imm = 0x3FE
jg 0xbb6a
movslq %eax, %rdi
movq %rsi, %r8
subq %rdi, %r8
shlq $0x3, %rdi
xorl %r9d, %r9d
leaq (%rdi,%r9,8), %rcx
movq (%r12,%rcx), %rcx
testq %rcx, %rcx
jne 0xbb77
incq %r9
cmpq %r9, %r8
jne 0xbb53
movl %esi, %eax
xorl %esi, %esi
addq $0x8, %rdx
testq %rsi, %rsi
jne 0xbb19
jmp 0xbb7e
addl %r9d, %eax
incl %eax
jmp 0xbb2a
leaq 0x21c(%rip), %rcx       # 0xbda1
movl $0x8, %edx
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x20b0
movq 0x18(%rsp), %rax
leal 0x12(%rax), %ecx
movl $0x84, %eax
xorl %edx, %edx
idivl %ecx
movl %eax, %ecx
leal (%rbx,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %eax, 0xc(%rsp)
testl %eax, %eax
jle 0xbd4f
movl 0xc(%rsp), %eax
movq %rax, 0x30(%rsp)
leaq (,%rax,8), %r12
xorl %eax, %eax
movq %rax, 0x28(%rsp)
cmpl %eax, 0x20(%rsp)
jle 0xbd29
movq 0x28(%rsp), %rax
movl %eax, %ebx
movq 0x10(%rsp), %r14
movq (%r14), %rbp
movq (%r15), %rdi
movq (%rbp), %rcx
leaq 0x4eea(%rip), %rsi      # 0x10aec
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x21e0
movq 0x18(%rbp), %rax
testb $0x20, 0xc(%rax)
movl $0x3d, %edi
je 0xbc22
movl $0x20, %edi
movq (%r15), %rsi
callq 0x2170
testb $0x8, 0x10(%rbp)
movq (%r15), %rcx
jne 0xbc4b
movl $0x6, %esi
movl $0x1, %edx
leaq 0x4ead(%rip), %rdi      # 0x10af1
callq 0x22d0
jmp 0xbc60
movzwl 0x8(%rbp), %edx
movq %rcx, %rdi
leaq 0x4e9f(%rip), %rsi      # 0x10af8
xorl %eax, %eax
callq 0x21e0
movq (%r15), %rsi
movq 0x18(%rbp), %rax
testb $0x20, 0xc(%rax)
movl $0x20, %r13d
movl $0x20, %edi
je 0xbc7d
movl $0x52, %edi
callq 0x2170
movq (%r15), %rsi
testb $0x2, 0x10(%rbp)
movl $0x20, %edi
je 0xbc95
movl $0x47, %edi
callq 0x2170
movq (%r15), %rsi
testb $0x40, 0x10(%rbp)
movl $0x20, %edi
je 0xbcad
movl $0x58, %edi
callq 0x2170
movq (%r15), %rsi
testb $0x20, 0x10(%rbp)
movl $0x20, %edi
je 0xbcc5
movl $0x4c, %edi
callq 0x2170
movq (%r15), %rsi
testb $0x4, 0x10(%rbp)
je 0xbcd9
movl $0x57, %r13d
movl %r13d, %edi
callq 0x2170
movq 0x18(%rbp), %rax
movl 0x18(%rax), %edx
movq (%r15), %rcx
testl %edx, %edx
je 0xbd02
movq %rcx, %rdi
leaq 0x4e04(%rip), %rsi      # 0x10afd
xorl %eax, %eax
callq 0x21e0
jmp 0xbd18
movl $0x6, %esi
movl $0x1, %edx
leaq 0x4df2(%rip), %rdi      # 0x10b05
callq 0x22d0
addq %r12, %r14
addl 0xc(%rsp), %ebx
cmpl %ebx, 0x20(%rsp)
jg 0xbbf1
movq (%r15), %rsi
movl $0xa, %edi
callq 0x2170
movq 0x28(%rsp), %rax
incq %rax
addq $0x8, 0x10(%rsp)
cmpq 0x30(%rsp), %rax
jne 0xbbd6
movq (%r15), %rcx
leaq 0x4db3(%rip), %rdi      # 0x10b0c
movl $0x15, %esi
movl $0x1, %edx
callq 0x22d0
leaq 0x9d01(%rip), %rbx      # 0x15a70
cmpl $0x0, (%rbx)
jle 0xbd92
xorl %r14d, %r14d
leaq 0x94f2(%rip), %r15      # 0x15270
movq (%r15,%r14,8), %rdi
callq 0xbdb2
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0xbd7e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq