// File: /workspace/github_cpp_makefile_star2_O1/sundale[P]gcode/tool_parse.cc
// Function: loadToolTable(char const*, CANON_TOOL_TABLE*, int*, char**, int)
int loadToolTable(const char *filename,
			 CANON_TOOL_TABLE toolTable[],
			 int fms[],
			 char *ttcomments[],
			 int random_toolchanger)
{
    int fakepocket = 0;
    int t;
    FILE *fp;
    char buffer[CANON_TOOL_ENTRY_LEN];
    char orig_line[CANON_TOOL_ENTRY_LEN];
    int pocket = 0;

    if(!filename) return -1;

    // open tool table file
    if (NULL == (fp = fopen(filename, "r"))) {
	// can't open file
	return -1;
    }
    // clear out tool table
    for (t = random_toolchanger? 0: 1; t < CANON_POCKETS_MAX; t++) {
        toolTable[t].toolno = -1;
        ZERO_EMC_POSE(toolTable[t].offset);
        toolTable[t].diameter = 0.0;
        toolTable[t].frontangle = 0.0;
        toolTable[t].backangle = 0.0;
        toolTable[t].orientation = 0;
        if(fms) fms[t] = 0;
        if(ttcomments) ttcomments[t][0] = '\0';
    }

    /*
      Override 0's with codes from tool file
      File format is:

      <header>
      <pocket # 0..CANON_TOOL_MAX> <FMS id> <length> <diameter>
      ...

    */

    while (!feof(fp)) {
        const char *token;
        char *buff, *comment;
        int toolno, orientation, valid = 1;
        EmcPose offset;  // tlo
        double diameter, frontangle, backangle;

        // for nonrandom machines, just read the tools into pockets 1..n
        // no matter their tool numbers.  NB leave the spindle pocket 0
        // unchanged/empty.

        if (NULL == fgets(buffer, CANON_TOOL_ENTRY_LEN, fp)) {
            break;
        }
        strcpy(orig_line, buffer);

        if(scan_old_style(buffer, toolTable, fms, ttcomments,
                                random_toolchanger, fakepocket)) continue;

        toolno = -1;
        diameter = frontangle = backangle = 0.0;
        orientation = 0;
        ZERO_EMC_POSE(offset);
        buff = strtok(buffer, ";");
        comment = strtok(NULL, "\n");

        token = strtok(buff, " ");
        while (token != NULL) {
            switch (toupper(token[0])) {
            case 'T':
                if (sscanf(&token[1], "%d", &toolno) != 1)
                    valid = 0;
                break;
            case 'P':
                if (sscanf(&token[1], "%d", &pocket) != 1) {
                    valid = 0;
                    break;
                }
                if (!random_toolchanger) {
                    fakepocket++;
                    if (fakepocket >= CANON_POCKETS_MAX) {
                        printf("too many tools. skipping tool %d\n", toolno);
                        valid = 0;
                        break;
                    }
                    if(fms) fms[fakepocket] = pocket;
                    pocket = fakepocket;
                }
                if (pocket < 0 || pocket >= CANON_POCKETS_MAX) {
                    printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX - 1, toolno);
                    valid = 0;
                    break;
                }
                break;
            case 'D':
                if (sscanf(&token[1], "%lf", &diameter) != 1)
                    valid = 0;
                break;
            case 'X':
                if (sscanf(&token[1], "%lf", &offset.tran.x) != 1)
                    valid = 0;
                break;
            case 'Y':
                if (sscanf(&token[1], "%lf", &offset.tran.y) != 1)
                    valid = 0;
                break;
            case 'Z':
                if (sscanf(&token[1], "%lf", &offset.tran.z) != 1)
                    valid = 0;
                break;
            case 'A':
                if (sscanf(&token[1], "%lf", &offset.a) != 1)
                    valid = 0;
                break;
            case 'B':
                if (sscanf(&token[1], "%lf", &offset.b) != 1)
                    valid = 0;
                break;
            case 'C':
                if (sscanf(&token[1], "%lf", &offset.c) != 1)
                    valid = 0;
                break;
            case 'U':
                if (sscanf(&token[1], "%lf", &offset.u) != 1)
                    valid = 0;
                break;
            case 'V':
                if (sscanf(&token[1], "%lf", &offset.v) != 1)
                    valid = 0;
                break;
            case 'W':
                if (sscanf(&token[1], "%lf", &offset.w) != 1)
                    valid = 0;
                break;
            case 'I':
                if (sscanf(&token[1], "%lf", &frontangle) != 1)
                    valid = 0;
                break;
            case 'J':
                if (sscanf(&token[1], "%lf", &backangle) != 1)
                    valid = 0;
                break;
            case 'Q':
                if (sscanf(&token[1], "%d", &orientation) != 1)
                    valid = 0;
                break;
            default:
                if (strncmp(token, "\n", 1) != 0)
                    valid = 0;
                break;
            }
            token = strtok(NULL, " ");
        }
        if (valid) {
            toolTable[pocket].toolno = toolno;
            toolTable[pocket].offset = offset;
            toolTable[pocket].diameter = diameter;
            toolTable[pocket].frontangle = frontangle;
            toolTable[pocket].backangle = backangle;
            toolTable[pocket].orientation = orientation;

            if (ttcomments && comment)
                strcpy(ttcomments[pocket], comment);
        } else {
            fprintf(stderr, "Unrecognized line skipped: %s", orig_line);
        }
        if (!random_toolchanger && toolTable[0].toolno == toolTable[pocket].toolno) {
            toolTable[0] = toolTable[pocket];
        }
    }

    // close the file
    fclose(fp);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c8, %rsp            # imm = 0x3C8
