// File: /workspace/github_c_makefile_star2_O2/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 0x8234
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rax, %rdi
xorl %edx, %edx
callq 0x8597
testq %rax, %rax
je 0x7bec
movq %rax, %r15
movq %r14, 0x10(%rsp)
leaq 0x15c09(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x974c
testq %rax, %rax
je 0x7ad6
movq %rax, %r12
leaq 0x15bf2(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x970c
movq %r12, %rdi
callq 0x7c44
movq %r15, %rdi
callq 0x7c63
movq %rax, %r15
leaq 0x15bd0(%rip), %rsi     # 0x1d6b8
movq %rax, %rdi
callq 0x97cb
movq %r15, %rbp
addq $0x28, %rbp
leaq 0x8(%rsp), %r12
movabsq $0x800000000000401, %r14 # imm = 0x800000000000401
movq 0x8(%rsp), %rdi
callq 0x824a
movq %rax, 0x8(%rsp)
movzbl (%rax), %eax
cmpq $0x3b, %rax
ja 0x7b24
btq %rax, %r14
jb 0x7b9a
callq 0x7ca7
movq %rax, %r13
movq 0x8(%rsp), %rdi
xorl %eax, %eax
cmpb $0x3f, (%rdi)
sete %al
movl %eax, 0x8(%r13)
jne 0x7b47
incq %rdi
movq %rdi, 0x8(%rsp)
movq %r12, %rsi
xorl %edx, %edx
callq 0x8597
movq %rax, 0x10(%r13)
testq %rax, %rax
je 0x7b9a
movq 0x8(%rsp), %rdi
callq 0x8234
movq %rax, 0x8(%rsp)
cmpb $0x3d, (%rax)
jne 0x7b89
incq %rax
movq %rax, %rdi
movq %r12, %rsi
callq 0x8286
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0x7c12
andq $0x0, (%r13)
movq %r13, (%rbp)
movq %r13, %rbp
jmp 0x7b06
callq 0xc798
movq %rax, %r12
leaq 0xdecb(%rip), %rax      # 0x15a74
xorl %ebp, %ebp
movl (%rax), %eax
orl %ebx, %eax
jne 0x7bbd
leaq 0xdec8(%rip), %rax      # 0x15a80
decl (%rax)
pushq $0x1
popq %rbp
movq (%r15), %rdx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movl %ebx, %ecx
callq 0x7863
leaq 0xdeaa(%rip), %rax      # 0x15a80
addl %ebp, (%rax)
movq 0x30(%r15), %rdi
testq %rdi, %rdi
je 0x7be6
callq 0xc802
movq %r12, 0x30(%r15)
jmp 0x7c00
movq (%r14), %rdi
leaq 0x7941(%rip), %rsi      # 0xf537
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x7668
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 0x7930(%rip), %rsi      # 0xf551
xorl %eax, %eax
callq 0x7668
leaq 0x15a89(%rip), %rsi     # 0x1d6b8
movq %r15, %rdi
callq 0x970c
movq %r15, %rdi
callq 0x7c44
xorl %r15d, %r15d
jmp 0x7c00