// File: /workspace/github_c_makefile_star2_O1/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 0x91f7
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 0x10715(%rip), %rdi     # 0x19790
leaq 0x20ae(%rip), %rsi      # 0xb130
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x20d0
testl %eax, %eax
js 0x9162
movq 0x106f3(%rip), %rdi     # 0x19790
leaq 0x20b2(%rip), %rsi      # 0xb156
movl $0x3, %edx
movl $0x100000, %ecx         # imm = 0x100000
callq 0x2310
movl %eax, 0x71ff(%rip)      # 0x102b8
testl %eax, %eax
js 0x917e
movq 0x106c8(%rip), %rdi     # 0x19790
leaq 0x20ba(%rip), %rsi      # 0xb189
callq 0x21f0
movq 0x106b5(%rip), %rdi     # 0x19790
callq 0x21b0
movl %eax, 0x71d6(%rip)      # 0x102bc
testl %eax, %eax
js 0x91a3
movq 0x1069b(%rip), %rdi     # 0x19790
movl 0x71bd(%rip), %esi      # 0x102b8
xorl %ebp, %ebp
movl $0x80, %edx
xorl %ecx, %ecx
callq 0x23c0
testl %eax, %eax
js 0x91c8
leaq 0x140(%rip), %rax       # 0x9258
movq %rax, (%rbx)
leaq 0x1d2(%rip), %rax       # 0x92f4
movq %rax, 0x8(%rbx)
leaq 0x263(%rip), %rax       # 0x9390
movq %rax, 0x10(%rbx)
leaq 0x2f4(%rip), %rax       # 0x942c
movq %rax, 0x18(%rbx)
leaq 0x384(%rip), %rax       # 0x94c7
movq %rax, 0x20(%rbx)
leaq 0x411(%rip), %rax       # 0x955f
movq %rax, 0x28(%rbx)
leaq 0x49e(%rip), %rax       # 0x95f7
movq %rax, 0x30(%rbx)
jmp 0x91f0
leaq 0x1fcf(%rip), %rdi      # 0xb138
movl %eax, %esi
xorl %eax, %eax
callq 0x8aa8
movl $0x2, 0x1060c(%rip)     # 0x19788
jmp 0x91eb
callq 0x91f7
movl 0x712f(%rip), %esi      # 0x102b8
leaq 0x1fcd(%rip), %rdi      # 0xb15d
xorl %eax, %eax
callq 0x8aa8
movl $0x3, 0x105e7(%rip)     # 0x19788
jmp 0x91eb
callq 0x91f7
movl 0x710e(%rip), %esi      # 0x102bc
leaq 0x1feb(%rip), %rdi      # 0xb1a0
xorl %eax, %eax
callq 0x8aa8
movl $0x4, 0x105c2(%rip)     # 0x19788
jmp 0x91eb
movl %eax, %r14d
callq 0x91f7
leaq 0x1fee(%rip), %rdi      # 0xb1c5
movl %r14d, %esi
xorl %eax, %eax
callq 0x8aa8
movl $0x5, 0x1059d(%rip)     # 0x19788
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
popq %rbx
popq %r14
popq %rbp
retq