// File: /workspace/github_cpp_makefile_star2_O3/sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_tool_length_offset(int, block_struct*, setup_struct*)
int Interp::convert_tool_length_offset(int g_code,       //!< g_code being executed (must be G_43 or G_49)
                                      block_pointer block,      //!< pointer to a block of RS274/NGC instructions
                                      setup_pointer settings)   //!< pointer to machine settings                 
{
  int index;
  EmcPose tool_offset;
  ZERO_EMC_POSE(tool_offset);

  CHKS((settings->cutter_comp_side != OFF),
       (_("Cannot change tool offset with cutter radius compensation on")));
  if (g_code == G_49) {
    index = 0;
  } else if (g_code == G_43) {
    if(block->h_flag == ON) {
        CHP((find_tool_pocket(settings, block->h_number, &index)));
    } else if (settings->toolchange_flag) {
        // we haven't loaded the tool and swapped pockets quite yet
        index = settings->current_pocket;
    } else {
        // tool change is done so pockets are swapped
        index = 0;
    }

    tool_offset.tran.x = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.tran.x);
    tool_offset.tran.y = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.tran.y);
    tool_offset.tran.z = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.tran.z);
    tool_offset.a = USER_TO_PROGRAM_ANG(settings->tool_table[index].offset.a);
    tool_offset.b = USER_TO_PROGRAM_ANG(settings->tool_table[index].offset.b);
    tool_offset.c = USER_TO_PROGRAM_ANG(settings->tool_table[index].offset.c);
    tool_offset.u = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.u);
    tool_offset.v = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.v);
    tool_offset.w = USER_TO_PROGRAM_LEN(settings->tool_table[index].offset.w);
  } else if (g_code == G_43_1) {
    tool_offset = settings->tool_offset;
    index = -1;
    if(block->x_flag == ON) tool_offset.tran.x = block->x_number;
    if(block->y_flag == ON) tool_offset.tran.y = block->y_number;
    if(block->z_flag == ON) tool_offset.tran.z = block->z_number;
    if(block->a_flag == ON) tool_offset.a = block->a_number;
    if(block->b_flag == ON) tool_offset.b = block->b_number;
    if(block->c_flag == ON) tool_offset.c = block->c_number;
    if(block->u_flag == ON) tool_offset.u = block->u_number;
    if(block->v_flag == ON) tool_offset.v = block->v_number;
    if(block->w_flag == ON) tool_offset.w = block->w_number;
  } else {
    ERS("BUG: Code not G43, G43.1, or G49");
  }
  USE_TOOL_LENGTH_OFFSET(tool_offset);

  settings->current_x += settings->tool_offset.tran.x - tool_offset.tran.x;
  settings->current_y += settings->tool_offset.tran.y - tool_offset.tran.y;
  settings->current_z += settings->tool_offset.tran.z - tool_offset.tran.z;
  settings->AA_current += settings->tool_offset.a - tool_offset.a;
  settings->BB_current += settings->tool_offset.b - tool_offset.b;
  settings->CC_current += settings->tool_offset.c - tool_offset.c;
  settings->u_current += settings->tool_offset.u - tool_offset.u;
  settings->v_current += settings->tool_offset.v - tool_offset.v;
  settings->w_current += settings->tool_offset.w - tool_offset.w;

  settings->tool_offset = tool_offset;
  settings->tool_offset_index = index;
  return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x160, %rsp            # imm = 0x160
