// File: /workspace/github_cpp_makefile_star2_O1/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 0x69ac4(%rip), %rcx     # 0x8f03b
leaq 0x18(%rsp), %rdx
movl $0x1, %esi
callq 0x1a520
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a130
cmpl $0x5, %eax
jne 0x25602
movq %rbx, %rdi
movl $0x3, %esi
callq 0x191c0
movq %rax, %r12
leaq 0xc(%rsp), %r13
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
callq 0x18310
movq %rax, %r15
cmpl $0x0, (%r13)
je 0x25682
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x18690
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x18420
jmp 0x2570f
movq %r14, 0x20(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x190f0
movq %rax, %r12
movq %rbx, %rdi
movl $0x3, %esi
callq 0x190f0
movq %rax, %r13
leaq 0xc(%rsp), %rbp
movl $0x20, %edx
movq %rbx, %rdi
movl $0x5, %esi
movq %rbp, %rcx
callq 0x19de0
movq %rax, %r15
movl (%rbp), %r14d
testl %r14d, %r14d
je 0x25669
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
callq 0x199e0
movl %eax, %ebp
jmp 0x25671
movq %r12, 0x10(%r15)
movl %r13d, 0x18(%r15)
testl %r14d, %r14d
jne 0x25711
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
movl $0x18, %esi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x256e8
movq %rax, %r13
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rax
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x28(%rax)
testl %eax, %eax
je 0x25722
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
movq %r13, %rsi
callq 0x182e0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18990
movq %rbx, %rdi
callq 0x199e0
jmp 0x2570f
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19970
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19be0
movl %eax, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9d89f(%rip), %rax     # 0xc2fc8
cmpl $0x0, (%rax)
je 0x25741
movq 0x10(%rsp), %rsi
leaq 0x6bc22(%rip), %rdi     # 0x9135c
xorl %eax, %eax
callq 0x18970
movq 0x10(%rsp), %rsi
leaq 0x6b86f(%rip), %rdx     # 0x90fbc
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x18330
movq 0x18(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %r14, 0x20(%rax)
movq 0x18(%rcx), %rdx
movq %rdx, 0x18(%rax)
leaq 0x1877(%rip), %rdx      # 0x26fea
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 0x19170
jmp 0x25711