// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %rbx
movq 0x3100e(%rip), %r13     # 0x40f60
movb (%r15), %al
leaq 0x1(%r15), %rcx
addq $0x2, %r15
movq %r15, %r12
movq %rcx, %r15
cmpb $0x23, %al
jne 0x10009
cmpl $0x0, 0x1b6b4(%r13)
jle 0xff9c
callq 0xaf80
movq %rbx, %rdi
leaq 0x25477(%rip), %rsi     # 0x353fb
xorl %edx, %edx
movl %eax, %ecx
leaq 0x2548b(%rip), %r8      # 0x3541a
movl $0x4c2, %r9d            # imm = 0x4C2
xorl %eax, %eax
callq 0xb470
movsbl (%r15), %eax
testl %eax, %eax
je 0x1023d
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
jbe 0x1001f
movl 0x1b6b4(%r13), %ecx
cmpb $0x3c, %al
je 0x10093
testl %ecx, %ecx
jle 0xffec
callq 0xaf80
movq %rbx, %rdi
leaq 0x254be(%rip), %rsi     # 0x35492
xorl %edx, %edx
movl %eax, %ecx
leaq 0x2543b(%rip), %r8      # 0x3541a
movl $0x50b, %r9d            # imm = 0x50B
xorl %eax, %eax
callq 0xb470
movb $0x23, (%r14)
incq %r14
movb (%r15), %al
incq %r15
incq %r12
testb %al, %al
jne 0xff63
jmp 0x1023d
movzbl %al, %ecx
testl %ecx, %ecx
je 0x1028a
movb %al, (%r14)
incq %r14
jmp 0xff52
cmpl $0x0, 0x1b6b4(%r13)
jle 0x10062
callq 0xaf80
movq %rbx, %rdi
leaq 0x25413(%rip), %rsi     # 0x3544b
xorl %edx, %edx
movl %eax, %ecx
leaq 0x253d7(%rip), %r8      # 0x3541a
movl $0x4ca, %r9d            # imm = 0x4CA
xorl %eax, %eax
callq 0xb470
movsbl (%r15), %eax
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0x1016d
xorl %edx, %edx
movb %al, -0x160(%rbp,%rdx)
leaq 0x1(%rdx), %rcx
movsbl (%r12), %eax
leal -0x30(%rax), %esi
cmpl $0x9, %esi
ja 0x1008b
incq %r12
cmpq $0xfe, %rdx
movq %rcx, %rdx
jb 0x10064
addq %rcx, %r15
jmp 0x1016f
testl %ecx, %ecx
jle 0x100be
callq 0xaf80
movq %rbx, %rdi
leaq 0x253ca(%rip), %rsi     # 0x35470
xorl %edx, %edx
movl %eax, %ecx
leaq 0x25369(%rip), %r8      # 0x3541a
movl $0x4da, %r9d            # imm = 0x4DA
xorl %eax, %eax
callq 0xb470
movb 0x1(%r15), %al
testb %al, %al
je 0x1023d
movzbl %al, %ecx
cmpl $0x29, %ecx
je 0x102a2
incq %r15
xorl %r13d, %r13d
testb %al, %al
je 0x102a2
movzbl %al, %ecx
cmpl $0x3e, %ecx
je 0x10134
movsbl %al, %r12d
movl %r12d, %edi
callq 0xa0b0
testl %eax, %eax
jne 0x10112
movl %r12d, %edi
callq 0xaf50
movslq %r13d, %r13
movb %al, -0x160(%rbp,%r13)
incl %r13d
movb 0x1(%r15), %al
incq %r15
cmpb $0x29, %al
sete %cl
cmpl $0xff, %r13d
setge %dl
orb %cl, %dl
je 0x100dc
cmpb $0x3e, %al
jne 0x102a2
incq %r15
movslq %r13d, %rax
movb $0x0, -0x160(%rbp,%rax)
movq %rbx, %rdi
leaq -0x160(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0xa520
cmpl $0x0, -0x2c(%rbp)
je 0x101c4
movsd -0x38(%rbp), %xmm0
movq 0x30df5(%rip), %r13     # 0x40f60
jmp 0x1019b
xorl %ecx, %ecx
movb $0x0, -0x160(%rbp,%rcx)
leaq -0x160(%rbp), %rdi
callq 0xb0a0
cmpl $0x1525, %eax           # imm = 0x1525
ja 0x101bf
movl %eax, %eax
movsd 0x17c0(%r13,%rax,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x1e, %esi
leaq -0x60(%rbp), %rdi
leaq 0x2ad7f(%rip), %rdx     # 0x3af2a
movb $0x1, %al
callq 0xb130
movl $0x1, %r12d
cmpl $0x1e, %eax
jae 0x101ce
jmp 0x101dc
xorl %r12d, %r12d
jmp 0x101ce
xorl %r12d, %r12d
movq 0x30d92(%rip), %r13     # 0x40f60
movl $0x232323, -0x5d(%rbp)  # imm = 0x232323
movl $0x23232323, -0x60(%rbp) # imm = 0x23232323
cmpl $0x0, 0x1b6b4(%r13)
jle 0x10219
callq 0xaf80
movq %rbx, %rdi
leaq 0x252d0(%rip), %rsi     # 0x354c5
xorl %edx, %edx
movl %eax, %ecx
leaq 0x2521a(%rip), %r8      # 0x3541a
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 0xff52
xorl %ecx, %ecx
movb %al, (%r14,%rcx)
movb -0x5f(%rbp,%rcx), %al
incq %rcx
testb %al, %al
jne 0x10226
addq %rcx, %r14
jmp 0xff52
leaq 0x251e8(%rip), %rdi     # 0x3542c
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
leaq 0xc6d0(%r13), %rdi
leaq 0x24020(%rip), %rsi     # 0x34284
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%r13)
movl $0x1, 0xd670(%r13)
movl $0x5, %eax
jmp 0x10290
movb $0x0, (%r14)
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25183(%rip), %rdi     # 0x3542c
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x30c9e(%rip), %rbx     # 0x40f60
leaq 0xc6d0(%rbx), %rdi
leaq 0x23fb4(%rip), %rsi     # 0x34284
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
jmp 0x10283
nop