// File: /workspace/github_c_makefile_star2_O0/jonof[P]jfaudiolib/src/driver_sdl.c
// Function: SDLDrv_PCM_Init
int SDLDrv_PCM_Init(int * mixrate, int * numchannels, int * samplebits, void * initdata)
{
    Uint32 inited;
    int err = 0;
    SDL_AudioSpec spec, actual;

    (void)initdata;

    if (Initialised) {
        SDLDrv_PCM_Shutdown();
    }

    inited = SDL_WasInit(SDL_INIT_EVERYTHING);

    if (inited == 0) {
        // nothing was initialised
        err = SDL_Init(SDL_INIT_AUDIO);
        StartedSDLInit |= 0x80000000 + SDL_INIT_AUDIO;
    } else if (!(inited & SDL_INIT_AUDIO)) {
        err = SDL_InitSubSystem(SDL_INIT_AUDIO);
        StartedSDLInit |= SDL_INIT_AUDIO;
    }

    if (err < 0) {
        ErrorCode = SDLErr_InitSubSystem;
        return SDLErr_Error;
    }

    StartedSDL |= SDL_INIT_AUDIO;

    #if (SDL_MAJOR_VERSION == 1)
    {
        char drivername[256] = "(error)";
        SDL_AudioDriverName(drivername, sizeof(drivername));
        ASS_Message("SDLDrv: audio driver: %s\n", drivername);
    }
    #else
    {
        const char * drivername = SDL_GetCurrentAudioDriver();
        ASS_Message("SDLDrv: audio driver: %s\n", drivername ? drivername : "(error)");
    }
    #endif

    memset(&spec, 0, sizeof(spec));
    spec.freq = *mixrate;
    spec.format = (*samplebits == 8) ? AUDIO_U8 : AUDIO_S16SYS;
    spec.channels = *numchannels;
    spec.callback = fillData;
    spec.userdata = 0;

    // Mix buffer to be a power of 2, min 512 samples, and 4096 samples at 48kHz.
    spec.samples = 512;
    while (spec.samples < (4096 * *mixrate / 48000))
        spec.samples += spec.samples;

    memset(&actual, 0, sizeof(actual));

    err = SDL_OpenAudio(&spec, &actual);
    if (err < 0) {
        ErrorCode = SDLErr_OpenAudio;
        return SDLErr_Error;
    }

    if (actual.freq == 0 || actual.channels == 0) {
        // hack for when SDL said it opened the audio, but clearly didn't
        SDL_CloseAudio();
        ErrorCode = SDLErr_OpenAudio;
        return SDLErr_Error;
    }

    err = 0;

    *mixrate = actual.freq;
    if (actual.format == AUDIO_U8 || actual.format == AUDIO_S16SYS) {
        *samplebits = actual.format & 0xff;
    } else {
        const char *format;
        switch (actual.format) {
            case AUDIO_U8: format = "AUDIO_U8"; break;
            case AUDIO_S8: format = "AUDIO_S8"; break;
            case AUDIO_U16LSB: format = "AUDIO_U16LSB"; break;
            case AUDIO_S16LSB: format = "AUDIO_S16LSB"; break;
            case AUDIO_U16MSB: format = "AUDIO_U16MSB"; break;
            case AUDIO_S16MSB: format = "AUDIO_S16MSB"; break;
            default: format = "?!"; break;
        }
        ASS_Message("SDLDrv: audio format: %s\n", format);
        ErrorCode = SDLErr_OpenAudio;
        err = 1;
    }
    if (actual.channels == 1 || actual.channels == 2) {
        *numchannels = actual.channels;
    } else {
        ASS_Message("SDLDrv: audio channels: %d\n", actual.channels);
        ErrorCode = SDLErr_OpenAudio;
        err = 1;
    }
    // ASS_Message("SDLDrv: actual samples = %d vs spec samples = %d\n", actual.samples, spec.samples);

    if (err) {
        SDL_CloseAudio();
        return SDLErr_Error;
    } else {
        Initialised = 1;
        return SDLErr_Ok;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x0, 0x1195b(%rip)     # 0x1f194
je 0xd840
callq 0xdb80
movl $0xf231, %edi           # imm = 0xF231
callq 0x2130
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0xd873
movl $0x10, %edi
callq 0x21a0
movl %eax, -0x30(%rbp)
movl 0x11932(%rip), %eax     # 0x1f198
orl $0x80000010, %eax       # imm = 0x80000010
movl %eax, 0x11927(%rip)     # 0x1f198
jmp 0xd89c
movl -0x2c(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xd89a
movl $0x10, %edi
callq 0x21b0
movl %eax, -0x30(%rbp)
movl 0x11907(%rip), %eax     # 0x1f198
orl $0x10, %eax
movl %eax, 0x118fe(%rip)     # 0x1f198
jmp 0xd89c
cmpl $0x0, -0x30(%rbp)
jge 0xd8b8
movl $0x2, 0x118e4(%rip)     # 0x1f190
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xdb70
movl 0x118de(%rip), %eax     # 0x1f19c
orl $0x10, %eax
movl %eax, 0x118d5(%rip)     # 0x1f19c
callq 0x2080
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0xd8e4
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0xd8f4
leaq 0x2353(%rip), %rax      # 0xfc3e
movq %rax, -0x88(%rbp)
jmp 0xd8f4
movq -0x88(%rbp), %rsi
leaq 0x2322(%rip), %rdi      # 0xfc24
movb $0x0, %al
callq 0xd640
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edx
movl $0x8010, %eax           # imm = 0x8010
movl $0x8, %ecx
cmpl $0x8, %edx
cmovel %ecx, %eax
movw %ax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movb %al, -0x4a(%rbp)
leaq 0x2a4(%rip), %rax       # 0xdbf0
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movw $0x200, -0x48(%rbp)     # imm = 0x200
movzwl -0x48(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0xc, %eax
movl $0xbb80, %ecx           # imm = 0xBB80
cltd
idivl %ecx
movl %eax, %ecx
movl -0x8c(%rbp), %eax
cmpl %ecx, %eax
jge 0xd995
movzwl -0x48(%rbp), %ecx
movzwl -0x48(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x48(%rbp)
jmp 0xd95e
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x2350
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0xd9d1
movl $0x3, 0x117cb(%rip)     # 0x1f190
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xdb70
cmpl $0x0, -0x70(%rbp)
je 0xd9e0
movzbl -0x6a(%rbp), %eax
cmpl $0x0, %eax
jne 0xd9fb
callq 0x2120
movl $0x3, 0x117a1(%rip)     # 0x1f190
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xdb70
movl $0x0, -0x30(%rbp)
movl -0x70(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movzwl -0x6c(%rbp), %eax
cmpl $0x8, %eax
je 0xda1f
movzwl -0x6c(%rbp), %eax
cmpl $0x8010, %eax           # imm = 0x8010
jne 0xda34
movzwl -0x6c(%rbp), %ecx
andl $0xff, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0xdb0a
movzwl -0x6c(%rbp), %eax
movl %eax, -0x90(%rbp)
subl $0x8, %eax
je 0xda8e
jmp 0xda45
movl -0x90(%rbp), %eax
subl $0x10, %eax
je 0xdaa8
jmp 0xda52
movl -0x90(%rbp), %eax
subl $0x1010, %eax           # imm = 0x1010
je 0xdac2
jmp 0xda61
movl -0x90(%rbp), %eax
subl $0x8008, %eax           # imm = 0x8008
je 0xda9b
jmp 0xda70
movl -0x90(%rbp), %eax
subl $0x8010, %eax           # imm = 0x8010
je 0xdab5
jmp 0xda7f
movl -0x90(%rbp), %eax
subl $0x9010, %eax           # imm = 0x9010
je 0xdacf
jmp 0xdadc
leaq 0x21b1(%rip), %rax      # 0xfc46
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21ad(%rip), %rax      # 0xfc4f
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21a9(%rip), %rax      # 0xfc58
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21a9(%rip), %rax      # 0xfc65
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21a9(%rip), %rax      # 0xfc72
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21a9(%rip), %rax      # 0xfc7f
movq %rax, -0x80(%rbp)
jmp 0xdae7
leaq 0x21a9(%rip), %rax      # 0xfc8c
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rsi
leaq 0x219d(%rip), %rdi      # 0xfc8f
movb $0x0, %al
callq 0xd640
movl $0x3, 0x1168d(%rip)     # 0x1f190
movl $0x1, -0x30(%rbp)
movzbl -0x6a(%rbp), %eax
cmpl $0x1, %eax
je 0xdb1c
movzbl -0x6a(%rbp), %eax
cmpl $0x2, %eax
jne 0xdb28
movzbl -0x6a(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0xdb4b
movzbl -0x6a(%rbp), %esi
leaq 0x2176(%rip), %rdi      # 0xfca9
movb $0x0, %al
callq 0xd640
movl $0x3, 0x1164c(%rip)     # 0x1f190
movl $0x1, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0xdb5f
callq 0x2120
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xdb70
movl $0x1, 0x1162b(%rip)     # 0x1f194
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)