// File: /workspace/github_cpp_makefile_star2_O2/tobias93[P]piano/rtmidi_build/RtMidi.cpp
// Function: MidiInAlsa::openPort(unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void MidiInAlsa :: openPort( unsigned int portNumber, const std::string portName )
{
  if ( connected_ ) {
    errorString_ = "MidiInAlsa::openPort: a valid connection already exists!";
    error( RtMidiError::WARNING, errorString_ );
    return;
  }

  unsigned int nSrc = this->getPortCount();
  if ( nSrc < 1 ) {
    errorString_ = "MidiInAlsa::openPort: no MIDI input sources found!";
    error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
    return;
  }

  snd_seq_port_info_t *src_pinfo;
  snd_seq_port_info_alloca( &src_pinfo );
  AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
  if ( portInfo( data->seq, src_pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) == 0 ) {
    std::ostringstream ost;
    ost << "MidiInAlsa::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
    errorString_ = ost.str();
    error( RtMidiError::INVALID_PARAMETER, errorString_ );
    return;
  }

  snd_seq_addr_t sender, receiver;
  sender.client = snd_seq_port_info_get_client( src_pinfo );
  sender.port = snd_seq_port_info_get_port( src_pinfo );
  receiver.client = snd_seq_client_id( data->seq );

  snd_seq_port_info_t *pinfo;
  snd_seq_port_info_alloca( &pinfo );
  if ( data->vport < 0 ) {
    snd_seq_port_info_set_client( pinfo, 0 );
    snd_seq_port_info_set_port( pinfo, 0 );
    snd_seq_port_info_set_capability( pinfo,
                                      SND_SEQ_PORT_CAP_WRITE |
                                      SND_SEQ_PORT_CAP_SUBS_WRITE );
    snd_seq_port_info_set_type( pinfo,
                                SND_SEQ_PORT_TYPE_MIDI_GENERIC |
                                SND_SEQ_PORT_TYPE_APPLICATION );
    snd_seq_port_info_set_midi_channels(pinfo, 16);
#ifndef AVOID_TIMESTAMPING
    snd_seq_port_info_set_timestamping(pinfo, 1);
    snd_seq_port_info_set_timestamp_real(pinfo, 1);    
    snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
#endif
    snd_seq_port_info_set_name(pinfo,  portName.c_str() );
    data->vport = snd_seq_create_port(data->seq, pinfo);
  
    if ( data->vport < 0 ) {
      errorString_ = "MidiInAlsa::openPort: ALSA error creating input port.";
      error( RtMidiError::DRIVER_ERROR, errorString_ );
      return;
    }
    data->vport = snd_seq_port_info_get_port(pinfo);
  }

  receiver.port = data->vport;

  if ( !data->subscription ) {
    // Make subscription
    if (snd_seq_port_subscribe_malloc( &data->subscription ) < 0) {
      errorString_ = "MidiInAlsa::openPort: ALSA error allocation port subscription.";
      error( RtMidiError::DRIVER_ERROR, errorString_ );
      return;
    }
    snd_seq_port_subscribe_set_sender(data->subscription, &sender);
    snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
    if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
      snd_seq_port_subscribe_free( data->subscription );
      data->subscription = 0;
      errorString_ = "MidiInAlsa::openPort: ALSA error making port connection.";
      error( RtMidiError::DRIVER_ERROR, errorString_ );
      return;
    }
  }

  if ( inputData_.doInput == false ) {
    // Start the input queue
#ifndef AVOID_TIMESTAMPING
    snd_seq_start_queue( data->seq, data->queue_id, NULL );
    snd_seq_drain_output( data->seq );
#endif
    // Start our MIDI input thread.
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    pthread_attr_setschedpolicy(&attr, SCHED_OTHER);

    inputData_.doInput = true;
    int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
    pthread_attr_destroy(&attr);
    if ( err ) {
      snd_seq_unsubscribe_port( data->seq, data->subscription );
      snd_seq_port_subscribe_free( data->subscription );
      data->subscription = 0;
      inputData_.doInput = false;
      errorString_ = "MidiInAlsa::openPort: error starting MIDI input thread!";
      error( RtMidiError::THREAD_ERROR, errorString_ );
      return;
    }
  }

  connected_ = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdi, %rbx
