// File: /workspace/github_c_makefile_star2_O2/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 0x9b84(%rip), %r12d     # 0x127b0
shll $0x3, %r12d
movq 0x9b71(%rip), %r14      # 0x127a8
testq %r14, %r14
jne 0x8c4e
movq %r12, %rdi
callq 0x51c0
movq %rax, %r14
movq %rax, 0x9b5a(%rip)      # 0x127a8
movq %r15, %rdi
movl %ebp, %esi
callq 0x962d
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
testb %al, %al
je 0x8eea
cmpb $0x0, 0x9b30(%rip)      # 0x127a0
je 0x8c8b
movq 0x9b2f(%rip), %r14      # 0x127a8
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x5080
jmp 0x8eb1
addq %r14, %r12
pushq $0x1
popq %rbp
leaq 0x97b0(%rip), %r15      # 0x12448
movq %r12, %rbx
subq %r14, %rbx
je 0x8eaa
movq 0x9ae5(%rip), %r13      # 0x12790
movq 0x9ae6(%rip), %rax      # 0x12798
cmpq %rax, %r13
jne 0x8e67
callq 0x56ec
movq 0x9ab9(%rip), %rdi      # 0x12780
movq 0x9aa2(%rip), %r13      # 0x12770
shlq $0x2, %r13
xorl %esi, %esi
movq %r13, %rdx
callq 0x5080
movq 0x9ad5(%rip), %rdi      # 0x127b8
xorl %esi, %esi
movq %r13, %rdx
callq 0x5080
xorl %edi, %edi
callq 0x84f7
movq 0x9a85(%rip), %rsi      # 0x12780
movq 0x9a6e(%rip), %rdx      # 0x12770
xorl %edi, %edi
callq 0x9c2a
movq 0x9a60(%rip), %rdx      # 0x12770
movq 0x9a69(%rip), %rdi      # 0x12780
movq 0x9a9a(%rip), %rax      # 0x127b8
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x8d39
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0x8d20
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
movl %ebp, %edi
callq 0x84f7
movq 0x9a2e(%rip), %rsi      # 0x12780
movq 0x9a17(%rip), %rdx      # 0x12770
movl %ebp, %edi
callq 0x9c2a
movq 0x9a09(%rip), %rdx      # 0x12770
movq 0x9a12(%rip), %rdi      # 0x12780
movq 0x9a43(%rip), %rax      # 0x127b8
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x8d90
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0x8d77
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x86ea
movq 0x99d9(%rip), %rsi      # 0x12780
movq 0x99c2(%rip), %rdx      # 0x12770
pushq $0x2
popq %rdi
callq 0x9c2a
movq 0x99b3(%rip), %rdx      # 0x12770
movq 0x99bc(%rip), %rdi      # 0x12780
movq 0x99ed(%rip), %rax      # 0x127b8
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x8de6
movss (%rdi,%rcx,4), %xmm0
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0x8dcd
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x5080
callq 0x894b
movq 0x9983(%rip), %rsi      # 0x12780
movq 0x996c(%rip), %rdx      # 0x12770
pushq $0x3
popq %rdi
callq 0x9c2a
movq 0x995d(%rip), %rax      # 0x12770
movq 0x999e(%rip), %rcx      # 0x127b8
movq 0x995f(%rip), %r13      # 0x12780
xorl %edx, %edx
cmpq %rdx, %rax
je 0x8e40
movss (%rcx,%rdx,4), %xmm0
addss (%r13,%rdx,4), %xmm0
movss %xmm0, (%r13,%rdx,4)
incq %rdx
jmp 0x8e23
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x8e60
movss 0x1c(%r15), %xmm0
mulss (%r13,%rcx,4), %xmm0
movss %xmm0, (%r13,%rcx,4)
incq %rcx
jmp 0x8e42
movq 0x9931(%rip), %rax      # 0x12798
sarq $0x2, %rbx
subq %r13, %rax
sarq $0x2, %rax
cmpq %rax, %rbx
cmovlq %rbx, %rax
movslq %eax, %rbx
leaq (,%rbx,4), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x55b0
leaq (,%rbx,4), %rax
addq %r13, %rax
movq %rax, 0x98ef(%rip)      # 0x12790
leaq (%r14,%rbx,4), %r14
jmp 0x8c98
movq 0x98f7(%rip), %r14      # 0x127a8
movzwl 0x98f8(%rip), %esi      # 0x127b0
movq %r14, %rdi
callq 0x963f
cmpb $0x0, 0x98d9(%rip)      # 0x127a0
jne 0x8eea
movzwl 0x98e0(%rip), %eax      # 0x127b0
cvtsi2ss %eax, %xmm0
mulss 0x3174(%rip), %xmm0     # 0xc050
divss 0x43a8(%rip), %xmm0     # 0xd28c
movss %xmm0, 0x4(%rsp)
movss 0x4(%rsp), %xmm0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq