// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/crossassemblers/macro11/macros.c
// Function: expandmacro
STREAM         *expandmacro(
    STREAM *refstr,
    MACRO *mac,
    char *cp)
{
    ARG            *arg,
                   *args,
                   *macarg;
    char           *label;
    STREAM         *str;
    BUFFER         *buf;
    int             nargs;
    int             onemore;

    args = NULL;
    arg = NULL;
    nargs = 0;      /* for the .NARG directive */
    onemore = 0;

    /* Parse the arguments */

    while (!EOL(*cp) || onemore) {
        char           *nextcp;

        /* Check for named argument */
        label = get_symbol(cp, &nextcp, NULL);
        if (label && (nextcp = skipwhite(nextcp), *nextcp == '=') && (macarg = find_arg(mac->args, label))) {
            /* Check if I've already got a value for it */
            if (find_arg(args, label) != NULL) {
                report(refstr, "Duplicate submission of keyword " "argument %s\n", label);
                free(label);
                free_args(args);
                return NULL;
            }

            arg = new_arg();
            arg->label = label;
            nextcp = skipwhite(nextcp + 1);
            arg->value = getstring_macarg(refstr, nextcp, &nextcp);
        } else {
            if (label)
                free(label);

            /* Find correct positional argument */

            for (macarg = mac->args; macarg != NULL; macarg = macarg->next) {
                if (find_arg(args, macarg->label) == NULL)
                    break;             /* This is the next positional arg */
            }

            if (macarg == NULL)
                break;                 /* Don't pick up any more arguments. */

            arg = new_arg();
            arg->label = memcheck(strdup(macarg->label));       /* Copy the name */
            arg->value = getstring_macarg(refstr, cp, &nextcp);
            nargs++;                   /* Count nonkeyword arguments only. */
        }

        arg->next = args;
        args = arg;

        /* If there is a trailing comma, there is an empty last argument */
        cp = skipdelim_comma(nextcp, &onemore);
    }

    /* Now go back and fill in defaults */  {
        int             locsym;

        if (last_macro_lsb != lsb)
            locsym = last_locsym = 32768;
        else
            locsym = last_locsym;
        last_macro_lsb = lsb;

        for (macarg = mac->args; macarg != NULL; macarg = macarg->next) {
            arg = find_arg(args, macarg->label);
            if (arg == NULL) {
                arg = new_arg();
                arg->label = memcheck(strdup(macarg->label));
                if (macarg->locsym) {
                    char            temp[32];

                    /* Here's where we generate local labels */
                    sprintf(temp, "%d$", locsym++);
                    arg->value = memcheck(strdup(temp));
                } else if (macarg->value) {
                    arg->value = memcheck(strdup(macarg->value));
                } else
                    arg->value = memcheck(strdup(""));

                arg->next = args;
                args = arg;
            }
        }

        last_locsym = locsym;
    }

    buf = subst_args(mac->text, args);

    str = new_macro_stream(refstr, buf, mac, nargs);

    free_args(args);
    buffer_free(buf);

    return str;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r12
movq %rdi, 0x10(%rsp)
leaq 0x8(%rsp), %rax
andl $0x0, (%rax)
movq %rsi, 0x18(%rsp)
leaq 0x28(%rsi), %r14
xorl %r15d, %r15d
leaq 0x20(%rsp), %r13
movl $0x0, 0xc(%rsp)
movzbl (%r12), %eax
testl %eax, %eax
je 0x7f1b
cmpl $0xa, %eax
je 0x7f1b
cmpb $0x3b, %al
jne 0x7f26
cmpl $0x0, 0x8(%rsp)
je 0x801e
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x8597
testq %rax, %rax
je 0x7faf
movq %rax, %rbp
movq 0x20(%rsp), %rdi
callq 0x8234
movq %rax, 0x20(%rsp)
cmpb $0x3d, (%rax)
jne 0x7fa7
movq (%r14), %rdi
movq %rbp, %rsi
callq 0x7df2
testq %rax, %rax
je 0x7fa7
movq %r15, %rdi
movq %rbp, %rsi
callq 0x7df2
testq %rax, %rax
jne 0x814d
callq 0x7ca7
movq %rax, %rbx
movq %rbp, 0x10(%rax)
movq 0x20(%rsp), %rdi
incq %rdi
callq 0x8234
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x7ea4
movq %rax, 0x18(%rbx)
jmp 0x8001
movq %rbp, %rdi
callq 0x2050
movq %r14, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x801e
movq 0x10(%rbp), %rsi
movq %r15, %rdi
callq 0x7df2
testq %rax, %rax
jne 0x7fb2
callq 0x7ca7
movq %rax, %rbx
movq 0x10(%rbp), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xcd2d
movq %rax, 0x10(%rbx)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x7ea4
movq %rax, 0x18(%rbx)
incl 0xc(%rsp)
movq %r15, (%rbx)
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x8263
movq %rax, %r12
movq %rbx, %r15
jmp 0x7f09
leaq 0xdb4f(%rip), %rax      # 0x15b74
leaq 0xdb3c(%rip), %rcx      # 0x15b68
movl (%rcx), %ecx
cmpl %ecx, (%rax)
jne 0x803e
leaq 0xd16b(%rip), %rdx      # 0x151a4
movl (%rdx), %r13d
jmp 0x8051
leaq 0xd15f(%rip), %rdx      # 0x151a4
movl $0x8000, (%rdx)         # imm = 0x8000
movl $0x8000, %r13d          # imm = 0x8000
movl %ecx, (%rax)
movq %r15, %r12
movq (%r14), %r14
testq %r14, %r14
je 0x80f6
movq 0x10(%r14), %rsi
movq %r12, %rdi
callq 0x7df2
testq %rax, %rax
jne 0x8056
callq 0x7ca7
movq %rax, %r15
movq 0x10(%r14), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xcd2d
movq %rax, 0x10(%r15)
cmpl $0x0, 0x8(%r14)
je 0x80cd
leal 0x1(%r13), %ebx
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
leaq 0x7505(%rip), %rsi      # 0xf5af
movl %r13d, %edx
xorl %eax, %eax
callq 0x22b0
movq %rbp, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xcd2d
movq %rax, 0x18(%r15)
movl %ebx, %r13d
jmp 0x80ee
movq 0x18(%r14), %rdi
testq %rdi, %rdi
jne 0x80dd
leaq 0x7aa0(%rip), %rdi      # 0xfb7d
callq 0x22e0
movq %rax, %rdi
callq 0xcd2d
movq %rax, 0x18(%r15)
movq %r12, (%r15)
jmp 0x8053
leaq 0xd0a7(%rip), %rax      # 0x151a4
movl %r13d, (%rax)
movq 0x18(%rsp), %rbx
movq 0x30(%rbx), %rdi
movq %r12, %rsi
callq 0x7cc9
movq %rax, %r15
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
movl 0xc(%rsp), %ecx
callq 0x77bd
movq %rax, %rbx
movq %r12, %rdi
callq 0x8177
movq %r15, %rdi
callq 0xc802
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x742e(%rip), %rsi      # 0xf582
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x7668
movq %rbp, %rdi
callq 0x2050
movq %r15, %rdi
callq 0x8177
jmp 0x813b