// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, (%rax)
je 0xb54e
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0xb54e
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x3b, %ecx
movb %al, -0x91(%rbp)
jne 0xb55b
cmpl $0x0, -0x58(%rbp)
setne %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0xb56a
jmp 0xb720
movq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xbf60
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xb655
movq -0x60(%rbp), %rdi
callq 0xb9e0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3d, %eax
jne 0xb655
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0xb380
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0xb655
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xb380
cmpq $0x0, %rax
je 0xb60e
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0xa030(%rip), %rsi      # 0x15618
movb $0x0, %al
callq 0xa890
movq -0x40(%rbp), %rdi
callq 0x2040
movq -0x30(%rbp), %rdi
callq 0xb8f0
movq $0x0, -0x8(%rbp)
jmp 0xb8d9
callq 0xb150
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
callq 0xb9e0
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xb480
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xb6f7
cmpq $0x0, -0x40(%rbp)
je 0xb665
movq -0x40(%rbp), %rdi
callq 0x2040
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xb6a0
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xb380
cmpq $0x0, %rax
jne 0xb691
jmp 0xb6a0
jmp 0xb693
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xb671
cmpq $0x0, -0x38(%rbp)
jne 0xb6a9
jmp 0xb720
callq 0xb150
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x136a0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xb480
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x60(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xba70
movq %rax, -0x20(%rbp)
jmp 0xb525
leaq 0xe46d(%rip), %rax      # 0x19b94
movl (%rax), %eax
leaq 0xe458(%rip), %rcx      # 0x19b88
cmpl (%rcx), %eax
je 0xb74a
leaq 0xda59(%rip), %rax      # 0x19194
movl $0x8000, (%rax)         # imm = 0x8000
movl $0x8000, -0x64(%rbp)    # imm = 0x8000
jmp 0xb756
leaq 0xda43(%rip), %rax      # 0x19194
movl (%rax), %eax
movl %eax, -0x64(%rbp)
leaq 0xe42b(%rip), %rax      # 0x19b88
movl (%rax), %ecx
leaq 0xe42e(%rip), %rax      # 0x19b94
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xb886
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xb380
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xb874
callq 0xb150
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x136a0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xb813
leaq -0x90(%rbp), %rdi
movl -0x64(%rbp), %edx
movl %edx, %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
leaq 0x9e5a(%rip), %rsi      # 0x15645
movb $0x0, %al
callq 0x22a0
leaq -0x90(%rbp), %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x136a0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xb861
movq -0x38(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0xb840
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x136a0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xb85f
leaq 0xa4b8(%rip), %rdi      # 0x15cff
callq 0x22d0
movq %rax, %rdi
callq 0x136a0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0xb861
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xb876
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xb774
movl -0x64(%rbp), %ecx
leaq 0xd904(%rip), %rax      # 0x19194
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0xb1b0
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x54(%rbp), %ecx
callq 0xaa40
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0xb8f0
movq -0x50(%rbp), %rdi
callq 0x12c20
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)