// File: tobias93[P]piano/rtmidi_build/tests/midiclock.cpp
// Function: clock_out()
int clock_out()
{
  RtMidiOut *midiout = 0;
  std::vector<unsigned char> message;
  int sleep_ms = 0, k = 0, j = 0;

  // RtMidiOut constructor
  try {
    midiout = new RtMidiOut();
  }
  catch ( RtMidiError &error ) {
    error.printMessage();
    exit( EXIT_FAILURE );
  }

  // Call function to select port.
  try {
    if ( chooseOutputPort( midiout ) == false ) goto cleanup;
  }
  catch ( RtMidiError &error ) {
    error.printMessage();
    goto cleanup;
  }

  // Period in ms = 100 BPM
  // 100*24 ticks / 1 minute, so (60*1000) / (100*24) = 25 ms / tick
  sleep_ms = 25;
  std::cout << "Generating clock at "
            << (60.0 / 24.0 / sleep_ms * 1000.0)
            << " BPM." << std::endl;

  // Send out a series of MIDI clock messages.
  // MIDI start
  message.clear();
  message.push_back( 0xFA );
  midiout->sendMessage( &message );
  std::cout << "MIDI start" << std::endl;

  for (j=0; j < 8; j++)
  {
    if (j > 0)
    {
      // MIDI continue
      message.clear();
      message.push_back( 0xFB );
      midiout->sendMessage( &message );
      std::cout << "MIDI continue" << std::endl;
    }

    for (k=0; k < 96; k++) {
      // MIDI clock
      message.clear();
      message.push_back( 0xF8 );
      midiout->sendMessage( &message );
      if (k % 24 == 0)
        std::cout << "MIDI clock (one beat)" << std::endl;
      SLEEP( sleep_ms );
    }

    // MIDI stop
    message.clear();
    message.push_back( 0xFC );
    midiout->sendMessage( &message );
    std::cout << "MIDI stop" << std::endl;
    SLEEP( 500 );
  }

  // MIDI stop
  message.clear();
  message.push_back( 0xFC );
  midiout->sendMessage( &message );
  std::cout << "MIDI stop" << std::endl;

  SLEEP( 500 );

  std::cout << "Done!" << std::endl;

  // Clean up
 cleanup:
  delete midiout;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq $0x0, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4000
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x10, %edi
callq 0x3160
movq %rax, -0x98(%rbp)
jmp 0x3794
movb $0x1, -0x6a(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x32b0
movq -0xa0(%rbp), %rdx
leaq 0x28f8(%rip), %rsi      # 0x60ae
leaq -0x68(%rbp), %rdi
callq 0x3ec0
jmp 0x37c1
movq -0x98(%rbp), %rdi
xorl %esi, %esi
leaq -0x68(%rbp), %rdx
callq 0x3140
jmp 0x37d5
movq -0x98(%rbp), %rax
movb $0x0, -0x6a(%rbp)
movq %rax, -0x10(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x30b0
leaq -0x69(%rbp), %rdi
callq 0x31a0
jmp 0x3881
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3850
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x382c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x30b0
leaq -0x69(%rbp), %rdi
callq 0x31a0
testb $0x1, -0x6a(%rbp)
jne 0x383d
jmp 0x384e
movq -0x98(%rbp), %rdi
movl $0x10, %esi
callq 0x3170
jmp 0x3850
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3ce2
movq -0x40(%rbp), %rdi
callq 0x3080
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movl $0x1, %edi
callq 0x31e0
movq -0x10(%rbp), %rdi
callq 0x3d00
movb %al, -0xa1(%rbp)
jmp 0x3892
movb -0xa1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x38ee
jmp 0x3c9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3ce2
movq -0x40(%rbp), %rdi
callq 0x3080
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
movl $0x2, -0x84(%rbp)
callq 0x3290
jmp 0x38f0
jmp 0x3915
movl -0x84(%rbp), %eax
subl $0x2, %eax
je 0x3c9e
jmp 0x3ccd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3ce2
movl $0x19, -0x2c(%rbp)
movq 0x56ad(%rip), %rdi      # 0x8fd0
leaq 0x2799(%rip), %rsi      # 0x60c3
callq 0x3150
movq %rax, -0xb0(%rbp)
jmp 0x3938
movq -0xb0(%rbp), %rdi
cvtsi2sdl -0x2c(%rbp), %xmm1
movsd 0x26bc(%rip), %xmm0     # 0x6008
divsd %xmm1, %xmm0
movsd 0x26b8(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
callq 0x32d0
movq %rax, -0xb8(%rbp)
jmp 0x396a
movq -0xb8(%rbp), %rdi
leaq 0x2760(%rip), %rsi      # 0x60d8
callq 0x3150
movq %rax, -0xc0(%rbp)
jmp 0x3986
movq -0xc0(%rbp), %rdi
movq 0x562c(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x399b
leaq -0x28(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x4020
movq -0xc8(%rbp), %rdi
movb $-0x6, -0x85(%rbp)
leaq -0x85(%rbp), %rsi
callq 0x4040
jmp 0x39c7
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4070
jmp 0x39d6
movq 0x55f3(%rip), %rdi      # 0x8fd0
leaq 0x26fa(%rip), %rsi      # 0x60de
callq 0x3150
movq %rax, -0xd0(%rbp)
jmp 0x39f2
movq -0xd0(%rbp), %rdi
movq 0x55c0(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3a07
movl $0x0, -0x34(%rbp)
cmpl $0x8, -0x34(%rbp)
jge 0x3bf3
cmpl $0x0, -0x34(%rbp)
jle 0x3a8c
leaq -0x28(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x4020
movq -0xd8(%rbp), %rdi
movb $-0x5, -0x86(%rbp)
leaq -0x86(%rbp), %rsi
callq 0x4040
jmp 0x3a4a
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4070
jmp 0x3a59
movq 0x5570(%rip), %rdi      # 0x8fd0
leaq 0x2682(%rip), %rsi      # 0x60e9
callq 0x3150
movq %rax, -0xe0(%rbp)
jmp 0x3a75
movq -0xe0(%rbp), %rdi
movq 0x553d(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3a8a
jmp 0x3a8c
movl $0x0, -0x30(%rbp)
cmpl $0x60, -0x30(%rbp)
jge 0x3b6b
leaq -0x28(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x4020
movq -0xe8(%rbp), %rdi
movb $-0x8, -0x87(%rbp)
leaq -0x87(%rbp), %rsi
callq 0x4040
jmp 0x3ac9
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4070
jmp 0x3ad8
movl -0x30(%rbp), %eax
movl $0x18, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x3b1b
movq 0x54e1(%rip), %rdi      # 0x8fd0
leaq 0x2601(%rip), %rsi      # 0x60f7
callq 0x3150
movq %rax, -0xf0(%rbp)
jmp 0x3b04
movq -0xf0(%rbp), %rdi
movq 0x54ae(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3b19
jmp 0x3b1b
cvtsi2sdl -0x2c(%rbp), %xmm0
movsd 0x24e8(%rip), %xmm1     # 0x6010
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
movq %rcx, %rax
sarq $0x3f, %rax
movl %eax, %edx
movsd 0x24d3(%rip), %xmm1     # 0x6018
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andl %edx, %eax
movl %ecx, %edi
orl %eax, %edi
callq 0x3240
jmp 0x3b5b
jmp 0x3b5d
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x3a93
leaq -0x28(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x4020
movq -0xf8(%rbp), %rdi
movb $-0x4, -0x88(%rbp)
leaq -0x88(%rbp), %rsi
callq 0x4040
jmp 0x3b97
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4070
jmp 0x3ba6
movq 0x5423(%rip), %rdi      # 0x8fd0
leaq 0x2559(%rip), %rsi      # 0x610d
callq 0x3150
movq %rax, -0x100(%rbp)
jmp 0x3bc2
movq -0x100(%rbp), %rdi
movq 0x53f0(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3bd7
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3240
jmp 0x3be3
jmp 0x3be5
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3a0e
leaq -0x28(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x4020
movq -0x108(%rbp), %rdi
movb $-0x4, -0x89(%rbp)
leaq -0x89(%rbp), %rsi
callq 0x4040
jmp 0x3c1f
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4070
jmp 0x3c2e
movq 0x539b(%rip), %rdi      # 0x8fd0
leaq 0x24d1(%rip), %rsi      # 0x610d
callq 0x3150
movq %rax, -0x110(%rbp)
jmp 0x3c4a
movq -0x110(%rbp), %rdi
movq 0x5368(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3c5f
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3240
jmp 0x3c6b
movq 0x535e(%rip), %rdi      # 0x8fd0
leaq 0x249e(%rip), %rsi      # 0x6117
callq 0x3150
movq %rax, -0x118(%rbp)
jmp 0x3c87
movq -0x118(%rbp), %rdi
movq 0x532b(%rip), %rsi      # 0x8fc0
callq 0x3180
jmp 0x3c9c
jmp 0x3c9e
movq -0x10(%rbp), %rax
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
je 0x3cbc
movq -0x120(%rbp), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl $0x0, -0x4(%rbp)
movl $0x1, -0x84(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x40a0
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x40a0
movq -0x40(%rbp), %rdi
callq 0x32a0
nopw %cs:(%rax,%rax)