// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/scripting.c
// Function: scriptingInit
void scriptingInit(int setup) {
    lua_State *lua = lua_open();

    if (setup) {
        server.lua_client = NULL;
        server.lua_caller = NULL;
        server.lua_timedout = 0;
        server.lua_always_replicate_commands = 0; /* Only DEBUG can change it.*/
        ldbInit();
    }

    luaLoadLibraries(lua);
    luaRemoveUnsupportedFunctions(lua);

    /* Initialize a dictionary we use to map SHAs to scripts.
     * This is useful for replication, as we need to replicate EVALSHA
     * as EVAL, so we need to remember the associated script. */
    server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);

    /* Register the redis commands table and fields */
    lua_newtable(lua);

    /* redis.call */
    lua_pushstring(lua,"call");
    lua_pushcfunction(lua,luaRedisCallCommand);
    lua_settable(lua,-3);

    /* redis.pcall */
    lua_pushstring(lua,"pcall");
    lua_pushcfunction(lua,luaRedisPCallCommand);
    lua_settable(lua,-3);

    /* redis.log and log levels. */
    lua_pushstring(lua,"log");
    lua_pushcfunction(lua,luaLogCommand);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_DEBUG");
    lua_pushnumber(lua,LL_DEBUG);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_VERBOSE");
    lua_pushnumber(lua,LL_VERBOSE);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_NOTICE");
    lua_pushnumber(lua,LL_NOTICE);
    lua_settable(lua,-3);

    lua_pushstring(lua,"LOG_WARNING");
    lua_pushnumber(lua,LL_WARNING);
    lua_settable(lua,-3);

    /* redis.sha1hex */
    lua_pushstring(lua, "sha1hex");
    lua_pushcfunction(lua, luaRedisSha1hexCommand);
    lua_settable(lua, -3);

    /* redis.error_reply and redis.status_reply */
    lua_pushstring(lua, "error_reply");
    lua_pushcfunction(lua, luaRedisErrorReplyCommand);
    lua_settable(lua, -3);
    lua_pushstring(lua, "status_reply");
    lua_pushcfunction(lua, luaRedisStatusReplyCommand);
    lua_settable(lua, -3);

    /* redis.replicate_commands */
    lua_pushstring(lua, "replicate_commands");
    lua_pushcfunction(lua, luaRedisReplicateCommandsCommand);
    lua_settable(lua, -3);

    /* redis.set_repl and associated flags. */
    lua_pushstring(lua,"set_repl");
    lua_pushcfunction(lua,luaRedisSetReplCommand);
    lua_settable(lua,-3);

    lua_pushstring(lua,"REPL_NONE");
    lua_pushnumber(lua,PROPAGATE_NONE);
    lua_settable(lua,-3);

    lua_pushstring(lua,"REPL_AOF");
    lua_pushnumber(lua,PROPAGATE_AOF);
    lua_settable(lua,-3);

    lua_pushstring(lua,"REPL_SLAVE");
    lua_pushnumber(lua,PROPAGATE_REPL);
    lua_settable(lua,-3);

    lua_pushstring(lua,"REPL_ALL");
    lua_pushnumber(lua,PROPAGATE_AOF|PROPAGATE_REPL);
    lua_settable(lua,-3);

    /* redis.breakpoint */
    lua_pushstring(lua,"breakpoint");
    lua_pushcfunction(lua,luaRedisBreakpointCommand);
    lua_settable(lua,-3);

    /* redis.debug */
    lua_pushstring(lua,"debug");
    lua_pushcfunction(lua,luaRedisDebugCommand);
    lua_settable(lua,-3);

    /* Finally set the table as 'redis' global var. */
    lua_setglobal(lua,"redis");

    /* Replace math.random and math.randomseed with our implementations. */
    lua_getglobal(lua,"math");

    lua_pushstring(lua,"random");
    lua_pushcfunction(lua,redis_math_random);
    lua_settable(lua,-3);

    lua_pushstring(lua,"randomseed");
    lua_pushcfunction(lua,redis_math_randomseed);
    lua_settable(lua,-3);

    lua_setglobal(lua,"math");

    /* Add a helper function that we use to sort the multi bulk output of non
     * deterministic commands, when containing 'false' elements. */
    {
        char *compare_func =    "function __redis__compare_helper(a,b)\n"
                                "  if a == false then a = '' end\n"
                                "  if b == false then b = '' end\n"
                                "  return a<b\n"
                                "end\n";
        luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def");
        lua_pcall(lua,0,0,0);
    }

    /* Add a helper function we use for pcall error reporting.
     * Note that when the error is in the C function we want to report the
     * information about the caller, that's what makes sense from the point
     * of view of the user debugging a script. */
    {
        char *errh_func =       "local dbg = debug\n"
                                "function __redis__err__handler(err)\n"
                                "  local i = dbg.getinfo(2,'nSl')\n"
                                "  if i and i.what == 'C' then\n"
                                "    i = dbg.getinfo(3,'nSl')\n"
                                "  end\n"
                                "  if i then\n"
                                "    return i.source .. ':' .. i.currentline .. ': ' .. err\n"
                                "  else\n"
                                "    return err\n"
                                "  end\n"
                                "end\n";
        luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
        lua_pcall(lua,0,0,0);
    }

    /* Create the (non connected) client that we use to execute Redis commands
     * inside the Lua interpreter.
     * Note: there is no need to create it again when this function is called
     * by scriptingReset(). */
    if (server.lua_client == NULL) {
        server.lua_client = createClient(-1);
        server.lua_client->flags |= CLIENT_LUA;
    }

    /* Lua beginners often don't use "local", this is likely to introduce
     * subtle bugs in their code. To prevent problems we protect accesses
     * to global variables. */
    scriptingEnableGlobalsProtection(lua);

    server.lua = lua;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %ebp
