// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/crossassemblers/macro11/macros.c
// Function: defmacro
MACRO          *defmacro(
    char *cp,
    STACK *stack,
    int called)
{
    MACRO          *mac;
    ARG            *arg,
                  **argtail;
    char           *label;

    cp = skipwhite(cp);
    label = get_symbol(cp, &cp, NULL);
    if (label == NULL) {
        report(stack->top, "Invalid macro definition\n");
        return NULL;
    }

    /* Allow redefinition of a macro; new definition replaces the old. */
    mac = (MACRO *) lookup_sym(label, &macro_st);
    if (mac) {
        /* Remove from the symbol table... */
        remove_sym(&mac->sym, &macro_st);
        free_macro(mac);
    }

    mac = new_macro(label);

    add_table(&mac->sym, &macro_st);

    argtail = &mac->args;
    cp = skipdelim(cp);

    while (!EOL(*cp)) {
        arg = new_arg();
        arg->locsym = *cp == '?';
        if (arg->locsym) /* special argument flag? */
            cp++;
        arg->label = get_symbol(cp, &cp, NULL);
        if (arg->label == NULL) {
            /* It turns out that I have code which is badly formatted
               but which MACRO.SAV assembles.  Sigh.  */
            /* So, just quit defining arguments. */
            break;
#if 0
            report(str, "Illegal macro argument\n");
            remove_sym(&mac->sym, &macro_st);
            free_macro(mac);
            return NULL;
#endif
        }

        cp = skipwhite(cp);
        if (*cp == '=') {
            /* Default substitution given */
            arg->value = getstring(cp + 1, &cp);
            if (arg->value == NULL) {
                report(stack->top, "Illegal macro argument\n");
                remove_sym(&mac->sym, &macro_st);
                free_macro(mac);
                return NULL;
            }
        }

        /* Append to list of arguments */
        arg->next = NULL;
        *argtail = arg;
        argtail = &arg->next;

        cp = skipdelim(cp);
    }

    /* Read the stream in until the end marker is hit */  {
        BUFFER         *gb;
        int             levelmod = 0;

        gb = new_buffer();

        if (!called && !list_md) {
            list_level--;
            levelmod = 1;
        }

        read_body(stack, gb, mac->sym.label, called);

        list_level += levelmod;

        if (mac->text != NULL)         /* Discard old macro body */
            buffer_free(mac->text);

        mac->text = gb;
    }

    return mac;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rsp, %r15
movq %rdi, (%r15)
callq 0x88a0
movq %rax, (%r15)
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x8c1a
testq %rax, %rax
je 0x8112
movq %rax, %r15
movq %r14, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
leaq 0x1570f(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x9ff5
testq %rax, %rax
je 0x7fd0
movq %rax, %r12
leaq 0x156f8(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x9f8c
movq %r12, %rdi
callq 0x816a
movq %r15, %rdi
callq 0x818e
movq %rax, %r15
leaq 0x156d6(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0xa0ce
movq %r15, %rbp
addq $0x28, %rbp
movq %rsp, %r12
movabsq $0x800000000000401, %r14 # imm = 0x800000000000401
movq (%rsp), %rdi
callq 0x88b6
movq %rax, (%rsp)
movzbl (%rax), %eax
cmpq $0x3b, %rax
ja 0x801e
btq %rax, %r14
jb 0x80bc
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe34f
movq %rax, %r13
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq (%rsp), %rax
xorl %ecx, %ecx
cmpb $0x3f, (%rax)
sete %cl
movl %ecx, 0x8(%r13)
jne 0x8061
incq %rax
movq %rax, (%rsp)
leaq 0x10(%r13), %rbx
movq (%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x8c1a
movq %rax, (%rbx)
testq %rax, %rax
je 0x80bc
movq (%rsp), %rdi
callq 0x88a0
movq %rax, (%rsp)
cmpb $0x3d, (%rax)
jne 0x80a8
incq %rax
movq %rax, %rdi
movq %r12, %rsi
callq 0x8913
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x8138
movq $0x0, (%r13)
movq %r13, (%rbp)
movq %r13, %rbp
jmp 0x7ffe
callq 0xdd0c
movq %rax, %r12
leaq 0xd9a9(%rip), %rax      # 0x15a74
xorl %ebp, %ebp
movl (%rax), %eax
movl 0xc(%rsp), %ecx
orl %ecx, %eax
jne 0x80e5
leaq 0xd9a2(%rip), %rax      # 0x15a80
decl (%rax)
movl $0x1, %ebp
movq (%r15), %rdx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x7d55
leaq 0xd984(%rip), %rax      # 0x15a80
addl %ebp, (%rax)
movq 0x30(%r15), %rdi
testq %rdi, %rdi
je 0x810c
callq 0xdd78
movq %r12, 0x30(%r15)
jmp 0x8126
movq (%r14), %rdi
leaq 0x8416(%rip), %rsi      # 0x10532
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x7b5a
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
movq (%rax), %rdi
leaq 0x8405(%rip), %rsi      # 0x1054c
xorl %eax, %eax
callq 0x7b5a
leaq 0x15563(%rip), %rsi     # 0x1d6b8
movq %r15, %rdi
callq 0x9f8c
movq %r15, %rdi
callq 0x816a
xorl %r15d, %r15d
jmp 0x8126