// File: /workspace/github_cpp_makefile_star2_O3/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 0x90e1
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x4260(%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 0x92b3
movq -0x120(%rbp), %rsi
jmp 0x92ab
movq %rdx, %r14
movl %esi, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x9247
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 0x92c2
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
movl 0xc(%r13), %eax
testl %eax, %eax
jns 0x9226
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 0x9569
movq %r15, %rdi
callq 0x77c0
movl %eax, 0xc(%r13)
movb %al, -0x29(%rbp)
cmpq $0x0, 0x10(%r13)
je 0x93a6
cmpb $0x0, 0x89(%rbx)
je 0x9465
movb $0x1, 0x10(%rbx)
jmp 0x92b3
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x40c9(%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 0x92b3
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 0x407a(%rip), %rsi      # 0xd352
movl $0x31, %edx
movq %r14, %rdi
callq 0x74a0
movl %r15d, %esi
leaq -0x2a8(%rbp), %rdi
callq 0x7230
leaq 0x4089(%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 0x9340
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 0x9382
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x7420
movq 0x7c2f(%rip), %rsi      # 0x10fb8
leaq -0x2a8(%rbp), %rdi
callq 0x71c0
leaq -0x238(%rbp), %rdi
callq 0x70c0
jmp 0x92b3
leaq 0x10(%r13), %r14
movq %r14, %rdi
callq 0x7390
testl %eax, %eax
js 0x95d6
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 0x9234
movq (%r14), %rdi
callq 0x76c0
movq $0x0, (%r14)
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x4000(%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 0x92b3
movq -0xa0(%rbp), %rsi
jmp 0x92ab
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       # 0x9733
movq %r15, %rsi
movq %r14, %rcx
callq 0x7900
movl %eax, %r14d
movq %r15, %rdi
callq 0x77d0
testl %r14d, %r14d
je 0x9241
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 0x3f2f(%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 0x92b3
movq -0x80(%rbp), %rsi
jmp 0x92ab
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3e1a(%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 0x92b3
movq -0xe0(%rbp), %rsi
jmp 0x92ab
leaq 0x18(%rbx), %rdi
movq 0x20(%rbx), %rdx
leaq 0x3de3(%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 0x92b3
movq -0xc0(%rbp), %rsi
jmp 0x92ab
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0xc0(%rbp), %rsi
jmp 0x9723
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0xa0(%rbp), %rsi
jmp 0x9723
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0xe0(%rbp), %rsi
jmp 0x9723
movq %rax, %rbx
movq -0x90(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0x80(%rbp), %rsi
jmp 0x9723
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x96ec
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x7420
jmp 0x96ec
jmp 0x96e9
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0x100(%rbp), %rsi
jmp 0x9723
movq %rax, %rbx
movq 0x78c5(%rip), %rsi      # 0x10fb8
leaq -0x2a8(%rbp), %rdi
callq 0x71c0
leaq -0x238(%rbp), %rdi
callq 0x70c0
jmp 0x972b
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x972b
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x7420
movq %rbx, %rdi
callq 0x78b0