// File: /workspace/github_c_makefile_star2_O2/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 0x8359a
movq %rax, %rbx
testl %ebp, %ebp
je 0x63a2d
leaq 0x95e98(%rip), %rax     # 0xf98a8
andl $0x0, 0xa0c(%rax)
andl $0x0, 0xa14(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x9d0(%rax)
callq 0x63e63
movq %rbx, %rdi
callq 0x63787
movq %rbx, %rdi
callq 0x6384f
leaq 0x953fc(%rip), %rdi     # 0xf8e40
xorl %esi, %esi
callq 0x2a155
leaq 0x95e56(%rip), %r15     # 0xf98a8
movq %rax, 0x9e0(%r15)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x78ddb
leaq 0x5eca1(%rip), %rsi     # 0xc270d
movq %rbx, %rdi
callq 0x78aec
leaq -0x6a0(%rip), %rsi      # 0x633db
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
pushq $-0x3
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5def6(%rip), %rsi     # 0xc198f
movq %rbx, %rdi
callq 0x78aec
leaq -0x6c5(%rip), %rsi      # 0x633e3
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5177b(%rip), %rsi     # 0xb523e
movq %rbx, %rdi
callq 0x78aec
leaq -0x485(%rip), %rsi      # 0x6364d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5730e(%rip), %rsi     # 0xbadfb
movq %rbx, %rdi
callq 0x78aec
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x572f4(%rip), %rsi     # 0xbae05
movq %rbx, %rdi
callq 0x78aec
movsd 0x4e89f(%rip), %xmm0    # 0xb23c0
movq %rbx, %rdi
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x572d7(%rip), %rsi     # 0xbae11
movq %rbx, %rdi
callq 0x78aec
movsd 0x56c0e(%rip), %xmm0    # 0xba758
movq %rbx, %rdi
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x572b9(%rip), %rsi     # 0xbae1c
movq %rbx, %rdi
callq 0x78aec
movsd 0x56bed(%rip), %xmm0    # 0xba760
movq %rbx, %rdi
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5729c(%rip), %rsi     # 0xbae28
movq %rbx, %rdi
callq 0x78aec
leaq -0x7b1(%rip), %rsi      # 0x633ea
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5727a(%rip), %rsi     # 0xbae30
movq %rbx, %rdi
callq 0x78aec
leaq -0x6fe(%rip), %rsi      # 0x634c7
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5725c(%rip), %rsi     # 0xbae3c
movq %rbx, %rdi
callq 0x78aec
leaq -0x716(%rip), %rsi      # 0x634d9
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5723f(%rip), %rsi     # 0xbae49
movq %rbx, %rdi
callq 0x78aec
leaq -0x72e(%rip), %rsi      # 0x634eb
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x57228(%rip), %rsi     # 0xbae5c
movq %rbx, %rdi
callq 0x78aec
leaq -0x66d(%rip), %rsi      # 0x635d6
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x57207(%rip), %rsi     # 0xbae65
movq %rbx, %rdi
callq 0x78aec
xorps %xmm0, %xmm0
movq %rbx, %rdi
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x571ed(%rip), %rsi     # 0xbae6f
movq %rbx, %rdi
callq 0x78aec
movq %rbx, %rdi
movsd 0x4e72b(%rip), %xmm0    # 0xb23c0
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x571cd(%rip), %rsi     # 0xbae78
movq %rbx, %rdi
callq 0x78aec
movq %rbx, %rdi
movsd 0x56a9a(%rip), %xmm0    # 0xba758
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x571af(%rip), %rsi     # 0xbae83
movq %rbx, %rdi
callq 0x78aec
movq %rbx, %rdi
movsd 0x56a79(%rip), %xmm0    # 0xba760
callq 0x78a66
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x5718f(%rip), %rsi     # 0xbae8c
movq %rbx, %rdi
callq 0x78aec
leaq -0x7dc(%rip), %rsi      # 0x63530
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x605e6(%rip), %rsi     # 0xc430d
movq %rbx, %rdi
callq 0x78aec
leaq -0x7e1(%rip), %rsi      # 0x63555
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x4b267(%rip), %rdx     # 0xaefb8
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x78efa
leaq 0x56e4d(%rip), %r14     # 0xbabb2
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x78d22
leaq 0x51376(%rip), %rsi     # 0xb50f2
movq %rbx, %rdi
callq 0x78aec
leaq 0x125(%rip), %rsi       # 0x63eb0
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
leaq 0x570f1(%rip), %rsi     # 0xbae97
movq %rbx, %rdi
callq 0x78aec
leaq 0x215(%rip), %rsi       # 0x63fca
movq %rbx, %rdi
xorl %edx, %edx
callq 0x78c17
movq %rbx, %rdi
movl %ebp, %esi
callq 0x78ed3
movq %rbx, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
movq %r14, %rdx
callq 0x78efa
leaq 0x570c2(%rip), %rsi     # 0xbaea2
leaq 0x57133(%rip), %rcx     # 0xbaf1a
pushq $0x77
popq %rdx
movq %rbx, %rdi
callq 0x83534
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x79168
leaq 0x57121(%rip), %rsi     # 0xbaf28
leaq 0x5721a(%rip), %rcx     # 0xbb028
movl $0xff, %edx
movq %rbx, %rdi
callq 0x83534
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x79168
cmpq $0x0, 0x9d0(%r15)
jne 0x63e49
pushq $-0x1
popq %rdi
callq 0x36144
movq %rax, 0x9d0(%r15)
orb $0x1, 0x99(%rax)
movq %rbx, %rdi
callq 0x63889
movq %rbx, 0x9c8(%r15)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq