// File: /workspace/github_cpp_makefile_star2_O2/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 0x5ba72(%rip), %rcx     # 0x7eb1d
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1a500
movq %rax, %r15
movq (%r14), %rax
movq 0x20(%rax), %r14
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x1a110
cmpl $0x5, %eax
jne 0x23136
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x18970
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0x1a110
movl %eax, %ebp
pushq $-0x2
popq %rsi
movq %rbx, %rdi
callq 0x18150
testl %ebp, %ebp
jle 0x23136
pushq $0x22
popq %rsi
pushq $0x28
popq %rdx
leaq 0x4(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rcx
callq 0x19dc0
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x231f6
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19150
jmp 0x231ec
pushq $0x2
popq %rsi
leaq 0x4(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rdx
callq 0x19240
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x23181
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %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 0x23272
movq %r15, 0x10(%r12)
movl %eax, %esi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x2324f
movq %rax, %r15
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq *0x1e0(%rax)
movl %eax, %r13d
pushq $0x1
popq %rbp
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x19150
movq %rbx, %rdi
testl %r13d, %r13d
je 0x23285
movq %r15, %rsi
callq 0x182e0
movq %rbx, %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x18980
movq %rbx, %rdi
callq 0x199c0
jmp 0x23272
leaq 0x5ba07(%rip), %r9      # 0x7ec04
pushq $0x2
popq %rsi
leaq 0x14(%rsp), %r13
leaq 0x4(%rsp), %rbp
movq %rbx, %rdi
movq %r13, %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
callq 0x19130
movq %rax, 0x20(%r12)
movl (%r13), %eax
movl %eax, 0x18(%r12)
cmpl $0x0, (%rbp)
je 0x23181
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19150
movl 0x4(%rsp), %edi
callq 0x19950
jmp 0x2316e
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %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 $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
xorl %edx, %edx
callq 0x187d0
xorl %r12d, %r12d
movl 0x14(%rsp), %eax
cmpq %rax, %r12
jae 0x2331f
movq 0x97cf3(%rip), %rax     # 0xbaf98
cmpl $0x0, (%rax)
je 0x232bc
movq (%r15,%r12,8), %rsi
leaq 0x5b970(%rip), %rdi     # 0x7ec25
xorl %eax, %eax
callq 0x18960
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
leaq 0x5a688(%rip), %rdx     # 0x7d952
xorl %ecx, %ecx
callq 0x18330
movq %rax, %r13
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r13)
movq %r14, 0x20(%r13)
movq %r14, %rdi
callq 0x19eb0
movq 0x18(%rax), %rax
movq %rax, 0x18(%r13)
leaq 0x312(%rip), %rax       # 0x2360a
movq %rax, 0x8(%r13)
incq %r12
movq %rbx, %rdi
pushq $-0x2
popq %rsi
movq %r12, %rdx
callq 0x19120
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x50(%r13)
jmp 0x23291
movq %rbx, %rdi
movq %r15, %rsi
callq 0x182e0
jmp 0x23274