callq 0x920c6
movq %rax, %rbx
testl %ebp, %ebp
je 0x6ed9f
leaq 0x92b26(%rip), %rax     # 0x1018a8
xorl %ecx, %ecx
movl %ecx, 0xa0c(%rax)
movl %ecx, 0xa14(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x9d0(%rax)
callq 0x6f212
movq %rbx, %rdi
callq 0x6eac3
movq %rbx, %rdi
callq 0x6eb8b
leaq 0x9208a(%rip), %rdi     # 0x100e40
xorl %esi, %esi
callq 0x2e68c
leaq 0x92ae4(%rip), %r15     # 0x1018a8
movq %rax, 0x9e0(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x86818
leaq 0x62e6c(%rip), %rsi     # 0xd1c4a
movq %rbx, %rdi
callq 0x86521
leaq -0x718(%rip), %rsi      # 0x6e6d5
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x620c4(%rip), %rsi     # 0xd0ecf
movq %rbx, %rdi
callq 0x86521
leaq -0x73b(%rip), %rsi      # 0x6e6df
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x55052(%rip), %rsi     # 0xc3e8a
movq %rbx, %rdi
callq 0x86521
leaq -0x4c4(%rip), %rsi      # 0x6e983
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b166(%rip), %rsi     # 0xc9fcb
movq %rbx, %rdi
callq 0x86521
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b149(%rip), %rsi     # 0xc9fd5
movq %rbx, %rdi
callq 0x86521
movsd 0x51b84(%rip), %xmm0    # 0xc0a20
movq %rbx, %rdi
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b129(%rip), %rsi     # 0xc9fe1
movq %rbx, %rdi
callq 0x86521
movsd 0x5aa60(%rip), %xmm0    # 0xc9928
movq %rbx, %rdi
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b108(%rip), %rsi     # 0xc9fec
movq %rbx, %rdi
callq 0x86521
movsd 0x5aa3c(%rip), %xmm0    # 0xc9930
movq %rbx, %rdi
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b0e8(%rip), %rsi     # 0xc9ff8
movq %rbx, %rdi
callq 0x86521
leaq -0x839(%rip), %rsi      # 0x6e6e6
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b0c3(%rip), %rsi     # 0xca000
movq %rbx, %rdi
callq 0x86521
leaq -0x77f(%rip), %rsi      # 0x6e7cd
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b0a2(%rip), %rsi     # 0xca00c
movq %rbx, %rdi
callq 0x86521
leaq -0x798(%rip), %rsi      # 0x6e7e1
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b082(%rip), %rsi     # 0xca019
movq %rbx, %rdi
callq 0x86521
leaq -0x7b1(%rip), %rsi      # 0x6e7f5
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b068(%rip), %rsi     # 0xca02c
movq %rbx, %rdi
callq 0x86521
leaq -0x6c9(%rip), %rsi      # 0x6e90a
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b044(%rip), %rsi     # 0xca035
movq %rbx, %rdi
callq 0x86521
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b027(%rip), %rsi     # 0xca03f
movq %rbx, %rdi
callq 0x86521
movq %rbx, %rdi
movsd 0x519f5(%rip), %xmm0    # 0xc0a20
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5b004(%rip), %rsi     # 0xca048
movq %rbx, %rdi
callq 0x86521
movq %rbx, %rdi
movsd 0x5a8d1(%rip), %xmm0    # 0xc9928
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5afe3(%rip), %rsi     # 0xca053
movq %rbx, %rdi
callq 0x86521
movq %rbx, %rdi
movsd 0x5a8ad(%rip), %xmm0    # 0xc9930
callq 0x8649b
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5afc0(%rip), %rsi     # 0xca05c
movq %rbx, %rdi
callq 0x86521
leaq -0x876(%rip), %rsi      # 0x6e835
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x6476c(%rip), %rsi     # 0xd3835
movq %rbx, %rdi
callq 0x86521
leaq -0x87a(%rip), %rsi      # 0x6e85e
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x4dfd2(%rip), %rdx     # 0xbd0c8
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x8693b
leaq 0x5ac78(%rip), %r14     # 0xc9d82
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x8675f
leaq 0x54c1d(%rip), %rsi     # 0xc3d3e
movq %rbx, %rdi
callq 0x86521
leaq 0x135(%rip), %rsi       # 0x6f265
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
leaq 0x5af19(%rip), %rsi     # 0xca067
movq %rbx, %rdi
callq 0x86521
leaq 0x249(%rip), %rsi       # 0x6f3a6
movq %rbx, %rdi
xorl %edx, %edx
callq 0x8664f
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x86914
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x8693b
leaq 0x5aee7(%rip), %rsi     # 0xca072
leaq 0x5af58(%rip), %rcx     # 0xca0ea
movl $0x77, %edx
movq %rbx, %rdi
callq 0x9204c
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x86bad
leaq 0x5af44(%rip), %rsi     # 0xca0f8
leaq 0x5b03d(%rip), %rcx     # 0xca1f8
movl $0xff, %edx
movq %rbx, %rdi
callq 0x9204c
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x86bad
cmpq $0x0, 0x9d0(%r15)
jne 0x6f1f8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3bdd4
movq %rax, 0x9d0(%r15)
orb $0x1, 0x99(%rax)
movq %rbx, %rdi
callq 0x6ebc5
movq %rbx, 0x9c8(%r15)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq