// File: fgheng[P]keysound/src/Mixer.cc
// Function: Mixer::mix16(unsigned char*, unsigned int)
void Mixer::mix16(uint8_t *buf, uint32_t size) {

    // 传入的buffer比较大， 超过了剩余的内存		The incoming buffer is relatively large and exceeds the remaining memory
    if (buffer_end - pos < size) {
        mtx.lock();
        for (int i = 0; i < (buffer_end-pos) / 2; i++) {
            int16_t A = *((int16_t *)pos + i);
            int16_t B = *((int16_t *)buf + i);
            *((int16_t *)pos + i) = mix_int16(A, B);
        }

        for (int i = 0; i < (size-(buffer_end-pos)) / 2; i++) {
            int16_t A = *((int16_t *)buffer_start + i);
            int16_t B = *((int16_t *)(buf + (buffer_end - pos)) + i);

            *((int16_t *)buffer_start + i) = mix_int16(A, B);
        }

        mtx.unlock();
    } else {
        mtx.lock();
        for (int i = 0; i < size / 2; i++) {
            int16_t A = *((int16_t *)pos + i);
            int16_t B = *((int16_t *)buf + i);
            *((int16_t *)pos + i) = mix_int16(A, B);
        }

        mtx.unlock();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movq 0x48(%rcx), %rax
movq 0x40(%rcx), %rcx
subq %rcx, %rax
movl -0x14(%rbp), %ecx
cmpq %rcx, %rax
jge 0xdd19
movq -0x38(%rbp), %rdi
callq 0xe250
movl $0x0, -0x18(%rbp)
movq -0x38(%rbp), %rcx
movslq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x48(%rcx), %rax
movq 0x40(%rcx), %rcx
subq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jge 0xdc71
movq -0x38(%rbp), %rdi
movq 0x40(%rdi), %rax
movslq -0x18(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x1c(%rbp)
movw -0x1a(%rbp), %ax
movswl %ax, %esi
movswl -0x1c(%rbp), %edx
callq 0xe2c0
movw %ax, %dx
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xdbf2
movl $0x0, -0x20(%rbp)
movq -0x38(%rbp), %rdx
movslq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movq 0x48(%rdx), %rcx
movq 0x40(%rdx), %rdx
subq %rdx, %rcx
subq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jge 0xdd0e
movq -0x38(%rbp), %rdi
movq 0x38(%rdi), %rax
movslq -0x20(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rdi), %rcx
movq 0x40(%rdi), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movslq -0x20(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x24(%rbp)
movw -0x22(%rbp), %ax
movswl %ax, %esi
movswl -0x24(%rbp), %edx
callq 0xe2c0
movw %ax, %dx
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xdc78
movq -0x38(%rbp), %rdi
callq 0xe2a0
jmp 0xdd90
movq -0x38(%rbp), %rdi
callq 0xe250
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x14(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jae 0xdd87
movq -0x38(%rbp), %rdi
movq 0x40(%rdi), %rax
movslq -0x28(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x2a(%rbp)
movq -0x10(%rbp), %rax
movslq -0x28(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x2c(%rbp)
movw -0x2a(%rbp), %ax
movswl %ax, %esi
movswl -0x2c(%rbp), %edx
callq 0xe2c0
movw %ax, %dx
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x28(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xdd29
movq -0x38(%rbp), %rdi
callq 0xe2a0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)