// File: /workspace/github_c_makefile_star2_O1/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 0x8640(%rip), %r12d     # 0x117b0
leal (,%r12,8), %ebx
cmpq $0x0, 0x8628(%rip)      # 0x117a8
jne 0x9191
movq %rbx, %rdi
callq 0x51c0
movq %rax, 0x8617(%rip)      # 0x117a8
movq 0x8610(%rip), %r14      # 0x117a8
movq %r15, %rdi
movl %ebp, %esi
callq 0x9ce1
xorps %xmm0, %xmm0
testb %al, %al
je 0x9462
cmpb $0x0, 0x85ec(%rip)      # 0x117a0
je 0x91cc
movq 0x85eb(%rip), %rdi      # 0x117a8
xorl %esi, %esi
movq %rbx, %rdx
callq 0x5080
jmp 0x9425
testq %r12, %r12
je 0x9425
addq %r14, %rbx
addl %r12d, %r12d
leaq 0x8266(%rip), %r13      # 0x11448
movq 0x85a7(%rip), %rax      # 0x11790
cmpq 0x85a8(%rip), %rax      # 0x11798
jne 0x93d1
callq 0x57ac
movq 0x857e(%rip), %rdi      # 0x11780
movq 0x8567(%rip), %r15      # 0x11770
shlq $0x2, %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x5080
movq 0x859a(%rip), %rdi      # 0x117b8
xorl %esi, %esi
movq %r15, %rdx
callq 0x5080
xorl %edi, %edi
callq 0x8827
movq 0x854a(%rip), %rsi      # 0x11780
movq 0x8533(%rip), %rdx      # 0x11770
xorl %edi, %edi
callq 0xa2f6
movq 0x8525(%rip), %rdx      # 0x11770
testq %rdx, %rdx
je 0x9277
movq 0x8529(%rip), %rax      # 0x11780
movq 0x855a(%rip), %rcx      # 0x117b8
xorl %esi, %esi
movss (%rax,%rsi,4), %xmm0
addss (%rcx,%rsi,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x9260
movq 0x8502(%rip), %rdi      # 0x11780
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
movl $0x1, %edi
callq 0x8827
movq 0x84e6(%rip), %rsi      # 0x11780
movq 0x84cf(%rip), %rdx      # 0x11770
movl $0x1, %edi
callq 0xa2f6
movq 0x84be(%rip), %rdx      # 0x11770
testq %rdx, %rdx
je 0x92de
movq 0x84c2(%rip), %rax      # 0x11780
movq 0x84f3(%rip), %rcx      # 0x117b8
xorl %esi, %esi
movss (%rax,%rsi,4), %xmm0
addss (%rcx,%rsi,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x92c7
movq 0x849b(%rip), %rdi      # 0x11780
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x8a94
movq 0x8484(%rip), %rsi      # 0x11780
movq 0x846d(%rip), %rdx      # 0x11770
movl $0x2, %edi
callq 0xa2f6
movq 0x845c(%rip), %rdx      # 0x11770
testq %rdx, %rdx
je 0x9340
movq 0x8460(%rip), %rax      # 0x11780
movq 0x8491(%rip), %rcx      # 0x117b8
xorl %esi, %esi
movss (%rax,%rsi,4), %xmm0
addss (%rcx,%rsi,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x9329
movq 0x8439(%rip), %rdi      # 0x11780
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x8dd8
movq 0x8422(%rip), %rsi      # 0x11780
movq 0x840b(%rip), %rdx      # 0x11770
movl $0x3, %edi
callq 0xa2f6
movq 0x83fa(%rip), %rax      # 0x11770
testq %rax, %rax
je 0x93c3
movq 0x8436(%rip), %rcx      # 0x117b8
movq 0x83f7(%rip), %rdx      # 0x11780
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm0
addss (%rdx,%rsi,4), %xmm0
movss %xmm0, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x938b
movq 0x83d7(%rip), %rcx      # 0x11780
xorl %edx, %edx
movss 0x1c(%r13), %xmm0
mulss (%rcx,%rdx,4), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x93ab
movq 0x83b6(%rip), %rax      # 0x11780
movq %rax, 0x83bf(%rip)      # 0x11790
movq 0x83c0(%rip), %rax      # 0x11798
movq 0x83b1(%rip), %r15      # 0x11790
subq %r15, %rax
sarq $0x2, %rax
cmpq %rax, %r12
cmovlq %r12, %rax
movslq %eax, %rbp
leaq (,%rbp,4), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x55a0
leaq (%r14,%rbp,4), %r14
movq %rbx, %r12
subq %r14, %r12
sarq $0x2, %r12
leaq (%r15,%rbp,4), %rax
movq %rax, 0x8374(%rip)      # 0x11790
cmpq %r14, %rbx
jne 0x91e2
movq 0x837c(%rip), %rdi      # 0x117a8
movzwl 0x837d(%rip), %esi      # 0x117b0
callq 0x9cf3
cmpb $0x0, 0x8361(%rip)      # 0x117a0
xorps %xmm0, %xmm0
jne 0x9462
movzwl 0x8365(%rip), %eax      # 0x117b0
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x2baa(%rip), %xmm0     # 0xc004
divss 0x3dfa(%rip), %xmm0     # 0xd25c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq