// File: /workspace/github_c_makefile_star2_O1/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 %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsp, %rax
movl $0x0, (%rax)
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movzbl (%rdx), %eax
testl %eax, %eax
je 0x8463
cmpl $0xa, %eax
je 0x8463
cmpb $0x3b, %al
jne 0x846d
cmpl $0x0, (%rsp)
je 0x8656
movq %rdx, 0x18(%rsp)
movq %rdx, %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x8c1a
testq %rax, %rax
je 0x84cc
movq %rax, %r14
movq 0x20(%rsp), %rdi
callq 0x88a0
movq %rax, 0x20(%rsp)
cmpb $0x3d, (%rax)
jne 0x84c4
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rbx
testq %rbx, %rbx
je 0x84c4
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x21d0
testl %eax, %eax
je 0x8597
movq (%rbx), %rbx
jmp 0x84a6
movq %r14, %rdi
callq 0x2050
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rbx
movl $0x3, %r13d
movq %rbx, %r15
testq %rbx, %rbx
je 0x863e
testq %r12, %r12
je 0x851e
movq 0x10(%r15), %r14
movq %r12, %rbp
movq 0x10(%rbp), %rdi
movq %r14, %rsi
callq 0x21d0
testl %eax, %eax
je 0x850e
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x84f3
jmp 0x851b
movq (%r15), %r15
testq %r15, %r15
jne 0x84e7
jmp 0x863e
movq %r15, %rbx
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe34f
movq %rax, %r15
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x10(%rbx), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe34f
movq %rax, 0x10(%r15)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x83f9
movq %rax, 0x18(%r15)
incl 0x4(%rsp)
movq %r12, (%r15)
movq 0x20(%rsp), %rdi
movq %rsp, %rsi
callq 0x88e1
movq %rax, %rdx
xorl %r13d, %r13d
movq %r15, %r12
jmp 0x8643
testq %r12, %r12
je 0x85b7
movq %r12, %rbx
movq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x21d0
testl %eax, %eax
je 0x8612
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x859f
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe34f
movq %rax, %r15
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq %r14, 0x10(%rax)
movq 0x20(%rsp), %rdi
incq %rdi
callq 0x88a0
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movq %rax, %rsi
leaq 0x20(%rsp), %rdx
callq 0x83f9
movq %rax, 0x18(%r15)
jmp 0x8579
movq 0x8(%rsp), %rdi
leaq 0x7f5f(%rip), %rsi      # 0x1057d
movq %r14, %rdx
xorl %eax, %eax
callq 0x7b5a
movq %r14, %rdi
callq 0x2050
movq %r12, %rdi
callq 0x87d1
movl $0x1, %r13d
movq 0x18(%rsp), %rdx
testl %r13d, %r13d
je 0x8453
cmpl $0x3, %r13d
jne 0x8774
leaq 0xd517(%rip), %rax      # 0x15b74
leaq 0xd504(%rip), %rcx      # 0x15b68
movl (%rcx), %ecx
cmpl %ecx, (%rax)
jne 0x8675
leaq 0xcb33(%rip), %rdx      # 0x151a4
movl (%rdx), %ebx
jmp 0x8687
leaq 0xcb28(%rip), %rdx      # 0x151a4
movl $0x8000, (%rdx)         # imm = 0x8000
movl $0x8000, %ebx           # imm = 0x8000
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
movq 0x28(%rax), %r13
testq %r13, %r13
je 0x8778
leaq 0x20(%rsp), %r14
testq %r12, %r12
je 0x86c5
movq 0x10(%r13), %r15
movq %r12, %rbp
movq 0x10(%rbp), %rdi
movq %r15, %rsi
callq 0x21d0
testl %eax, %eax
je 0x8739
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x86ac
movl $0x20, %edi
callq 0x2230
movq %rax, %rdi
callq 0xe34f
movq %rax, %r15
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x10(%r13), %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe34f
movq %rax, 0x10(%r15)
cmpl $0x0, 0x8(%r13)
je 0x873e
leal 0x1(%rbx), %ebp
movq %r14, %rdi
leaq 0x7e92(%rip), %rsi      # 0x105aa
movl %ebx, %edx
xorl %eax, %eax
callq 0x22b0
movq %r14, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0xe34f
movq %rax, 0x18(%r15)
movl %ebp, %ebx
jmp 0x875f
movq %r12, %r15
jmp 0x8765
movq 0x18(%r13), %rdi
testq %rdi, %rdi
jne 0x874e
leaq 0x83d3(%rip), %rdi      # 0x10b21
callq 0x22e0
movq %rax, %rdi
callq 0xe34f
movq %rax, 0x18(%r15)
movq %r12, (%r15)
movq %r15, %r12
movq (%r13), %r13
testq %r13, %r13
jne 0x86a0
jmp 0x877b
xorl %ebx, %ebx
jmp 0x87bf
movq %r12, %r15
leaq 0xca22(%rip), %rax      # 0x151a4
movl %ebx, (%rax)
movq 0x10(%rsp), %rbx
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0x8201
movq %rax, %r14
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
movl 0x4(%rsp), %ecx
callq 0x7cad
movq %rax, %rbx
movq %r15, %rdi
callq 0x87d1
movq %r14, %rdi
callq 0xdd78
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq