// File: stetre[P]moonvulkan/src/udata.c
// Function: moonvulkan_udata_new
void *udata_new(lua_State *L, size_t size, uint64_t id_, const char *mt)
/* Creates a new Lua userdata, optionally sets its metatable to mt (if != NULL),
 * associates the userdata with the passed id and pushes the userdata on the stack.
 *
 * The userdata can be subsquently pushed on the Lua stack with udata_push(L, id).
 * (This is useful to bind C/C++ objects to Lua userdata).
 *
 * If id=0, the pointer to the memory area allocated by Lua is used as identifier
 * (this function returnes it).
 */
    {
    udata_t *udata;
    if((udata = (udata_t*)Malloc(L, sizeof(udata_t))) == NULL) 
        { luaL_error(L, "cannot allocate memory"); return NULL; }
    memset(udata, 0, sizeof(udata_t));
    udata->mem = lua_newuserdata(L, size);
    if(!udata->mem)
        {
        Free(L, udata);
        luaL_error(L, "lua_newuserdata error"); 
        return NULL;
        }
    udata->id = id_ != 0 ? id_ : (uint64_t)(uintptr_t)(udata->mem);
    if(udata_search(udata->id))
        { 
        Free(L, udata);
        luaL_error(L, "duplicated object %I", id_); 
        return NULL; 
        }
    /* create a reference for later push's */
    lua_pushvalue(L, -1); /* the newly created userdata */
    udata->ref = luaL_ref(L, LUA_REGISTRYINDEX);
    udata_insert(udata);
    if(mt)
        {
        udata->mt = mt;
        luaL_getmetatable(L, mt);
        lua_setmetatable(L, -2);
        }
    return udata->mem;
    }

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x28(%rsp), %rdi
movl $0x40, %esi
callq 0x192c0
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
jne 0x66443
movq 0x28(%rsp), %rdi
leaq 0x7d5cf(%rip), %rsi     # 0xe39fd
movb $0x0, %al
callq 0x19ce0
movq $0x0, 0x30(%rsp)
jmp 0x66597
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x18bd0
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x19170
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x8(%rsp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x664ab
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x18300
movq 0x28(%rsp), %rdi
leaq 0x7d57e(%rip), %rsi     # 0xe3a14
movb $0x0, %al
callq 0x19ce0
movq $0x0, 0x30(%rsp)
jmp 0x66597
cmpq $0x0, 0x18(%rsp)
je 0x664be
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
jmp 0x664cb
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, (%rsp)
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x665b0
cmpq $0x0, %rax
je 0x6651e
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x18300
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rdx
leaq 0x7d51e(%rip), %rsi     # 0xe3a2a
movb $0x0, %al
callq 0x19ce0
movq $0x0, 0x30(%rsp)
jmp 0x66597
movq 0x28(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x18f40
movq 0x28(%rsp), %rdi
movl $0xfff0b9d8, %esi       # imm = 0xFFF0B9D8
callq 0x18800
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x8(%rsp), %rdi
callq 0x665e0
cmpq $0x0, 0x10(%rsp)
je 0x66589
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x38(%rax)
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rdx
movl $0xfff0b9d8, %esi       # imm = 0xFFF0B9D8
callq 0x1a5a0
movq 0x28(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x191b0
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)