// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movl $0x1, %esi
leaq 0x58(%rsp), %rdx
leaq 0xa91a6(%rip), %rcx     # 0xd0ad5
callq 0x1a640
movq %rax, 0x28(%rsp)
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movl $0x2, %esi
callq 0x1a240
cmpl $0x5, %eax
jne 0x2799e
movq 0x68(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x189e0
movq 0x68(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a240
cmpl $0x0, %eax
setle %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x50(%rsp)
movq 0x68(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x18170
jmp 0x279a6
movl $0x1, 0x50(%rsp)
cmpl $0x0, 0x50(%rsp)
je 0x27a1e
movq 0x68(%rsp), %rdi
movl $0x2, %esi
leaq 0x54(%rsp), %rdx
callq 0x19310
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x27a19
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x186d0
movq 0x18(%rsp), %rdi
movq %rax, %rdx
movl $0x2, %esi
callq 0x18460
movl %eax, 0x74(%rsp)
jmp 0x27cf3
jmp 0x27af2
movq 0x68(%rsp), %rdi
movl $0x22, %esi
movl $0x28, %edx
leaq 0x54(%rsp), %rcx
callq 0x19ee0
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x27a6a
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x74(%rsp)
jmp 0x27cf3
movq 0x68(%rsp), %rdi
movl $0x2, %esi
leaq 0x4c(%rsp), %rdx
leaq 0x54(%rsp), %rcx
xorl %eax, %eax
movl %eax, %r8d
leaq 0xa9132(%rip), %r9      # 0xd0bbc
callq 0x191f0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x20(%rax)
movl 0x4c(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x18(%rax)
cmpl $0x0, 0x54(%rsp)
je 0x27af0
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x54(%rsp), %edi
callq 0x19a60
movq 0x10(%rsp), %rdi
movq %rax, %rdx
movl $0x2, %esi
callq 0x18460
movl %eax, 0x74(%rsp)
jmp 0x27cf3
jmp 0x27af2
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x68(%rsp), %rdi
movl 0x4c(%rsp), %eax
movl %eax, %esi
shlq $0x3, %rsi
callq 0x18050
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x27b61
movq 0x68(%rsp), %rdi
movq 0x30(%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 0x27cf3
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x1e0(%rax), %rax
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq *%rax
movl %eax, 0x44(%rsp)
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
cmpl $0x0, 0x44(%rsp)
je 0x27be9
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x18300
cmpl $0x0, 0x44(%rsp)
je 0x27bda
movq 0x68(%rsp), %rdi
movl 0x44(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x68(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x74(%rsp)
jmp 0x27cf3
jmp 0x27bdc
movl $0x0, 0x74(%rsp)
jmp 0x27cf3
movq 0x68(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x18830
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jae 0x27cdc
jmp 0x27c0f
movq 0xd5382(%rip), %rax     # 0xfcf98
cmpl $0x0, (%rax)
je 0x27c36
movq 0x38(%rsp), %rax
movl 0x48(%rsp), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0xa8fae(%rip), %rdi     # 0xd0bdd
movb $0x0, %al
callq 0x189d0
jmp 0x27c38
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x48(%rsp), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0xa7d01(%rip), %rdx     # 0xcf952
xorl %ecx, %ecx
callq 0x18350
movq %rax, 0x60(%rsp)
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x20(%rsp), %rdi
callq 0x19fd0
movq 0x18(%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x60(%rsp), %rax
leaq 0x504(%rip), %rcx       # 0x281a0
movq %rcx, 0x8(%rax)
movq 0x68(%rsp), %rdi
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191e0
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x50(%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x27bff
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x18300
movl $0x1, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopl (%rax)