// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/LuaInterpolate.cpp
// Function: t2::LuaEnvLookupScope::LuaEnvLookupScope(t2::LuaEnvLookup&, char const*, unsigned long)
explicit LuaEnvLookupScope(LuaEnvLookup& lookup, const char* key, size_t len)
  {
    m_LuaState = lookup.m_LuaState;
    m_Valid    = false;

    lua_State* L = lookup.m_LuaState;

    // If we have a table of per-call variables, look there first.
    if (int var_table_idx = lookup.m_VarIndex)
    {
      lua_getfield(L, var_table_idx, key);
      if (!lua_isnil(L, -1))
      {
        if (LUA_TTABLE != lua_type(L, -1))
        {
          lua_newtable(L);
          lua_pushvalue(L, -2);
          lua_rawseti(L, -2, 1);
          lua_replace(L, -2);
        }
        m_Count = lua_objlen(L, -1);
        m_Valid = true;
        return;
      }
      else
      {
        lua_pop(L, 1);
      }
    }

    // Next, walk through the environment tables. 
    lua_pushvalue(L, lookup.m_EnvIndex);
    while (!lua_isnil(L, -1))
    {
      lua_getfield(L, -1, "parent");
      lua_getfield(L, -2, "vars");
      // remove env table index from stack, replace it with its parent table
      lua_remove(L, -3);

      lua_getfield(L, -1, key);
      if (!lua_isnil(L, -1))
      {
        luaL_checktype(L, -1, LUA_TTABLE);
        lua_remove(L, -3);
        lua_remove(L, -2);
        m_Count = lua_objlen(L, -1);
        m_Valid = true;
        return;
      }

      // Pop nil value, and vars table. Leaving us with next table to try.
      lua_pop(L, 2);
    }
    // Pop nil table.
    lua_pop(L, 1);

    luaL_error(L, "No key '%s' present in environment", key);
  }

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq (%rsi), %r14
movq %r14, (%rdi)
movb $0x0, 0x8(%rdi)
movl 0xc(%rsi), %esi
testl %esi, %esi
je 0x86e4
movq %r14, %rdi
movq %r15, %rdx
callq 0x1aa34
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a39a
movq %r14, %rdi
testl %eax, %eax
je 0x86da
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a39a
cmpl $0x5, %eax
je 0x87cf
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1aaed
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a37a
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl $0x1, %edx
callq 0x1acd4
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a2bb
jmp 0x87cf
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a039
movl 0x8(%r12), %esi
movq %r14, %rdi
callq 0x1a37a
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a39a
testl %eax, %eax
je 0x877c
leaq 0x27d65(%rip), %r12     # 0x3046e
leaq 0x27d65(%rip), %r13     # 0x30475
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r12, %rdx
callq 0x1aa34
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %r13, %rdx
callq 0x1aa34
movq %r14, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a190
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdx
callq 0x1aa34
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a39a
movq %r14, %rdi
testl %eax, %eax
jne 0x87a6
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a039
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a39a
testl %eax, %eax
jne 0x8710
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a039
leaq 0x27cea(%rip), %rsi     # 0x3047a
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1b417
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x5, %edx
callq 0x1b7bf
movq %r14, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a190
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a190
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a645
movq %rax, 0x10(%rbx)
movb $0x1, 0x8(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq