// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/crossassemblers/macro11/symbols.c
// Function: add_sym
SYMBOL         *add_sym(
    char *labelraw,
    unsigned value,
    unsigned flags,
    SECTION *section,
    SYMBOL_TABLE *table)
{
    SYMBOL         *sym;
    char            label[SYMMAX_MAX + 1];      // big size

    if (isdigit((unsigned char)labelraw[0])) {
        // Don't truncate local labels
        strncpy(label, labelraw, SYMMAX_MAX);
        label[SYMMAX_MAX] = 0;
    } else {
        //JH: truncate symbol to SYMMAX
        strncpy(label, labelraw, symbol_len);
        label[symbol_len] = 0;
    }

    sym = lookup_sym(label, table);
    if (sym != NULL) {
        // A symbol registered as "undefined" can be changed.

        if ((sym->flags & SYMBOLFLAG_UNDEFINED) && !(flags & SYMBOLFLAG_UNDEFINED)) {
            sym->flags &= ~(SYMBOLFLAG_PERMANENT | SYMBOLFLAG_UNDEFINED);
        }

        /* Check for compatible definition */
        else if (sym->section == section && sym->value == value) {
            sym->flags |= flags;       /* Merge flags quietly */
            return sym;                /* 's okay */
        }

        if (!(sym->flags & SYMBOLFLAG_PERMANENT)) {
            /* permit redefinition */
            sym->value = value;
            sym->flags |= flags;
            sym->section = section;
            return sym;
        }

        return NULL;                   /* Bad symbol redefinition */
    }

    sym = new_sym(label);
    sym->flags = flags;
    sym->stmtno = stmtno;
    sym->section = section;
    sym->value = value;

    add_table(sym, table);

    return sym;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %rbx
movq %rcx, %r15
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r14
callq 0x2300
movq (%rax), %rax
movzbl (%r14), %ecx
testb $0x8, 0x1(%rax,%rcx,2)
jne 0xa16a
movslq 0xb993(%rip), %r13      # 0x15ae8
movq %rsp, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2080
movb $0x0, (%rsp,%r13)
jmp 0xa182
movq %rsp, %r13
movl $0x40, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2080
movb $0x0, 0x40(%r13)
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x9ff5
testq %rax, %rax
je 0xa1b9
movq %rax, %r14
movl 0x10(%rax), %eax
testb $0x10, %bpl
jne 0xa256
movl %eax, %ecx
andl $0x10, %ecx
je 0xa256
andl $-0x12, %eax
movl %eax, 0x10(%r14)
jmp 0xa26a
movl $0x28, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe34f
movq %rax, %r14
movq %rsp, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe34f
movq %rax, (%r14)
movq $0x0, 0x18(%r14)
movl $0x0, 0x8(%r14)
movl %ebp, 0x10(%r14)
leaq 0xb968(%rip), %rcx      # 0x15b64
movl (%rcx), %ecx
movl %ecx, 0xc(%r14)
movq %r15, 0x18(%r14)
movl %r12d, 0x8(%r14)
movb (%rax), %dl
xorl %ecx, %ecx
testb %dl, %dl
je 0xa248
incq %rax
movl %ecx, %esi
addl %ecx, %esi
movsbl %dl, %ecx
xorl %esi, %ecx
movb (%rax), %dl
incq %rax
testb %dl, %dl
jne 0xa215
movl %ecx, %eax
imulq $0x401005, %rax, %rax   # imm = 0x401005
shrq $0x20, %rax
movl %ecx, %edx
subl %eax, %edx
shrl %edx
addl %eax, %edx
shrl $0x9, %edx
movl %edx, %eax
shll $0xa, %eax
subl %eax, %edx
addl %edx, %ecx
movq (%rbx,%rcx,8), %rax
movq %rax, 0x20(%r14)
movq %r14, (%rbx,%rcx,8)
jmp 0xa285
cmpq %r15, 0x18(%r14)
jne 0xa26a
cmpl %r12d, 0x8(%r14)
jne 0xa26a
orl %ebp, %eax
movl %eax, 0x10(%r14)
jmp 0xa285
movl 0x10(%r14), %eax
testb $0x1, %al
jne 0xa282
movl %r12d, 0x8(%r14)
orl %ebp, %eax
movl %eax, 0x10(%r14)
movq %r15, 0x18(%r14)
jmp 0xa285
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq