// File: /workspace/github_cpp_makefile_star2_O1/sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_param_comment(char*, char*, int)
int Interp::convert_param_comment(char *comment, char *expanded, int len)
{
    int i;
    char param[LINELEN+1];
    int paramNumber;
    int stat;
    double value;
    char valbuf[VAL_LEN]; // max double length + room
    char *v;
    int found;

    while(*comment)
    {
        if(*comment == '#')
        {
            found = 0;
            logDebug("a parameter");

            // skip over the '#'
            comment++;
            CHKS((0 == *comment), NCE_NAMED_PARAMETER_NOT_TERMINATED);

            if(isdigit(*comment))  // is this numeric param?
            {
                logDebug("numeric parameter");
                for(i=0; isdigit(*comment)&& (i<LINELEN); i++)
                {
                    param[i] = *comment++;
                }
                param[i] = 0;
                paramNumber = atoi(param);
                if((paramNumber >= 0) &&
                   (paramNumber < RS274NGC_MAX_PARAMETERS))
                {
                    value = _setup.parameters[paramNumber];
                    found = 1;
                }
            }
            else if(*comment == '<')
            {
                logDebug("name parameter");
                // this is a name parameter
                // skip over the '<'
                comment++;
                CHKS((0 == *comment), NCE_NAMED_PARAMETER_NOT_TERMINATED);

                for(i=0; (')' != *comment) &&
                        (i<LINELEN) && (0 != *comment);)
                {
                    if('>' == *comment)
                    {
                        break;     // done
                    }
                    if(isspace(*comment)) // skip space inside the param
                    {
                        comment++;
                        continue;
                    }
                    else
                    {
		        // if tolower is a macro, may need this int
		        int c = *comment++;
                        param[i] = tolower(c);
                        i++;
                    }
                }
                if('>' != *comment)
                {
                    ERS(NCE_NAMED_PARAMETER_NOT_TERMINATED);
                    logDebug("parameter not terminated");
                }
                else
                {
                    comment++;
                }

                // terminate the name
                param[i] = 0;

                // now lookup the name
                find_named_param(param, &stat, &value);
                if(stat)
                {
                    found = 1;
                }
            }
            else
            {
                // neither numeric or name
                logDebug("neither numeric nor name");
                // just store the '#'
                *expanded++ = '#';

                CHKS((*comment == 0), NCE_NAMED_PARAMETER_NOT_TERMINATED);
                continue;
            }

            // we have a parameter -- now insert it
            // we have the value
            if(found)
            {
                int n = snprintf(valbuf, VAL_LEN, "%lf", value);
                bool fail = (n >= VAL_LEN || n < 0);
                if(fail)
                    strcpy(valbuf, "######");

            }
            else
            {
                strcpy(valbuf, "######");
            }
            logDebug("found:%d value:|%s|", found, valbuf);

            v = valbuf;
            while(*v)
            {
                *expanded++ = *v++;
            }
        }
        else  // not a '#'
        {
            *expanded++ = *comment++;
        }
    }
    *expanded = 0; // the final nul
    
    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, -0x30(%rbp)
movq 0x33c71(%rip), %rbx     # 0x43f60
movzbl (%r13), %eax
cmpl $0x23, %eax
je 0x1030c
testl %eax, %eax
je 0x10677
incq %r13
movb %al, (%r14)
incq %r14
jmp 0x102ef
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x1033d
callq 0xaf80
movq -0x30(%rbp), %rdi
leaq 0x280d6(%rip), %rsi     # 0x383fb
xorl %edx, %edx
movl %eax, %ecx
leaq 0x280ea(%rip), %r8      # 0x3841a
movl $0x4c2, %r9d            # imm = 0x4C2
xorl %eax, %eax
callq 0xb470
movsbl 0x1(%r13), %eax
testl %eax, %eax
je 0x105fe
leaq 0x1(%r13), %r15
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x103c3
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x10387
callq 0xaf80
movq -0x30(%rbp), %rdi
leaq 0x280dc(%rip), %rsi     # 0x3844b
xorl %edx, %edx
movl %eax, %ecx
leaq 0x280a0(%rip), %r8      # 0x3841a
movl $0x4ca, %r9d            # imm = 0x4CA
xorl %eax, %eax
callq 0xb470
movsbl (%r15), %eax
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x1041f
xorl %edx, %edx
movb %al, -0x160(%rbp,%rdx)
leaq 0x1(%rdx), %rcx
movsbl 0x2(%r13,%rdx), %eax
leal -0x30(%rax), %esi
cmpl $0x9, %esi
ja 0x103be
cmpq $0xfe, %rdx
movq %rcx, %rdx
jb 0x10399
addq %rcx, %r15
jmp 0x10421
movl 0x1b6b4(%rbx), %ecx
cmpb $0x3c, %al
jne 0x10456
testl %ecx, %ecx
jle 0x103fd
callq 0xaf80
movq -0x30(%rbp), %rdi
leaq 0x2808b(%rip), %rsi     # 0x38470
xorl %edx, %edx
movl %eax, %ecx
leaq 0x2802a(%rip), %r8      # 0x3841a
movl $0x4da, %r9d            # imm = 0x4DA
xorl %eax, %eax
callq 0xb470
cmpb $0x0, 0x2(%r13)
je 0x105fe
addq $0x2, %r13
movb (%r13), %al
cmpb $0x29, %al
jne 0x104a8
xorl %ecx, %ecx
jmp 0x10525
xorl %ecx, %ecx
movb $0x0, -0x160(%rbp,%rcx)
leaq -0x160(%rbp), %rdi
callq 0xb0a0
cmpl $0x1525, %eax           # imm = 0x1525
ja 0x1049b
movl %eax, %eax
movsd 0x17c0(%rbx,%rax,8), %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x1, %r12d
xorl %eax, %eax
jmp 0x104a0
testl %ecx, %ecx
jle 0x10482
callq 0xaf80
movq -0x30(%rbp), %rdi
leaq 0x28028(%rip), %rsi     # 0x38492
xorl %edx, %edx
movl %eax, %ecx
leaq 0x27fa5(%rip), %r8      # 0x3841a
movl $0x50b, %r9d            # imm = 0x50B
xorl %eax, %eax
callq 0xb470
movb $0x23, (%r14)
incq %r14
cmpb $0x0, (%r15)
movq %r15, %r13
jne 0x102ef
jmp 0x105fe
xorl %r12d, %r12d
movb $0x1, %al
movq %r15, %r13
jmp 0x1056b
xorl %r12d, %r12d
xorl %ebx, %ebx
incq %r13
movq %r13, %rcx
testb %al, %al
je 0x1061a
movq %rcx, %r13
movzbl %al, %ecx
cmpl $0x3e, %ecx
je 0x1051d
movsbl %al, %r15d
movl %r15d, %edi
callq 0xa0b0
testl %eax, %eax
je 0x104ec
movb (%r13), %al
cmpb $0x29, %al
sete %dl
orb %bl, %dl
leaq 0x1(%r13), %rcx
testb $0x1, %dl
je 0x104b3
jmp 0x10522
movl %r15d, %edi
callq 0xaf50
movb %al, -0x160(%rbp,%r12)
leaq 0x1(%r12), %rcx
cmpq $0xfe, %r12
setae %bl
movb (%r13), %al
cmpb $0x29, %al
sete %dl
orb %bl, %dl
movq %rcx, %r12
je 0x104ad
jmp 0x10525
decq %r13
jmp 0x10530
movq %r12, %rcx
movq %rcx, %r12
cmpb $0x3e, %al
jne 0x1061a
incq %r13
movslq %r12d, %rax
movb $0x0, -0x160(%rbp,%rax)
movq -0x30(%rbp), %rdi
leaq -0x160(%rbp), %rsi
leaq -0x34(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0xa520
xorl %r12d, %r12d
cmpl $0x0, -0x34(%rbp)
sete %al
setne %r12b
movq 0x339f5(%rip), %rbx     # 0x43f60
testb %al, %al
jne 0x10590
movsd -0x40(%rbp), %xmm0
movl $0x1e, %esi
leaq -0x60(%rbp), %rdi
leaq 0x2d9f6(%rip), %rdx     # 0x3df7a
movb $0x1, %al
callq 0xb130
cmpl $0x1e, %eax
jb 0x1059e
movl $0x232323, -0x5d(%rbp)  # imm = 0x232323
movl $0x23232323, -0x60(%rbp) # imm = 0x23232323
cmpl $0x0, 0x1b6b4(%rbx)
jle 0x105db
callq 0xaf80
movq -0x30(%rbp), %rdi
leaq 0x27f0e(%rip), %rsi     # 0x384c5
xorl %edx, %edx
movl %eax, %ecx
leaq 0x27e58(%rip), %r8      # 0x3841a
movl $0x521, %r9d            # imm = 0x521
xorl %eax, %eax
leaq -0x60(%rbp), %r10
pushq %r10
pushq %r12
callq 0xb470
addq $0x10, %rsp
movb -0x60(%rbp), %al
testb %al, %al
je 0x102ef
leaq -0x5f(%rbp), %rcx
movb %al, (%r14)
incq %r14
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x105ea
jmp 0x102ef
leaq 0x27e27(%rip), %rdi     # 0x3842c
callq 0xa1f0
movq -0x30(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
jmp 0x1063b
leaq 0x27e0b(%rip), %rdi     # 0x3842c
callq 0xa1f0
movq -0x30(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x33925(%rip), %rbx     # 0x43f60
movl $0x0, 0xd670(%rbx)
leaq 0xc6d0(%rbx), %rdi
leaq 0x26c31(%rip), %rsi     # 0x37284
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %eax
jmp 0x1067d
movb $0x0, (%r14)
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop