// File: /workspace/github_cpp_makefile_star2_O3/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 $0x3e8, %rsp            # imm = 0x3E8
movl %r8d, -0x3c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x88ee
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, %r12
leaq 0xee7(%rip), %rsi       # 0x90b4
callq 0x41b0
testq %rax, %rax
je 0x88ee
movq %rax, %r15
xorl %ecx, %ecx
cmpl $0x0, -0x3c(%rbp)
sete %cl
leaq (%r14,%rcx,8), %rax
imulq $0x70, %rcx, %rsi
shll $0x2, %ecx
leaq (%rcx,%r13), %rdx
addq %r12, %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 %r13, %r13
je 0x8233
movl $0x0, (%rdx,%rdi)
testq %r14, %r14
je 0x8240
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 0x8202
movq %r15, %rdi
callq 0x4240
testl %eax, %eax
jne 0x88e4
leaq -0x410(%rbp), %rax
leaq 0x17b9(%rip), %rbx      # 0x9a30
movq $0x0, -0x50(%rbp)
movq %r12, -0x60(%rbp)
movq %rax, %r12
movq %r13, -0x68(%rbp)
movq %r15, -0x38(%rbp)
movq %r14, -0x88(%rbp)
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdx
callq 0x4270
testq %rax, %rax
je 0x88e0
leaq -0x310(%rbp), %rdi
movq %r12, %rsi
callq 0x4150
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x183d(%rip), %rsi      # 0x9b08
leaq -0x40(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x78(%rbp), %r8
leaq -0xc0(%rbp), %r9
xorl %eax, %eax
leaq -0x210(%rbp), %r10
pushq %r10
leaq -0x7c(%rbp), %r10
pushq %r10
leaq -0xb0(%rbp), %r10
pushq %r10
leaq -0xb8(%rbp), %r10
pushq %r10
leaq -0x70(%rbp), %r10
pushq %r10
callq 0x4160
addq $0x30, %rsp
testl %eax, %eax
je 0x8337
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x8, %ecx
jne 0x8337
cmpl $0x0, -0x3c(%rbp)
je 0x8737
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rdx
movl %edx, %r12d
jmp 0x875f
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x17e6(%rip), %rsi      # 0x9b2b
leaq -0x40(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x78(%rbp), %r8
leaq -0x70(%rbp), %r9
xorl %eax, %eax
leaq -0x210(%rbp), %r10
pushq %r10
callq 0x4160
addq $0x10, %rsp
testl %eax, %eax
je 0x8390
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x4, %ecx
jne 0x8390
cmpl $0x0, -0x3c(%rbp)
je 0x87d9
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rdx
movl %edx, %r12d
jmp 0x8813
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movq %rax, -0x90(%rbp)
movq %rax, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
movl $0x0, -0x54(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
xorl %r14d, %r14d
movq %rax, -0xd0(%rbp)
movq %r12, %rdi
leaq 0x16b0(%rip), %rsi      # 0x9a98
callq 0x4260
movq %rax, %r12
xorl %edi, %edi
leaq 0x1542(%rip), %rsi      # 0x993b
callq 0x4260
movq %rax, -0xa8(%rbp)
movq %r12, %rdi
leaq 0xe07(%rip), %rsi       # 0x9216
callq 0x4260
testq %rax, %rax
je 0x867d
movq %rax, %r12
movl $0x1, %r13d
movb (%r12), %r15b
movsbl %r15b, %edi
callq 0x42d0
addl $-0x41, %eax
cmpl $0x19, %eax
ja 0x84f7
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
incq %r12
movq %r12, %rdi
leaq 0xc64(%rip), %rsi       # 0x90b9
leaq -0xf8(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xc4b(%rip), %rsi       # 0x90b9
leaq -0x108(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xc32(%rip), %rsi       # 0x90b9
leaq -0xd8(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xc19(%rip), %rsi       # 0x90b9
leaq -0x110(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xc00(%rip), %rsi       # 0x90b9
leaq -0xe8(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xbe7(%rip), %rsi       # 0x90b9
leaq -0x90(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xbce(%rip), %rsi       # 0x90b9
leaq -0xd0(%rbp), %rdx
jmp 0x85c9
cmpb $0xa, %r15b
jmp 0x85d3
incq %r12
movq %r12, %rdi
leaq 0xbac(%rip), %rsi       # 0x90b9
leaq -0xf0(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0x1574(%rip), %rsi      # 0x9a9a
leaq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x4160
cmpl $0x1, %eax
jne 0x85f3
cmpl $0x0, -0x3c(%rbp)
je 0x85f8
movl -0x30(%rbp), %r12d
jmp 0x863f
incq %r12
movq %r12, %rdi
leaq 0x1540(%rip), %rsi      # 0x9a9a
leaq -0x54(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xb4c(%rip), %rsi       # 0x90b9
leaq -0x100(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xb36(%rip), %rsi       # 0x90b9
leaq -0xa0(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xb20(%rip), %rsi       # 0x90b9
leaq -0x98(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0xb0a(%rip), %rsi       # 0x90b9
leaq -0xe0(%rbp), %rdx
jmp 0x85c9
incq %r12
movq %r12, %rdi
leaq 0x14d5(%rip), %rsi      # 0x9a9a
leaq -0x44(%rbp), %rdx
xorl %eax, %eax
callq 0x4160
cmpl $0x1, %eax
cmovnel %r14d, %r13d
xorl %edi, %edi
leaq 0xc36(%rip), %rsi       # 0x9216
callq 0x4260
movq %rax, %r12
testq %rax, %rax
jne 0x8426
jmp 0x8663
xorl %r13d, %r13d
jmp 0x85d7
movq -0x50(%rbp), %rax
leal 0x1(%rax), %r12d
cmpl $0x37, %eax
jl 0x8622
movl -0x44(%rbp), %esi
xorl %r13d, %r13d
leaq 0x148b(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
movl %r12d, %eax
movq %rax, -0x50(%rbp)
jmp 0x85d7
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
je 0x8634
movl -0x30(%rbp), %eax
movslq %r12d, %rcx
movl %eax, (%rdx,%rcx,4)
movl %r12d, -0x30(%rbp)
movl %r12d, %eax
movq %rax, -0x50(%rbp)
cmpl $0x38, %r12d
jb 0x85d7
movl -0x44(%rbp), %edx
xorl %r13d, %r13d
leaq 0x146d(%rip), %rdi      # 0x9abf
movl $0x37, %esi
xorl %eax, %eax
callq 0x4030
jmp 0x85d7
testl %r13d, %r13d
movq -0x60(%rbp), %r12
movq -0x88(%rbp), %r14
je 0x88b8
movq -0x68(%rbp), %r13
jmp 0x8688
movq -0x60(%rbp), %r12
movq -0x88(%rbp), %r14
testq %r14, %r14
setne %r8b
movl -0x44(%rbp), %ecx
movslq -0x30(%rbp), %rdx
imulq $0x70, %rdx, %rax
addq %r12, %rax
addq $0x8, %rax
movl %ecx, -0x8(%rax)
movl $0x9, %ecx
movq %rax, %rdi
leaq -0x110(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, 0x48(%rax)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, 0x50(%rax)
movsd -0x90(%rbp), %xmm0
movsd %xmm0, 0x58(%rax)
movl -0x54(%rbp), %ecx
movl %ecx, 0x60(%rax)
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
setne %al
andb %r8b, %al
cmpb $0x1, %al
jne 0x8700
movq (%r14,%rdx,8), %rdi
callq 0x4150
movq -0x38(%rbp), %r15
cmpl $0x0, -0x3c(%rbp)
jne 0x889f
movl (%r12), %eax
movslq -0x30(%rbp), %rcx
imulq $0x70, %rcx, %rsi
cmpl (%r12,%rsi), %eax
jne 0x889f
addq %r12, %rsi
movl $0xe, %ecx
movq %r12, %rdi
rep  movsq (%rsi), %es:(%rdi)
jmp 0x889f
movq -0x50(%rbp), %rcx
leal 0x1(%rcx), %r12d
cmpl $0x37, %ecx
jge 0x87e6
testq %r13, %r13
je 0x8758
movl -0x2c(%rbp), %ecx
movslq %r12d, %rdx
movl %ecx, (%r13,%rdx,4)
movl %r12d, -0x2c(%rbp)
movl %r12d, %ecx
movl -0x40(%rbp), %edx
cmpl $0x38, %ecx
jae 0x881b
cmpl $0x9, %eax
setne %sil
testq %r14, %r14
sete %dil
movl %ecx, %eax
imulq $0x70, %rax, %rcx
movq -0x60(%rbp), %r8
movl %edx, (%r8,%rcx)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, 0x18(%r8,%rcx)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, 0x8(%r8,%rcx)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, 0x50(%r8,%rcx)
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, 0x58(%r8,%rcx)
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, 0x60(%r8,%rcx)
movl -0x7c(%rbp), %edx
movl %edx, 0x68(%r8,%rcx)
jmp 0x887f
movq -0x50(%rbp), %rcx
leal 0x1(%rcx), %r12d
cmpl $0x37, %ecx
jl 0x87fc
movl -0x40(%rbp), %esi
leaq 0x12ad(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
jmp 0x8894
testq %r13, %r13
je 0x880c
movl -0x2c(%rbp), %ecx
movslq %r12d, %rdx
movl %ecx, (%r13,%rdx,4)
movl %r12d, -0x2c(%rbp)
movl %r12d, %ecx
movl -0x40(%rbp), %edx
cmpl $0x38, %ecx
jb 0x8830
leaq 0x129d(%rip), %rdi      # 0x9abf
movl $0x37, %esi
xorl %eax, %eax
callq 0x4030
jmp 0x8894
cmpl $0x5, %eax
setne %sil
testq %r14, %r14
sete %dil
movl %ecx, %eax
imulq $0x70, %rax, %rcx
movq -0x60(%rbp), %r8
movl %edx, (%r8,%rcx)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, 0x18(%r8,%rcx)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, 0x50(%r8,%rcx)
movq $0x0, 0x8(%r8,%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%r8,%rcx)
movl $0x0, 0x68(%r8,%rcx)
orb %sil, %dil
jne 0x8894
movq (%r14,%rax,8), %rdi
leaq -0x210(%rbp), %rsi
callq 0x4150
movq -0x38(%rbp), %r15
movl %r12d, %eax
movq %rax, -0x50(%rbp)
movq %r15, %rdi
callq 0x4240
testl %eax, %eax
leaq -0x410(%rbp), %r12
je 0x8295
jmp 0x88e4
movq 0x4721(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x1221(%rip), %rsi      # 0x9aea
leaq -0x310(%rbp), %rdx
xorl %eax, %eax
callq 0x4230
movq -0x68(%rbp), %r13
jmp 0x8700
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x4170
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq