// File: /workspace/github_c_makefile_star2_O1/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 0x877e
movq %r14, %rdi
movq %r15, %rdx
callq 0x1b08b
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9f8
movq %r14, %rdi
testl %eax, %eax
je 0x8774
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9f8
cmpl $0x5, %eax
je 0x8869
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1b144
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a9d8
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl $0x1, %edx
callq 0x1b324
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a91c
jmp 0x8869
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7aa
movl 0x8(%r12), %esi
movq %r14, %rdi
callq 0x1a9d8
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9f8
testl %eax, %eax
je 0x8816
leaq 0x27ccb(%rip), %r12     # 0x3046e
leaq 0x27ccb(%rip), %r13     # 0x30475
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r12, %rdx
callq 0x1b08b
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %r13, %rdx
callq 0x1b08b
movq %r14, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a7ee
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdx
callq 0x1b08b
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9f8
movq %r14, %rdi
testl %eax, %eax
jne 0x8840
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a7aa
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1a9f8
testl %eax, %eax
jne 0x87aa
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7aa
leaq 0x27c50(%rip), %rsi     # 0x3047a
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x1ba66
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x5, %edx
callq 0x1be0e
movq %r14, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x1a7ee
movq %r14, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x1a7ee
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x1aca3
movq %rax, 0x10(%rbx)
movb $0x1, 0x8(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq