// File: /workspace/github_cpp_makefile_star2_O3/khedoros[P]khednes/apu.cpp
// Function: apu::init(bool)
void apu::init(bool headless/*=false*/) {

    //dat.buffer = &(buffer[0]); Set this after the number of samples has been determined
    dat.apui = this;
    dat.first = 1;
    dat.buffered = 0;

    desired.freq = SAMPLE_RATE;
    desired.format = AUDIO_U8;
    desired.channels = 1;

    //#ifndef _WIN32
    //desired.samples = SAMPLES_PER_FRAME * 2; //Works for PulseAudio
    //#else
    desired.samples = 1024;                  //Works for DirectSound
    //#endif
    desired.callback = my_callback;
    desired.userdata = &dat;

    int init_success = 0;

    #ifdef _WIN32
    const char * desired_driver = "directsound";
    #else
    const char * desired_driver = "pulseaudio";
    #endif

    if(headless) {
        printf("In headless mode; going to init dummy audio driver.\n");
        init_success = SDL_AudioInit("dummy");
    }
    else {
        for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
            const char* driver_name = SDL_GetAudioDriver(i);
            printf("Audio driver #%d: %s\n", i, driver_name);
            if(strcmp(driver_name, desired_driver) == 0) {
                cout<<"Going to init "<<desired_driver<<" driver."<<endl;
                init_success = SDL_AudioInit(desired_driver);
            }
        }
    }
    
    const char * device_name = "";
    if(init_success == 0) {
        cout<<"I think I successfully init'd the driver. Here are the "<<SDL_GetNumAudioDevices(0)<<" audio devices it provides: "<<endl;
        for(int i=0;i<SDL_GetNumAudioDevices(0);++i) {
            device_name = SDL_GetAudioDeviceName(i,0);
            #ifdef _WIN32
            if(i == 0 && device_name) id = SDL_OpenAudioDevice(device_name, 0, &desired, &obtained, 0);
            #endif
            
            if(device_name) {
                printf("Audio device #%d: %s\n", i, device_name);
            }
            else
                printf("Audio device #%d: NULL!?!\n", i);
        }
    }

    #ifndef _WIN32
    id = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
    #endif    

    if(id <= 0) {
        cerr<<"Couldn't open the audio device: "<<SDL_GetError()<<endl;
        //return 1;
    }
    else {
        if(SDL_GetCurrentAudioDriver()) {
            cout<<"Opened audio device with driver: "<<SDL_GetCurrentAudioDriver()<<endl;
        }
        else {
            cout<<"Doesn't seem like a driver was successfully init'd!"<<endl;

        }
    }

    if(id > 0) {
    //if(desired.freq != obtained.freq || desired.format != obtained.format || desired.channels != obtained.channels || desired.samples != obtained.samples) {
        //cout<<"Desired:\n\tFreq: "<<desired.freq<<"\n\tFormat: "<<desired.format<<"\n\tChannels: "<<int(desired.channels)<<"\n\tSamples: "<<desired.samples<<endl<<endl;
        cout<<"Obtained:\n\tFreq: "<<obtained.freq<<"\n\tFormat: "<<obtained.format<<"\n\tChannels: "<<int(obtained.channels)<<"\n\tSamples: "<<obtained.samples<<endl;
            //cout<<"Bufsize: "<<bufsize<<endl;
        size_t bufsize = SAMPLES_PER_FRAME * obtained.samples;
        buffer.resize(bufsize,0);
        dat.buffer = &(buffer[0]);
    //}
    //cout<<"byte size: "<<obtained.size<<endl;

    //if(obtained.freq != SAMPLE_RATE) {
    //    cout<<"Didn't get the frequency I wanted. Should be interesting."<<endl;
    //}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r12
leaq 0x18(%rdi), %rax
movq %rdi, 0x20(%rdi)
movb $0x1, %cl
movb %cl, 0x28(%rdi)
movl $0x0, 0x2c(%rdi)
leaq 0x70(%rdi), %r13
movl $0xac44, 0x70(%rdi)     # imm = 0xAC44
movw $0x8, 0x74(%rdi)
movb %cl, 0x76(%rdi)
movw $0x400, 0x78(%rdi)      # imm = 0x400
leaq -0x134(%rip), %rcx      # 0x106b4
movq %rcx, 0x80(%rdi)
movq %rax, 0x88(%rdi)
testl %esi, %esi
je 0x10824
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
leaq 0x5823(%rip), %rdi      # 0x1602e
callq 0x4540
leaq 0x562e(%rip), %rdi      # 0x15e45
callq 0x4520
movl %eax, %r14d
jmp 0x108f7
callq 0x4100
testl %eax, %eax
jle 0x1090a
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
leaq 0x5609(%rip), %r15      # 0x15e4b
leaq 0x55f1(%rip), %r12      # 0x15e3a
movq 0xb760(%rip), %r13      # 0x1bfb0
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x4620
movq %rax, %rbx
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4050
movq %rbx, %rdi
movq %r12, %rsi
callq 0x44d0
testl %eax, %eax
jne 0x108e8
movl $0xe, %edx
movq %r13, %rdi
leaq 0x55d5(%rip), %rsi      # 0x15e61
callq 0x4410
movl $0xa, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x4410
movl $0x8, %edx
movq %r13, %rdi
leaq 0x57a9(%rip), %rsi      # 0x16059
callq 0x4410
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r13, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq %r12, %rdi
callq 0x4520
movl %eax, %r14d
incl %ebp
callq 0x4100
cmpl %eax, %ebp
jl 0x10855
testl %r14d, %r14d
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
jne 0x109c7
movq 0xb69f(%rip), %rbx      # 0x1bfb0
leaq 0x5558(%rip), %rsi      # 0x15e70
movl $0x37, %edx
movq %rbx, %rdi
callq 0x4410
xorl %edi, %edi
callq 0x4300
movq %rbx, %rdi
movl %eax, %esi
callq 0x45e0
movq %rax, %rbx
leaq 0x5568(%rip), %rsi      # 0x15ea8
movl $0x1c, %edx
movq %rax, %rdi
callq 0x4410
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
xorl %edi, %edi
callq 0x4300
testl %eax, %eax
jle 0x109c7
leaq 0x5555(%rip), %r14      # 0x15edb
leaq 0x5538(%rip), %r15      # 0x15ec5
xorl %ebp, %ebp
movl %ebp, %edi
xorl %esi, %esi
callq 0x4130
testq %rax, %rax
je 0x109ae
movq %r15, %rdi
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4050
jmp 0x109ba
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x4050
incl %ebp
xorl %edi, %edi
callq 0x4300
cmpl %eax, %ebp
jl 0x1098f
leaq 0x90(%r12), %rcx
xorl %edi, %edi
xorl %esi, %esi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x4430
movl %eax, 0x68(%r12)
testl %eax, %eax
je 0x10a1f
callq 0x40b0
movq 0xb5bd(%rip), %r14      # 0x1bfb0
testq %rax, %rax
je 0x10a5a
leaq 0x5518(%rip), %rsi      # 0x15f17
movl $0x21, %edx
movq %r14, %rdi
callq 0x4410
callq 0x40b0
testq %rax, %rax
jne 0x10a44
movq 0xb593(%rip), %r14      # 0x1bfb0
jmp 0x10a77
movq 0xb5ca(%rip), %r14      # 0x1bff0
leaq 0x54c9(%rip), %rsi      # 0x15ef6
movl $0x20, %edx
movq %r14, %rdi
callq 0x4410
callq 0x42f0
testq %rax, %rax
je 0x10a70
movq %rax, %r15
movq %rax, %rdi
callq 0x41c0
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x10a69
leaq 0x54d8(%rip), %rsi      # 0x15f39
movl $0x33, %edx
movq %r14, %rdi
callq 0x4410
jmp 0x10a8f
movq 0xb579(%rip), %r14      # 0x1bff0
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x45d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
cmpl $0x0, 0x68(%r12)
je 0x10bbb
movq 0xb4e7(%rip), %rbx      # 0x1bfb0
leaq 0x549d(%rip), %rsi      # 0x15f6d
movl $0x11, %edx
movq %rbx, %rdi
callq 0x4410
movl 0x90(%r12), %esi
movq %rbx, %rdi
callq 0x45e0
movq %rax, %rbx
leaq 0x5488(%rip), %rsi      # 0x15f7f
movl $0xa, %edx
movq %rax, %rdi
callq 0x4410
movzwl 0x94(%r12), %esi
movq %rbx, %rdi
callq 0x4280
movq %rax, %rbx
leaq 0x546b(%rip), %rsi      # 0x15f8a
movl $0xc, %edx
movq %rax, %rdi
callq 0x4410
movzbl 0x96(%r12), %esi
movq %rbx, %rdi
callq 0x45e0
movq %rax, %rbx
leaq 0x5450(%rip), %rsi      # 0x15f97
movl $0xb, %edx
movq %rax, %rdi
callq 0x4410
movzwl 0x98(%r12), %esi
movq %rbx, %rdi
callq 0x4280
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4290
movzwl 0x98(%r12), %eax
imulq $0x2df, %rax, %rsi      # imm = 0x2DF
leaq 0x38(%r12), %rdi
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
callq 0xa08e
movq 0x38(%r12), %rax
movq %rax, 0x18(%r12)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq