// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r8
movq %rdi, (%rsp)
cmpl $0x0, 0x1a314(%rip)     # 0x26514
je 0xc4fb
testl %esi, %esi
je 0xc74a
movq (%rsp), %rax
movl 0xb71c4(%rax), %eax
movl 0x1a2ee(%rip), %ecx     # 0x2650c
orl 0x1a2ec(%rip), %ecx     # 0x26510
movl 0x1a506(%rip), %edx     # 0x26730
shrl $0x9, %edx
orl %ecx, %edx
leaq 0x1a3ea(%rip), %rcx     # 0x26620
andl (%rcx,%rdx,4), %eax
imull 0x1a2a0(%rip), %eax     # 0x264e0
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
shrq $0x26, %rax
movl %esi, %edx
leaq 0x7(%rdx), %rcx
andq $-0x8, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
xorl %edx, %edx
movdqa 0x4644(%rip), %xmm1     # 0x108b0
movdqa 0x464c(%rip), %xmm2     # 0x108c0
movdqa 0x4654(%rip), %xmm3     # 0x108d0
movdqa 0x465c(%rip), %xmm4     # 0x108e0
movdqa 0x4664(%rip), %xmm5     # 0x108f0
pxor %xmm5, %xmm0
pcmpeqd %xmm6, %xmm6
movq %rdx, %xmm7
pshufd $0x44, %xmm7, %xmm7     # xmm7 = xmm7[0,1,0,1]
movdqa %xmm7, %xmm8
por %xmm4, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm9    # xmm9 = xmm10[0,0,2,2]
pshuflw $0xe8, %xmm9, %xmm11    # xmm11 = xmm9[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
pshuflw $0xe8, %xmm8, %xmm12    # xmm12 = xmm8[0,2,2,3,4,5,6,7]
pand %xmm11, %xmm12
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
pshuflw $0xe8, %xmm10, %xmm11   # xmm11 = xmm10[0,2,2,3,4,5,6,7]
por %xmm12, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
movd %xmm11, %esi
testb $0x1, %sil
je 0xc304
movw %ax, (%r8,%rdx,2)
pand %xmm9, %xmm8
por %xmm10, %xmm8
packssdw %xmm8, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
movd %xmm8, %esi
shrl $0x10, %esi
testb $0x1, %sil
je 0xc331
movw %ax, 0x2(%r8,%rdx,2)
movdqa %xmm7, %xmm9
por %xmm3, %xmm9
pxor %xmm5, %xmm9
movdqa %xmm9, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm8    # xmm8 = xmm10[0,0,2,2]
pcmpeqd %xmm0, %xmm9
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
movdqa %xmm9, %xmm11
pand %xmm8, %xmm11
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
por %xmm10, %xmm11
packssdw %xmm11, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
pextrw $0x2, %xmm11, %esi
testb $0x1, %sil
je 0xc391
movw %ax, 0x4(%r8,%rdx,2)
pshufhw $0x84, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm9, %xmm9     # xmm9 = xmm9[0,1,2,3,4,5,4,6]
pand %xmm8, %xmm9
pshufhw $0x84, %xmm10, %xmm8    # xmm8 = xmm10[0,1,2,3,4,5,4,6]
por %xmm9, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
pextrw $0x3, %xmm8, %esi
testb $0x1, %sil
je 0xc3c9
movw %ax, 0x6(%r8,%rdx,2)
movdqa %xmm7, %xmm8
por %xmm2, %xmm8
pxor %xmm5, %xmm8
movdqa %xmm8, %xmm10
pcmpgtd %xmm0, %xmm10
pshufd $0xa0, %xmm10, %xmm9    # xmm9 = xmm10[0,0,2,2]
pshuflw $0xe8, %xmm9, %xmm11    # xmm11 = xmm9[0,2,2,3,4,5,6,7]
pcmpeqd %xmm0, %xmm8
pshufd $0xf5, %xmm8, %xmm8     # xmm8 = xmm8[1,1,3,3]
pshuflw $0xe8, %xmm8, %xmm12    # xmm12 = xmm8[0,2,2,3,4,5,6,7]
pand %xmm11, %xmm12
pshufd $0xf5, %xmm10, %xmm10   # xmm10 = xmm10[1,1,3,3]
pshuflw $0xe8, %xmm10, %xmm11   # xmm11 = xmm10[0,2,2,3,4,5,6,7]
por %xmm12, %xmm11
pxor %xmm6, %xmm11
packssdw %xmm11, %xmm11
pextrw $0x4, %xmm11, %esi
testb $0x1, %sil
je 0xc431
movw %ax, 0x8(%r8,%rdx,2)
pand %xmm9, %xmm8
por %xmm10, %xmm8
packssdw %xmm8, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm8
pextrw $0x5, %xmm8, %esi
testb $0x1, %sil
je 0xc45c
movw %ax, 0xa(%r8,%rdx,2)
por %xmm1, %xmm7
pxor %xmm5, %xmm7
movdqa %xmm7, %xmm9
pcmpgtd %xmm0, %xmm9
pshufd $0xa0, %xmm9, %xmm8     # xmm8 = xmm9[0,0,2,2]
pcmpeqd %xmm0, %xmm7
pshufd $0xf5, %xmm7, %xmm7     # xmm7 = xmm7[1,1,3,3]
movdqa %xmm7, %xmm10
pand %xmm8, %xmm10
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
por %xmm9, %xmm10
packssdw %xmm10, %xmm10
pxor %xmm6, %xmm10
packssdw %xmm10, %xmm10
pextrw $0x6, %xmm10, %esi
testb $0x1, %sil
je 0xc4b3
movw %ax, 0xc(%r8,%rdx,2)
pshufhw $0x84, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm7, %xmm7     # xmm7 = xmm7[0,1,2,3,4,5,4,6]
pand %xmm8, %xmm7
pshufhw $0x84, %xmm9, %xmm8     # xmm8 = xmm9[0,1,2,3,4,5,4,6]
por %xmm7, %xmm8
pxor %xmm6, %xmm8
packssdw %xmm8, %xmm7
pextrw $0x7, %xmm7, %esi
testb $0x1, %sil
je 0xc4e9
movw %ax, 0xe(%r8,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rcx
jne 0xc294
jmp 0xc74a
testl %esi, %esi
je 0xc74a
movl %esi, %eax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
movq %r8, 0x8(%rsp)
cmpl $0x0, 0x19fcb(%rip)     # 0x264e4
je 0xc527
movl $0x1, 0x19fc7(%rip)     # 0x264ec
jmp 0xc55b
movzbl 0x1a1f2(%rip), %eax     # 0x26720
addl 0x19fc4(%rip), %eax     # 0x264f8
movl %eax, 0x19fbe(%rip)     # 0x264f8
cmpl $0x400, %eax            # imm = 0x400
jl 0xc55b
xorb $0x1, 0x19fa4(%rip)     # 0x264ec
movl 0x1a1da(%rip), %ecx     # 0x26728
addl %ecx, %eax
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
movl %eax, 0x19f9d(%rip)     # 0x264f8
cmpl $0x0, 0x19f86(%rip)     # 0x264e8
je 0xc578
movl $0x1, 0x19f82(%rip)     # 0x264f0
movl $0x1, %eax
jmp 0xc60c
cmpb $0x1, 0x1a1c1(%rip)     # 0x26740
jne 0xc5b8
movl 0x19f79(%rip), %eax     # 0x26500
movl %eax, %ecx
incl %ecx
movslq %ecx, %rcx
imulq $-0x7f7f7f7f, %rcx, %rcx # imm = 0x80808081
shrq $0x20, %rcx
addl %eax, %ecx
incl %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x7, %ecx
addl %edx, %ecx
movl %ecx, %edx
shll $0x8, %edx
subl %edx, %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x19f48(%rip)     # 0x26500
movzbl 0x1a161(%rip), %ecx     # 0x26720
addl 0x19f37(%rip), %ecx     # 0x264fc
movl %ecx, 0x19f31(%rip)     # 0x264fc
cmpl $0x400, %ecx            # imm = 0x400
jl 0xc5ff
movb $0x1, 0x1a166(%rip)     # 0x26740
movl 0x19f10(%rip), %eax     # 0x264f0
xorl $0x1, %eax
movl %eax, 0x19f07(%rip)     # 0x264f0
movl 0x1a13d(%rip), %edx     # 0x2672c
addl %edx, %ecx
addl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
movl %ecx, 0x19eff(%rip)     # 0x264fc
jmp 0xc60c
movb $0x0, 0x1a13a(%rip)     # 0x26740
movl 0x19ee4(%rip), %eax     # 0x264f0
cmpl $0x0, 0x19efd(%rip)     # 0x26510
setne %bpl
cmpl $0x0, 0x19ece(%rip)     # 0x264ec
setne %cl
cmpl $0x0, 0x19ee4(%rip)     # 0x2650c
setne %r12b
testl %eax, %eax
setne %sil
xorl %r15d, %r15d
cmpl $0x0, 0x19ecc(%rip)     # 0x26508
je 0xc655
movslq 0x19ebb(%rip), %rax     # 0x26500
leaq 0x19ed4(%rip), %rdx     # 0x26520
cmpb $0x0, (%rax,%rdx)
setne %al
jmp 0xc657
xorl %eax, %eax
andb %cl, %bpl
je 0xc686
movq (%rsp), %rbx
movl 0xb61b0(%rbx), %edx
movq %rbx, %rdi
movl %esi, %r15d
xorl %esi, %esi
movq %rax, %r13
callq 0xc87c
movl %r15d, %esi
movl %eax, %r15d
movq %r13, %rax
andl 0xb71bc(%rbx), %r15d
andb %sil, %r12b
je 0xc6b0
movq (%rsp), %rbx
movl 0xb61b4(%rbx), %edx
movq %rbx, %rdi
movl $0x1, %esi
callq 0xc87c
andl 0xb71c0(%rbx), %eax
movl 0x1a082(%rip), %ecx     # 0x26730
jmp 0xc6dc
testb %al, %al
je 0xc71f
movl 0x1a076(%rip), %ecx     # 0x26730
movl %ecx, %edx
shrl $0x7, %edx
orl $0x80, %edx
andl $-0x4, %edx
movq (%rsp), %rax
movl 0xb71c4(%rax), %eax
leaq 0x19f47(%rip), %rsi     # 0x26620
andl (%rdx,%rsi), %eax
movq 0x8(%rsp), %r8
addl %eax, %r15d
testl %ecx, %ecx
setne %al
andb %al, %bpl
cmpb $0x1, %bpl
jne 0xc724
shrl $0x7, %ecx
andl $-0x4, %ecx
movl %ecx, %eax
orl $0xc0, %eax
leaq 0x19f1a(%rip), %rdx     # 0x26620
imull (%rax,%rdx), %r15d
orl $0x40, %ecx
movl (%rcx,%rdx), %ecx
addl %ecx, %ecx
movl %r15d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
jmp 0xc724
movq 0x8(%rsp), %r8
imull 0x19db4(%rip), %r15d    # 0x264e0
imulq $0x10624dd3, %r15, %rax # imm = 0x10624DD3
shrq $0x26, %rax
movw %ax, (%r8,%r14,2)
incq %r14
cmpl %r14d, 0x10(%rsp)
jne 0xc512
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop