// File: stetre[P]moonvulkan/src/layers.c
// Function: EnumerateInstanceExtensionProperties
static int EnumerateInstanceExtensionProperties(lua_State *L)
    {
    int err;
    uint32_t count, remaining, tot, i;
    VkResult ec;
    VkExtensionProperties* properties;//[N];
    const char *layer_name = luaL_optstring(L, 1, NULL);
    int byname = optboolean(L, 2, 0);

#define CLEANUP zfreearrayVkExtensionProperties(L, properties, N, 1) 
    properties = znewarrayVkExtensionProperties(L, N, &err);
    if(err) { CLEANUP; lua_error(L); }
    lua_newtable(L);
    ec = vk.EnumerateInstanceExtensionProperties(layer_name, &remaining, NULL);
    if(ec) { CLEANUP; CheckError(L, ec); }
    if(remaining==0) { CLEANUP; return 1; }

    tot = 0;
    do {
        if(remaining > N)
            { count = N; remaining -= N; }
        else
            { count = remaining; remaining = 0; }

        ec = vk.EnumerateInstanceExtensionProperties(layer_name, &count, properties);
        if(ec && ec!=VK_INCOMPLETE) { CLEANUP; CheckError(L, ec); }
        
        if(byname)
            {
            for(i = 0; i < count; i++)
                {
                lua_pushstring(L, properties[i].extensionName);
                zpushVkExtensionProperties(L, &properties[i]);
                lua_rawset(L, -3);
                }
            }
        else
            {
            for(i = 0; i < count; i++)
                {
                zpushVkExtensionProperties(L, &properties[i]);
                lua_rawseti(L, -2, ++tot);
                }
            }

        } while(remaining > 0);
    
    CLEANUP;
#undef CLEANUP
    return 1;
    }

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x18900
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x198b0
movl %eax, 0xc(%rsp)
movq 0x38(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
leaq 0x34(%rsp), %r8
callq 0x19920
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x599b1
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movq 0x38(%rsp), %rdi
callq 0x19ad0
movq 0x38(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x18830
movq 0xa35fa(%rip), %rax     # 0xfcfc0
movq (%rax), %rax
movq 0x10(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *%rax
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
je 0x59a36
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
cmpl $0x0, 0x20(%rsp)
je 0x59a32
movq 0x38(%rsp), %rdi
movl 0x20(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x38(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x44(%rsp)
jmp 0x59c27
jmp 0x59a34
jmp 0x59a36
cmpl $0x0, 0x2c(%rsp)
jne 0x59a6e
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movl $0x1, 0x44(%rsp)
jmp 0x59c27
movl $0x0, 0x28(%rsp)
cmpl $0x20, 0x2c(%rsp)
jbe 0x59a92
movl $0x20, 0x30(%rsp)
movl 0x2c(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x2c(%rsp)
jmp 0x59aa2
movl 0x2c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0xa3517(%rip), %rax     # 0xfcfc0
movq (%rax), %rax
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
leaq 0x30(%rsp), %rsi
callq *%rax
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x20(%rsp)
je 0x59b21
cmpl $0x5, 0x20(%rsp)
je 0x59b21
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
cmpl $0x0, 0x20(%rsp)
je 0x59b1d
movq 0x38(%rsp), %rdi
movl 0x20(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x38(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x44(%rsp)
jmp 0x59c27
jmp 0x59b1f
jmp 0x59b21
cmpl $0x0, 0xc(%rsp)
je 0x59b92
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x59b90
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %eax
imulq $0x104, %rax, %rax      # imm = 0x104
addq %rax, %rsi
callq 0x1a420
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %eax
imulq $0x104, %rax, %rax      # imm = 0x104
addq %rax, %rsi
callq 0x1a6d0
movq 0x38(%rsp), %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x18860
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x59b30
jmp 0x59bee
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x59bec
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x24(%rsp), %eax
imulq $0x104, %rax, %rax      # imm = 0x104
addq %rax, %rsi
callq 0x1a6d0
movq 0x38(%rsp), %rdi
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movl %eax, %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191e0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x59b9a
jmp 0x59bee
jmp 0x59bf0
cmpl $0x0, 0x2c(%rsp)
ja 0x59a76
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl $0x104, %edx            # imm = 0x104
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq