// File: fgheng[P]keysound/src/Mixer.cc
// Function: Mixer::get_mix(unsigned char*, unsigned int)
void Mixer::get_mix(uint8_t *buf, uint32_t size) {
    // 取数据，pos会移动	Fetch data, pos will move
    mtx.lock();

    uint32_t buf_pos = 0;
    // uint32_t need = size;

    while (size - buf_pos > 0) {
        if (size - buf_pos > buffer_end - pos) {
            std::memcpy(buf + buf_pos, pos, buffer_end - pos);
            // 拷贝过后的数据归0	Return the data after copying to 0
            std::memset(pos, 0, buffer_end-pos);
            buf_pos += buffer_end - pos;
            pos = buffer_start;
        } else {
            std::memcpy(buf + buf_pos, pos, size - buf_pos);
            std::memset(pos, 0, size - buf_pos);
            pos += size - buf_pos;

            // buf_pos += (size - buf_pos);
            break;
        }
    }

    // // 请求的数据块大于剩余的数据块		The requested data block is larger than the remaining data block
    // if (size > buffer_end - pos) {
        // std::cout << "---------------------" << std::endl;
        // std::cout << "pos start " << pos - buffer_start << std::endl;
        // std::cout << "size " << size << std::endl;
        // std::cout << "buffer_end - pos " << buffer_end - pos << std::endl;
        // std::memcpy(buf, pos, buffer_end - pos);
        // // 拷贝过后的数据归0			Return the data after copying to 0
        // std::memset(pos, 0, buffer_end-pos);

        // uint32_t left = size - (buffer_end - pos);
        // std::cout << "left is " << size - (buffer_end - pos) << std::endl;
        // std::memcpy(buf + (buffer_end - pos), buffer_start, left);
        // std::cout << "buf + (bufend - pos) " << (buffer_end - pos) << std::endl;
        // std::memset(buffer_start, 0, left);

        // pos = buffer_start + left;
        // std::cout << "pos end " << pos - buffer_start << std::endl;

    // } else {
        // std::memcpy(buf, pos, size);
        // std::memset(pos, 0, size);
        // pos += size;
    // }
    mtx.unlock();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x20(%rbp)
callq 0xe250
movl $0x0, -0x18(%rbp)
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
cmpl $0x0, %eax
jbe 0xe188
movq -0x20(%rbp), %rdx
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
movl %eax, %eax
movq 0x48(%rdx), %rcx
movq 0x40(%rdx), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jle 0xe130
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %ecx
addq %rcx, %rdi
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x40(%rax), %rax
subq %rax, %rdx
callq 0x42e0
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
movq 0x48(%rax), %rdx
movq 0x40(%rax), %rax
subq %rax, %rdx
xorl %esi, %esi
callq 0x41d0
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
movq 0x40(%rax), %rcx
subq %rcx, %rdx
movl -0x18(%rbp), %ecx
addq %rdx, %rcx
movl %ecx, -0x18(%rbp)
movq 0x38(%rax), %rcx
movq %rcx, 0x40(%rax)
jmp 0xe183
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %ecx
addq %rcx, %rdi
movq 0x40(%rax), %rsi
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x42e0
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x14(%rbp), %eax
subl -0x18(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x41d0
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %edx
subl -0x18(%rbp), %edx
movq 0x40(%rax), %rcx
movl %edx, %edx
addq %rdx, %rcx
movq %rcx, 0x40(%rax)
jmp 0xe188
jmp 0xe0a7
movq -0x20(%rbp), %rdi
callq 0xe2a0
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)