// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/object.c
// Function: tryObjectEncoding
robj *tryObjectEncoding(robj *o) {
    long value;

    sds s = o->ptr;
    size_t len;

    /* Make sure this is a string object, the only type we encode
     * in this function. Other types use encoded memory efficient
     * representations but are handled by the commands implementing
     * the type. */
    redisAssertWithInfo(NULL,o,o->type == REDIS_STRING);

    /* We try some specialized encoding only for objects that are
     * RAW or EMBSTR encoded, in other words objects that are still
     * in represented by an actually array of chars. */
    // 只在字符串的编码为 RAW 或者 EMBSTR 时尝试进行编码
    if (!sdsEncodedObject(o)) return o;

    /* It's not safe to encode shared objects: shared objects can be shared
     * everywhere in the "object space" of Redis and may end in places where
     * they are not handled. We handle them only as values in the keyspace. */
     // 不对共享对象进行编码
     if (o->refcount > 1) return o;

    /* Check if we can represent this string as a long integer.
     * Note that we are sure that a string larger than 21 chars is not
     * representable as a 32 nor 64 bit integer. */
    // 对字符串进行检查
    // 只对长度小于或等于 21 字节，并且可以被解释为整数的字符串进行编码
    len = sdslen(s);
    if (len <= 21 && string2l(s,len,&value)) {
        /* This object is encodable as a long. Try to use a shared object.
         * Note that we avoid using shared integers when maxmemory is used
         * because every object needs to have a private LRU field for the LRU
         * algorithm to work well. */
        if (server.maxmemory == 0 &&
            value >= 0 &&
            value < REDIS_SHARED_INTEGERS)
        {
            decrRefCount(o);
            incrRefCount(shared.integers[value]);
            return shared.integers[value];
        } else {
            if (o->encoding == REDIS_ENCODING_RAW) sdsfree(o->ptr);
            o->encoding = REDIS_ENCODING_INT;
            o->ptr = (void*) value;
            return o;
        }
    }

    /* If the string is small and is still RAW encoded,
     * try the EMBSTR encoding which is more efficient.
     * In this representation the object and the SDS string are allocated
     * in the same chunk of memory to save space and cache misses. */
    // 尝试将 RAW 编码的字符串编码为 EMBSTR 编码
    if (len <= REDIS_ENCODING_EMBSTR_SIZE_LIMIT) {
        robj *emb;

        if (o->encoding == REDIS_ENCODING_EMBSTR) return o;
        emb = createEmbeddedStringObject(s,sdslen(s));
        decrRefCount(o);
        return emb;
    }

    /* We can't encode the object...
     *
     * Do the last try, and at least optimize the SDS string inside
     * the string object to require little space, in case there
     * is more than 10% of free space at the end of the SDS string.
     *
     * We do that only for relatively large strings as this branch
     * is only entered if the length of the string is greater than
     * REDIS_ENCODING_EMBSTR_SIZE_LIMIT. */
    // 这个对象没办法进行编码，尝试从 SDS 中移除所有空余空间
    if (o->encoding == REDIS_ENCODING_RAW &&
        sdsavail(s) > len/10)
    {
        o->ptr = sdsRemoveFreeSpace(o->ptr);
    }

    /* Return the original object. */
    return o;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl (%rdi), %eax
testb $0xf, %al
jne 0x30d6b
movl %eax, %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x30c6a
cmpl $0x1, 0x4(%rbx)
jle 0x30c75
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq 0x8(%rbx), %r14
movslq -0x8(%r14), %rsi
cmpq $0x15, %rsi
ja 0x30cdc
movq %rsp, %rdx
movq %r14, %rdi
callq 0x30284
testl %eax, %eax
je 0x30d10
leaq 0x9930f(%rip), %rax     # 0xc9fa8
cmpq $0x0, 0x720(%rax)
sete %cl
movq (%rsp), %rax
cmpq $0x2710, %rax           # imm = 0x2710
setb %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x30d41
movq %rbx, %rdi
callq 0x30b0c
movq (%rsp), %rax
leaq 0x99b31(%rip), %rcx     # 0xca800
movq 0x1b0(%rcx,%rax,8), %rbx
incl 0x4(%rbx)
jmp 0x30c6a
cmpl $0x27, %esi
jbe 0x30d12
testb $-0x10, %al
jne 0x30c6a
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
movq %rsi, %rax
mulq %rcx
movslq -0x4(%r14), %rax
shrq $0x3, %rdx
cmpq %rax, %rdx
jae 0x30c6a
movq %r14, %rdi
callq 0x2859a
jmp 0x30d62
movl (%rbx), %eax
andl $0xf0, %eax
cmpl $0x80, %eax
je 0x30c6a
movslq -0x8(%r14), %rsi
movq %r14, %rdi
callq 0x30609
movq %rax, %r14
movq %rbx, %rdi
callq 0x30b0c
movq %r14, %rbx
jmp 0x30c6a
movl (%rbx), %ecx
testb $-0x10, %cl
jne 0x30d57
movq 0x8(%rbx), %rdi
callq 0x2850d
movl (%rbx), %ecx
movq (%rsp), %rax
andl $0xffffff0f, %ecx       # imm = 0xFFFFFF0F
orl $0x10, %ecx
movl %ecx, (%rbx)
movq %rax, 0x8(%rbx)
jmp 0x30c6a
leaq 0x66014(%rip), %rdx     # 0x96d86
leaq 0x665e6(%rip), %rcx     # 0x9735f
xorl %edi, %edi
movq %rbx, %rsi
movl $0x216, %r8d            # imm = 0x216
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0