// File: /workspace/github_cpp_makefile_star2_O2/stetre[P]moonvulkan/src/device_memory.c
// Function: Allocate
static int Allocate(lua_State *L)
    {
    int err;
    VkResult ec;
    ud_info_t *ud_info;
    ud_t *ud, *device_ud;
    VkDeviceMemory device_memory;
    VkMemoryAllocateInfo* info;
    VkDevice device = checkdevice(L, 1, &device_ud);
    const VkAllocationCallbacks *allocator = NULL;

#define CLEANUP zfreeVkMemoryAllocateInfo(L, info, 1)
    if(lua_istable(L, 2))
        {
        allocator = optallocator(L, 3);
        info = zcheckVkMemoryAllocateInfo(L, 2, &err);
        if(err) { CLEANUP; return argerror(L, 2); }
        }
    else
        {
        VkDeviceSize allocationSize = luaL_checkinteger(L, 2);
        uint32_t memoryTypeIndex = luaL_checkinteger(L, 3);
        info = znewVkMemoryAllocateInfo(L, &err);
        if(err) { CLEANUP; return lua_error(L); }
        info->allocationSize = allocationSize;
        info->memoryTypeIndex = memoryTypeIndex;
        }

    ud_info = (ud_info_t*)MallocNoErr(L, sizeof(ud_info_t));
    if(!ud_info) { CLEANUP; return errmemory(L); }

    ec = device_ud->ddt->AllocateMemory(device, info, allocator, &device_memory);
    
    if(ec) { CLEANUP; Free(L, ud_info); CheckError(L, ec); return 0; }
    TRACE_CREATE(device_memory, "device_memory");
    ud = newuserdata_nondispatchable(L, device_memory, DEVICE_MEMORY_MT);
    ud->parent_ud = device_ud;
    ud->device = device;
    ud->instance = device_ud->instance;
    ud->destructor = freedevice_memory;
    ud->allocator = allocator;
    ud->ddt = device_ud->ddt;
    ud->info = ud_info;
    ud_info->maxsz = info->allocationSize;
    CLEANUP;
#undef CLEANUP
    return 1;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x58488(%rip), %rcx     # 0x7d03b
pushq $0x1
popq %rbp
leaq 0x20(%rsp), %rdx
movl %ebp, %esi
callq 0x1a500
movq %rax, %r13
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x1a110
cmpl $0x5, %eax
jne 0x24c2d
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x191a0
movq %rax, %r12
pushq $0x2
popq %rsi
leaq 0x4(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x18310
movq %rax, %r15
cmpl $0x0, (%r14)
je 0x24c7d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19150
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x18680
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x18420
jmp 0x24d22
movq %r13, 0x10(%rsp)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x190d0
movq %rax, %r12
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x190d0
movq %rax, %r13
pushq $0x5
popq %rsi
pushq $0x20
popq %rdx
leaq 0x4(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rcx
callq 0x19dc0
movq %rax, %r15
cmpl $0x0, (%r14)
je 0x24c84
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19150
jmp 0x24cf5
movq %r13, 0x10(%rsp)
jmp 0x24c8f
movq %r12, 0x10(%r15)
movl %r13d, 0x18(%r15)
xorl %r12d, %r12d
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x24cff
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rax
leaq 0x18(%rsp), %rcx
movq %r12, %r13
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq *0x28(%rax)
testl %eax, %eax
je 0x24d35
movl %eax, %r14d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19150
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x182e0
movq %rbx, %rdi
xorl %esi, %esi
movl %r14d, %edx
callq 0x18980
movq %rbx, %rdi
callq 0x199c0
jmp 0x24d22
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19150
pushq $-0x6
popq %rdi
callq 0x19950
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19bc0
movl %eax, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9625c(%rip), %rax     # 0xbaf98
cmpl $0x0, (%rax)
je 0x24d54
movq 0x18(%rsp), %rsi
leaq 0x5a60f(%rip), %rdi     # 0x7f35c
xorl %eax, %eax
callq 0x18960
movq 0x18(%rsp), %rsi
leaq 0x5a25c(%rip), %rdx     # 0x7efbc
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x18330
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %r12, 0x20(%rax)
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rax)
leaq 0x16fa(%rip), %rdx      # 0x26480
movq %rdx, 0x8(%rax)
movq %r13, 0x40(%rax)
movq 0x50(%rcx), %rcx
movq %rcx, 0x50(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x58(%rax)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rcx)
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x19150
jmp 0x24d24