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

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

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

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

  if ( data->vport < 0 ) {
    data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
                                              SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
                                              SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION );
    if ( data->vport < 0 ) {
      errorString_ = "MidiOutAlsa::openPort: ALSA error creating output port.";
      error( RtMidiError::DRIVER_ERROR, errorString_ );
      return;
    }
  }

  sender.port = data->vport;

  // Make subscription
  if (snd_seq_port_subscribe_malloc( &data->subscription ) < 0) {
    snd_seq_port_subscribe_free( data->subscription );
    errorString_ = "MidiOutAlsa::openPort: error allocating 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);
  snd_seq_port_subscribe_set_time_update(data->subscription, 1);
  snd_seq_port_subscribe_set_time_real(data->subscription, 1);
  if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
    snd_seq_port_subscribe_free( data->subscription );
    errorString_ = "MidiOutAlsa::openPort: ALSA error making port connection.";
    error( RtMidiError::DRIVER_ERROR, errorString_ );
    return;
  }

  connected_ = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movq %rdx, -0x2a8(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2a0(%rbp)
testb $0x1, 0x10(%rax)
je 0x120f2
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x2b8(%rbp)
leaq 0x7521(%rip), %rsi      # 0x195b8
callq 0xdfb0
movq -0x2b8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0xd3d0
movq -0x2a0(%rbp), %rdi
movq -0x2b0(%rbp), %rdx
xorl %esi, %esi
callq 0xd5f0
jmp 0x120ca
leaq -0x38(%rbp), %rdi
callq 0xd600
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xd600
jmp 0x125ce
movq -0x2a0(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
movl %eax, -0x48(%rbp)
cmpl $0x1, -0x48(%rbp)
jae 0x1217f
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x2c8(%rbp)
leaq 0x74d1(%rip), %rsi      # 0x195f2
callq 0xdfb0
movq -0x2c8(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x2c0(%rbp)
callq 0xd3d0
movq -0x2a0(%rbp), %rdi
movq -0x2c0(%rbp), %rdx
movl $0x3, %esi
callq 0xd5f0
jmp 0x12157
leaq -0x68(%rbp), %rdi
callq 0xd600
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xd600
jmp 0x125ce
jmp 0x12181
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, -0x2d0(%rbp)
callq 0xe2f0
movq -0x2d0(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0xd530
movq -0x2a0(%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 $0x42, %edx
callq 0xd4c0
cmpl $0x0, %eax
jne 0x12314
leaq -0x1f0(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0xdf80
movq -0x2e0(%rbp), %rdi
leaq 0x7417(%rip), %rsi      # 0x19627
callq 0xda30
movq %rax, -0x2d8(%rbp)
jmp 0x1221e
movq -0x2d8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0xdf30
movq %rax, -0x2e8(%rbp)
jmp 0x12236
movq -0x2e8(%rbp), %rdi
leaq 0x7148(%rip), %rsi      # 0x1938c
callq 0xda30
jmp 0x1224b
leaq -0x210(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0xdad0
jmp 0x12260
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x2f0(%rbp)
leaq -0x210(%rbp), %rsi
movq %rsi, -0x2f8(%rbp)
callq 0xddc0
movq -0x2f8(%rbp), %rdi
callq 0xd600
movq -0x2f0(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0xd3d0
jmp 0x122a6
movq -0x2a0(%rbp), %rdi
movl $0x6, %esi
leaq -0x230(%rbp), %rdx
callq 0xd5f0
jmp 0x122c0
leaq -0x230(%rbp), %rdi
callq 0xd600
leaq -0x1f0(%rbp), %rdi
callq 0xd1a0
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x12303
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 0x125ce
movq -0x70(%rbp), %rdi
callq 0xd380
movb %al, -0x234(%rbp)
movq -0x70(%rbp), %rdi
callq 0xe390
movb %al, -0x233(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rdi
callq 0xe7b0
movb %al, -0x232(%rbp)
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0x1241e
movq -0x2a8(%rbp), %rdi
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x300(%rbp)
callq 0xd3b0
movq -0x300(%rbp), %rdi
movq %rax, %rsi
movl $0x21, %edx
movl $0x100002, %ecx         # imm = 0x100002
callq 0xe210
movl %eax, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x78(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0x1241c
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x310(%rbp)
leaq 0x72a5(%rip), %rsi      # 0x1965a
callq 0xdfb0
movq -0x310(%rbp), %rsi
leaq -0x258(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0xd3d0
movq -0x2a0(%rbp), %rdi
movq -0x308(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x123ee
leaq -0x258(%rbp), %rdi
callq 0xd600
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x258(%rbp), %rdi
callq 0xd600
jmp 0x125ce
jmp 0x1241e
movq -0x78(%rbp), %rax
movl 0xc(%rax), %eax
movb %al, -0x231(%rbp)
movq -0x78(%rbp), %rdi
addq $0x10, %rdi
callq 0xd940
cmpl $0x0, %eax
jge 0x124ce
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xe1c0
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x320(%rbp)
leaq 0x722b(%rip), %rsi      # 0x19692
callq 0xdfb0
movq -0x320(%rbp), %rsi
leaq -0x278(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0xd3d0
movq -0x2a0(%rbp), %rdi
movq -0x318(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x124a0
leaq -0x278(%rbp), %rdi
callq 0xd600
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x278(%rbp), %rdi
callq 0xd600
jmp 0x125ce
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 0x10(%rax), %rdi
movl $0x1, %esi
callq 0xdec0
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %esi
callq 0xe3d0
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xd390
cmpl $0x0, %eax
je 0x125be
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xe1c0
movq -0x2a0(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x330(%rbp)
leaq 0x7170(%rip), %rsi      # 0x196cd
callq 0xdfb0
movq -0x330(%rbp), %rsi
leaq -0x298(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0xd3d0
movq -0x2a0(%rbp), %rdi
movq -0x328(%rbp), %rdx
movl $0x8, %esi
callq 0xd5f0
jmp 0x12596
leaq -0x298(%rbp), %rdi
callq 0xd600
jmp 0x125c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x298(%rbp), %rdi
callq 0xd600
jmp 0x125ce
movq -0x2a0(%rbp), %rax
movb $0x1, 0x10(%rax)
movq %rbp, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0xe590
nopw (%rax,%rax)