// File: stetre[P]moonvulkan/src/surface.c
// Function: GetPhysicalDeviceSurfaceFormats
static int GetPhysicalDeviceSurfaceFormats(lua_State *L)
    {
    int err;
    VkResult ec;
    uint32_t count, i;
    VkSurfaceFormatKHR* formats;
    VkSurfaceKHR surface;
    ud_t *ud;
    VkPhysicalDevice physdev = checkphysical_device(L, 1, &ud);

    if(ud->idt->GetPhysicalDeviceSurfaceFormats2KHR)
        return GetPhysicalDeviceSurfaceFormats2(L, physdev, ud);
    CheckInstancePfn(L, ud, GetPhysicalDeviceSurfaceFormatsKHR);
    surface = checksurface(L, 2, NULL);

    lua_newtable(L);
    ec = ud->idt->GetPhysicalDeviceSurfaceFormatsKHR(physdev, surface, &count, NULL);
    CheckError(L, ec);
    if(count == 0) return 1;

#define CLEANUP zfreearrayVkSurfaceFormatKHR(L, formats, count, 1)
    formats = znewarrayVkSurfaceFormatKHR(L, count, &err);
    if(err) { CLEANUP; return lua_error(L); }
    
    ec = ud->idt->GetPhysicalDeviceSurfaceFormatsKHR(physdev, surface, &count, formats);
    if(ec) { CLEANUP; CheckError(L, ec); return 0; }
    for(i = 0; i < count; i++)
        {
        zpushVkSurfaceFormatKHR(L, &formats[i]);
        lua_rawseti(L, -2, i+1);
        }
    CLEANUP;
#undef CLEANUP
    return 1;
    }

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movl $0x1, %esi
leaq 0x10(%rsp), %rdx
leaq 0x6db9d(%rip), %rcx     # 0xd0eac
callq 0x1a640
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x88(%rax)
je 0x63349
movq 0x38(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x64400
movl %eax, 0x44(%rsp)
jmp 0x63577
jmp 0x6334b
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x80(%rax)
jne 0x6337a
movq 0x38(%rsp), %rdi
leaq 0x8025b(%rip), %rsi     # 0xe35c5
movb $0x0, %al
callq 0x19ce0
movl %eax, 0x44(%rsp)
jmp 0x63577
jmp 0x6337c
movq 0x38(%rsp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
leaq 0x7f10f(%rip), %rcx     # 0xe24a0
callq 0x1a640
movq %rax, 0x18(%rsp)
movq 0x38(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x18830
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x80(%rax), %rax
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
xorl %ecx, %ecx
callq *%rax
movl %eax, 0x30(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0x633fa
movq 0x38(%rsp), %rdi
movl 0x30(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x38(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x44(%rsp)
jmp 0x63577
jmp 0x633fc
cmpl $0x0, 0x2c(%rsp)
jne 0x63410
movl $0x1, 0x44(%rsp)
jmp 0x63577
movq 0x38(%rsp), %rdi
movl 0x2c(%rsp), %ecx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x8, %edx
leaq 0x34(%rsp), %r8
callq 0x19920
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x6346f
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x2c(%rsp), %ecx
movl $0x8, %edx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movq 0x38(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x44(%rsp)
jmp 0x63577
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rax
movq 0x80(%rax), %rax
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rcx
leaq 0x2c(%rsp), %rdx
callq *%rax
movl %eax, 0x30(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0x634f9
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x2c(%rsp), %ecx
movl $0x8, %edx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
cmpl $0x0, 0x30(%rsp)
je 0x634ed
movq 0x38(%rsp), %rdi
movl 0x30(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x38(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x44(%rsp)
jmp 0x63577
jmp 0x634ef
movl $0x0, 0x44(%rsp)
jmp 0x63577
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jae 0x6354c
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x28(%rsp), %eax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x18cb0
movq 0x38(%rsp), %rdi
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191e0
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x63501
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x2c(%rsp), %ecx
movl $0x8, %edx
movl $0x1, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x18dd0
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq