// File: /workspace/github_cpp_makefile_star2_O1/stetre[P]moonvulkan/src/descriptor_set.c
// Function: Create
static int Create(lua_State *L)
    {
    ud_t *ud, *descriptor_pool_ud;
    int err, isinfo;
    uint32_t count, i;
    VkResult ec;
    VkDescriptorSet *descriptor_set;
    VkDescriptorSetAllocateInfo* info;

    VkDescriptorPool descriptor_pool = checkdescriptor_pool(L, 1, &descriptor_pool_ud);
    VkDevice device = descriptor_pool_ud->device;

    /* since both versions of the function have a table as second argoment, we need a bit
     * of euristics to determine if the argument is a descriptorsetallocateinfo or a
     * list of descriptor_set_layout objects.
     */
    if(lua_istable(L, 2))
        { lua_rawgeti(L, 2, 1); isinfo = lua_isnoneornil(L, -1); lua_pop(L, 1); }
    else
        isinfo = 1; /* will fail later */

#define CLEANUP zfreeVkDescriptorSetAllocateInfo(L, info, 1)
    if(isinfo)
        {
        info = zcheckVkDescriptorSetAllocateInfo(L, 2, &err);
        if(err) { CLEANUP; return argerror(L, 2); }
        }
    else
        {
        info = znewVkDescriptorSetAllocateInfo(L, &err);
        if(err) { CLEANUP; return lua_error(L); }
        info->pSetLayouts = checkdescriptor_set_layoutlist(L, 2, &count, &err, NULL);
        info->descriptorSetCount = count;
        if(err) { CLEANUP; return argerrorc(L, 2, err); }
        }

    info->descriptorPool = descriptor_pool;

    descriptor_set = (VkDescriptorSet*)MallocNoErr(L, sizeof(VkDescriptorSet)*count);
    if(!descriptor_set) { CLEANUP; return errmemory(L); }

    ec = descriptor_pool_ud->ddt->AllocateDescriptorSets(device, info, descriptor_set);
    CLEANUP;
#undef CLEANUP
    if(ec)
        { Free(L, descriptor_set); CheckError(L, ec); return 0; }

    lua_newtable(L);
    for(i=0; i < count; i++)
        {
        TRACE_CREATE(descriptor_set[i], "descriptor_set");
        ud = newuserdata_nondispatchable(L, descriptor_set[i], DESCRIPTOR_SET_MT);
        ud->parent_ud = descriptor_pool_ud;
        ud->device = device;
        ud->instance = UD(device)->instance;
        ud->destructor = freedescriptor_set;
        lua_rawseti(L, -2, i+1);
        ud->ddt = descriptor_pool_ud->ddt;
        }

    Free(L, descriptor_set);
    return 1;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x6d1c2(%rip), %rcx     # 0x90b1d
leaq 0x10(%rsp), %r14
movl $0x1, %esi
movq %r14, %rdx
callq 0x1a520
movq %rax, %r15
movq (%r14), %rax
movq 0x20(%rax), %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a130
cmpl $0x5, %eax
jne 0x239f4
movl $0x1, %edx
movq %rbx, %rdi
movl $0x2, %esi
callq 0x18980
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a130
movl %eax, %ebp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x18150
testl %ebp, %ebp
jle 0x239f4
leaq 0xc(%rsp), %r13
movl $0x28, %edx
movq %rbx, %rdi
movl $0x22, %esi
movq %r13, %rcx
callq 0x19de0
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x23a4b
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19170
jmp 0x23b03
leaq 0xc(%rsp), %r13
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
callq 0x19260
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x23aa1
movq %rbx, %rdi
movq %r12, %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 0x23be9
leaq 0x6d1b2(%rip), %r9      # 0x90c04
leaq 0x8(%rsp), %r13
leaq 0xc(%rsp), %rbp
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
callq 0x19150
movq %rax, 0x20(%r12)
movl (%r13), %eax
movl %eax, 0x18(%r12)
cmpl $0x0, (%rbp)
je 0x23aa1
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19170
movl 0xc(%rsp), %edi
callq 0x19970
jmp 0x23a36
movq %r15, 0x10(%r12)
movl 0x8(%rsp), %esi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x23b10
movq %rax, %r15
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq *0x1e0(%rax)
movl %eax, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
testl %ebp, %ebp
je 0x23b3c
movq %r15, %rsi
callq 0x182e0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18990
movq %rbx, %rdi
callq 0x199e0
jmp 0x23be9
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19170
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19970
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19be0
jmp 0x23be9
xorl %esi, %esi
xorl %edx, %edx
callq 0x187e0
cmpl $0x0, 0x8(%rsp)
je 0x23bd9
leaq 0x3b7(%rip), %r12       # 0x23f0e
xorl %ebp, %ebp
movq 0x9f468(%rip), %rax     # 0xc2fc8
cmpl $0x0, (%rax)
je 0x23b77
movq (%r15,%rbp,8), %rsi
leaq 0x6d0b5(%rip), %rdi     # 0x90c25
xorl %eax, %eax
callq 0x18970
movq (%r15,%rbp,8), %rsi
movq %rbx, %rdi
leaq 0x6bdcd(%rip), %rdx     # 0x8f952
xorl %ecx, %ecx
callq 0x18330
movq %rax, %r13
movq 0x10(%rsp), %rax
movq %rax, 0x10(%r13)
movq %r14, 0x20(%r13)
movq %r14, %rdi
callq 0x19ed0
movq 0x18(%rax), %rax
movq %rax, 0x18(%r13)
movq %r12, 0x8(%r13)
incq %rbp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %rbp, %rdx
callq 0x19140
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x50(%r13)
movl 0x8(%rsp), %eax
cmpq %rax, %rbp
jb 0x23b59
movq %rbx, %rdi
movq %r15, %rsi
callq 0x182e0
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq