// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x3ace8
jmp 0x3ad13
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0xa6159(%rip), %rdx     # 0xe0e50
leaq 0xa6749(%rip), %rcx     # 0xe1447
movl $0x216, %r8d            # imm = 0x216
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x3ad42
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x8, %eax
je 0x3ad42
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jle 0x3ad59
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
movq -0x20(%rbp), %rdi
callq 0x3a090
movq %rax, -0x28(%rbp)
cmpq $0x15, -0x28(%rbp)
ja 0x3ae32
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x399f0
cmpl $0x0, %eax
je 0x3ae32
leaq 0xd8b66(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x720(%rax)
jne 0x3adea
cmpq $0x0, -0x18(%rbp)
jl 0x3adea
cmpq $0x2710, -0x18(%rbp)    # imm = 0x2710
jge 0x3adea
movq -0x10(%rbp), %rdi
callq 0x3aaa0
movq -0x18(%rbp), %rcx
leaq 0xd938f(%rip), %rax     # 0x114150
movq 0x1b0(%rax,%rcx,8), %rdi
callq 0x3a3f0
movq -0x18(%rbp), %rcx
leaq 0xd9377(%rip), %rax     # 0x114150
movq 0x1b0(%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x3ae08
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2c160
movq -0x10(%rbp), %rax
movl (%rax), %ecx
andl $0xffffff0f, %ecx       # imm = 0xFFFFFF0F
orl $0x10, %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
cmpq $0x27, -0x28(%rbp)
ja 0x3ae8b
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x3ae57
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3a090
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x3a1c0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3aaa0
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3aee3
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x3aedb
movq -0x20(%rbp), %rdi
callq 0x3a0b0
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl $0xa, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jbe 0x3aedb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2c2f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)