// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %rdi, %rbx
leaq 0x66b88(%rip), %rcx     # 0x8c03b
leaq 0x10(%rsp), %rdx
movl $0x1, %esi
callq 0x1a4e0
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a0f0
movq %rbx, %rdi
cmpl $0x5, %eax
jne 0x2553e
movl $0x3, %esi
callq 0x191b0
movq %rax, %r12
leaq 0x4(%rsp), %r13
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
callq 0x18300
movq %rax, %r15
cmpl $0x0, (%r13)
je 0x2559b
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x18680
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x18410
jmp 0x25628
movl $0x2, %esi
callq 0x190e0
movq %rax, %r12
movq %rbx, %rdi
movl $0x3, %esi
callq 0x190e0
movq %rax, %r13
leaq 0x4(%rsp), %rbp
movl $0x20, %edx
movq %rbx, %rdi
movl $0x5, %esi
movq %rbp, %rcx
callq 0x19da0
movq %rax, %r15
cmpl $0x0, (%rbp)
je 0x25590
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
jmp 0x255f7
movq %r12, 0x10(%r15)
movl %r13d, 0x18(%r15)
xorl %r12d, %r12d
movl $0x18, %esi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x25601
movq %rax, %r13
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x28(%rax)
testl %eax, %eax
je 0x2563b
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movq %rbx, %rdi
movq %r13, %rsi
callq 0x182d0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18980
movq %rbx, %rdi
callq 0x199a0
jmp 0x25628
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19930
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19ba0
movl %eax, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9a986(%rip), %rax     # 0xbffc8
cmpl $0x0, (%rax)
je 0x2565a
movq 0x8(%rsp), %rsi
leaq 0x68d09(%rip), %rdi     # 0x8e35c
xorl %eax, %eax
callq 0x18960
movq 0x8(%rsp), %rsi
leaq 0x68956(%rip), %rdx     # 0x8dfbc
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x18320
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %r14, 0x20(%rax)
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rax)
leaq 0x1866(%rip), %rdx      # 0x26ef2
movq %rdx, 0x8(%rax)
movq %r12, 0x40(%rax)
movq 0x50(%rcx), %rcx
movq %rcx, 0x50(%rax)
movq %r13, 0x58(%rax)
movq 0x10(%r15), %rax
movq %rax, 0x10(%r13)
movl $0x1, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
jmp 0x2562a