// File: /workspace/github_c_makefile_star2_O0/gplessis[P]dotdeb-redis/src/sort.c
// Function: lookupKeyByPattern
robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
    char *p, *f, *k;
    sds spat, ssub;
    robj *keyobj, *fieldobj = NULL, *o;
    int prefixlen, sublen, postfixlen, fieldlen;

    /* If the pattern is "#" return the substitution object itself in order
     * to implement the "SORT ... GET #" feature. */
    spat = pattern->ptr;
    if (spat[0] == '#' && spat[1] == '\0') {
        incrRefCount(subst);
        return subst;
    }

    /* The substitution object may be specially encoded. If so we create
     * a decoded object on the fly. Otherwise getDecodedObject will just
     * increment the ref count, that we'll decrement later. */
    subst = getDecodedObject(subst);
    ssub = subst->ptr;

    /* If we can't find '*' in the pattern we return NULL as to GET a
     * fixed key does not make sense. */
    p = strchr(spat,'*');
    if (!p) {
        decrRefCount(subst);
        return NULL;
    }

    /* Find out if we're dealing with a hash dereference. */
    if ((f = strstr(p+1, "->")) != NULL && *(f+2) != '\0') {
        fieldlen = sdslen(spat)-(f-spat)-2;
        fieldobj = createStringObject(f+2,fieldlen);
    } else {
        fieldlen = 0;
    }

    /* Perform the '*' substitution. */
    prefixlen = p-spat;
    sublen = sdslen(ssub);
    postfixlen = sdslen(spat)-(prefixlen+1)-(fieldlen ? fieldlen+2 : 0);
    keyobj = createStringObject(NULL,prefixlen+sublen+postfixlen);
    k = keyobj->ptr;
    memcpy(k,spat,prefixlen);
    memcpy(k+prefixlen,ssub,sublen);
    memcpy(k+prefixlen+sublen,p+1,postfixlen);
    decrRefCount(subst); /* Incremented by decodeObject() */

    /* Lookup substituted key */
    o = lookupKeyRead(db,keyobj);
    if (o == NULL) goto noobj;

    if (fieldobj) {
        if (o->type != OBJ_HASH) goto noobj;

        /* Retrieve value from hash by the field name. This operation
         * already increases the refcount of the returned object. */
        o = hashTypeGetObject(o, fieldobj);
    } else {
        if (o->type != OBJ_STRING) goto noobj;

        /* Every object that this function returns needs to have its refcount
         * increased. sortCommand decreases it again. */
        incrRefCount(o);
    }
    decrRefCount(keyobj);
    if (fieldobj) decrRefCount(fieldobj);
    return o;

noobj:
    decrRefCount(keyobj);
    if (fieldlen) decrRefCount(fieldobj);
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x79aea
movq -0x40(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x79aea
movq -0x20(%rbp), %rdi
callq 0x487b0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x79d0e
movq -0x20(%rbp), %rdi
callq 0x494f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movl $0x2a, %esi
callq 0x267f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x79b32
movq -0x20(%rbp), %rdi
callq 0x48f70
movq $0x0, -0x8(%rbp)
jmp 0x79d0e
movq -0x28(%rbp), %rdi
addq $0x1, %rdi
leaq 0xc7a77(%rip), %rsi     # 0x1415b8
callq 0x266c0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x79b92
movq -0x30(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x79b92
movq -0x40(%rbp), %rdi
callq 0x79d20
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rdx
subq %rdx, %rcx
subq %rcx, %rax
subq $0x2, %rax
movl %eax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
addq $0x2, %rdi
movslq -0x70(%rbp), %rsi
callq 0x486a0
movq %rax, -0x58(%rbp)
jmp 0x79b99
movl $0x0, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rdi
callq 0x79d20
movl %eax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
callq 0x79d20
movl -0x64(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x70(%rbp)
je 0x79bdd
movl -0x70(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x7c(%rbp)
jmp 0x79be4
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x79be4
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movl %eax, -0x6c(%rbp)
movl -0x64(%rbp), %eax
addl -0x68(%rbp), %eax
addl -0x6c(%rbp), %eax
movslq %eax, %rsi
xorl %eax, %eax
movl %eax, %edi
callq 0x486a0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x64(%rbp), %rdx
callq 0x26c60
movq -0x38(%rbp), %rdi
movslq -0x64(%rbp), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
movslq -0x68(%rbp), %rdx
callq 0x26c60
movq -0x38(%rbp), %rdi
movslq -0x64(%rbp), %rax
addq %rax, %rdi
movslq -0x68(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
addq $0x1, %rsi
movslq -0x6c(%rbp), %rdx
callq 0x26c60
movq -0x20(%rbp), %rdi
callq 0x48f70
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x4a6c0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x79c88
jmp 0x79cee
cmpq $0x0, -0x58(%rbp)
je 0x79cb2
movq -0x60(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x79c9f
jmp 0x79cee
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x666c0
movq %rax, -0x60(%rbp)
jmp 0x79ccb
movq -0x60(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x79cc2
jmp 0x79cee
movq -0x60(%rbp), %rdi
callq 0x487b0
movq -0x50(%rbp), %rdi
callq 0x48f70
cmpq $0x0, -0x58(%rbp)
je 0x79ce4
movq -0x58(%rbp), %rdi
callq 0x48f70
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x79d0e
movq -0x50(%rbp), %rdi
callq 0x48f70
cmpl $0x0, -0x70(%rbp)
je 0x79d06
movq -0x58(%rbp), %rdi
callq 0x48f70
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)