// File: sundale[P]gcode/rs274ngc/interp_write.cc
// Function: Interp::write_m_codes(block_struct*, setup_struct*)
int Interp::write_m_codes(block_pointer block,   //!< pointer to a block of RS274/NGC instructions
                         setup_pointer settings)        //!< pointer to machine settings                 
{
  int *emz;

  emz = settings->active_m_codes;
  emz[0] = settings->sequence_number;   /* 0 seq number  */
  emz[1] = (block == NULL) ? -1 : block->m_modes[4];    /* 1 stopping    */
  emz[2] = (settings->spindle_turning == CANON_STOPPED) ? 5 :   /* 2 spindle     */
    (settings->spindle_turning == CANON_CLOCKWISE) ? 3 : 4;
  emz[3] =                      /* 3 tool change */
    (block == NULL) ? -1 : block->m_modes[6];
  emz[4] =                      /* 4 mist        */
    (settings->mist == ON) ? 7 : (settings->flood == ON) ? -1 : 9;
  emz[5] =                      /* 5 flood       */
    (settings->flood == ON) ? 8 : -1;
  if (settings->feed_override == ON) {
    if (settings->speed_override == ON) emz[6] =  48;
    else emz[6] = 50;
  } else if (settings->speed_override == ON) {
    emz[6] = 51;
  } else emz[6] = 49;
  
  emz[7] =                      /* 7 overrides   */
    (settings->adaptive_feed == ON) ? 52 : -1;

  emz[8] =                      /* 8 overrides   */
    (settings->feed_hold == ON) ? 53 : -1;

  return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0xc6b4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x10(%rbp)
jne 0x6abef
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x24(%rbp)
jmp 0x6abfc
movq -0x10(%rbp), %rax
movl 0x1f0(%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, 0xc6cc(%rax)
jne 0x6ac1d
movl $0x5, %eax
movl %eax, -0x28(%rbp)
jmp 0x6ac3a
movq -0x18(%rbp), %rax
movl 0xc6cc(%rax), %edx
movl $0x4, %eax
movl $0x3, %ecx
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpq $0x0, -0x10(%rbp)
jne 0x6ac55
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x2c(%rbp)
jmp 0x6ac62
movq -0x10(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rax
movb 0x1797(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6ac8a
movl $0x7, %eax
movl %eax, -0x30(%rbp)
jmp 0x6acac
movq -0x18(%rbp), %rax
movb 0x1688(%rax), %al
andb $0x1, %al
movzbl %al, %edx
movl $0x9, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rax
movb 0x1688(%rax), %al
andb $0x1, %al
movzbl %al, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x8, %eax
cmpl $0x1, %edx
cmovel %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
movb 0x674(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6ad1e
movq -0x18(%rbp), %rax
movb 0xc6c8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6ad11
movq -0x20(%rbp), %rax
movl $0x30, 0x18(%rax)
jmp 0x6ad1c
movq -0x20(%rbp), %rax
movl $0x32, 0x18(%rax)
jmp 0x6ad4c
movq -0x18(%rbp), %rax
movb 0xc6c8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x6ad3f
movq -0x20(%rbp), %rax
movl $0x33, 0x18(%rax)
jmp 0x6ad4a
movq -0x20(%rbp), %rax
movl $0x31, 0x18(%rax)
jmp 0x6ad4c
movq -0x18(%rbp), %rax
movb 0x1b6b0(%rax), %al
andb $0x1, %al
movzbl %al, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x34, %eax
cmpl $0x1, %edx
cmovel %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
movb 0x1b6b1(%rax), %al
andb $0x1, %al
movzbl %al, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0x35, %eax
cmpl $0x1, %edx
cmovel %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x20(%rax)
xorl %eax, %eax
popq %rbp
retq
nopl (%rax)