cmpb $0x1, 0x10(%rdi)
jne 0x8c77
leaq 0x18(%rbx), %r14
leaq 0x36b2(%rip), %rsi      # 0xc2f6
movq %r14, %rdi
callq 0x7af0
leaq -0x130(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x7240
leaq -0x130(%rbp), %rdi
jmp 0x8e11
movq %rdx, %r14
movl %esi, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x8dd7
callq 0x7890
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
callq 0x7890
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x71f0
movq 0x8(%rbx), %r13
movq (%r13), %rdi
pushq $0x21
popq %rdx
movq %r12, %rsi
movl %r15d, %ecx
callq 0x71d0
testl %eax, %eax
je 0x8e25
movq %r12, %rdi
callq 0x7130
movb %al, -0x50(%rbp)
movq %r12, %rdi
callq 0x7900
movb %al, -0x4f(%rbp)
movq (%r13), %rdi
callq 0x7b30
movb %al, -0x2a(%rbp)
callq 0x7890
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
callq 0x7890
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x71f0
movl 0xc(%r13), %eax
testl %eax, %eax
jns 0x8db6
movq %r15, %rdi
xorl %esi, %esi
callq 0x76c0
movq %r15, %rdi
xorl %esi, %esi
callq 0x72c0
pushq $0x42
popq %rsi
movq %r15, %rdi
callq 0x78f0
movq %r15, %rdi
movl $0x100002, %esi         # imm = 0x100002
callq 0x7210
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x7190
pushq $0x1
popq %r12
movq %r15, %rdi
movl %r12d, %esi
callq 0x77c0
movq %r15, %rdi
movl %r12d, %esi
callq 0x7830
movl 0x48(%r13), %esi
movq %r15, %rdi
callq 0x7a70
movq (%r14), %rsi
movq %r15, %rdi
callq 0x7b10
movq (%r13), %rdi
movq %r15, %rsi
callq 0x70e0
movl %eax, 0xc(%r13)
testl %eax, %eax
js 0x9022
movq %r15, %rdi
callq 0x7900
movl %eax, 0xc(%r13)
movb %al, -0x29(%rbp)
cmpq $0x0, 0x10(%r13)
je 0x8ebd
cmpb $0x0, 0x89(%rbx)
je 0x8f4b
movb $0x1, 0x10(%rbx)
jmp 0x8e16
leaq 0x18(%rbx), %r14
leaq 0x354d(%rip), %rsi      # 0xc32f
movq %r14, %rdi
callq 0x7af0
leaq -0x110(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
pushq $0x3
popq %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x7240
leaq -0x110(%rbp), %rdi
callq 0x7180
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x2a8(%rbp), %r14
movq %r14, %rdi
callq 0x76d0
leaq 0x3527(%rip), %rsi      # 0xc362
movq %r14, %rdi
callq 0x7470
movl %r15d, %esi
movq %rax, %rdi
callq 0x7290
leaq 0x353f(%rip), %rsi      # 0xc394
movq %rax, %rdi
callq 0x7470
leaq -0x2a0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7860
leaq 0x18(%rbx), %r14
leaq -0x50(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x7600
movq %r15, %rdi
callq 0x7180
leaq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x7720
pushq $0x6
popq %rsi
leaq -0x70(%rbp), %rdx
movq %rbx, %rdi
callq 0x7240
leaq -0x70(%rbp), %rdi
callq 0x7180
leaq -0x2a8(%rbp), %rdi
callq 0x7090
jmp 0x8e16
leaq 0x10(%r13), %r14
movq %r14, %rdi
callq 0x7400
testl %eax, %eax
js 0x9061
movq 0x10(%r13), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7330
movq 0x10(%r13), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x79b0
movq (%r13), %rdi
movq 0x10(%r13), %rsi
callq 0x7140
testl %eax, %eax
je 0x8dc4
movq (%r14), %rdi
callq 0x77e0
andq $0x0, (%r14)
leaq 0x18(%rbx), %r14
leaq 0x3500(%rip), %rsi      # 0xc417
movq %r14, %rdi
callq 0x7af0
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
pushq $0x8
popq %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x7240
leaq -0xb0(%rbp), %rdi
jmp 0x8e11
leaq 0x50(%rbx), %r14
movq (%r13), %rdi
movl 0x48(%r13), %esi
pushq $0x1e
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7500
movq (%r13), %rdi
callq 0x7a20
leaq -0x2a8(%rbp), %r15
movq %r15, %rdi
callq 0x75e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x7640
movq %r15, %rdi
xorl %esi, %esi
callq 0x7740
movb $0x1, 0x89(%rbx)
leaq 0x30(%r13), %rdi
leaq 0x172(%rip), %rdx       # 0x9114
movq %r15, %rsi
movq %r14, %rcx
callq 0x7a80
movl %eax, %r14d
movq %r15, %rdi
callq 0x7910
testl %r14d, %r14d
je 0x8dd1
movq (%r13), %rdi
movq 0x10(%r13), %rsi
callq 0x79e0
movq 0x10(%r13), %rdi
callq 0x77e0
andq $0x0, 0x10(%r13)
movb $0x0, 0x89(%rbx)
leaq 0x18(%rbx), %r14
leaq 0x3462(%rip), %rsi      # 0xc450
movq %r14, %rdi
callq 0x7af0
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
pushq $0xa
popq %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x7240
leaq -0x90(%rbp), %rdi
jmp 0x8e11
leaq 0x18(%rbx), %r14
leaq 0x3375(%rip), %rsi      # 0xc3a2
movq %r14, %rdi
callq 0x7af0
leaq -0xf0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
pushq $0x8
popq %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x7240
leaq -0xf0(%rbp), %rdi
jmp 0x8e11
leaq 0x18(%rbx), %r14
leaq 0x336c(%rip), %rsi      # 0xc3d8
movq %r14, %rdi
callq 0x7af0
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7720
pushq $0x8
popq %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x7240
leaq -0xd0(%rbp), %rdi
jmp 0x8e11
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x9107
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x9107
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
jmp 0x9107
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x9107
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x7180
jmp 0x90ef
jmp 0x90ec
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
jmp 0x9107
movq %rax, %rbx
leaq -0x2a8(%rbp), %rdi
callq 0x7090
jmp 0x910c
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x7180
movq %rbx, %rdi
callq 0x7a10