// File: /workspace/github_c_makefile_star2_O3/baines[P]MiniGBS/audio.c
// Function: update_square
void update_square(bool ch2){
	struct chan* c = chans + ch2;
	if(!c->powered) return;

	set_note_freq(c, 4194304.0f / (float)((2048 - c->freq) << 5));
	c->freq_inc *= 8.0f;

	for(int i = 0; i < nsamples; i+=2){
		update_len(c);

		if(c->enabled){
			update_env(c);
			if(!ch2) update_sweep(c);

			float pos = 0.0f;
			float prev_pos = 0.0f;
			float sample = 0.0f;

			while(update_freq(c, &pos)){
				c->duty_counter = (c->duty_counter + 1) & 7;
				sample += ((pos - prev_pos) / c->freq_inc) * (float)c->val;
				c->val = (c->duty & (1 << c->duty_counter)) ? 1 : -1;
				prev_pos = pos;
			}
			sample += ((pos - prev_pos) / c->freq_inc) * (float)c->val;
			sample = hipass(c, sample * (c->volume / 15.0f));

			if(!muted[c-chans]){
				samples[i+0] += sample * 0.25f * c->on_left * vol_l;
				samples[i+1] += sample * 0.25f * c->on_right * vol_r;
			}
		}
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edi, %r15d
imulq $0x70, %r15, %rax
leaq 0x8e25(%rip), %r14      # 0x115b0
cmpb $0x1, 0x1(%rax,%r14)
jne 0x899c
movl %edi, %ebx
addq %rax, %r14
movzwl 0xc(%r14), %eax
shll $0x5, %eax
movl $0x10000, %ecx          # imm = 0x10000
subl %eax, %ecx
cvtsi2ss %ecx, %xmm1
movss 0x4ae1(%rip), %xmm0     # 0xd298
divss %xmm1, %xmm0
movaps %xmm0, %xmm1
divss 0x4ac6(%rip), %xmm1     # 0xd28c
movss %xmm1, 0x14(%r14)
divss 0x4abc(%rip), %xmm0     # 0xd290
callq 0x51f0
divss 0x8dc3(%rip), %xmm0     # 0x115a4
callq 0x50d0
cvttss2si %xmm0, %eax
cmpl $-0x2f, %eax
movl $0xffffffd0, %ecx       # imm = 0xFFFFFFD0
cmovgel %eax, %ecx
addl $0x30, %ecx
movl %ecx, 0x1c(%r14)
movss 0x14(%r14), %xmm0
mulss 0x4a92(%rip), %xmm0     # 0xd29c
movss %xmm0, 0x14(%r14)
cmpq $0x0, 0x8f58(%rip)      # 0x11770
je 0x899c
xorl %r12d, %r12d
leaq 0x8d80(%rip), %r13      # 0x115a8
movq %r14, %rdi
callq 0x8587
cmpb $0x1, (%r14)
jne 0x898b
movq %r14, %rdi
callq 0x851d
testb %bl, %bl
jne 0x884e
movq %r14, %rdi
callq 0x865d
movss 0x14(%r14), %xmm0
movss 0x10(%r14), %xmm3
addss %xmm0, %xmm3
movss 0x37f6(%rip), %xmm5     # 0xc05c
ucomiss %xmm5, %xmm3
movl 0x18(%r14), %eax
xorps %xmm2, %xmm2
jbe 0x88dd
movl 0x5c(%r14), %edx
movl 0x60(%r14), %ecx
xorps %xmm1, %xmm1
movss 0x4a0f(%rip), %xmm6     # 0xd294
xorps %xmm7, %xmm7
movaps %xmm2, %xmm4
addss %xmm6, %xmm3
movaps %xmm0, %xmm2
subss %xmm3, %xmm2
incl %ecx
andl $0x7, %ecx
movaps %xmm2, %xmm3
subss %xmm4, %xmm3
divss %xmm0, %xmm3
xorps %xmm4, %xmm4
cvtsi2ss %eax, %xmm4
mulss %xmm3, %xmm4
addss %xmm4, %xmm1
btl %ecx, %edx
movl $0x0, %eax
adcl $-0x1, %eax
orl $0x1, %eax
movaps %xmm0, %xmm3
subss %xmm2, %xmm3
ucomiss %xmm5, %xmm3
addss %xmm7, %xmm3
ja 0x8888
movl %ecx, 0x60(%r14)
movl %eax, 0x18(%r14)
jmp 0x88e0
xorps %xmm1, %xmm1
movss %xmm3, 0x10(%r14)
movaps %xmm0, %xmm3
subss %xmm2, %xmm3
divss %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm3, %xmm0
xorps %xmm2, %xmm2
cvtsi2ssl 0x4(%r14), %xmm2
addss %xmm1, %xmm0
divss 0x498f(%rip), %xmm2     # 0xd2a0
mulss %xmm0, %xmm2
movaps %xmm2, %xmm0
subss 0x58(%r14), %xmm0
movss 0x8c7a(%rip), %xmm1     # 0x115a0
mulss %xmm0, %xmm1
subss %xmm1, %xmm2
movss %xmm2, 0x58(%r14)
cmpb $0x0, (%r15,%r13)
jne 0x898b
mulss 0x4961(%rip), %xmm0     # 0xd2a4
movq 0x8e36(%rip), %rax      # 0x11780
movzwl 0x2(%r14), %ecx
movd %ecx, %xmm1
xorps %xmm2, %xmm2
punpcklbw %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3],xmm1[4],xmm2[4],xmm1[5],xmm2[5],xmm1[6],xmm2[6],xmm1[7],xmm2[7]
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
cvtdq2ps %xmm1, %xmm1
unpcklps %xmm0, %xmm0            # xmm0 = xmm0[0,0,1,1]
mulps %xmm1, %xmm0
movsd (%rax,%r12,4), %xmm1
movss 0x8e13(%rip), %xmm2     # 0x11788
movss 0x8dfb(%rip), %xmm3     # 0x11778
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
mulps %xmm0, %xmm3
addps %xmm1, %xmm3
movlps %xmm3, (%rax,%r12,4)
addq $0x2, %r12
cmpq %r12, 0x8dda(%rip)      # 0x11770
ja 0x8828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq