// File: /workspace/github_c_makefile_star2_O3/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
callq 0x8744
movq %rsp, %rsi
movq %rax, (%rsi)
movq %rax, %rdi
xorl %edx, %edx
callq 0x8af9
testq %rax, %rax
je 0x7fed
movq %rax, %r15
movq %r14, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
leaq 0x15830(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x9e32
testq %rax, %rax
je 0x7eaf
movq %rax, %r12
leaq 0x15819(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x9dc9
movq %r12, %rdi
callq 0x8045
movq %r15, %rdi
callq 0x8069
movq %rax, %r15
leaq 0x157f7(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x9f26
movq %r15, %rbp
addq $0x28, %rbp
movq %rsp, %r12
movabsq $0x800000000000401, %r14 # imm = 0x800000000000401
movq (%rsp), %rdi
callq 0x875a
movq %rax, (%rsp)
movzbl (%rax), %eax
cmpq $0x3b, %rax
ja 0x7efd
btq %rax, %r14
jb 0x7f97
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe094
movq %rax, %r13
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq (%rsp), %rdi
xorl %eax, %eax
cmpb $0x3f, (%rdi)
sete %al
movl %eax, 0x8(%r13)
jne 0x7f40
incq %rdi
movq %rdi, (%rsp)
leaq 0x10(%r13), %rbx
movq %r12, %rsi
xorl %edx, %edx
callq 0x8af9
movq %rax, (%rbx)
testq %rax, %rax
je 0x7f97
movq (%rsp), %rdi
callq 0x8744
movq %rax, (%rsp)
cmpb $0x3d, (%rax)
jne 0x7f83
incq %rax
movq %rax, %rdi
movq %r12, %rsi
callq 0x87b7
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x8013
movq $0x0, (%r13)
movq %r13, (%rbp)
movq %r13, %rbp
jmp 0x7edd
callq 0xda54
movq %rax, %r12
leaq 0xdace(%rip), %rax      # 0x15a74
xorl %ebp, %ebp
movl (%rax), %eax
movl 0xc(%rsp), %ecx
orl %ecx, %eax
jne 0x7fc0
leaq 0xdac7(%rip), %rax      # 0x15a80
decl (%rax)
movl $0x1, %ebp
movq (%r15), %rdx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x7c31
leaq 0xdaa9(%rip), %rax      # 0x15a80
addl %ebp, (%rax)
movq 0x30(%r15), %rdi
testq %rdi, %rdi
je 0x7fe7
callq 0xdac0
movq %r12, 0x30(%r15)
jmp 0x8001
movq (%r14), %rdi
leaq 0x852b(%rip), %rsi      # 0x10522
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x7a34
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 0x851a(%rip), %rsi      # 0x1053c
xorl %eax, %eax
callq 0x7a34
leaq 0x15688(%rip), %rsi     # 0x1d6b8
movq %r15, %rdi
callq 0x9dc9
movq %r15, %rdi
callq 0x8045
xorl %r15d, %r15d
jmp 0x8001