// File: /workspace/github_cpp_makefile_star2_O1/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 0x1272e
leaq 0x2703e(%rip), %rdi     # 0x39713
callq 0xa1f0
movq %r14, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x31872(%rip), %rbx     # 0x43f60
movl $0x0, 0xd670(%rbx)
leaq 0xc6d0(%rbx), %rdi
leaq 0x24f9b(%rip), %rsi     # 0x376a1
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %r14d
jmp 0x12dfd
movq %rcx, %rbx
cmpl $0x1ae, %esi            # imm = 0x1AE
je 0x12862
cmpl $0x1af, %esi            # imm = 0x1AF
je 0x12782
cmpl $0x1ea, %esi            # imm = 0x1EA
jne 0x128f7
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 0x12c26
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 0x127e3
movsd 0x278(%rdx), %xmm1
cmpb $0x1, 0x280(%rdx)
jne 0x127f4
movsd 0x288(%rdx), %xmm2
cmpb $0x1, 0x290(%rdx)
jne 0x12805
movsd 0x298(%rdx), %xmm3
cmpb $0x1, (%rdx)
jne 0x1280f
movsd 0x8(%rdx), %xmm0
cmpb $0x1, 0x10(%rdx)
jne 0x1281a
movsd 0x18(%rdx), %xmm4
cmpb $0x1, 0x20(%rdx)
jne 0x12825
movsd 0x28(%rdx), %xmm5
cmpb $0x1, 0x244(%rdx)
jne 0x12836
movsd 0x248(%rdx), %xmm6
cmpb $0x1, 0x250(%rdx)
jne 0x12847
movsd 0x258(%rdx), %xmm7
cmpb $0x1, 0x260(%rdx)
jne 0x12c26
movsd 0x268(%rdx), %xmm8
jmp 0x12c26
cmpb $0x1, 0x190(%rdx)
jne 0x12906
movl 0x194(%rdx), %edx
leaq -0x14(%rbp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa930
testl %eax, %eax
je 0x12921
movl %eax, %r14d
movq 0x316ca(%rip), %rbx     # 0x43f60
movslq 0xd670(%rbx), %rax
cmpq $0x30, %rax
jg 0x12dfd
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
leaq (%rbx,%rax), %rdi
addq $0xc6d0, %rdi           # imm = 0xC6D0
leaq 0x24de0(%rip), %rsi     # 0x376a1
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 0x12dfd
leaq 0x26e52(%rip), %rsi     # 0x39750
movq %r14, %rdi
jmp 0x126e0
cmpb $0x1, 0xc682(%rbx)
jne 0x1291a
movl 0x5fc(%rbx), %eax
movl %eax, -0x14(%rbp)
jmp 0x12921
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6d0(%rbx,%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0xa990
movsd %xmm0, -0xd0(%rbp)
movq 0x31615(%rip), %r14     # 0x43f60
movl 0x1690(%r14), %eax
movsd 0x24566(%rip), %xmm0    # 0x36ec0
movsd %xmm0, -0x68(%rbp)
cmpl $0x3, %eax
je 0x1296c
movsd 0x24534(%rip), %xmm0    # 0x36ea0
movsd 0x24554(%rip), %xmm1    # 0x36ec8
movsd %xmm1, -0x70(%rbp)
cmpl $0x1, %eax
je 0x12981
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 0x24505(%rip), %xmm0    # 0x36ec0
cmpl $0x3, %eax
je 0x129c8
movsd 0x244d8(%rip), %xmm0    # 0x36ea0
movsd 0x244f8(%rip), %xmm1    # 0x36ec8
cmpl $0x1, %eax
je 0x129d8
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 0x244ae(%rip), %xmm0    # 0x36ec0
cmpl $0x3, %eax
je 0x12a1f
movsd 0x24481(%rip), %xmm0    # 0x36ea0
movsd 0x244a1(%rip), %xmm1    # 0x36ec8
cmpl $0x1, %eax
je 0x12a2f
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 0x243ee(%rip), %xmm0    # 0x36ec0
cmpl $0x3, %eax
je 0x12adf
movsd 0x243c1(%rip), %xmm0    # 0x36ea0
movsd 0x243e1(%rip), %xmm1    # 0x36ec8
cmpl $0x1, %eax
je 0x12aef
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 0x2439a(%rip), %xmm0    # 0x36ec0
cmpl $0x3, %eax
je 0x12b33
movsd 0x2436d(%rip), %xmm0    # 0x36ea0
movsd 0x2438d(%rip), %xmm1    # 0x36ec8
cmpl $0x1, %eax
je 0x12b43
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 0x12b7c
movsd 0x24329(%rip), %xmm1    # 0x36ea0
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 0x12bde
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