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

    if (core_idx >= MAX_NUM_VPU_CORE)
        return 0;

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

    if (vdi->task_num > 1) {
        // means that the opened instance remains
        vdi->task_num--;
        return 0;
    }

    if (vdi->vdb_register.virt_addr)
        munmap((void *) vdi->vdb_register.virt_addr, vdi->vdb_register.size);

    memset(&vdi->vdb_register, 0x00, sizeof(vpu_buffer_t));
    vdb.size = 0;
    // get common memory information to free virtual address
    for (i = 0; i < MAX_VPU_BUFFER_POOL; i++) {
        if (vdi->vpu_common_memory.phys_addr >= vdi->vpu_buffer_pool[i].vdb.phys_addr
           && vdi->vpu_common_memory.phys_addr < (vdi->vpu_buffer_pool[i].vdb.phys_addr + vdi->vpu_buffer_pool[i].vdb.size)) {
            vdi->vpu_buffer_pool[i].inuse = 0;
            vdi->vpu_buffer_pool_count--;
            vdb = vdi->vpu_buffer_pool[i].vdb;
            break;
        }
    }

    if (vdb.size > 0) {
        munmap((void *) vdb.virt_addr, vdb.size);
        memset(&vdi->vpu_common_memory, 0x00, sizeof(vpu_buffer_t));
    }

    if (vdi->instance_pool.virt_addr)
        munmap((void *) vdi->instance_pool.virt_addr, vdi->instance_pool.size);

    vdi->task_num--;

    if (vdi->vpu_fd >= 0) {
        close(vdi->vpu_fd);
        vdi->vpu_fd = -1;
    }
    memset(vdi, 0x00, sizeof(vdi_info_t));
    vdi->vpu_fd = -1;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x1, -0x8(%rbp)
jb 0x9cad
movl $0x0, -0x4(%rbp)
jmp 0x9edc
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x29557(%rip), %rax     # 0x33210
imulq $0x2888, %rcx, %rcx     # imm = 0x2888
addq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x9ce5
movq -0x38(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jl 0x9ce5
movq -0x38(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
jne 0x9cf1
movl $0x0, -0x4(%rbp)
jmp 0x9edc
movq -0x38(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jbe 0x9d14
movq -0x38(%rbp), %rax
movl 0x18(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x18(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x9edc
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x9d35
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x38(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, %esi
callq 0x2100
movq -0x38(%rbp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20f0
movl $0x0, -0x30(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $0x100, -0xc(%rbp)      # imm = 0x100
jge 0x9e43
movq -0x38(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x38(%rbp), %rcx
addq $0x80, %rcx
movslq -0xc(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jb 0x9e33
movq -0x38(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x38(%rbp), %rcx
addq $0x80, %rcx
movslq -0xc(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
movq -0x38(%rbp), %rdx
addq $0x80, %rdx
movslq -0xc(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movl (%rdx), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x9e33
movq -0x38(%rbp), %rax
addq $0x80, %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x20(%rax)
movq -0x38(%rbp), %rax
movl 0x2880(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x2880(%rax)
movq -0x38(%rbp), %rax
addq $0x80, %rax
movslq -0xc(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x28(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x9e43
jmp 0x9e35
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x9d57
cmpl $0x0, -0x30(%rbp)
jbe 0x9e6b
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movl %eax, %esi
callq 0x2100
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x20f0
movq -0x38(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x9e8c
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x38(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, %esi
callq 0x2100
movq -0x38(%rbp), %rax
movl 0x18(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jl 0x9eba
movq -0x38(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x2370
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x2888, %edx           # imm = 0x2888
callq 0x20f0
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)