// File: /workspace/github_c_makefile_star2_O2/jonof[P]jfaudiolib/src/driver_alsa.c
// Function: ALSADrv_MIDI_Init
int ALSADrv_MIDI_Init(midifuncs *funcs, const char *params)
{
    int result;

    (void)params;
    
    ALSADrv_MIDI_Shutdown();
    memset(funcs, 0, sizeof(midifuncs));

    result = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
    if (result < 0) {
        ASS_Message("ALSADrv: snd_seq_open err %d\n", result);
        ErrorCode = ALSAErr_SeqOpen;
        return ALSAErr_Error;
    }
    
    seq_port = snd_seq_create_simple_port(seq, "output",
                  SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE,
                  SND_SEQ_PORT_TYPE_APPLICATION);
    if (seq_port < 0) {
        ALSADrv_MIDI_Shutdown();
        ASS_Message("ALSADrv: snd_seq_create_simple_port err %d\n", seq_port);
        ErrorCode = ALSAErr_CreateSimplePort;
        return ALSAErr_Error;
    }
    
    snd_seq_set_client_name(seq, "JFAudioLib application");

    seq_queue = snd_seq_alloc_queue(seq);
    if (seq_queue < 0) {
        ALSADrv_MIDI_Shutdown();
        ASS_Message("ALSADrv: snd_seq_alloc_queue err %d\n", seq_queue);
        ErrorCode = ALSAErr_AllocQueue;
        return ALSAErr_Error;
    }
    
    result = snd_seq_connect_to(seq, seq_port, 128, 0);
    if (result < 0) {
        ALSADrv_MIDI_Shutdown();
        ASS_Message("ALSADrv: snd_seq_connect_to err %d\n", result);
        ErrorCode = ALSAErr_ConnectTo;
        return ALSAErr_Error;
    }
    
    funcs->NoteOff = Func_NoteOff;
    funcs->NoteOn  = Func_NoteOn;
    funcs->PolyAftertouch = Func_PolyAftertouch;
    funcs->ControlChange = Func_ControlChange;
    funcs->ProgramChange = Func_ProgramChange;
    funcs->ChannelAftertouch = Func_ChannelAftertouch;
    funcs->PitchBend = Func_PitchBend;
    
    return ALSAErr_Ok;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
callq 0x883a
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
leaq 0x100dc(%rip), %rdi     # 0x187a0
leaq 0x1a45(%rip), %rsi      # 0xa110
pushq $0x3
popq %rdx
xorl %ecx, %ecx
callq 0x20d0
testl %eax, %eax
js 0x87a7
movq 0x100bc(%rip), %rdi     # 0x187a0
leaq 0x1a4b(%rip), %rsi      # 0xa136
pushq $0x3
popq %rdx
movl $0x100000, %ecx         # imm = 0x100000
callq 0x2330
movl %eax, 0x6bca(%rip)      # 0xf2c8
testl %eax, %eax
js 0x87c3
movq 0x10093(%rip), %rdi     # 0x187a0
leaq 0x1a55(%rip), %rsi      # 0xa169
callq 0x2210
movq 0x10080(%rip), %rdi     # 0x187a0
callq 0x21c0
movl %eax, 0x6ba1(%rip)      # 0xf2cc
testl %eax, %eax
js 0x87e8
movq 0x10066(%rip), %rdi     # 0x187a0
movl 0x6b88(%rip), %esi      # 0xf2c8
xorl %ebp, %ebp
movl $0x80, %edx
xorl %ecx, %ecx
callq 0x23e0
testl %eax, %eax
js 0x880d
leaq 0x138(%rip), %rax       # 0x8895
movq %rax, (%rbx)
leaq 0x15a(%rip), %rax       # 0x88c1
movq %rax, 0x8(%rbx)
leaq 0x17b(%rip), %rax       # 0x88ed
movq %rax, 0x10(%rbx)
leaq 0x19c(%rip), %rax       # 0x8919
movq %rax, 0x18(%rbx)
leaq 0x1bc(%rip), %rax       # 0x8944
movq %rax, 0x20(%rbx)
leaq 0x1da(%rip), %rax       # 0x896d
movq %rax, 0x28(%rbx)
leaq 0x1f8(%rip), %rax       # 0x8996
movq %rax, 0x30(%rbx)
jmp 0x8833
leaq 0x196a(%rip), %rdi      # 0xa118
movl %eax, %esi
xorl %eax, %eax
callq 0x813d
movl $0x2, 0xffd7(%rip)      # 0x18798
jmp 0x8830
callq 0x883a
movl 0x6afa(%rip), %esi      # 0xf2c8
leaq 0x1968(%rip), %rdi      # 0xa13d
xorl %eax, %eax
callq 0x813d
movl $0x3, 0xffb2(%rip)      # 0x18798
jmp 0x8830
callq 0x883a
movl 0x6ad9(%rip), %esi      # 0xf2cc
leaq 0x1986(%rip), %rdi      # 0xa180
xorl %eax, %eax
callq 0x813d
movl $0x4, 0xff8d(%rip)      # 0x18798
jmp 0x8830
movl %eax, %r14d
callq 0x883a
leaq 0x1989(%rip), %rdi      # 0xa1a5
movl %r14d, %esi
xorl %eax, %eax
callq 0x813d
movl $0x5, 0xff68(%rip)      # 0x18798
pushq $-0x1
popq %rbp
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq