// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
leaq 0xed3cf(%rip), %rax     # 0x1781d8
movq 0x9c8(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x5c(%rbp)
xorl %edi, %edi
callq 0x8f240
leaq 0xed3af(%rip), %rax     # 0x1781d8
movl $0x0, 0x9fc(%rax)
leaq 0xed39e(%rip), %rax     # 0x1781d8
movl $0x0, 0x9f8(%rax)
leaq 0xed38d(%rip), %rax     # 0x1781d8
movl 0xa14(%rax), %ecx
leaq 0xed380(%rip), %rax     # 0x1781d8
movl %ecx, 0xa00(%rax)
leaq 0xed373(%rip), %rax     # 0x1781d8
movl $0x0, 0xa04(%rax)
leaq 0xed362(%rip), %rax     # 0x1781d8
movl $0x3, 0xa08(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x58(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x49ea0
cmpl $0x0, %eax
je 0x8aea7
jmp 0x8b4c9
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x38(%rcx), %ecx
subl $0x3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jle 0x8aed2
movq -0x8(%rbp), %rdi
leaq 0xb93cd(%rip), %rsi     # 0x144295
callq 0x42f90
jmp 0x8b4c9
cmpq $0x0, -0x58(%rbp)
jge 0x8aeee
movq -0x8(%rbp), %rdi
leaq 0xb93e5(%rip), %rsi     # 0x1442c9
callq 0x42f90
jmp 0x8b4c9
jmp 0x8aef0
movb $0x66, -0x50(%rbp)
movb $0x5f, -0x4f(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x8af54
leaq -0x50(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x895c0
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq %rax, %rdx
callq 0x87f70
jmp 0x8afe3
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x28, -0x64(%rbp)
jge 0x8afdf
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x41, %eax
jl 0x8afae
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5a, %eax
jg 0x8afae
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsbl (%rax,%rcx), %eax
addl $0x20, %eax
movl %eax, -0x94(%rbp)
jmp 0x8afc0
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movb %al, %cl
movl -0x64(%rbp), %eax
addl $0x2, %eax
cltq
movb %cl, -0x50(%rbp,%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x8af6f
movb $0x0, -0x26(%rbp)
movq -0x18(%rbp), %rdi
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
leaq 0xb92f7(%rip), %rdx     # 0x1442ea
callq 0xad850
movq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0xad850
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xacaa0
cmpl $0x0, %eax
jne 0x8b0df
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0xac570
cmpl $0x0, -0xc(%rbp)
je 0x8b05f
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0xac570
movq -0x8(%rbp), %rdi
leaq 0xedbf2(%rip), %rax     # 0x178c40
movq 0x98(%rax), %rsi
callq 0x42cc0
jmp 0x8b4c9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rcx
callq 0x8aae0
cmpl $-0x1, %eax
jne 0x8b094
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0xac570
jmp 0x8b4c9
movq -0x18(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movl $0xffffd8ee, %esi       # imm = 0xFFFFD8EE
callq 0xad850
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xacaa0
cmpl $0x0, %eax
je 0x8b0bb
jmp 0x8b0dd
leaq 0xb923e(%rip), %rdi     # 0x144300
leaq 0xb90e6(%rip), %rsi     # 0x1441af
movl $0x505, %edx            # imm = 0x505
callq 0x76ce0
movl $0x1, %edi
callq 0x263c0
jmp 0x8b0df
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdx
addq $0x18, %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0xb9217(%rip), %rsi     # 0x144313
callq 0x8aa30
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdx
addq $0x18, %rdx
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq -0x8(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x3, %eax
cltq
subq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0xb91e3(%rip), %rsi     # 0x144318
callq 0x8aa30
leaq 0xed097(%rip), %rax     # 0x1781d8
movq 0x9d0(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %esi
callq 0x4aef0
movq -0x8(%rbp), %rcx
leaq 0xed075(%rip), %rax     # 0x1781d8
movq %rcx, 0x9d8(%rax)
callq 0x2ff70
movq %rax, %rcx
leaq 0xed05f(%rip), %rax     # 0x1781d8
movq %rcx, 0x9f0(%rax)
leaq 0xed051(%rip), %rax     # 0x1781d8
movl $0x0, 0xa10(%rax)
leaq 0xed040(%rip), %rax     # 0x1781d8
cmpq $0x0, 0x9e8(%rax)
jle 0x8b1df
leaq 0xed02f(%rip), %rax     # 0x1781d8
cmpq $0x0, 0x828(%rax)
jne 0x8b1df
cmpl $0x0, 0x1019da(%rip)    # 0x18cb94
jne 0x8b1df
movq -0x18(%rbp), %rdi
leaq -0x4c7(%rip), %rsi      # 0x8ad00
movl $0x8, %edx
movl $0x186a0, %ecx          # imm = 0x186A0
callq 0xaeb70
movl $0x1, -0x5c(%rbp)
jmp 0x8b215
cmpl $0x0, 0x1019ae(%rip)    # 0x18cb94
je 0x8b213
leaq 0xecfe9(%rip), %rax     # 0x1781d8
movq 0x9c8(%rax), %rdi
leaq 0x2e3(%rip), %rsi       # 0x8b4e0
movl $0xc, %edx
movl $0x186a0, %ecx          # imm = 0x186A0
callq 0xaeb70
movl $0x1, -0x5c(%rbp)
jmp 0x8b215
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0xae1c0
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x8b244
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0xaeb70
leaq 0xecf8d(%rip), %rax     # 0x1781d8
cmpl $0x0, 0xa0c(%rax)
je 0x8b28c
leaq 0xecf7d(%rip), %rax     # 0x1781d8
movl $0x0, 0xa0c(%rax)
leaq 0xecf6c(%rip), %rax     # 0x1781d8
movq 0x40(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x8(%rbp), %r8
movl $0x1, %edx
leaq -0x49147(%rip), %rcx    # 0x42140
callq 0x2a8f0
leaq 0xecf45(%rip), %rax     # 0x1781d8
movq $0x0, 0x9d8(%rax)
movq 0x101c63(%rip), %rax    # 0x18cf08
addq $0x1, %rax
movq %rax, 0x101c58(%rip)    # 0x18cf08
cmpq $0x32, 0x101c50(%rip)   # 0x18cf08
jne 0x8b2d8
movq -0x18(%rbp), %rdi
movl $0x5, %esi
movl $0x32, %edx
callq 0xae500
movq $0x0, 0x101c30(%rip) # 0x18cf08
cmpl $0x0, -0x60(%rbp)
je 0x8b335
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edx
callq 0xacf60
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rdx
movq %rax, %rcx
leaq 0xb8fff(%rip), %rsi     # 0x14431d
movb $0x0, %al
callq 0x42fd0
movq -0x18(%rbp), %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0xac570
jmp 0x8b350
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x88680
movq -0x18(%rbp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0xac570
leaq 0xece81(%rip), %rax     # 0x1781d8
cmpl $0x0, 0xa00(%rax)
je 0x8b3c6
movq -0x8(%rbp), %rdi
callq 0x33cf0
leaq 0xece68(%rip), %rax     # 0x1781d8
cmpl $0x0, 0xa04(%rax)
je 0x8b3c4
leaq 0xb8fc4(%rip), %rdi     # 0x144344
movl $0x4, %esi
callq 0x486a0
movq %rax, -0x78(%rbp)
leaq 0xece43(%rip), %rax     # 0x1781d8
movq 0x370(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %esi
leaq -0x78(%rbp), %rdx
movl $0x1, %ecx
movl $0x3, %r8d
callq 0x33bf0
movq -0x78(%rbp), %rdi
callq 0x48f70
jmp 0x8b3c6
cmpl $0x0, -0xc(%rbp)
je 0x8b4c9
leaq 0xece01(%rip), %rax     # 0x1781d8
cmpl $0x0, 0xa00(%rax)
jne 0x8b4c9
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x52080
cmpl $0x0, %eax
jne 0x8b4c7
leaq 0xecdcf(%rip), %rax     # 0x1781d8
movq 0x9e0(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x2e280
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x51f60
cmpq $0x0, -0x80(%rbp)
je 0x8b447
jmp 0x8b472
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0xb8ef3(%rip), %rdx     # 0x144349
leaq 0xb8d52(%rip), %rcx     # 0x1441af
movl $0x56f, %r8d            # imm = 0x56F
callq 0x78360
movl $0x1, %edi
callq 0x263c0
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq 0xb8ed4(%rip), %rdi     # 0x144358
movl $0x4, %esi
callq 0x486a0
movq %rax, %rdi
callq 0x49080
movq -0xb0(%rbp), %rdi
movq %rax, %rdx
xorl %esi, %esi
callq 0x46ad0
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rdx
movl $0x1, %esi
callq 0x46ad0
movq -0x8(%rbp), %rdi
movl $0x3, %esi
callq 0x33ca0
jmp 0x8b4c9
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)