movq %rdi, %r14
cmpl $0x0, 0x624(%rcx)
je 0x11d7c
leaq 0x249e6(%rip), %rdi     # 0x36713
callq 0xa1f0
movq %r14, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x2f21a(%rip), %rbx     # 0x40f60
leaq 0xc6d0(%rbx), %rdi
leaq 0x2294d(%rip), %rsi     # 0x346a1
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %r14d
jmp 0x12450
movq %rcx, %rbx
cmpl $0x1ae, %esi            # imm = 0x1AE
je 0x11eb0
cmpl $0x1af, %esi            # imm = 0x1AF
je 0x11dd0
cmpl $0x1ea, %esi            # imm = 0x1EA
jne 0x11f45
movl $0x0, -0x14(%rbp)
xorpd %xmm8, %xmm8
xorpd %xmm7, %xmm7
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
jmp 0x12279
movsd 0xd678(%rbx), %xmm1
movsd 0xd680(%rbx), %xmm2
movsd 0xd688(%rbx), %xmm3
movsd 0xd690(%rbx), %xmm0
movsd 0xd698(%rbx), %xmm4
movsd 0xd6a0(%rbx), %xmm5
movsd 0xd6a8(%rbx), %xmm6
movsd 0xd6b0(%rbx), %xmm7
movsd 0xd6b8(%rbx), %xmm8
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
cmpb $0x1, 0x270(%rdx)
jne 0x11e31
movsd 0x278(%rdx), %xmm1
cmpb $0x1, 0x280(%rdx)
jne 0x11e42
movsd 0x288(%rdx), %xmm2
cmpb $0x1, 0x290(%rdx)
jne 0x11e53
movsd 0x298(%rdx), %xmm3
cmpb $0x1, (%rdx)
jne 0x11e5d
movsd 0x8(%rdx), %xmm0
cmpb $0x1, 0x10(%rdx)
jne 0x11e68
movsd 0x18(%rdx), %xmm4
cmpb $0x1, 0x20(%rdx)
jne 0x11e73
movsd 0x28(%rdx), %xmm5
cmpb $0x1, 0x244(%rdx)
jne 0x11e84
movsd 0x248(%rdx), %xmm6
cmpb $0x1, 0x250(%rdx)
jne 0x11e95
movsd 0x258(%rdx), %xmm7
cmpb $0x1, 0x260(%rdx)
jne 0x12279
movsd 0x268(%rdx), %xmm8
jmp 0x12279
cmpb $0x1, 0x190(%rdx)
jne 0x11f54
movl 0x194(%rdx), %edx
leaq -0x14(%rbp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa930
testl %eax, %eax
je 0x11f73
movl %eax, %r14d
movq 0x2f07c(%rip), %rbx     # 0x40f60
movslq 0xd670(%rbx), %rax
cmpq $0x30, %rax
jg 0x12450
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
leaq (%rbx,%rax), %rdi
addq $0xc6d0, %rdi           # imm = 0xC6D0
leaq 0x22792(%rip), %rsi     # 0x346a1
movl $0x50, %edx
callq 0xa8a0
movslq 0xd670(%rbx), %rax
leaq (%rax,%rax,4), %rcx
shlq $0x4, %rcx
xorl %edx, %edx
movb %dl, 0xc71f(%rbx,%rcx)
incl %eax
movl %eax, 0xd670(%rbx)
movb %dl, 0xc720(%rbx,%rcx)
jmp 0x12450
leaq 0x24804(%rip), %rsi     # 0x36750
movq %r14, %rdi
jmp 0x11d38
cmpb $0x1, 0xc682(%rbx)
jne 0x11f68
movl 0x5fc(%rbx), %eax
movl %eax, -0x14(%rbp)
jmp 0x11f76
movl $0x0, -0x14(%rbp)
xorl %eax, %eax
jmp 0x11f76
movl -0x14(%rbp), %eax
cltq
imulq $0x70, %rax, %rax
movsd 0xd6d0(%rbx,%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0xa990
movsd %xmm0, -0xd0(%rbp)
movq 0x2efc2(%rip), %r14     # 0x40f60
movl 0x1690(%r14), %eax
movsd 0x21f13(%rip), %xmm0    # 0x33ec0
movsd %xmm0, -0x68(%rbp)
cmpl $0x3, %eax
je 0x11fbf
movsd 0x21ee1(%rip), %xmm0    # 0x33ea0
movsd 0x21f01(%rip), %xmm1    # 0x33ec8
movsd %xmm1, -0x70(%rbp)
cmpl $0x1, %eax
je 0x11fd4
movaps %xmm0, %xmm1
movsd %xmm1, -0xd8(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6d8(%rbx,%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
callq 0xa990
movsd %xmm0, -0xc0(%rbp)
movl 0x1690(%r14), %eax
movsd 0x21eb2(%rip), %xmm0    # 0x33ec0
cmpl $0x3, %eax
je 0x1201b
movsd 0x21e85(%rip), %xmm0    # 0x33ea0
movsd 0x21ea5(%rip), %xmm1    # 0x33ec8
cmpl $0x1, %eax
je 0x1202b
movaps %xmm0, %xmm1
movsd %xmm1, -0xc8(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6e0(%rbx,%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
callq 0xa990
movsd %xmm0, -0xb0(%rbp)
movl 0x1690(%r14), %eax
movsd 0x21e5b(%rip), %xmm0    # 0x33ec0
cmpl $0x3, %eax
je 0x12072
movsd 0x21e2e(%rip), %xmm0    # 0x33ea0
movsd 0x21e4e(%rip), %xmm1    # 0x33ec8
cmpl $0x1, %eax
je 0x12082
movaps %xmm0, %xmm1
movsd %xmm1, -0xb8(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6e8(%rbx,%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0xaf60
movsd %xmm0, -0xa8(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6f0(%rbx,%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
callq 0xaf60
movsd %xmm0, -0xa0(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6f8(%rbx,%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0xaf60
movsd %xmm0, -0x98(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd700(%rbx,%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xa990
movsd %xmm0, -0x88(%rbp)
movl 0x1690(%r14), %eax
movsd 0x21d9b(%rip), %xmm0    # 0x33ec0
cmpl $0x3, %eax
je 0x12132
movsd 0x21d6e(%rip), %xmm0    # 0x33ea0
movsd 0x21d8e(%rip), %xmm1    # 0x33ec8
cmpl $0x1, %eax
je 0x12142
movaps %xmm0, %xmm1
movsd %xmm1, -0x90(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd708(%rbx,%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0xa990
movsd %xmm0, -0x78(%rbp)
movl 0x1690(%r14), %eax
movsd 0x21d47(%rip), %xmm0    # 0x33ec0
cmpl $0x3, %eax
je 0x12186
movsd 0x21d1a(%rip), %xmm0    # 0x33ea0
movsd 0x21d3a(%rip), %xmm1    # 0x33ec8
cmpl $0x1, %eax
je 0x12196
movaps %xmm0, %xmm1
movsd %xmm1, -0x80(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd710(%rbx,%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
callq 0xa990
movl 0x1690(%r14), %eax
cmpl $0x3, %eax
je 0x121cf
movsd 0x21cd6(%rip), %xmm1    # 0x33ea0
movsd %xmm1, -0x68(%rbp)
movsd -0x60(%rbp), %xmm1
divsd -0xd0(%rbp), %xmm1
movsd -0x58(%rbp), %xmm2
divsd -0xc0(%rbp), %xmm2
movsd -0x50(%rbp), %xmm3
divsd -0xb0(%rbp), %xmm3
movsd -0x30(%rbp), %xmm6
divsd -0x88(%rbp), %xmm6
movsd -0x28(%rbp), %xmm7
divsd -0x78(%rbp), %xmm7
movsd -0x20(%rbp), %xmm8
divsd %xmm0, %xmm8
cmpl $0x1, %eax
movsd -0x40(%rbp), %xmm4
movsd -0x38(%rbp), %xmm5
je 0x12231
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
divsd -0x70(%rbp), %xmm8
divsd -0xd8(%rbp), %xmm1
divsd -0xc8(%rbp), %xmm2
divsd -0xb8(%rbp), %xmm3
movsd -0x48(%rbp), %xmm0
divsd -0xa8(%rbp), %xmm0
divsd -0xa0(%rbp), %xmm4
divsd -0x98(%rbp), %xmm5
divsd -0x90(%rbp), %xmm6
divsd -0x80(%rbp), %xmm7
movsd %xmm1, -0x60(%rbp)
movsd %xmm2, -0x58(%rbp)
movsd %xmm3, -0x50(%rbp)
movsd %xmm0, -0x48(%rbp)
movsd %xmm4, -0x40(%rbp)
movsd %xmm5, -0x38(%rbp)
movsd %xmm6, -0x30(%rbp)
movsd %xmm7, -0x28(%rbp)
movsd %xmm8, -0x20(%rbp)
leaq -0x120(%rbp), %rsi
movsd %xmm1, (%rsi)
movsd %xmm2, 0x8(%rsi)
movsd %xmm3, 0x10(%rsi)
movsd %xmm0, 0x18(%rsi)
movsd %xmm4, 0x20(%rsi)
movsd %xmm5, 0x28(%rsi)
movsd %xmm6, 0x30(%rsi)
movsd %xmm7, 0x38(%rsi)
movsd %xmm8, 0x40(%rsi)
movl $0x9, %ecx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0xb3e0
movsd 0xd678(%rbx), %xmm0
movsd 0xd680(%rbx), %xmm1
movsd -0x60(%rbp), %xmm9
subsd %xmm9, %xmm0
addsd 0x600(%rbx), %xmm0
movsd %xmm0, 0x600(%rbx)
movsd -0x58(%rbp), %xmm8
subsd %xmm8, %xmm1
addsd 0x608(%rbx), %xmm1
movsd %xmm1, 0x608(%rbx)
movsd 0xd688(%rbx), %xmm0
movsd -0x50(%rbp), %xmm7
subsd %xmm7, %xmm0
addsd 0x610(%rbx), %xmm0
movsd %xmm0, 0x610(%rbx)
movsd 0xd690(%rbx), %xmm0
movsd -0x48(%rbp), %xmm6
subsd %xmm6, %xmm0
addsd 0x8(%rbx), %xmm0
movsd %xmm0, 0x8(%rbx)
movsd 0xd698(%rbx), %xmm0
movsd -0x40(%rbp), %xmm5
subsd %xmm5, %xmm0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x20(%rbx)
movsd 0xd6a0(%rbx), %xmm0
movsd -0x38(%rbp), %xmm4
subsd %xmm4, %xmm0
addsd 0x38(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movsd 0xd6a8(%rbx), %xmm0
movsd -0x30(%rbp), %xmm3
subsd %xmm3, %xmm0
addsd 0x50(%rbx), %xmm0
movsd %xmm0, 0x50(%rbx)
movsd 0xd6b0(%rbx), %xmm0
movsd -0x28(%rbp), %xmm2
subsd %xmm2, %xmm0
addsd 0x68(%rbx), %xmm0
movsd %xmm0, 0x68(%rbx)
movsd 0xd6b8(%rbx), %xmm0
movsd -0x20(%rbp), %xmm1
subsd %xmm1, %xmm0
addsd 0x80(%rbx), %xmm0
movsd %xmm0, 0x80(%rbx)
movsd %xmm9, 0xd678(%rbx)
movsd %xmm8, 0xd680(%rbx)
movsd %xmm7, 0xd688(%rbx)
movsd %xmm6, 0xd690(%rbx)
movsd %xmm5, 0xd698(%rbx)
movsd %xmm4, 0xd6a0(%rbx)
movsd %xmm3, 0xd6a8(%rbx)
movsd %xmm2, 0xd6b0(%rbx)
movsd %xmm1, 0xd6b8(%rbx)
movl -0x14(%rbp), %eax
movl %eax, 0x168c(%rbx)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbx
popq %r14
popq %rbp
retq
nop