// File: /workspace/github_cpp_makefile_star2_O3/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 0x6a29a(%rip), %rcx     # 0x8db1d
leaq 0x10(%rsp), %r14
movl $0x1, %esi
movq %r14, %rdx
callq 0x1a4e0
movq %rax, %r15
movq (%r14), %rax
movq 0x20(%rax), %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a0f0
cmpl $0x5, %eax
jne 0x23920
movl $0x1, %edx
movq %rbx, %rdi
movl $0x2, %esi
callq 0x18970
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a0f0
movl %eax, %ebp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x18150
testl %ebp, %ebp
jle 0x23920
leaq 0x8(%rsp), %r13
movl $0x28, %edx
movq %rbx, %rdi
movl $0x22, %esi
movq %r13, %rcx
callq 0x19da0
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x239e8
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19160
jmp 0x239db
leaq 0x8(%rsp), %r13
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
callq 0x19250
movq %rax, %r12
cmpl $0x0, (%r13)
je 0x23973
movq %rbx, %rdi
movq %r12, %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 0x23b1e
movq %r15, 0x10(%r12)
movl %eax, %esi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x23a45
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 0x19160
movq %rbx, %rdi
testl %ebp, %ebp
je 0x23a71
movq %r15, %rsi
callq 0x182d0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18980
movq %rbx, %rdi
callq 0x199a0
jmp 0x23b1e
leaq 0x6a215(%rip), %r9      # 0x8dc04
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
movq %rbx, %rdi
movl $0x2, %esi
movq %r13, %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
callq 0x19140
movq %rax, 0x20(%r12)
movl (%r13), %eax
movl %eax, 0x18(%r12)
cmpl $0x0, (%rbp)
je 0x23973
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19160
movl 0x8(%rsp), %edi
callq 0x19930
jmp 0x2395e
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x19160
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19930
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19ba0
jmp 0x23b1e
xorl %esi, %esi
xorl %edx, %edx
callq 0x187d0
cmpl $0x0, 0xc(%rsp)
je 0x23b0e
leaq 0x3ae(%rip), %r12       # 0x23e3a
xorl %ebp, %ebp
movq 0x9c533(%rip), %rax     # 0xbffc8
cmpl $0x0, (%rax)
je 0x23aac
movq (%r15,%rbp,8), %rsi
leaq 0x6a180(%rip), %rdi     # 0x8dc25
xorl %eax, %eax
callq 0x18960
movq (%r15,%rbp,8), %rsi
movq %rbx, %rdi
leaq 0x68e98(%rip), %rdx     # 0x8c952
xorl %ecx, %ecx
callq 0x18320
movq %rax, %r13
movq 0x10(%rsp), %rax
movq %rax, 0x10(%r13)
movq %r14, 0x20(%r13)
movq %r14, %rdi
callq 0x19e90
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 0x19130
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x50(%r13)
movl 0xc(%rsp), %eax
cmpq %rax, %rbp
jb 0x23a8e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x182d0
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq