// File: /workspace/github_cpp_makefile_star2_O1/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 0x6fb19(%rip), %rcx     # 0x902f8
leaq 0x10(%rsp), %rdx
movl $0x1, %esi
callq 0x1a520
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x1a130
cmpl $0x5, %eax
jne 0x2085a
leaq 0xc(%rsp), %r12
movq %rbx, %rdi
movl $0x2, %esi
movq %r12, %rdx
callq 0x19dd0
movq %rax, %r15
cmpl $0x0, (%r12)
je 0x208d1
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x18690
movq %rbx, %rdi
movl $0x2, %esi
movq %rax, %rdx
callq 0x18420
jmp 0x20989
movq %rbx, %rdi
movl $0x1f, %esi
movl $0x2, %edx
callq 0x19c20
movl %eax, %ebp
movq %rbx, %rdi
movl $0x3, %esi
callq 0x190f0
movq %rax, %r12
leaq 0xc(%rsp), %r13
movl $0x20, %edx
movq %rbx, %rdi
movl $0x28, %esi
movq %r13, %rcx
callq 0x19de0
movq %rax, %r15
movl (%r13), %r13d
testl %r13d, %r13d
je 0x208c0
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movq %rbx, %rdi
callq 0x199e0
movl %eax, %ebp
jmp 0x208c8
movl %ebp, 0x18(%r15)
movl %r12d, 0x1c(%r15)
testl %r13d, %r13d
jne 0x2098b
movq %r14, 0x10(%r15)
movl 0x1c(%r15), %r13d
testq %r13, %r13
je 0x20946
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x18050
testq %rax, %rax
je 0x20962
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 0x19170
movq %rbx, %rdi
testl %ebp, %ebp
je 0x2099c
movq %r14, %rsi
callq 0x182e0
movq %rbx, %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x18990
movq %rbx, %rdi
callq 0x199e0
jmp 0x20989
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
leaq 0x6f9b3(%rip), %rsi     # 0x90310
movq %rbx, %rdi
jmp 0x20982
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x19170
movl $0xfffffffa, %edi       # imm = 0xFFFFFFFA
callq 0x19970
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x19be0
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 0x187e0
negq %r13
movl $0x1, %r15d
leaq 0x3a1(%rip), %r12       # 0x20d56
movl $0x1, %ebp
movq 0xa2607(%rip), %rax     # 0xc2fc8
cmpl $0x0, (%rax)
je 0x209d9
movq -0x8(%r14,%r15,8), %rsi
leaq 0x6f94c(%rip), %rdi     # 0x9031e
xorl %eax, %eax
callq 0x18970
movq -0x8(%r14,%r15,8), %rsi
movq %rbx, %rdi
leaq 0x6ef21(%rip), %rdx     # 0x8f909
movl $0x1, %ecx
callq 0x18330
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 0x19140
leaq (%r15,%r13), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x209ba
jmp 0x2098b