movl %r8d, -0x40(%rbp)
movq %rdx, -0x78(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x8b6c
movq %rcx, %r14
movq %rsi, %r15
leaq 0xc5a(%rip), %rsi       # 0x90b4
callq 0x41c0
testq %rax, %rax
je 0x8b6c
movq %rax, %r13
xorl %ecx, %ecx
cmpl $0x0, -0x40(%rbp)
sete %cl
leaq (%r14,%rcx,8), %rax
imulq $0x70, %rcx, %rsi
shll $0x2, %ecx
movq -0x78(%rbp), %r9
leaq (%r9,%rcx), %rdx
addq %r15, %rsi
addq $0x8, %rsi
xorl %edi, %edi
xorps %xmm0, %xmm0
movl $0xffffffff, -0x8(%rsi) # imm = 0xFFFFFFFF
movups %xmm0, (%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x40(%rsi)
movups %xmm0, 0x50(%rsi)
movl $0x0, 0x60(%rsi)
testq %r9, %r9
je 0x84c4
movl $0x0, (%rdx,%rdi)
testq %r14, %r14
je 0x84d1
movq (%rax,%rdi,2), %r8
movb $0x0, (%r8)
addq $0x70, %rsi
leaq (%rcx,%rdi), %r8
addq $0x4, %r8
addq $0x4, %rdi
cmpq $0xe0, %r8
jne 0x8493
movq %r15, -0x68(%rbp)
movq %r14, -0x70(%rbp)
leaq 0xd25(%rip), %rbx       # 0x921e
leaq 0x1530(%rip), %r14      # 0x9a30
xorl %r15d, %r15d
movq %r13, -0x90(%rbp)
movq %r13, %rdi
callq 0x4250
testl %eax, %eax
jne 0x8b62
leaq -0x3f0(%rbp), %r12
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdx
callq 0x4280
testq %rax, %rax
je 0x8613
movq %rax, -0x38(%rbp)
leaq -0x2f0(%rbp), %rdi
movq %r12, %rsi
callq 0x4160
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x15ad(%rip), %rsi      # 0x9b08
leaq -0x44(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x88(%rbp), %r9
xorl %eax, %eax
leaq -0x1f0(%rbp), %r10
pushq %r10
leaq -0x94(%rbp), %r10
pushq %r10
leaq -0xa8(%rbp), %r10
pushq %r10
leaq -0x80(%rbp), %r10
pushq %r10
leaq -0x50(%rbp), %r10
pushq %r10
callq 0x4170
addq $0x30, %rsp
testl %eax, %eax
je 0x85c1
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x8, %ecx
jne 0x85c1
cmpl $0x0, -0x40(%rbp)
je 0x8620
movq -0x70(%rbp), %r8
movq -0x68(%rbp), %r9
jmp 0x864f
subq $0x8, %rsp
xorl %r13d, %r13d
movq %r12, %rdi
leaq 0x1559(%rip), %rsi      # 0x9b2b
leaq -0x44(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x50(%rbp), %r9
xorl %eax, %eax
leaq -0x1f0(%rbp), %r10
pushq %r10
callq 0x4170
addq $0x10, %rsp
testl %eax, %eax
je 0x861b
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x4, %ecx
jne 0x8618
cmpl $0x0, -0x40(%rbp)
je 0x86c6
jmp 0x8709
jmp 0x8b2f
xorl %r13d, %r13d
jmp 0x8795
leal 0x1(%r15), %r12d
cmpl $0x37, %r15d
jge 0x86d0
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
movq -0x68(%rbp), %r9
movq %r12, %r15
je 0x8647
movl -0x2c(%rbp), %ecx
movslq %r15d, %rdx
movl %ecx, (%rsi,%rdx,4)
movl %r15d, -0x2c(%rbp)
movq -0x70(%rbp), %r8
movl -0x2c(%rbp), %ecx
movl -0x44(%rbp), %edx
cmpq $0x38, %rcx
jae 0x871d
cmpl $0x9, %eax
setne %al
testq %r8, %r8
sete %sil
imulq $0x70, %rcx, %rdi
movl %edx, (%r9,%rdi)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, 0x18(%r9,%rdi)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, 0x8(%r9,%rdi)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, 0x50(%r9,%rdi)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, 0x58(%r9,%rdi)
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, 0x60(%r9,%rdi)
movl -0x94(%rbp), %edx
movl %edx, 0x68(%r9,%rdi)
jmp 0x877d
leal 0x1(%r15), %r12d
cmpl $0x37, %r15d
jl 0x86f0
movl -0x44(%rbp), %esi
leaq 0x13c3(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
movb $0x1, %r13b
movq -0x38(%rbp), %rax
movq %r12, %r15
jmp 0x8799
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
movq %r12, %r15
je 0x8705
movl -0x2c(%rbp), %ecx
movslq %r15d, %rdx
movl %ecx, (%rsi,%rdx,4)
movl %r15d, -0x2c(%rbp)
movq -0x70(%rbp), %r8
movq -0x68(%rbp), %r9
movl -0x2c(%rbp), %ecx
movl -0x44(%rbp), %edx
cmpq $0x38, %rcx
jb 0x8735
leaq 0x139b(%rip), %rdi      # 0x9abf
movl $0x37, %esi
xorl %eax, %eax
callq 0x4030
movb $0x1, %r13b
jmp 0x8795
cmpl $0x5, %eax
setne %al
testq %r8, %r8
sete %sil
imulq $0x70, %rcx, %rdi
movl %edx, (%r9,%rdi)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, 0x18(%r9,%rdi)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, 0x50(%r9,%rdi)
movq $0x0, 0x8(%r9,%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%r9,%rdi)
movl $0x0, 0x68(%r9,%rdi)
movb $0x1, %r13b
orb %al, %sil
jne 0x8795
movq (%r8,%rcx,8), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x4160
movq -0x38(%rbp), %rax
testb %r13b, %r13b
je 0x87aa
movq -0x90(%rbp), %r13
jmp 0x8b2f
movq %r15, -0x58(%rbp)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq %r13, -0x88(%rbp)
movq %r13, -0x60(%rbp)
movq %r13, -0x1f0(%rbp)
movl $0x0, -0x80(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movq %r13, -0xb0(%rbp)
leaq -0x3f0(%rbp), %rdi
leaq 0x1293(%rip), %rsi      # 0x9a98
callq 0x4270
movq %rax, %r15
xorl %edi, %edi
leaq 0x112d(%rip), %rsi      # 0x9943
callq 0x4270
movq %rax, -0xa0(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4270
testq %rax, %rax
je 0x8a75
movq %rax, %r15
movl $0x1, %r13d
movb (%r15), %r12b
movsbl %r12b, %edi
callq 0x42e0
addl $-0x41, %eax
cmpl $0x19, %eax
ja 0x890f
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
incq %r15
movq %r15, %rdi
leaq 0x84c(%rip), %rsi       # 0x90b9
leaq -0xd8(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x833(%rip), %rsi       # 0x90b9
leaq -0xe8(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x81a(%rip), %rsi       # 0x90b9
leaq -0xb8(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x801(%rip), %rsi       # 0x90b9
leaq -0xf0(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x7e8(%rip), %rsi       # 0x90b9
leaq -0xc8(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x7cf(%rip), %rsi       # 0x90b9
leaq -0x88(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x7b6(%rip), %rsi       # 0x90b9
leaq -0xb0(%rbp), %rdx
jmp 0x89e1
cmpb $0xa, %r12b
jmp 0x89eb
incq %r15
movq %r15, %rdi
leaq 0x794(%rip), %rsi       # 0x90b9
leaq -0xd0(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x115c(%rip), %rsi      # 0x9a9a
leaq -0x3c(%rbp), %rdx
xorl %eax, %eax
callq 0x4170
cmpl $0x1, %eax
jne 0x8a0c
cmpl $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rax
je 0x8a11
movl %eax, %r15d
jmp 0x8a46
incq %r15
movq %r15, %rdi
leaq 0x1125(%rip), %rsi      # 0x9a9a
leaq -0x80(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x731(%rip), %rsi       # 0x90b9
leaq -0xe0(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x71b(%rip), %rsi       # 0x90b9
leaq -0x1f0(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x705(%rip), %rsi       # 0x90b9
leaq -0x60(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x6f2(%rip), %rsi       # 0x90b9
leaq -0xc0(%rbp), %rdx
jmp 0x89e1
incq %r15
movq %r15, %rdi
leaq 0x10bd(%rip), %rsi      # 0x9a9a
leaq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x4170
cmpl $0x1, %eax
movl $0x0, %eax
cmovnel %eax, %r13d
xorl %edi, %edi
movq %rbx, %rsi
callq 0x4270
movq %rax, %r15
testq %rax, %rax
jne 0x883f
jmp 0x8a6e
xorl %r13d, %r13d
jmp 0x89f4
leal 0x1(%rax), %r15d
cmpl $0x37, %eax
jl 0x8a30
movl -0x50(%rbp), %esi
xorl %r13d, %r13d
leaq 0x1076(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
jmp 0x8a65
movq -0x78(%rbp), %rdx
testq %rdx, %rdx
je 0x8a42
movl -0x3c(%rbp), %eax
movslq %r15d, %rcx
movl %eax, (%rdx,%rcx,4)
movl %r15d, -0x3c(%rbp)
cmpl $0x38, -0x3c(%rbp)
jb 0x8a65
movl -0x50(%rbp), %edx
xorl %r13d, %r13d
leaq 0x1066(%rip), %rdi      # 0x9abf
movl $0x37, %esi
xorl %eax, %eax
callq 0x4030
movl %r15d, %eax
movq %rax, -0x58(%rbp)
jmp 0x89f4
testl %r13d, %r13d
sete %r13b
testb %r13b, %r13b
movq -0x70(%rbp), %r9
movq -0x68(%rbp), %r15
jne 0x8b3a
testq %r9, %r9
setne %r8b
movl -0x50(%rbp), %ecx
movslq -0x3c(%rbp), %rdx
imulq $0x70, %rdx, %rax
addq %r15, %rax
addq $0x8, %rax
movl %ecx, -0x8(%rax)
movl $0x9, %ecx
movq %rax, %rdi
leaq -0xf0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movsd -0x1f0(%rbp), %xmm0
movsd %xmm0, 0x48(%rax)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, 0x50(%rax)
movsd -0x88(%rbp), %xmm0
movsd %xmm0, 0x58(%rax)
movl -0x80(%rbp), %ecx
movl %ecx, 0x60(%rax)
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
setne %al
andb %r8b, %al
cmpb $0x1, %al
movq -0x90(%rbp), %r13
jne 0x8b02
movq (%r9,%rdx,8), %rdi
callq 0x4160
cmpl $0x0, -0x40(%rbp)
jne 0x8b27
movl (%r15), %eax
movslq -0x3c(%rbp), %rcx
imulq $0x70, %rcx, %rsi
cmpl (%r15,%rsi), %eax
jne 0x8b27
addq %r15, %rsi
movl $0xe, %ecx
movq %r15, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq -0x38(%rbp), %rax
movq -0x58(%rbp), %r15
testq %rax, %rax
jne 0x850a
jmp 0x8b62
movq 0x449f(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0xf9f(%rip), %rsi       # 0x9aea
leaq -0x2f0(%rbp), %rdx
xorl %eax, %eax
callq 0x4240
movq -0x90(%rbp), %r13
jmp 0x8b02
movq %r13, %rdi
callq 0x4180
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x3c8, %rsp            # imm = 0x3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq