// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/ziplist.c
// Function: ziplistFind
unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
    int skipcnt = 0;
    unsigned char vencoding = 0;
    long long vll = 0;

    // 只要未到达列表末端，就一直迭代
    // T = O(N^2)
    while (p[0] != ZIP_END) {
        unsigned int prevlensize, encoding, lensize, len;
        unsigned char *q;

        ZIP_DECODE_PREVLENSIZE(p, prevlensize);
        ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
        q = p + prevlensize + lensize;

        if (skipcnt == 0) {

            /* Compare current entry with specified entry */
            // 对比字符串值
            // T = O(N)
            if (ZIP_IS_STR(encoding)) {
                if (len == vlen && memcmp(q, vstr, vlen) == 0) {
                    return p;
                }
            } else {
                /* Find out if the searched field can be encoded. Note that
                 * we do it only the first time, once done vencoding is set
                 * to non-zero and vll is set to the integer value. */
                // 因为传入值有可能被编码了，
                // 所以当第一次进行值对比时，程序会对传入值进行解码
                // 这个解码操作只会进行一次
                if (vencoding == 0) {
                    if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
                        /* If the entry can't be encoded we set it to
                         * UCHAR_MAX so that we don't retry again the next
                         * time. */
                        vencoding = UCHAR_MAX;
                    }
                    /* Must be non-zero by now */
                    assert(vencoding);
                }

                /* Compare current entry with specified entry, do it only
                 * if vencoding != UCHAR_MAX because if there is no encoding
                 * possible for the field it can't be a valid integer. */
                // 对比整数值
                if (vencoding != UCHAR_MAX) {
                    // T = O(1)
                    long long ll = zipLoadInteger(q, encoding);
                    if (ll == vll) {
                        return p;
                    }
                }
            }

            /* Reset skip count */
            skipcnt = skip;
        } else {
            /* Skip entry */
            skipcnt--;
        }

        /* Move to next entry */
        // 后移指针，指向后置节点
        p = q + len;
    }

    // 没有找到指定的节点
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
leaq 0x7(%rsp), %rax
movb $0x0, (%rax)
leaq 0x18(%rsp), %rax
movq $0x0, (%rax)
movl %edx, %eax
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
movzbl (%r12), %edx
cmpl $0xfe, %edx
je 0x2c7c2
movl $0x1, %eax
cmpl $0xff, %edx
jne 0x2c7c7
jmp 0x2c954
movl $0x5, %eax
leaq (%r12,%rax), %r15
movzbl (%r12,%rax), %r13d
movl %r13d, %r14d
andl $-0x40, %r14d
cmpl $0xc0, %r13d
cmovael %r13d, %r14d
movzbl %r14b, %edx
cmpl $0xbf, %r14d
ja 0x2c810
cmpb $-0x80, %r14b
je 0x2c86c
cmpl $0x40, %edx
je 0x2c855
testl %edx, %edx
jne 0x2c971
andl $0x3f, %r13d
movl $0x1, %eax
jmp 0x2c895
movl $0x1, %eax
cmpl $0xdf, %edx
jle 0x2c83d
cmpl $0xe0, %edx
je 0x2c87a
cmpl $0xf0, %edx
je 0x2c882
movl $0x1, %r13d
cmpl $0xfe, %edx
je 0x2c895
jmp 0x2c892
cmpl $0xc0, %edx
je 0x2c88a
cmpl $0xd0, %edx
jne 0x2c892
movl $0x4, %r13d
jmp 0x2c895
andl $0x3f, %r13d
shll $0x8, %r13d
movzbl 0x1(%r15), %eax
orl %eax, %r13d
movl $0x2, %eax
jmp 0x2c895
movl 0x1(%r15), %r13d
bswapl %r13d
movl $0x5, %eax
jmp 0x2c895
movl $0x8, %r13d
jmp 0x2c895
movl $0x3, %r13d
jmp 0x2c895
movl $0x2, %r13d
jmp 0x2c895
xorl %r13d, %r13d
addq %rax, %r15
testl %ecx, %ecx
je 0x2c8a5
decl %ecx
movl %ecx, %edx
jmp 0x2c93b
cmpl $0xbf, %r14d
ja 0x2c8da
cmpl %ebp, %r13d
jne 0x2c939
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x1f6a0
movl %ebx, %edx
testl %eax, %eax
jne 0x2c93b
xorl %ecx, %ecx
xorl %esi, %esi
movq %r12, 0x8(%rsp)
jmp 0x2c949
cmpb $0x0, 0x7(%rsp)
jne 0x2c907
movq 0x10(%rsp), %rdi
movl %ebp, %esi
leaq 0x18(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x2c6e7
testl %eax, %eax
jne 0x2c900
movb $-0x1, 0x7(%rsp)
cmpb $0x0, 0x7(%rsp)
je 0x2c986
cmpb $-0x1, 0x7(%rsp)
je 0x2c939
movq %r15, %rdi
movl %r14d, %esi
callq 0x2c3e1
xorl %ecx, %ecx
movl %ebx, %edx
movl $0x0, %esi
cmpq 0x18(%rsp), %rax
movq 0x8(%rsp), %rax
cmoveq %r12, %rax
movq %rax, 0x8(%rsp)
jne 0x2c93b
jmp 0x2c949
movl %ebx, %edx
movl %r13d, %eax
addq %rax, %r15
movb $0x1, %sil
movl %edx, %ecx
movq %r15, %r12
testb %sil, %sil
jne 0x2c7a3
jmp 0x2c95d
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c93b(%rip), %rdi     # 0x992b3
leaq 0x6a2d6(%rip), %rsi     # 0x96c55
movl $0x60e, %edx            # imm = 0x60E
jmp 0x2c999
leaq 0x6a2d2(%rip), %rdi     # 0x96c5f
leaq 0x6a2c1(%rip), %rsi     # 0x96c55
movl $0x629, %edx            # imm = 0x629
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0