// File: /workspace/github_cpp_makefile_star2_O2/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 0x12971
leaq 0x1fdf6(%rip), %rdi     # 0x3271d
callq 0xb220
movq %r14, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
movq 0x2a620(%rip), %rbx     # 0x3cf60
leaq 0xc6d0(%rbx), %rdi
leaq 0x1dd53(%rip), %rsi     # 0x306a1
pushq $0x50
popq %rdx
callq 0xb940
andw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
pushq $0x5
popq %r14
jmp 0x13030
movq %rcx, %rbx
cmpl $0x1ae, %esi            # imm = 0x1AE
je 0x12a9f
cmpl $0x1af, %esi            # imm = 0x1AF
je 0x129c2
cmpl $0x1ea, %esi            # imm = 0x1EA
jne 0x12b2a
andl $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 0x12e5b
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
orl $-0x1, -0x14(%rbp)
cmpb $0x1, 0x270(%rdx)
jne 0x12a20
movsd 0x278(%rdx), %xmm1
cmpb $0x1, 0x280(%rdx)
jne 0x12a31
movsd 0x288(%rdx), %xmm2
cmpb $0x1, 0x290(%rdx)
jne 0x12a42
movsd 0x298(%rdx), %xmm3
cmpb $0x1, (%rdx)
jne 0x12a4c
movsd 0x8(%rdx), %xmm0
cmpb $0x1, 0x10(%rdx)
jne 0x12a57
movsd 0x18(%rdx), %xmm4
cmpb $0x1, 0x20(%rdx)
jne 0x12a62
movsd 0x28(%rdx), %xmm5
cmpb $0x1, 0x244(%rdx)
jne 0x12a73
movsd 0x248(%rdx), %xmm6
cmpb $0x1, 0x250(%rdx)
jne 0x12a84
movsd 0x258(%rdx), %xmm7
cmpb $0x1, 0x260(%rdx)
jne 0x12e5b
movsd 0x268(%rdx), %xmm8
jmp 0x12e5b
cmpb $0x1, 0x190(%rdx)
jne 0x12b39
movl 0x194(%rdx), %edx
leaq -0x14(%rbp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xb9d0
testl %eax, %eax
je 0x12b55
movl %eax, %r14d
movq 0x2a48d(%rip), %rbx     # 0x3cf60
movslq 0xd670(%rbx), %rax
cmpq $0x30, %rax
jg 0x13030
imulq $0x50, %rax, %rax
leaq (%rbx,%rax), %rdi
addq $0xc6d0, %rdi           # imm = 0xC6D0
leaq 0x1dba7(%rip), %rsi     # 0x306a1
pushq $0x50
popq %rdx
callq 0xb940
movslq 0xd670(%rbx), %rax
imulq $0x50, %rax, %rcx
xorl %edx, %edx
movb %dl, 0xc71f(%rbx,%rcx)
incl %eax
movl %eax, 0xd670(%rbx)
movb %dl, 0xc720(%rbx,%rcx)
jmp 0x13030
leaq 0x1fc29(%rip), %rsi     # 0x3275a
movq %r14, %rdi
jmp 0x12932
cmpb $0x1, 0xc682(%rbx)
jne 0x12b4d
movl 0x5fc(%rbx), %eax
movl %eax, -0x14(%rbp)
jmp 0x12b58
andl $0x0, -0x14(%rbp)
xorl %eax, %eax
jmp 0x12b58
movl -0x14(%rbp), %eax
cltq
imulq $0x70, %rax, %rax
movsd 0xd6d0(%rbx,%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0xba40
movsd %xmm0, -0xd0(%rbp)
movq 0x2a3e0(%rip), %r14     # 0x3cf60
movl 0x1690(%r14), %eax
movsd 0x1d331(%rip), %xmm0    # 0x2fec0
movsd %xmm0, -0x68(%rbp)
cmpl $0x3, %eax
je 0x12ba1
movsd 0x1d2ff(%rip), %xmm0    # 0x2fea0
movsd 0x1d31f(%rip), %xmm1    # 0x2fec8
movsd %xmm1, -0x70(%rbp)
cmpl $0x1, %eax
je 0x12bb6
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 0xba40
movsd %xmm0, -0xc0(%rbp)
movl 0x1690(%r14), %eax
movsd 0x1d2d0(%rip), %xmm0    # 0x2fec0
cmpl $0x3, %eax
je 0x12bfd
movsd 0x1d2a3(%rip), %xmm0    # 0x2fea0
movsd 0x1d2c3(%rip), %xmm1    # 0x2fec8
cmpl $0x1, %eax
je 0x12c0d
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 0xba40
movsd %xmm0, -0xb0(%rbp)
movl 0x1690(%r14), %eax
movsd 0x1d279(%rip), %xmm0    # 0x2fec0
cmpl $0x3, %eax
je 0x12c54
movsd 0x1d24c(%rip), %xmm0    # 0x2fea0
movsd 0x1d26c(%rip), %xmm1    # 0x2fec8
cmpl $0x1, %eax
je 0x12c64
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 0xc090
movsd %xmm0, -0xa8(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6f0(%rbx,%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
callq 0xc090
movsd %xmm0, -0xa0(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd6f8(%rbx,%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0xc090
movsd %xmm0, -0x98(%rbp)
movslq -0x14(%rbp), %rax
imulq $0x70, %rax, %rax
movsd 0xd700(%rbx,%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movsd %xmm0, -0x88(%rbp)
movl 0x1690(%r14), %eax
movsd 0x1d1b9(%rip), %xmm0    # 0x2fec0
cmpl $0x3, %eax
je 0x12d14
movsd 0x1d18c(%rip), %xmm0    # 0x2fea0
movsd 0x1d1ac(%rip), %xmm1    # 0x2fec8
cmpl $0x1, %eax
je 0x12d24
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 0xba40
movsd %xmm0, -0x78(%rbp)
movl 0x1690(%r14), %eax
movsd 0x1d165(%rip), %xmm0    # 0x2fec0
cmpl $0x3, %eax
je 0x12d68
movsd 0x1d138(%rip), %xmm0    # 0x2fea0
movsd 0x1d158(%rip), %xmm1    # 0x2fec8
cmpl $0x1, %eax
je 0x12d78
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 0xba40
movl 0x1690(%r14), %eax
cmpl $0x3, %eax
je 0x12db1
movsd 0x1d0f4(%rip), %xmm1    # 0x2fea0
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 0x12e13
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)
pushq $0x9
popq %rcx
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
callq 0xc550
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