// File: 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
subq $0x3b0, %rsp            # imm = 0x3B0
movq %rdx, -0x310(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x308(%rbp)
testb $0x1, 0x10(%rax)
je 0x10582
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x320(%rbp)
leaq 0x8dc7(%rip), %rsi      # 0x192ee
callq 0xdfb0
movq -0x320(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x318(%rbp), %rdx
xorl %esi, %esi
callq 0xd5f0
jmp 0x1055a
leaq -0x38(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
movq -0x308(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
movl %eax, -0x48(%rbp)
cmpl $0x1, -0x48(%rbp)
jae 0x1060f
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x330(%rbp)
leaq 0x8d76(%rip), %rsi      # 0x19327
callq 0xdfb0
movq -0x330(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x328(%rbp), %rdx
movl $0x3, %esi
callq 0xd5f0
jmp 0x105e7
leaq -0x68(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
jmp 0x10611
callq 0xe2f0
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x338(%rbp)
callq 0xe2f0
movq -0x338(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0xd530
movq -0x308(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rsi
movl -0xc(%rbp), %ecx
movl $0x21, %edx
callq 0xd4c0
cmpl $0x0, %eax
jne 0x107a4
leaq -0x1f0(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0xdf80
movq -0x348(%rbp), %rdi
leaq 0x8cba(%rip), %rsi      # 0x1935a
callq 0xda30
movq %rax, -0x340(%rbp)
jmp 0x106ae
movq -0x340(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0xdf30
movq %rax, -0x350(%rbp)
jmp 0x106c6
movq -0x350(%rbp), %rdi
leaq 0x8cb8(%rip), %rsi      # 0x1938c
callq 0xda30
jmp 0x106db
leaq -0x210(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0xdad0
jmp 0x106f0
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x358(%rbp)
leaq -0x210(%rbp), %rsi
movq %rsi, -0x360(%rbp)
callq 0xddc0
movq -0x360(%rbp), %rdi
callq 0xd600
movq -0x358(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0xd3d0
jmp 0x10736
movq -0x308(%rbp), %rdi
movl $0x6, %esi
leaq -0x230(%rbp), %rdx
callq 0xd5f0
jmp 0x10750
leaq -0x230(%rbp), %rdi
callq 0xd600
leaq -0x1f0(%rbp), %rdi
callq 0xd1a0
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x10793
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x230(%rbp), %rdi
callq 0xd600
leaq -0x1f0(%rbp), %rdi
callq 0xd1a0
jmp 0x10c9a
movq -0x70(%rbp), %rdi
callq 0xd380
movb %al, -0x232(%rbp)
movq -0x70(%rbp), %rdi
callq 0xe390
movb %al, -0x231(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0xe7b0
movb %al, -0x234(%rbp)
callq 0xe2f0
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rax
movq %rax, -0x368(%rbp)
callq 0xe2f0
movq -0x368(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0xd530
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0x10992
movq -0x240(%rbp), %rdi
xorl %esi, %esi
callq 0xdf60
movq -0x240(%rbp), %rdi
xorl %esi, %esi
callq 0xd700
movq -0x240(%rbp), %rdi
movl $0x42, %esi
callq 0xe380
movq -0x240(%rbp), %rdi
movl $0x100002, %esi         # imm = 0x100002
callq 0xd580
movq -0x240(%rbp), %rdi
movl $0x10, %esi
callq 0xd430
movq -0x240(%rbp), %rdi
movl $0x1, %esi
callq 0xe160
movq -0x240(%rbp), %rdi
movl $0x1, %esi
callq 0xe260
movq -0x240(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x48(%rax), %esi
callq 0xe630
movq -0x310(%rbp), %rdi
movq -0x240(%rbp), %rax
movq %rax, -0x370(%rbp)
callq 0xd3b0
movq -0x370(%rbp), %rdi
movq %rax, %rsi
callq 0xe780
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x240(%rbp), %rsi
callq 0xd2f0
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0x1097d
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x380(%rbp)
leaq 0x8a84(%rip), %rsi      # 0x1939a
callq 0xdfb0
movq -0x380(%rbp), %rsi
leaq -0x260(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x378(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x1094f
leaq -0x260(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x260(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
movq -0x240(%rbp), %rdi
callq 0xe390
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
movb %al, -0x233(%rbp)
movq -0x78(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x10b24
movq -0x78(%rbp), %rdi
addq $0x10, %rdi
callq 0xd940
cmpl $0x0, %eax
jge 0x10a44
movq -0x308(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x390(%rbp)
leaq 0x89f3(%rip), %rsi      # 0x193d0
callq 0xdfb0
movq -0x390(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x388(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x10a16
leaq -0x280(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x280(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x232(%rbp), %rsi
callq 0xd810
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x234(%rbp), %rsi
callq 0xe520
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xd390
cmpl $0x0, %eax
je 0x10b22
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xe1c0
movq -0x308(%rbp), %rdi
movq -0x78(%rbp), %rax
movq $0x0, 0x10(%rax)
addq $0x18, %rdi
movq %rdi, -0x3a0(%rbp)
leaq 0x8954(%rip), %rsi      # 0x1940f
callq 0xdfb0
movq -0x3a0(%rbp), %rsi
leaq -0x2a0(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x398(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x10af4
leaq -0x2a0(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
jmp 0x10b24
movq -0x308(%rbp), %rax
movb 0x89(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x10c8a
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movl 0x48(%rax), %esi
movl $0x1e, %edx
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0xdbe0
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0xe5b0
leaq -0x2d8(%rbp), %rdi
callq 0xdda0
leaq -0x2d8(%rbp), %rdi
xorl %esi, %esi
callq 0xdea0
leaq -0x2d8(%rbp), %rdi
xorl %esi, %esi
callq 0xe040
movq -0x308(%rbp), %rcx
movb $0x1, 0x89(%rcx)
movq -0x78(%rbp), %rdi
addq $0x30, %rdi
addq $0x50, %rcx
leaq -0x2d8(%rbp), %rsi
leaq 0xf6(%rip), %rdx        # 0x10cb0
callq 0xe650
movl %eax, -0x2dc(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0xe3a0
cmpl $0x0, -0x2dc(%rbp)
je 0x10c88
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rax), %rsi
callq 0xe560
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xe1c0
movq -0x308(%rbp), %rdi
movq -0x78(%rbp), %rax
movq $0x0, 0x10(%rax)
movb $0x0, 0x89(%rdi)
addq $0x18, %rdi
movq %rdi, -0x3b0(%rbp)
leaq 0x8821(%rip), %rsi      # 0x19448
callq 0xdfb0
movq -0x3b0(%rbp), %rsi
leaq -0x300(%rbp), %rdi
movq %rdi, -0x3a8(%rbp)
callq 0xd3d0
movq -0x308(%rbp), %rdi
movq -0x3a8(%rbp), %rdx
movl $0xa, %esi
callq 0xd5f0
jmp 0x10c60
leaq -0x300(%rbp), %rdi
callq 0xd600
jmp 0x10c95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x300(%rbp), %rdi
callq 0xd600
jmp 0x10c9a
jmp 0x10c8a
movq -0x308(%rbp), %rax
movb $0x1, 0x10(%rax)
movq %rbp, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0xe590
nopw %cs:(%rax,%rax)