// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movl $0x1, %esi
leaq 0x48(%rsp), %rdx
leaq 0xa4d8c(%rip), %rcx     # 0xcf03b
callq 0x1a640
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x68(%rsp), %rdi
movl $0x2, %esi
callq 0x1a240
cmpl $0x5, %eax
jne 0x2a35f
movq 0x68(%rsp), %rdi
movl $0x3, %esi
callq 0x19260
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rdi
movl $0x2, %esi
leaq 0x64(%rsp), %rdx
callq 0x18330
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x64(%rsp)
je 0x2a35a
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x68(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x186d0
movq 0x10(%rsp), %rdi
movq %rax, %rdx
movl $0x2, %esi
callq 0x18460
movl %eax, 0x74(%rsp)
jmp 0x2a5ac
jmp 0x2a3ec
movq 0x68(%rsp), %rdi
movl $0x2, %esi
callq 0x19190
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movl $0x3, %esi
callq 0x19190
movl %eax, 0x1c(%rsp)
movq 0x68(%rsp), %rdi
movl $0x5, %esi
movl $0x20, %edx
leaq 0x64(%rsp), %rcx
callq 0x19ee0
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x64(%rsp)
je 0x2a3d2
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x74(%rsp)
jmp 0x2a5ac
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0x10(%rax)
movl 0x1c(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x68(%rsp), %rdi
movl $0x18, %esi
callq 0x18050
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x2a448
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19a60
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x19ce0
movl %eax, 0x74(%rsp)
jmp 0x2a5ac
movq 0x48(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x28(%rax), %rax
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq *%rax
movl %eax, 0x60(%rsp)
cmpl $0x0, 0x60(%rsp)
je 0x2a4d2
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x18300
cmpl $0x0, 0x60(%rsp)
je 0x2a4c3
movq 0x68(%rsp), %rdi
movl 0x60(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x68(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x74(%rsp)
jmp 0x2a5ac
jmp 0x2a4c5
movl $0x0, 0x74(%rsp)
jmp 0x2a5ac
jmp 0x2a4d4
movq 0xd2abd(%rip), %rax     # 0xfcf98
cmpl $0x0, (%rax)
je 0x2a4f3
movq 0x40(%rsp), %rsi
leaq 0xa6e28(%rip), %rdi     # 0xd1314
movb $0x0, %al
callq 0x189d0
jmp 0x2a4f5
movq 0x68(%rsp), %rdi
movq 0x40(%rsp), %rsi
leaq 0xa6a6e(%rip), %rdx     # 0xd0f74
xorl %ecx, %ecx
callq 0x18350
movq %rax, 0x50(%rsp)
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x50(%rsp), %rax
leaq 0x17c4(%rip), %rcx      # 0x2bd10
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x48(%rsp), %rax
movq 0x50(%rax), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x58(%rax)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movl $0x1, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)