// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/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. */
	// 如果模式是 # ，那么直接返回 subst
    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
    subst = getDecodedObject(subst);
    // 指向 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. */
	// 如果模式不是 "#" ，并且模式中不带 '*' ，那么直接返回 NULL
    // 因为一直返回固定的键是没有意义的
    p = strchr(spat,'*');
    if (!p) {
        decrRefCount(subst);
        return NULL;
    }

    /* Find out if we're dealing with a hash dereference. */
	// 检查指定的是字符串键还是 Hash 键
    if ((f = strstr(p+1, "->")) != NULL && *(f+2) != '\0') {
		// Hash 键
        // 域的长度
        fieldlen = sdslen(spat)-(f-spat)-2;
        // 域的对象
        fieldobj = createStringObject(f+2,fieldlen);
    } else {
		// 字符串键，没有域
        fieldlen = 0;
    }

    /* Perform the '*' substitution. */
	// 根据模式，进行替换
	// 比如说， subst 为 www ，模式为 nono_happ_*
	// 那么替换结果就是 nono_happ_www
    // 又比如说， subst 为 peter ，模式为 *-info->age
    // 那么替换结果就是 peter-info->age
    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 */
	// 查找替换 key
    o = lookupKeyRead(db,keyobj);
    if (o == NULL) goto noobj;

    // 这是一个 Hash 键
    if (fieldobj) {
        if (o->type != REDIS_HASH) goto noobj;

        /* Retrieve value from hash by the field name. This operation
         * already increases the refcount of the returned object. */
		// 从 Hash 键的指定域中获取值
        o = hashTypeGetObject(o, fieldobj);

    // 这是一个字符串键
    } else {
        if (o->type != REDIS_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 0x4e68e
cmpb $0x0, 0x1(%r12)
je 0x4e7d5
movq %r14, %rdi
callq 0x30ac3
movq %rax, %r14
movq 0x8(%rax), %rbp
movq %r12, %rdi
movl $0x2a, %esi
callq 0x1f790
testq %rax, %rax
je 0x4e7d0
movq %rax, %r15
leaq 0x1(%rax), %rdi
leaq 0x4d3c0(%rip), %rsi     # 0x9ba81
movq %rdi, 0x20(%rsp)
callq 0x1f660
testq %rax, %rax
movq %rbx, 0x30(%rsp)
movq %r14, 0x28(%rsp)
je 0x4e706
cmpb $0x0, 0x2(%rax)
je 0x4e706
movq %rax, %rdi
addq $0x2, %rdi
movl -0x8(%r12), %ecx
movl %r12d, %edx
subl %eax, %edx
leal (%rdx,%rcx), %ebx
addl $-0x2, %ebx
movslq %ebx, %rsi
callq 0x303b7
movq %rax, 0x8(%rsp)
jmp 0x4e711
movq $0x0, 0x8(%rsp)
xorl %ebx, %ebx
subq %r12, %r15
movslq -0x8(%rbp), %rdx
movq %rdx, 0x18(%rsp)
leal 0x2(%rbx), %eax
testl %ebx, %ebx
movq %rbx, 0x10(%rsp)
cmovel %ebx, %eax
movl -0x8(%r12), %ecx
movq %r15, %r14
notq %r14
subq %rax, %r14
addq %rcx, %r14
leal (%r15,%rdx), %eax
addl %r14d, %eax
movslq %eax, %rsi
xorl %edi, %edi
callq 0x303b7
movq %rax, %rbx
movq 0x8(%rax), %r13
movslq %r15d, %r15
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1fbd0
addq %r15, %r13
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
callq 0x1fbd0
addq %r15, %r13
movslq %r14d, %rdx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x1fbd0
movq 0x28(%rsp), %rdi
callq 0x30834
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x3136a
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x4e7e3
movq %rax, %r14
movl (%rax), %eax
andl $0xf, %eax
testq %r15, %r15
je 0x4e7df
cmpl $0x4, %eax
jne 0x4e7e3
movq %r14, %rdi
movq %r15, %rsi
callq 0x42479
movq %rax, %r14
jmp 0x4e817
movq %r14, %rdi
jmp 0x4e7f5
movq %r14, %rdi
callq 0x3041b
jmp 0x4e7fd
testl %eax, %eax
je 0x4e80f
movq %rbx, %rdi
callq 0x30834
cmpl $0x0, 0x10(%rsp)
je 0x4e7fa
movq %r15, %rdi
callq 0x30834
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 0x3041b
movq %rbx, %rdi
callq 0x30834
testq %r15, %r15
je 0x4e7fd
movq %r15, %rdi
callq 0x30834
jmp 0x4e7fd