// File: /workspace/github_c_makefile_star2_O0/calmopyrin[P]tedplay/Tedsound.cpp
// Function: TED::renderSound(unsigned int, short*)
void TED::renderSound(unsigned int nrsamples, short *buffer)
{
	// Calculate the buffer...
	if (DAStatus) {// digi?
		int sample = (volumeTable[Snd1Status | Snd2Status | (Volume >> 9)] & channelMask[2]) * masterVolume / 1000;
		for (;nrsamples--;) {
			*buffer++ = short(sample);
		}
	} else {
		unsigned int result;
		for (;nrsamples--;) {
			// Channel 1
			if (dcOutput[0]) {
				FlipFlop[0] = 1;
			} else if ((oscCount[0] += oscStep) >= OSCRELOADVAL) {
				FlipFlop[0] ^= 1;
				oscCount[0] = OscReload[0] + (oscCount[0] - OSCRELOADVAL);
			}
			// Channel 2
			static bool channelOverFlow = false;
			if (dcOutput[1]) {
				FlipFlop[1] = 1;
			}
			else {
				if (channelOverFlow)
					NoiseCounter = (NoiseCounter + 1) % 255;
				if ((oscCount[1] += oscStep) >= OSCRELOADVAL) {
					channelOverFlow = true;
					FlipFlop[1] ^= 1;
					oscCount[1] = OscReload[1] + (oscCount[1] - OSCRELOADVAL);
				}
				else
					channelOverFlow = false;
			}
			int s1on = (Snd1Status && FlipFlop[0]);
			int s2on = (Snd2Status && FlipFlop[1]);
			int non = SndNoiseStatus && noise[NoiseCounter];
			result = s1on ? (getWaveSample(0, waveForm[0]) & channelMask[0]) : 0;
			if (s2on) {
				result += getWaveSample(1, waveForm[1]) & channelMask[1];
			} else if (non) {
				result += volumeTable[0x20 | (Volume >> 9)] & channelMask[2];
			}
			if ((s2on || non) && s1on && Volume) {
				unsigned int vol = Volume >> 9;
				result = (result * volumeTable[0x30 | vol]) / (volumeTable[0x10 | vol] * 2);
			}

			*buffer++ = result * masterVolume / 1000;
		}   // for
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, 0x1ba8e(%rip)     # 0x359b0
je 0x19f94
movq -0x38(%rbp), %rcx
movl 0x1ba7e(%rip), %eax     # 0x359ac
orl 0x1ba74(%rip), %eax     # 0x359a8
movl 0x1bc92(%rip), %edx     # 0x35bcc
sarl $0x9, %edx
orl %edx, %eax
movslq %eax, %rdx
leaq 0x1bb77(%rip), %rax     # 0x35ac0
movl (%rax,%rdx,4), %eax
andl 0xb71c4(%rcx), %eax
imull 0x1ba27(%rip), %eax     # 0x35980
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
movl %eax, -0x1c(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0xc(%rbp)
cmpl $0x0, %eax
je 0x19f8f
movl -0x1c(%rbp), %eax
movw %ax, %cx
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x18(%rbp)
movw %cx, (%rax)
jmp 0x19f65
jmp 0x1a224
jmp 0x19f96
movl -0xc(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0xc(%rbp)
cmpl $0x0, %eax
je 0x1a222
cmpl $0x0, 0x1b9d3(%rip)     # 0x35984
je 0x19fbf
movl $0x1, 0x1b9cf(%rip)     # 0x3598c
jmp 0x1a003
movl 0x1bbfb(%rip), %eax     # 0x35bc0
addl 0x1b9c9(%rip), %eax     # 0x35994
movl %eax, 0x1b9c3(%rip)     # 0x35994
cmpl $0x400, %eax            # imm = 0x400
jl 0x1a001
movl 0x1b9ae(%rip), %eax     # 0x3598c
xorl $0x1, %eax
movl %eax, 0x1b9a5(%rip)     # 0x3598c
movl 0x1bbd7(%rip), %eax     # 0x35bc4
movl 0x1b9a1(%rip), %ecx     # 0x35994
subl $0x400, %ecx            # imm = 0x400
addl %ecx, %eax
movl %eax, 0x1b993(%rip)     # 0x35994
jmp 0x1a003
cmpl $0x0, 0x1b97e(%rip)     # 0x35988
je 0x1a018
movl $0x1, 0x1b97a(%rip)     # 0x35990
jmp 0x1a08c
testb $0x1, 0x1bbc1(%rip)     # 0x35be0
je 0x1a038
movl 0x1b975(%rip), %eax     # 0x3599c
addl $0x1, %eax
movl $0xff, %ecx
cltd
idivl %ecx
movl %edx, 0x1b964(%rip)     # 0x3599c
movl 0x1bb82(%rip), %eax     # 0x35bc0
addl 0x1b954(%rip), %eax     # 0x35998
movl %eax, 0x1b94e(%rip)     # 0x35998
cmpl $0x400, %eax            # imm = 0x400
jl 0x1a083
movb $0x1, 0x1bb88(%rip)     # 0x35be0
movl 0x1b932(%rip), %eax     # 0x35990
xorl $0x1, %eax
movl %eax, 0x1b929(%rip)     # 0x35990
movl 0x1bb5b(%rip), %eax     # 0x35bc8
movl 0x1b925(%rip), %ecx     # 0x35998
subl $0x400, %ecx            # imm = 0x400
addl %ecx, %eax
movl %eax, 0x1b917(%rip)     # 0x35998
jmp 0x1a08a
movb $0x0, 0x1bb56(%rip)     # 0x35be0
jmp 0x1a08c
xorl %eax, %eax
cmpl $0x0, 0x1b917(%rip)     # 0x359ac
movb %al, -0x39(%rbp)
je 0x1a0a7
cmpl $0x0, 0x1b8eb(%rip)     # 0x3598c
setne %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x24(%rbp)
xorl %eax, %eax
cmpl $0x0, 0x1b8ed(%rip)     # 0x359a8
movb %al, -0x3a(%rbp)
je 0x1a0cd
cmpl $0x0, 0x1b8c9(%rip)     # 0x35990
setne %al
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x28(%rbp)
xorl %eax, %eax
cmpl $0x0, 0x1b8c3(%rip)     # 0x359a4
movb %al, -0x3b(%rbp)
je 0x1a0fe
movslq 0x1b8af(%rip), %rcx     # 0x3599c
leaq 0x1b8cc(%rip), %rax     # 0x359c0
cmpb $0x0, (%rax,%rcx)
setne %al
movb %al, -0x3b(%rbp)
movb -0x3b(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x1a12f
movq -0x38(%rbp), %rdi
movl 0xb61b0(%rdi), %edx
xorl %esi, %esi
callq 0x1a4e0
movq -0x38(%rbp), %rcx
andl 0xb71bc(%rcx), %eax
movl %eax, -0x40(%rbp)
jmp 0x1a136
xorl %eax, %eax
movl %eax, -0x40(%rbp)
jmp 0x1a136
movl -0x40(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x1a168
movq -0x38(%rbp), %rdi
movl 0xb61b4(%rdi), %edx
movl $0x1, %esi
callq 0x1a4e0
movq -0x38(%rbp), %rcx
andl 0xb71c0(%rcx), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x1a199
cmpl $0x0, -0x2c(%rbp)
je 0x1a197
movq -0x38(%rbp), %rcx
movl 0x1ba54(%rip), %eax     # 0x35bcc
sarl $0x9, %eax
orl $0x20, %eax
movslq %eax, %rdx
leaq 0x1b938(%rip), %rax     # 0x35ac0
movl (%rax,%rdx,4), %eax
andl 0xb71c4(%rcx), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x1a199
cmpl $0x0, -0x28(%rbp)
jne 0x1a1a5
cmpl $0x0, -0x2c(%rbp)
je 0x1a1f5
cmpl $0x0, -0x24(%rbp)
je 0x1a1f5
cmpl $0x0, 0x1ba1a(%rip)     # 0x35bcc
je 0x1a1f5
movl 0x1ba12(%rip), %eax     # 0x35bcc
sarl $0x9, %eax
movl %eax, -0x30(%rbp)
movl -0x20(%rbp), %eax
movl -0x30(%rbp), %ecx
orl $0x30, %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x1b8ec(%rip), %rcx     # 0x35ac0
imull (%rcx,%rdx,4), %eax
movl -0x30(%rbp), %ecx
orl $0x10, %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x1b8d7(%rip), %rcx     # 0x35ac0
movl (%rcx,%rdx,4), %ecx
shll %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
imull 0x1b781(%rip), %eax     # 0x35980
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
movw %ax, %cx
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x18(%rbp)
movw %cx, (%rax)
jmp 0x19f96
jmp 0x1a224
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)