// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x6cbbd(%rip), %rcx     # 0x8d2f8
leaq 0x10(%rsp), %rdx
movl $0x1, %esi
callq 0x1a4e0
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a0f0
cmpl $0x5, %eax
jne 0x207b6
leaq 0xc(%rsp), %r12
movq %rbx, %rdi
movl $0x2, %esi
movq %r12, %rdx
callq 0x19d90
movq %rax, %r15
cmpl $0x0, (%r12)
je 0x20810
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x18680
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x18410
jmp 0x208d5
movq %rbx, %rdi
movl $0x1f, %esi
movl $0x2, %edx
callq 0x19be0
movl %eax, %ebp
movq %rbx, %rdi
movl $0x3, %esi
callq 0x190e0
movq %rax, %r12
leaq 0xc(%rsp), %r13
movl $0x20, %edx
movq %rbx, %rdi
movl $0x28, %esi
movq %r13, %rcx
callq 0x19da0
movq %rax, %r15
cmpl $0x0, (%r13)
je 0x20816
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
jmp 0x20888
movl 0x1c(%r15), %r12d
jmp 0x2081e
movl %ebp, 0x18(%r15)
movl %r12d, 0x1c(%r15)
movq %r14, 0x10(%r15)
testl %r12d, %r12d
je 0x20892
movl %r12d, %r13d
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x208ae
movq %rax, %r14
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x50(%rax), %rax
movq %r15, %rsi
movq %r14, %rdx
callq *0x238(%rax)
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movq %rbx, %rdi
testl %ebp, %ebp
je 0x208e8
movq %r14, %rsi
callq 0x182d0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18980
movq %rbx, %rdi
callq 0x199a0
jmp 0x208d5
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
leaq 0x6ca67(%rip), %rsi     # 0x8d310
movq %rbx, %rdi
jmp 0x208ce
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19160
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19930
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19ba0
movl %eax, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %esi, %esi
xorl %edx, %edx
callq 0x187d0
negq %r13
movl $0x1, %r15d
leaq 0x3a5(%rip), %r12       # 0x20ca6
movl $0x1, %ebp
movq 0x9f6bb(%rip), %rax     # 0xbffc8
cmpl $0x0, (%rax)
je 0x20925
movq -0x8(%r14,%r15,8), %rsi
leaq 0x6ca00(%rip), %rdi     # 0x8d31e
xorl %eax, %eax
callq 0x18960
movq -0x8(%r14,%r15,8), %rsi
movq %rbx, %rdi
leaq 0x6bfd5(%rip), %rdx     # 0x8c909
movl $0x1, %ecx
callq 0x18320
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
movups 0x18(%rcx), %xmm0
movups %xmm0, 0x18(%rax)
movq %r12, 0x8(%rax)
movq 0x50(%rcx), %rcx
movq %rcx, 0x50(%rax)
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %r15, %rdx
callq 0x19130
leaq (%r15,%r13), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x20906
jmp 0x208d7