// File: /workspace/github_cpp_makefile_star2_O3/khedoros[P]khednes/apu.cpp
// Function: apu::get_sample(int)
int apu::get_sample(int voice) {
    if(!enabled[voice] || length[voice] == 0 || wavelength_count_reset[voice] == 0) {
        //cout<<"Returning 0"<<endl;
        //cout<<"Enabled: "<<enabled[voice]<<" wavelength: "<<wavelength_count[voice]<<endl;
        return 0;
    }
    //cout<<"Generating something."<<endl;
    int to_gen = 0;
    float interleave = float(CLK_PER_FRAME * 20) / float(SAMPLE_RATE);
    if(interleave - int(interleave) >= 0.5) to_gen = int(interleave) + 1;
    else to_gen = int(interleave);
    //cout<<"to_gen: "<<to_gen<<endl;
    int accum = 0;
    int vol = 0;
    while(to_gen > 0) {
        //cout<<to_gen<<" left to go!"<<endl;
        int this_one = 0;
        if(wavelength_count[voice] == 0) clock_duty(voice);
        if(wavelength_count[voice] >= to_gen) {
            this_one = to_gen;
        }
        else this_one = wavelength_count[voice];
            
        //cout<<"Doing "<<this_one<<" for this iteration"<<endl;
        switch(voice) {
            case SQ1:
                vol = sq1_duty();
                break;
            case SQ2:
                vol = sq2_duty();
                break;
            case TRI:
                vol = tri_duty();
                break;
            case NOISE:
                vol = noise_duty();
                break;
        }
        //if(vol != 0) cout<<"Vol: "<<vol<<endl;
        to_gen -= this_one;
        wavelength_count[voice] -= this_one;
        accum += (vol * this_one);
        if(wavelength_count[voice] == 0) {
            //clock the channel's duty thingie
            clock_duty(voice);
        }
    }
    //cout<<"Accum: "<<accum<<" returning: "<<accum/41<<endl;
    return accum / 41;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0xc(%rsp)
movslq %esi, %r15
xorl %eax, %eax
cmpb $0x1, 0xb4(%rdi,%r15)
jne 0x11a3b
movq %rdi, %r14
cmpl $0x0, 0x174(%rdi,%r15,4)
je 0x11a3b
cmpl $0x0, 0x188(%r14,%r15,4)
je 0x11a3b
movl $0x29, %ebx
xorl %r12d, %r12d
movl 0xc(%rsp), %eax
movq %rax, 0x40(%rsp)
leaq 0xf0(%r14), %rax
movq %rax, 0x38(%rsp)
leaq 0xbc(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0xc0(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0xf4(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0xc8(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0xfc(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movl 0x19c(%r14,%r15,4), %eax
testl %eax, %eax
jne 0x118f4
movq %r14, %rdi
movl %r15d, %esi
callq 0x11b32
movl 0x19c(%r14,%r15,4), %eax
cmpl %ebx, %eax
movl %ebx, %ebp
cmovll %eax, %ebp
cmpl $0x3, 0xc(%rsp)
ja 0x119f7
movq 0x40(%rsp), %rcx
leaq 0x445e(%rip), %rdx      # 0x15d70
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl 0x104(%r14), %ecx
movslq 0x1b0(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1cc2c(%rip), %rsi     # 0x2e560
addq %rsi, %rcx
movzbl (%rdx,%rcx), %r12d
cmpb $0x0, 0xe4(%r14)
movq 0x30(%rsp), %rcx
cmovneq 0x38(%rsp), %rcx
jmp 0x119f3
movslq 0x1b8(%r14), %rcx
leaq 0x1cbde(%rip), %rdx     # 0x2e540
movzbl (%rcx,%rdx), %r12d
jmp 0x119f7
movl 0x110(%r14), %ecx
movslq 0x1bc(%r14), %rdx
movq %rcx, %rsi
shlq $0xf, %rsi
subq %rcx, %rsi
leaq 0xcb75(%rip), %rcx      # 0x1e500
addq %rcx, %rsi
cmpb $0x0, 0xe7(%r14)
movzbl (%rdx,%rsi), %ecx
movq 0x18(%rsp), %rdx
cmovneq 0x10(%rsp), %rdx
imull (%rdx), %ecx
leal (%rcx,%rcx,2), %edx
leal (%rcx,%rcx,2), %r12d
addl $0x3, %r12d
testl %edx, %edx
cmovnsl %edx, %r12d
sarl $0x2, %r12d
jmp 0x119f7
movl 0x108(%r14), %ecx
movslq 0x1b4(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1cb88(%rip), %rsi     # 0x2e560
addq %rsi, %rcx
movzbl (%rdx,%rcx), %r12d
cmpb $0x0, 0xe5(%r14)
movq 0x28(%rsp), %rcx
cmovneq 0x20(%rsp), %rcx
imull (%rcx), %r12d
movl %eax, %ecx
subl %ebp, %ecx
movl %ecx, 0x19c(%r14,%r15,4)
cmpl %ebx, %eax
jg 0x11a13
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x11b32
subl %ebp, %ebx
imull %r12d, %ebp
addl %ebp, %r13d
testl %ebx, %ebx
jg 0x118d5
movslq %r13d, %rax
imulq $0x63e7063f, %rax, %rax # imm = 0x63E7063F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x24, %rax
addl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq