// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %r12
cmpb $0x23, (%r12)
jne 0x59714
cmpb $0x0, 0x1(%r12)
je 0x5986f
movq %r14, %rdi
callq 0x3a57d
movq %rax, %r14
movq 0x8(%rax), %r13
pushq $0x2a
popq %rsi
movq %r12, %rdi
callq 0x267f0
testq %rax, %rax
je 0x5986a
movq %rax, %r15
movq %rbx, 0x30(%rsp)
leaq 0x1(%rax), %rdi
leaq 0x5ed7e(%rip), %rsi     # 0xb84c8
movq %rdi, 0x20(%rsp)
callq 0x266b0
testq %rax, %rax
movq %r14, 0x28(%rsp)
je 0x59793
movq %rax, %rbx
cmpb $0x0, 0x2(%rax)
je 0x59793
movq %rbx, %rbp
addq $0x2, %rbp
movq %r12, %rdi
callq 0x598c8
movl %r12d, %ecx
subl %ebx, %ecx
leal (%rcx,%rax), %ebx
addl $-0x2, %ebx
movslq %ebx, %rsi
movq %rbp, %rdi
callq 0x39e29
movq %rax, 0x8(%rsp)
jmp 0x5979e
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
subq %r12, %r15
movq %r13, %rdi
callq 0x598c8
movq %rax, %rbp
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x598c8
leal 0x2(%rbx), %ecx
testl %ebx, %ebx
movq %rbx, 0x10(%rsp)
cmovel %ebx, %ecx
movq %r15, %r14
notq %r14
addq %rax, %r14
subq %rcx, %r14
leal (%r15,%rbp), %eax
addl %r14d, %eax
movslq %eax, %rsi
xorl %edi, %edi
callq 0x39e29
movq %rax, %rbx
movq %r13, %rbp
movq 0x8(%rax), %r13
movslq %r15d, %r15
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x26c50
addq %r15, %r13
movslq 0x18(%rsp), %r15
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x26c50
addq %r15, %r13
movslq %r14d, %rdx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x26c50
movq 0x28(%rsp), %rdi
callq 0x3a265
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x3aebb
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x5987d
movq %rax, %r14
movl (%rax), %eax
andl $0xf, %eax
testq %r15, %r15
je 0x59879
cmpl $0x4, %eax
jne 0x5987d
movq %r14, %rdi
movq %r15, %rsi
callq 0x4c848
movq %rax, %r14
jmp 0x598b1
movq %r14, %rdi
jmp 0x5988f
movq %r14, %rdi
callq 0x39e74
jmp 0x59897
testl %eax, %eax
je 0x598a9
movq %rbx, %rdi
callq 0x3a265
cmpl $0x0, 0x10(%rsp)
je 0x59894
movq %r15, %rdi
callq 0x3a265
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x39e74
movq %rbx, %rdi
callq 0x3a265
testq %r15, %r15
je 0x59897
movq %r15, %rdi
callq 0x3a265
jmp 0x59897