// File: /workspace/github_cpp_makefile_star2_O1/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 0x9161
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x41e0(%rip), %rcx      # 0xd2e6
movl $0x38, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0x120(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x7200
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0x120(%rbp), %rsi
jmp 0x932e
movq %rdx, %r14
movl %esi, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x92ca
callq 0x7760
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
callq 0x7760
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x71d0
movq 0x8(%rbx), %r13
movq (%r13), %rdi
movq %r12, %rsi
movl $0x21, %edx
movl %r15d, %ecx
callq 0x71a0
testl %eax, %eax
je 0x9345
movq %r12, %rdi
callq 0x7110
movb %al, -0x70(%rbp)
movq %r12, %rdi
callq 0x77c0
movb %al, -0x6f(%rbp)
movq (%r13), %rdi
callq 0x79a0
movb %al, -0x2a(%rbp)
callq 0x7760
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
callq 0x7760
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x71d0
cmpl $0x0, 0xc(%r13)
jns 0x92a5
movq %r15, %rdi
xorl %esi, %esi
callq 0x7600
movq %r15, %rdi
xorl %esi, %esi
callq 0x7270
movq %r15, %rdi
movl $0x42, %esi
callq 0x77b0
movq %r15, %rdi
movl $0x100002, %esi         # imm = 0x100002
callq 0x71f0
movq %r15, %rdi
movl $0x10, %esi
callq 0x7170
movq %r15, %rdi
movl $0x1, %esi
callq 0x76a0
movq %r15, %rdi
movl $0x1, %esi
callq 0x7720
movl 0x48(%r13), %esi
movq %r15, %rdi
callq 0x78f0
movq (%r14), %rsi
movq %r15, %rdi
callq 0x7980
movq (%r13), %rdi
movq %r15, %rsi
callq 0x70b0
movl %eax, 0xc(%r13)
testl %eax, %eax
js 0x95ec
movq %r15, %rdi
callq 0x77c0
movl %eax, 0xc(%r13)
movb 0xc(%r13), %al
movb %al, -0x29(%rbp)
cmpq $0x0, 0x10(%r13)
je 0x9429
cmpb $0x0, 0x89(%rbx)
je 0x94e8
movb $0x1, 0x10(%rbx)
jmp 0x9336
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x4046(%rip), %rcx      # 0xd31f
movl $0x32, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0x100(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
movl $0x3, %esi
movq %r14, %rdx
callq 0x7200
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x7420
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 0x7610
leaq 0x3ff7(%rip), %rsi      # 0xd352
movl $0x31, %edx
movq %r14, %rdi
callq 0x74a0
movl %r15d, %esi
leaq -0x2a8(%rbp), %rdi
callq 0x7230
leaq 0x4006(%rip), %rsi      # 0xd384
movl $0xd, %edx
movq %rax, %rdi
callq 0x74a0
leaq -0x2a0(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x7740
leaq 0x18(%rbx), %rdi
leaq -0x70(%rbp), %r14
movq %r14, %rsi
callq 0x7560
movq (%r14), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x93c3
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x7420
leaq -0x40(%rbp), %r14
movq %r14, -0x10(%r14)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x7130
leaq -0x50(%rbp), %rdx
movq %rbx, %rdi
movl $0x6, %esi
callq 0x7200
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x9405
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x7420
movq 0x7bac(%rip), %rsi      # 0x10fb8
leaq -0x2a8(%rbp), %rdi
callq 0x71c0
leaq -0x238(%rbp), %rdi
callq 0x70c0
jmp 0x9336
leaq 0x10(%r13), %r14
movq %r14, %rdi
callq 0x7390
testl %eax, %eax
js 0x9659
movq 0x10(%r13), %rdi
leaq -0x70(%rbp), %rsi
callq 0x72e0
movq 0x10(%r13), %rdi
leaq -0x2a(%rbp), %rsi
callq 0x7850
movq (%r13), %rdi
movq 0x10(%r13), %rsi
callq 0x7120
testl %eax, %eax
je 0x92b7
movq (%r14), %rdi
callq 0x76c0
movq $0x0, (%r14)
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3f7d(%rip), %rcx      # 0xd407
movl $0x38, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0xa0(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0xb0(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
movl $0x8, %esi
movq %r14, %rdx
callq 0x7200
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0xa0(%rbp), %rsi
jmp 0x932e
leaq 0x50(%rbx), %r14
movq (%r13), %rdi
movl 0x48(%r13), %esi
movl $0x1e, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7470
movq (%r13), %rdi
callq 0x78c0
leaq -0x2a8(%rbp), %r15
movq %r15, %rdi
callq 0x7540
movq %r15, %rdi
xorl %esi, %esi
callq 0x7590
movq %r15, %rdi
xorl %esi, %esi
callq 0x7650
movb $0x1, 0x89(%rbx)
leaq 0x30(%r13), %rdi
leaq 0x275(%rip), %rdx       # 0x97b6
movq %r15, %rsi
movq %r14, %rcx
callq 0x7900
movl %eax, %r14d
movq %r15, %rdi
callq 0x77d0
testl %r14d, %r14d
je 0x92c4
movq (%r13), %rdi
movq 0x10(%r13), %rsi
callq 0x7880
movq 0x10(%r13), %rdi
callq 0x76c0
movq $0x0, 0x10(%r13)
movb $0x0, 0x89(%rbx)
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3eac(%rip), %rcx      # 0xd440
movl $0x37, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0x80(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
movl $0xa, %esi
movq %r14, %rdx
callq 0x7200
movq -0x90(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0x80(%rbp), %rsi
jmp 0x932e
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3d97(%rip), %rcx      # 0xd392
movl $0x35, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0xe0(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
movl $0x8, %esi
movq %r14, %rdx
callq 0x7200
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0xe0(%rbp), %rsi
jmp 0x932e
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3d60(%rip), %rcx      # 0xd3c8
movl $0x3e, %r8d
xorl %esi, %esi
callq 0x7710
leaq -0xc0(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
callq 0x7130
movq %rbx, %rdi
movl $0x8, %esi
movq %r14, %rdx
callq 0x7200
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
je 0x9336
movq -0xc0(%rbp), %rsi
jmp 0x932e
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0xc0(%rbp), %rsi
jmp 0x97a6
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0xa0(%rbp), %rsi
jmp 0x97a6
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0xe0(%rbp), %rsi
jmp 0x97a6
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0x80(%rbp), %rsi
jmp 0x97a6
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x976f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x7420
jmp 0x976f
jmp 0x976c
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0x100(%rbp), %rsi
jmp 0x97a6
movq %rax, %rbx
movq 0x7842(%rip), %rsi      # 0x10fb8
leaq -0x2a8(%rbp), %rdi
callq 0x71c0
leaq -0x238(%rbp), %rdi
callq 0x70c0
jmp 0x97ae
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x97ae
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x7420
movq %rbx, %rdi
callq 0x78b0