// File: /workspace/github_c_makefile_star2_O0/xiane[P]libvphevcodec/vdi.cpp
// Function: allocate_common_memory(unsigned int)
s32 allocate_common_memory(u32 core_idx) {
    vdi_info_t *vdi;
    vpu_buffer_t vdb;
    s32 i;

    if (core_idx >= MAX_NUM_VPU_CORE)
        return -1;

    vdi = &s_vdi_info[core_idx];
    if (!vdi || vdi->vpu_fd < 0 || vdi->opened == 0)
        return -1;

    memset(&vdb, 0x00, sizeof(vpu_buffer_t));

    vdb.size = SIZE_COMMON * MAX_NUM_VPU_CORE;
    vdb.cached = 0;
    if (ioctl(vdi->vpu_fd, VDI_IOCTL_GET_COMMON_MEMORY, &vdb) < 0) {
        VLOG(ERR, "[VDI] fail to vdi_allocate_dma_memory size=0x%x", vdb.size);
        return -1;
    }

    vdb.virt_addr = (ulong) mmap(NULL,
                                 vdb.size,
                                 PROT_READ | PROT_WRITE,
                                 MAP_SHARED,
                                 vdi->vpu_fd,
                                 vdb.phys_addr);
    if ((void *) vdb.virt_addr == MAP_FAILED) {
        VLOG(ERR, "[VDI] fail to map common memory phyaddr=0x%lx, size = 0x%x", vdb.phys_addr, vdb.size);
        return -1;
    }

    VLOG(DEBUG, "[VDI] allocate_common_memory, physaddr=0x%lx, virtaddr=0x%lx, size=%d", vdb.phys_addr, vdb.virt_addr, vdb.size);

    // convert os driver buffer type to vpu buffer type
    vdi->pvip->vpu_common_buffer.size = SIZE_COMMON;
    vdi->pvip->vpu_common_buffer.cached = vdb.cached;
    vdi->pvip->vpu_common_buffer.phys_addr = (ulong) vdb.phys_addr;
    vdi->pvip->vpu_common_buffer.base = (ulong) vdb.base;
    vdi->pvip->vpu_common_buffer.virt_addr = (ulong) vdb.virt_addr;
    memcpy(&vdi->vpu_common_memory, &vdi->pvip->vpu_common_buffer, sizeof(vpu_buffer_t));

    for (i = 0; i < MAX_VPU_BUFFER_POOL; i++) {
        if (vdi->vpu_buffer_pool[i].inuse == 0) {
            vdi->vpu_buffer_pool[i].vdb = vdb;
            vdi->vpu_buffer_pool_count++;
            vdi->vpu_buffer_pool[i].inuse = 1;
            VLOG(DEBUG, "[VDI] allocate_common_memory->update pool %d, physaddr=0x%lx, virtaddr=0x%lx ~ 0x%lx, size=0x%x",
                i,vdi->vpu_common_memory.phys_addr,  vdi->vpu_common_memory.virt_addr, vdi->vpu_common_memory.virt_addr + vdi->vpu_common_memory.size,vdi->vpu_common_memory.size);

            break;
        }
    }

    VLOG(INFO, "[VDI] allocate_common_memory physaddr=0x%lx, virtaddr=0x%lx, size=0x%x",
                 vdi->vpu_common_memory.phys_addr,  vdi->vpu_common_memory.virt_addr, vdi->vpu_common_memory.size);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x1, -0x8(%rbp)
jb 0x99ed
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c7b
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x29817(%rip), %rax     # 0x33210
imulq $0x2888, %rcx, %rcx     # imm = 0x2888
addq %rcx, %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x9a25
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jl 0x9a25
movq -0x10(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x9a31
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c7b
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20f0
movl $0x80000, -0x30(%rbp)   # imm = 0x80000
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
movl $0x40205606, %esi       # imm = 0x40205606
leaq -0x30(%rbp), %rdx
movb $0x0, %al
callq 0x2230
cmpl $0x0, %eax
jge 0x9a98
jmp 0x9a6d
movl -0x30(%rbp), %esi
leaq 0x26b9a(%rip), %rdi     # 0x30611
movb $0x0, %al
callq 0x2050
leaq 0x267e1(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c7b
movl -0x30(%rbp), %eax
movl %eax, %esi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r8d
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x2320
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x9b01
jmp 0x9ad2
movq -0x28(%rbp), %rsi
movl -0x30(%rbp), %edx
leaq 0x26cd8(%rip), %rdi     # 0x307b8
movb $0x0, %al
callq 0x2050
leaq 0x26778(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9c7b
jmp 0x9b03
jmp 0x9b05
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl $0x80000, 0x20000(%rax) # imm = 0x80000
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl %ecx, 0x20004(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20008(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20010(%rax)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x20018(%rax)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x20000(%rcx), %rdx
movq %rdx, 0x60(%rax)
movq 0x20008(%rcx), %rdx
movq %rdx, 0x68(%rax)
movq 0x20010(%rcx), %rdx
movq %rdx, 0x70(%rax)
movq 0x20018(%rcx), %rcx
movq %rcx, 0x78(%rax)
movl $0x0, -0x34(%rbp)
cmpl $0x100, -0x34(%rbp)     # imm = 0x100
jge 0x9c3f
movq -0x10(%rbp), %rax
addq $0x80, %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x20(%rax)
jne 0x9c2f
movq -0x10(%rbp), %rax
addq $0x80, %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl 0x2880(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2880(%rax)
movq -0x10(%rbp), %rax
addq $0x80, %rax
movslq -0x34(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x1, 0x20(%rax)
jmp 0x9c2d
jmp 0x9c3f
jmp 0x9c31
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9ba0
jmp 0x9c41
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdx
movq -0x10(%rbp), %rax
movl 0x60(%rax), %ecx
leaq 0x26b94(%rip), %rdi     # 0x307f3
movb $0x0, %al
callq 0x2050
leaq 0x265f9(%rip), %rdi     # 0x30266
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)