// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/t_set.c
// Function: setTypeAdd
int setTypeAdd(robj *subject, robj *value) {
    long long llval;

    // 字典
    if (subject->encoding == REDIS_ENCODING_HT) {
        // 将 value 作为键， NULL 作为值，将元素添加到字典中
        if (dictAdd(subject->ptr,value,NULL) == DICT_OK) {
            incrRefCount(value);
            return 1;
        }

    // intset
    } else if (subject->encoding == REDIS_ENCODING_INTSET) {
        
        // 如果对象的值可以编码为整数的话，那么将对象的值添加到 intset 中
        if (isObjectRepresentableAsLongLong(value,&llval) == REDIS_OK) {
            uint8_t success = 0;
            subject->ptr = intsetAdd(subject->ptr,llval,&success);
            if (success) {
                /* Convert to regular set when the intset contains
                 * too many entries. */
                // 添加成功
                // 检查集合在添加新元素之后是否需要转换为字典
                if (intsetLen(subject->ptr) > server.set_max_intset_entries)
                    setTypeConvert(subject,REDIS_ENCODING_HT);
                return 1;
            }

        // 如果对象的值不能编码为整数，那么将集合从 intset 编码转换为 HT 编码
        // 然后再执行添加操作
        } else {
            /* Failed to get integer from object, convert to regular set. */
            setTypeConvert(subject,REDIS_ENCODING_HT);

            /* The set *was* an intset and this value is not integer
             * encodable, so dictAdd should always work. */
            redisAssertWithInfo(NULL,value,dictAdd(subject->ptr,value,NULL) == DICT_OK);
            incrRefCount(value);
            return 1;
        }

    // 未知编码
    } else {
        redisPanic("Unknown set encoding");
    }

    // 添加失败，元素已经存在
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x4ad85
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x236d0
cmpl $0x0, %eax
jne 0x4ad80
movq -0x18(%rbp), %rdi
callq 0x3a3f0
movl $0x1, -0x4(%rbp)
jmp 0x4aea5
jmp 0x4ae9e
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x6, %eax
jne 0x4ae7a
movq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x3ac00
cmpl $0x0, %eax
jne 0x4ae11
movb $0x0, -0x21(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
leaq -0x21(%rbp), %rdx
callq 0x67370
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpb $0x0, -0x21(%rbp)
je 0x4ae0f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x67ab0
movl %eax, %eax
leaq 0xc8b0c(%rip), %rcx     # 0x1138f8
cmpq 0x778(%rcx), %rax
jbe 0x4ae03
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4aeb0
movl $0x1, -0x4(%rbp)
jmp 0x4aea5
jmp 0x4ae78
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4aeb0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x236d0
cmpl $0x0, %eax
jne 0x4ae3b
jmp 0x4ae66
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edi
leaq 0x9833f(%rip), %rdx     # 0xe3189
leaq 0x98364(%rip), %rcx     # 0xe31b5
movl $0x61, %r8d
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x18(%rbp), %rdi
callq 0x3a3f0
movl $0x1, -0x4(%rbp)
jmp 0x4aea5
jmp 0x4ae9c
leaq 0x97ce3(%rip), %rdi     # 0xe2b64
leaq 0x9832d(%rip), %rsi     # 0xe31b5
movl $0x68, %edx
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x4ae9e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop