// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/scripting.c
// Function: evalGenericCommand
void evalGenericCommand(client *c, int evalsha) {
    lua_State *lua = server.lua;
    char funcname[43];
    long long numkeys;
    int delhook = 0, err;

    /* When we replicate whole scripts, we want the same PRNG sequence at
     * every call so that our PRNG is not affected by external state. */
    redisSrand48(0);

    /* We set this flag to zero to remember that so far no random command
     * was called. This way we can allow the user to call commands like
     * SRANDMEMBER or RANDOMKEY from Lua scripts as far as no write command
     * is called (otherwise the replication and AOF would end with non
     * deterministic sequences).
     *
     * Thanks to this flag we'll raise an error every time a write command
     * is called after a random command was used. */
    server.lua_random_dirty = 0;
    server.lua_write_dirty = 0;
    server.lua_replicate_commands = server.lua_always_replicate_commands;
    server.lua_multi_emitted = 0;
    server.lua_repl = PROPAGATE_AOF|PROPAGATE_REPL;

    /* Get the number of arguments that are keys */
    if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != C_OK)
        return;
    if (numkeys > (c->argc - 3)) {
        addReplyError(c,"Number of keys can't be greater than number of args");
        return;
    } else if (numkeys < 0) {
        addReplyError(c,"Number of keys can't be negative");
        return;
    }

    /* We obtain the script SHA1, then check if this function is already
     * defined into the Lua state */
    funcname[0] = 'f';
    funcname[1] = '_';
    if (!evalsha) {
        /* Hash the code if this is an EVAL call */
        sha1hex(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
    } else {
        /* We already have the SHA if it is a EVALSHA */
        int j;
        char *sha = c->argv[1]->ptr;

        /* Convert to lowercase. We don't use tolower since the function
         * managed to always show up in the profiler output consuming
         * a non trivial amount of time. */
        for (j = 0; j < 40; j++)
            funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ?
                sha[j]+('a'-'A') : sha[j];
        funcname[42] = '\0';
    }

    /* Push the pcall error handler function on the stack. */
    lua_getglobal(lua, "__redis__err__handler");

    /* Try to lookup the Lua function */
    lua_getglobal(lua, funcname);
    if (lua_isnil(lua,-1)) {
        lua_pop(lua,1); /* remove the nil from the stack */
        /* Function not defined... let's define it if we have the
         * body of the function. If this is an EVALSHA call we can just
         * return an error. */
        if (evalsha) {
            lua_pop(lua,1); /* remove the error handler from the stack. */
            addReply(c, shared.noscripterr);
            return;
        }
        if (luaCreateFunction(c,lua,funcname,c->argv[1]) == C_ERR) {
            lua_pop(lua,1); /* remove the error handler from the stack. */
            /* The error is sent to the client by luaCreateFunction()
             * itself when it returns C_ERR. */
            return;
        }
        /* Now the following is guaranteed to return non nil */
        lua_getglobal(lua, funcname);
        serverAssert(!lua_isnil(lua,-1));
    }

    /* Populate the argv and keys table accordingly to the arguments that
     * EVAL received. */
    luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
    luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);

    /* Select the right DB in the context of the Lua client */
    selectDb(server.lua_client,c->db->id);

    /* Set a hook in order to be able to stop the script execution if it
     * is running for too much time.
     * We set the hook only if the time limit is enabled as the hook will
     * make the Lua script execution slower.
     *
     * If we are debugging, we set instead a "line" hook so that the
     * debugger is call-back at every line executed by the script. */
    server.lua_caller = c;
    server.lua_time_start = mstime();
    server.lua_kill = 0;
    if (server.lua_time_limit > 0 && server.masterhost == NULL &&
        ldb.active == 0)
    {
        lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
        delhook = 1;
    } else if (ldb.active) {
        lua_sethook(server.lua,luaLdbLineHook,LUA_MASKLINE|LUA_MASKCOUNT,100000);
        delhook = 1;
    }

    /* At this point whether this script was never seen before or if it was
     * already defined, we can call it. We have zero arguments and expect
     * a single return value. */
    err = lua_pcall(lua,0,1,-2);

    /* Perform some cleanup that we need to do both on error and success. */
    if (delhook) lua_sethook(lua,NULL,0,0); /* Disable hook */
    if (server.lua_timedout) {
        server.lua_timedout = 0;
        /* Restore the readable handler that was unregistered when the
         * script timeout was detected. */
        aeCreateFileEvent(server.el,c->fd,AE_READABLE,
                          readQueryFromClient,c);
    }
    server.lua_caller = NULL;

    /* Call the Lua garbage collector from time to time to avoid a
     * full cycle performed by Lua, which adds too latency.
     *
     * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
     * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
     * for every command uses too much CPU. */
    #define LUA_GC_CYCLE_PERIOD 50
    {
        static long gc_count = 0;

        gc_count++;
        if (gc_count == LUA_GC_CYCLE_PERIOD) {
            lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);
            gc_count = 0;
        }
    }

    if (err) {
        addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
            funcname, lua_tostring(lua,-1));
        lua_pop(lua,2); /* Consume the Lua reply and remove error handler. */
    } else {
        /* On success convert the Lua return value into Redis protocol, and
         * send it to * the client. */
        luaReplyToRedisReply(c,lua); /* Convert and consume the reply. */
        lua_pop(lua,1); /* Remove the error handler. */
    }

    /* If we are using single commands replication, emit EXEC if there
     * was at least a write. */
    if (server.lua_replicate_commands) {
        preventCommandPropagation(c);
        if (server.lua_multi_emitted) {
            robj *propargv[1];
            propargv[0] = createStringObject("EXEC",4);
            alsoPropagate(server.execCommand,c->db->id,propargv,1,
                PROPAGATE_AOF|PROPAGATE_REPL);
            decrRefCount(propargv[0]);
        }
    }

    /* EVALSHA should be propagated to Slave and AOF file as full EVAL, unless
     * we are sure that the script was already in the context of all the
     * attached slaves *and* the current AOF file if enabled.
     *
     * To do so we use a cache of SHA1s of scripts that we already propagated
     * as full EVAL, that's called the Replication Script Cache.
     *
     * For repliation, everytime a new slave attaches to the master, we need to
     * flush our cache of scripts that can be replicated as EVALSHA, while
     * for AOF we need to do so every time we rewrite the AOF file. */
    if (evalsha && !server.lua_replicate_commands) {
        if (!replicationScriptCacheExists(c->argv[1]->ptr)) {
            /* This script is not in our script cache, replicate it as
             * EVAL, then add it into the script cache, as from now on
             * slaves and AOF know about it. */
            robj *script = dictFetchValue(server.lua_scripts,c->argv[1]->ptr);

            replicationScriptCacheAdd(c->argv[1]->ptr);
            serverAssertWithInfo(c,NULL,script != NULL);
            rewriteClientCommandArgument(c,0,
                resetRefCount(createStringObject("EVAL",4)));
            rewriteClientCommandArgument(c,1,script);
            forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x955ac(%rip), %r13     # 0xf98a8
movq 0x9c8(%r13), %r14
xorl %edi, %edi
callq 0x670eb
andq $0x0, 0x9f8(%r13)
movl 0xa14(%r13), %eax
movl %eax, 0xa00(%r13)
movabsq $0x300000000, %rax      # imm = 0x300000000
movq %rax, 0xa04(%r13)
movq 0x40(%rbx), %rax
movq 0x10(%rax), %rsi
movq %rsp, %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x3ab03
testl %eax, %eax
jne 0x643bd
movq (%rsp), %rax
movslq 0x38(%rbx), %rcx
addq $-0x3, %rcx
cmpq %rcx, %rax
jle 0x64364
leaq 0x56e43(%rip), %rsi     # 0xbb1a5
jmp 0x643b5
testq %rax, %rax
js 0x643ae
movw $0x5f66, 0x10(%rsp)     # imm = 0x5F66
testl %ebp, %ebp
je 0x643cc
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
xorl %ecx, %ecx
cmpq $0x28, %rcx
je 0x643a7
movzbl (%rax,%rcx), %edx
leal -0x41(%rdx), %esi
leal 0x20(%rdx), %edi
cmpb $0x1a, %sil
movzbl %dil, %esi
cmovael %edx, %esi
movb %sil, 0x12(%rsp,%rcx)
incq %rcx
jmp 0x64382
movb $0x0, 0x3a(%rsp)
jmp 0x643f3
leaq 0x56e24(%rip), %rsi     # 0xbb1d9
movq %rbx, %rdi
callq 0x36b6f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12(%rsp), %r15
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %r12
movq %r12, %rdi
callq 0x63370
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x623fc
leaq 0x56e00(%rip), %rdx     # 0xbb1fa
movq %r14, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x78d22
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x78d22
pushq $-0x1
popq %rsi
movq %r14, %rdi
callq 0x786b8
testl %eax, %eax
je 0x644d8
movq 0x40(%rbx), %rdx
addq $0x18, %rdx
movl (%rsp), %ecx
leaq 0x56de5(%rip), %rsi     # 0xbb223
movq %r14, %rdi
callq 0x6400d
movq 0x40(%rbx), %rax
movq (%rsp), %rsi
leaq (%rax,%rsi,8), %rdx
addq $0x18, %rdx
movl 0x38(%rbx), %ecx
subl %esi, %ecx
addl $-0x3, %ecx
leaq 0x56dc3(%rip), %rsi     # 0xbb228
movq %r14, %rdi
callq 0x6400d
movq 0x9d0(%r13), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
callq 0x3b3b1
movq %rbx, 0x9d8(%r13)
callq 0x2b8ae
movq %rax, 0x9f0(%r13)
andl $0x0, 0xa10(%r13)
cmpq $0x0, 0x9e8(%r13)
setg %cl
cmpq $0x0, 0x828(%r13)
sete %dl
movl 0xa9dad(%rip), %eax     # 0x10e264
testl %eax, %eax
sete %sil
andb %dl, %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x64511
leaq -0x2ae(%rip), %rsi      # 0x64222
pushq $0x8
popq %rdx
movq %r14, %rdi
jmp 0x6452a
pushq $-0x2
popq %rsi
movq %r14, %rdi
callq 0x78478
testl %ebp, %ebp
je 0x64709
pushq $-0x2
popq %rsi
movq %r14, %rdi
callq 0x78478
leaq 0x95e13(%rip), %rax     # 0xfa310
movq 0x98(%rax), %rsi
movq %rbx, %rdi
callq 0x36987
jmp 0x643bd
testl %eax, %eax
je 0x64765
movq 0x9c8(%r13), %rdi
leaq 0x27c(%rip), %rsi       # 0x647a3
pushq $0xc
popq %rdx
movl $0x186a0, %ecx          # imm = 0x186A0
callq 0x79648
xorl %r12d, %r12d
pushq $0x1
popq %rdx
pushq $-0x2
popq %r15
movq %r14, %rdi
xorl %esi, %esi
movl %r15d, %ecx
callq 0x79168
movl %eax, 0xc(%rsp)
testb %r12b, %r12b
jne 0x64562
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x79648
cmpl $0x0, 0xa0c(%r13)
je 0x6458d
andl $0x0, 0xa0c(%r13)
movq 0x40(%r13), %rdi
movl 0x8(%rbx), %esi
leaq -0x2e251(%rip), %rcx    # 0x36331
pushq $0x1
popq %rdx
movq %rbx, %r8
callq 0x28ae8
andq $0x0, 0x9d8(%r13)
movq 0xaa03c(%rip), %rax     # 0x10e5d8
incq %rax
movq %rax, 0xaa032(%rip)     # 0x10e5d8
cmpq $0x32, %rax
jne 0x645c2
pushq $0x5
popq %rsi
pushq $0x32
popq %rdx
movq %r14, %rdi
callq 0x79319
andq $0x0, 0xaa016(%rip)     # 0x10e5d8
cmpl $0x0, 0xc(%rsp)
je 0x645f5
pushq $-0x1
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x788e1
leaq 0x56c50(%rip), %rsi     # 0xbb22d
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x36b96
pushq $-0x3
popq %r15
jmp 0x64600
movq %rbx, %rdi
movq %r14, %rsi
callq 0x628cd
movq %r14, %rdi
movl %r15d, %esi
callq 0x78478
cmpl $0x0, 0xa00(%r13)
je 0x64663
movq %rbx, %rdi
callq 0x2e22e
cmpl $0x0, 0xa04(%r13)
je 0x64663
leaq 0x56c26(%rip), %rdi     # 0xbb254
pushq $0x4
popq %rsi
callq 0x39e29
leaq 0x40(%rsp), %r14
movq %rax, (%r14)
movq 0x370(%r13), %rdi
movq 0x10(%rbx), %rax
movl 0x30(%rax), %esi
pushq $0x1
popq %rcx
pushq $0x3
popq %r8
movq %r14, %rdx
callq 0x2e189
movq (%r14), %rdi
callq 0x3a265
testl %ebp, %ebp
je 0x643bd
cmpl $0x0, 0xa00(%r13)
jne 0x643bd
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x3fcb0
testl %eax, %eax
jne 0x643bd
movq 0x9e0(%r13), %rdi
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x2a900
movq %rax, %r14
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x3fbf9
testq %r14, %r14
je 0x6477d
leaq 0x56b9a(%rip), %rdi     # 0xbb268
pushq $0x4
popq %rsi
callq 0x39e29
movq %rax, %rdi
callq 0x3a309
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x39072
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x39072
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x2e213
jmp 0x643bd
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rcx
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6408f
cmpl $-0x1, %eax
je 0x6476d
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0x78d22
pushq $-0x1
popq %rsi
movq %r14, %rdi
callq 0x786b8
testl %eax, %eax
jne 0x6442c
leaq 0x56abe(%rip), %rdi     # 0xbb210
leaq 0x56966(%rip), %rsi     # 0xbb0bf
movl $0x505, %edx            # imm = 0x505
callq 0x57ac8
jmp 0x6479b
movb $0x1, %r12b
jmp 0x64537
pushq $-0x2
popq %rsi
movq %r14, %rdi
callq 0x78478
jmp 0x643bd
leaq 0x56ad5(%rip), %rdx     # 0xbb259
leaq 0x56934(%rip), %rcx     # 0xbb0bf
movq %rbx, %rdi
xorl %esi, %esi
movl $0x56f, %r8d            # imm = 0x56F
callq 0x588a4
pushq $0x1
popq %rdi
callq 0x263c0