// File: /workspace/github_c_makefile_star2_O3/baines[P]MiniGBS/audio.c
// Function: audio_update
float audio_update(struct pollfd* fds, int nfds){
	static float* buf = NULL;
	const size_t bufsz = (pcm_period_size*2) * sizeof(float);

	if(!buf){
		buf = malloc(bufsz);
	}

	float* p = buf;
	float* end = buf + (bufsz/sizeof(float));

	if(!audio_output_ready(fds, nfds)) {
		return 0;
	}

	if(paused){
		memset(buf, 0, bufsz);
		goto out;
	}

	while(end - p){
		if(sample_ptr == sample_end){
			cpu_frame();

			memset(samples    , 0, nsamples * sizeof(float));
			memset(samples_tmp, 0, nsamples * sizeof(float));

			update_square(0);
			ui_osc_draw(0, samples, nsamples);

			for(size_t i = 0; i < nsamples; ++i) samples_tmp[i] += samples[i];
			memset(samples, 0, nsamples * sizeof(float));

			update_square(1);
			ui_osc_draw(1, samples, nsamples);

			for(size_t i = 0; i < nsamples; ++i) samples_tmp[i] += samples[i];
			memset(samples, 0, nsamples * sizeof(float));

			update_wave();
			ui_osc_draw(2, samples, nsamples);

			for(size_t i = 0; i < nsamples; ++i) samples_tmp[i] += samples[i];
			memset(samples, 0, nsamples * sizeof(float));

			update_noise();
			ui_osc_draw(3, samples, nsamples);

			for(size_t i = 0; i < nsamples; ++i) samples[i] += samples_tmp[i];

			for(size_t i = 0; i < nsamples; ++i){
				samples[i] *= cfg.volume;
			}

			sample_ptr = samples;
		}

		int n = MIN(end - p, sample_end - sample_ptr);
		memcpy(p, sample_ptr, n * sizeof(float));
		sample_ptr += n;
		p += n;
	}

out:
	audio_output_write(buf, pcm_period_size);

	if(paused) {
		return 0;
	}

	return (pcm_period_size * 1000.0f / FREQ);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %r15
movzwl 0x87af(%rip), %r12d     # 0x117b0
leal (,%r12,8), %eax
movq %rax, (%rsp)
movq 0x8794(%rip), %r14      # 0x117a8
testq %r14, %r14
jne 0x902c
movq (%rsp), %rdi
callq 0x51c0
movq %rax, %r14
movq %rax, 0x877c(%rip)      # 0x117a8
movq %r15, %rdi
movl %ebp, %esi
callq 0x9c21
xorps %xmm0, %xmm0
testb %al, %al
je 0x92d5
cmpb $0x0, 0x8758(%rip)      # 0x117a0
je 0x9061
movq 0x8757(%rip), %rdi      # 0x117a8
xorl %esi, %esi
movq (%rsp), %rdx
callq 0x5080
jmp 0x9298
testq %r12, %r12
je 0x9298
addq %r14, (%rsp)
addl %r12d, %r12d
movq 0x8718(%rip), %r15      # 0x11790
movq 0x8719(%rip), %rbp      # 0x11798
leaq 0x83c2(%rip), %r13      # 0x11448
cmpq %rbp, %r15
jne 0x924b
callq 0x57ac
movq 0x86e5(%rip), %rdi      # 0x11780
movq 0x86ce(%rip), %r15      # 0x11770
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x5080
movq 0x8701(%rip), %rdi      # 0x117b8
xorl %esi, %esi
movq %r15, %rdx
callq 0x5080
xorl %edi, %edi
callq 0x8774
movq 0x86b1(%rip), %rsi      # 0x11780
movq 0x869a(%rip), %rdx      # 0x11770
xorl %edi, %edi
callq 0xa22c
movq 0x868c(%rip), %rdx      # 0x11770
movq 0x8695(%rip), %rdi      # 0x11780
testq %rdx, %rdx
je 0x9110
movq 0x86c1(%rip), %rax      # 0x117b8
xorl %ecx, %ecx
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x90f9
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
movl $0x1, %edi
callq 0x8774
movq 0x8654(%rip), %rsi      # 0x11780
movq 0x863d(%rip), %rdx      # 0x11770
movl $0x1, %edi
callq 0xa22c
movq 0x862c(%rip), %rdx      # 0x11770
movq 0x8635(%rip), %rdi      # 0x11780
testq %rdx, %rdx
je 0x9170
movq 0x8661(%rip), %rax      # 0x117b8
xorl %ecx, %ecx
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x9159
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x89a6
movq 0x85f9(%rip), %rsi      # 0x11780
movq 0x85e2(%rip), %rdx      # 0x11770
movl $0x2, %edi
callq 0xa22c
movq 0x85d1(%rip), %rdx      # 0x11770
movq 0x85da(%rip), %rdi      # 0x11780
testq %rdx, %rdx
je 0x91cb
movq 0x8606(%rip), %rax      # 0x117b8
xorl %ecx, %ecx
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
cmpq %rcx, %rdx
jne 0x91b4
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x8cb7
movq 0x859e(%rip), %rsi      # 0x11780
movq 0x8587(%rip), %rdx      # 0x11770
movl $0x3, %edi
callq 0xa22c
movq 0x8576(%rip), %rax      # 0x11770
movq 0x857f(%rip), %r15      # 0x11780
testq %rax, %rax
je 0x9244
movq 0x85ab(%rip), %rcx      # 0x117b8
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm0
addss (%r15,%rdx,4), %xmm0
movss %xmm0, (%r15,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x920f
xorl %ecx, %ecx
movss 0x1c(%r13), %xmm0
mulss (%r15,%rcx,4), %xmm0
movss %xmm0, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x922a
movq 0x854d(%rip), %rbp      # 0x11798
movq %rbp, %rax
subq %r15, %rax
sarq $0x2, %rax
cmpq %rax, %r12
cmovlq %r12, %rax
movslq %eax, %rbx
leaq (,%rbx,4), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x55a0
leaq (%r14,%rbx,4), %r14
movq (%rsp), %rax
movq %rax, %r12
subq %r14, %r12
sarq $0x2, %r12
leaq (%r15,%rbx,4), %r15
movq %r15, 0x8501(%rip)      # 0x11790
cmpq %r14, %rax
jne 0x9086
movq 0x8509(%rip), %rdi      # 0x117a8
movzwl 0x850a(%rip), %esi      # 0x117b0
callq 0x9c33
cmpb $0x0, 0x84ee(%rip)      # 0x117a0
xorps %xmm0, %xmm0
jne 0x92d5
movzwl 0x84f2(%rip), %eax      # 0x117b0
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x2d83(%rip), %xmm0     # 0xc050
divss 0x3fb7(%rip), %xmm0     # 0xd28c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq