// File: stetre[P]moonvulkan/src/layers.c
// Function: EnumerateInstanceLayerProperties
static int EnumerateInstanceLayerProperties(lua_State *L)
    {
    int err;
    uint32_t count, remaining, tot, i;
    VkResult ec;
    VkLayerProperties* properties;//[N];
    int byname = optboolean(L, 1, 0);

#define CLEANUP zfreearrayVkLayerProperties(L, properties, N, 1) 
    properties = znewarrayVkLayerProperties (L, N, &err);
    if(err) { CLEANUP; lua_error(L); }
    lua_newtable(L);
    ec = vk.EnumerateInstanceLayerProperties(&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.EnumerateInstanceLayerProperties(&count, properties);
        if(ec && ec!=VK_INCOMPLETE) { CLEANUP; CheckError(L, ec); }
        
        if(byname)
            {
            for(i = 0; i < count; i++)
                {
                lua_pushstring(L, properties[i].layerName);
                zpushVkLayerProperties(L, &properties[i]);
                lua_rawset(L, -3);
                }
            }
        else
            {
            for(i = 0; i < count; i++)
                {
                zpushVkLayerProperties(L, &properties[i]);
                lua_rawseti(L, -2, ++tot);
                }
            }
        } while(remaining > 0);
    
    CLEANUP;
#undef CLEANUP
    return 1;
    }

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x198b0
movl %eax, 0x4(%rsp)
movq 0x28(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
leaq 0x24(%rsp), %r8
callq 0x19920
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x24(%rsp)
je 0x59ca6
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movq 0x28(%rsp), %rdi
callq 0x19ad0
movq 0x28(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x18830
leaq 0x1c(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movq 0xa32fc(%rip), %rax     # 0xfcfc0
callq *0x8(%rax)
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
je 0x59d24
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
cmpl $0x0, 0x10(%rsp)
je 0x59d20
movq 0x28(%rsp), %rdi
movl 0x10(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x28(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x34(%rsp)
jmp 0x59f11
jmp 0x59d22
jmp 0x59d24
cmpl $0x0, 0x1c(%rsp)
jne 0x59d5c
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movl $0x1, 0x34(%rsp)
jmp 0x59f11
movl $0x0, 0x18(%rsp)
cmpl $0x20, 0x1c(%rsp)
jbe 0x59d80
movl $0x20, 0x20(%rsp)
movl 0x1c(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x1c(%rsp)
jmp 0x59d90
movl 0x1c(%rsp), %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0xa3229(%rip), %rax     # 0xfcfc0
movq 0x8(%rax), %rax
movq 0x8(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq *%rax
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
je 0x59e0b
cmpl $0x5, 0x10(%rsp)
je 0x59e0b
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
cmpl $0x0, 0x10(%rsp)
je 0x59e07
movq 0x28(%rsp), %rdi
movl 0x10(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x28(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x34(%rsp)
jmp 0x59f11
jmp 0x59e09
jmp 0x59e0b
cmpl $0x0, 0x4(%rsp)
je 0x59e7c
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x20(%rsp), %eax
jae 0x59e7a
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
imulq $0x208, %rax, %rax      # imm = 0x208
addq %rax, %rsi
callq 0x1a420
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
imulq $0x208, %rax, %rax      # imm = 0x208
addq %rax, %rsi
callq 0x18e90
movq 0x28(%rsp), %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x18860
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x59e1a
jmp 0x59ed8
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x20(%rsp), %eax
jae 0x59ed6
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl 0x14(%rsp), %eax
imulq $0x208, %rax, %rax      # imm = 0x208
addq %rax, %rsi
callq 0x18e90
movq 0x28(%rsp), %rdi
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movl %eax, %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191e0
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x59e84
jmp 0x59ed8
jmp 0x59eda
cmpl $0x0, 0x1c(%rsp)
ja 0x59d64
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x208, %edx            # imm = 0x208
movl $0x20, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw (%rax,%rax)