// File: stetre[P]moonvulkan/src/command_buffer.c
// Function: Create
static int Create(lua_State *L)
    {
    int err;
    ud_t *ud, *command_pool_ud;
    VkResult ec;
    uint32_t i, count;
    VkCommandBuffer *command_buffer;
    VkCommandBufferAllocateInfo* info;
    VkCommandPool command_pool = checkcommand_pool(L, 1, &command_pool_ud);

#define CLEANUP zfreeVkCommandBufferAllocateInfo(L, info, 1)
    if(lua_istable(L, 2))
        {
        info = zcheckVkCommandBufferAllocateInfo(L, 2, &err);
        if(err) { CLEANUP; return argerror(L, 2); }
        }
    else
        {
        VkCommandBufferLevel level = checkcommandbufferlevel(L, 2);
        uint32_t commandBufferCount = luaL_checkinteger(L, 3);
        info = znewVkCommandBufferAllocateInfo(L, &err);
        if(err) { CLEANUP; return lua_error(L); }
        info->level = level;
        info->commandBufferCount = commandBufferCount;
        }

    info->commandPool = command_pool;
    count = info->commandBufferCount;
    if(count == 0) { CLEANUP; return luaL_error(L, "invalid count"); }

    command_buffer = (VkCommandBuffer*)MallocNoErr(L, sizeof(VkCommandBuffer)*count);
    if(!command_buffer) { CLEANUP; return errmemory(L); }

    ec = command_pool_ud->ddt->AllocateCommandBuffers(command_pool_ud->device, info, command_buffer);
    CLEANUP;
#undef CLEANUP
    if(ec) { Free(L, command_buffer); CheckError(L, ec); return 0; }

    lua_newtable(L);
    for(i=0; i<count; i++)
        {
        TRACE_CREATE(command_buffer[i], "command_buffer");
        ud = newuserdata_dispatchable(L, command_buffer[i], COMMAND_BUFFER_MT);
        ud->parent_ud = command_pool_ud;
        ud->device = command_pool_ud->device;
        ud->instance = command_pool_ud->instance;
        ud->destructor = freecommand_buffer;
        ud->ddt = command_pool_ud->ddt;
        lua_rawseti(L, -2, i+1);
        }
    return 1;
    }

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movl $0x1, %esi
leaq 0x40(%rsp), %rdx
leaq 0xad8c9(%rip), %rcx     # 0xd02f8
callq 0x1a640
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rdi
movl $0x2, %esi
callq 0x1a240
cmpl $0x5, %eax
jne 0x22abe
movq 0x58(%rsp), %rdi
movl $0x2, %esi
leaq 0x54(%rsp), %rdx
callq 0x19ed0
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x22ab9
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0x186d0
movq 0x8(%rsp), %rdi
movq %rax, %rdx
movl $0x2, %esi
callq 0x18460
movl %eax, 0x64(%rsp)
jmp 0x22d86
jmp 0x22b4d
movq 0x58(%rsp), %rdi
movl $0x1f, %esi
movl $0x2, %edx
callq 0x19d20
movl %eax, 0x14(%rsp)
movq 0x58(%rsp), %rdi
movl $0x3, %esi
callq 0x19190
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %rdi
movl $0x28, %esi
movl $0x20, %edx
leaq 0x54(%rsp), %rcx
callq 0x19ee0
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x22b35
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x58(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x64(%rsp)
jmp 0x22d86
movl 0x14(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x18(%rax)
movl 0x10(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1c(%rax)
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rax
movl 0x1c(%rax), %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jne 0x22b9e
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x58(%rsp), %rdi
leaq 0xad782(%rip), %rsi     # 0xd0310
movb $0x0, %al
callq 0x19ce0
movl %eax, 0x64(%rsp)
jmp 0x22d86
movq 0x58(%rsp), %rdi
movl 0x34(%rsp), %eax
movl %eax, %esi
shlq $0x3, %rsi
callq 0x18050
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x22bfd
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19a60
movq (%rsp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x19ce0
movl %eax, 0x64(%rsp)
jmp 0x22d86
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rax
movq 0x238(%rax), %rax
movq 0x40(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq *%rax
movl %eax, 0x3c(%rsp)
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x19210
cmpl $0x0, 0x3c(%rsp)
je 0x22c89
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x18300
cmpl $0x0, 0x3c(%rsp)
je 0x22c7a
movq 0x58(%rsp), %rdi
movl 0x3c(%rsp), %edx
xorl %esi, %esi
callq 0x18a00
movq 0x58(%rsp), %rdi
callq 0x19ad0
movl %eax, 0x64(%rsp)
jmp 0x22d86
jmp 0x22c7c
movl $0x0, 0x64(%rsp)
jmp 0x22d86
movq 0x58(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x18830
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x34(%rsp), %eax
jae 0x22d7e
jmp 0x22caf
movq 0xda2e2(%rip), %rax     # 0xfcf98
cmpl $0x0, (%rax)
je 0x22cd6
movq 0x28(%rsp), %rax
movl 0x38(%rsp), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0xad64f(%rip), %rdi     # 0xd031e
movb $0x0, %al
callq 0x189d0
jmp 0x22cd8
movq 0x58(%rsp), %rdi
movq 0x28(%rsp), %rax
movl 0x38(%rsp), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0xacc18(%rip), %rdx     # 0xcf909
movl $0x1, %ecx
callq 0x18350
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x48(%rsp), %rax
leaq 0x4c2(%rip), %rcx       # 0x23200
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
movq 0x50(%rax), %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x58(%rsp), %rdi
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191e0
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x22c9f
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nop