// File: /workspace/github_c_makefile_star2_O3/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 %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x8(%rsp), %rax
movl $0x0, (%rax)
leaq 0x20(%rsp), %r14
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movzbl (%r12), %eax
testl %eax, %eax
je 0x833a
cmpl $0xa, %eax
je 0x833a
cmpb $0x3b, %al
jne 0x8345
cmpl $0x0, 0x8(%rsp)
je 0x84da
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x8af9
testq %rax, %rax
je 0x839d
movq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x8744
movq %rax, 0x20(%rsp)
cmpb $0x3d, (%rax)
jne 0x8395
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rbx
testq %rbx, %rbx
je 0x8395
movq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x21d0
testl %eax, %eax
je 0x8462
movq (%rbx), %rbx
jmp 0x8377
movq %r15, %rdi
callq 0x2050
movq 0x18(%rsp), %rax
movq 0x28(%rax), %r15
movq %r15, %rbp
testq %r15, %r15
je 0x84da
testq %r13, %r13
je 0x83e9
movq %r13, %r14
movq 0x10(%rbp), %rbx
movq 0x10(%r14), %rdi
movq %rbx, %rsi
callq 0x21d0
testl %eax, %eax
je 0x83d8
movq (%r14), %r14
testq %r14, %r14
jne 0x83be
jmp 0x83e6
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x83b2
jmp 0x84da
movq %rbp, %r15
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe094
movq %rax, %rbx
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x10(%r15), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe094
movq %rax, 0x10(%rbx)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %r14
movq %r14, %rdx
callq 0x82c4
movq %rax, 0x18(%rbx)
incl 0xc(%rsp)
movq %r13, (%rbx)
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x8785
movq %rax, %r12
movq %rbx, %r13
jmp 0x8328
testq %r13, %r13
je 0x8486
movq %r13, %rbx
movq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x21d0
testl %eax, %eax
je 0x8646
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x846a
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe094
movq %rax, %rbx
movl $0x0, 0x8(%rax)
xorl %eax, %eax
movq %rax, (%rbx)
movq %rax, 0x18(%rbx)
movq %r15, 0x10(%rbx)
movq 0x20(%rsp), %rdi
incq %rdi
callq 0x8744
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x82c4
movq %rax, 0x18(%rbx)
jmp 0x8445
leaq 0xd693(%rip), %rax      # 0x15b74
leaq 0xd680(%rip), %rcx      # 0x15b68
movl (%rcx), %ecx
cmpl %ecx, (%rax)
jne 0x84fa
leaq 0xccaf(%rip), %rdx      # 0x151a4
movl (%rdx), %r12d
jmp 0x850d
leaq 0xcca3(%rip), %rdx      # 0x151a4
movl $0x8000, (%rdx)         # imm = 0x8000
movl $0x8000, %r12d          # imm = 0x8000
movl %ecx, (%rax)
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rbp
testq %rbp, %rbp
je 0x85fc
leaq 0x20(%rsp), %r14
testq %r13, %r13
je 0x854a
movq 0x10(%rbp), %rbx
movq %r13, %r15
movq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x21d0
testl %eax, %eax
je 0x85c1
movq (%r15), %r15
testq %r15, %r15
jne 0x8532
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe094
movq %rax, %r15
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x10(%rbp), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe094
movq %rax, 0x10(%r15)
cmpl $0x0, 0x8(%rbp)
je 0x85c6
leal 0x1(%r12), %ebx
movq %r14, %rdi
leaq 0x7ffc(%rip), %rsi      # 0x1059a
movl %r12d, %edx
xorl %eax, %eax
callq 0x22b0
movq %r14, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe094
movq %rax, 0x18(%r15)
movl %ebx, %r12d
jmp 0x85e7
movq %r13, %r15
jmp 0x85ed
movq 0x18(%rbp), %rdi
testq %rdi, %rdi
jne 0x85d6
leaq 0x8557(%rip), %rdi      # 0x10b2d
callq 0x22e0
movq %rax, %rdi
callq 0xe094
movq %rax, 0x18(%r15)
movq %r13, (%r15)
movq %r15, %r13
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x8526
jmp 0x85ff
movq %r13, %r15
leaq 0xcb9e(%rip), %rax      # 0x151a4
movl %r12d, (%rax)
movq 0x18(%rsp), %rbx
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0x80dc
movq %rax, %r14
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
movl 0xc(%rsp), %ecx
callq 0x7b89
movq %rax, %rbx
movq %r15, %rdi
callq 0x8680
movq %r14, %rdi
callq 0xdac0
jmp 0x866e
leaq 0x7f20(%rip), %rsi      # 0x1056d
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x7a34
movq %r15, %rdi
callq 0x2050
movq %r13, %rdi
callq 0x8680
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq