// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/pubsub.c
// Function: pubsubUnsubscribeChannel
int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
    dictEntry *de;
    list *clients;
    listNode *ln;
    int retval = 0;

    /* Remove the channel from the client -> channels hash table */
    // 将频道 channel 从 client->channels 字典中移除
    incrRefCount(channel); /* channel may be just a pointer to the same object
                            we have in the hash tables. Protect it... */
    // 示意图：
    // before:
    // {
    //  'channel-x': NULL,
    //  'channel-y': NULL,
    //  'channel-z': NULL,
    // }
    // after unsubscribe channel-y ：
    // {
    //  'channel-x': NULL,
    //  'channel-z': NULL,
    // }
    if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {

        // channel 移除成功，表示客户端订阅了这个频道，执行以下代码

        retval = 1;
        /* Remove the client from the channel -> clients list hash table */
        // 从 channel->clients 的 clients 链表中，移除 client 
        // 示意图：
        // before:
        // {
        //  'channel-x' : [c1, c2, c3],
        // }
        // after c2 unsubscribe channel-x:
        // {
        //  'channel-x' : [c1, c3]
        // }
        de = dictFind(server.pubsub_channels,channel);
        redisAssertWithInfo(c,NULL,de != NULL);
        clients = dictGetVal(de);
        ln = listSearchKey(clients,c);
        redisAssertWithInfo(c,NULL,ln != NULL);
        listDelNode(clients,ln);

        // 如果移除 client 之后链表为空，那么删除这个 channel 键
        // 示意图：
        // before
        // {
        //  'channel-x' : [c1]
        // }
        // after c1 ubsubscribe channel-x
        // then also delete 'channel-x' key in dict
        // {
        //  // nothing here
        // }
        if (listLength(clients) == 0) {
            /* Free the list and associated hash entry at all if this was
             * the latest client, so that it will be possible to abuse
             * Redis PUBSUB creating millions of channels. */
            dictDelete(server.pubsub_channels,channel);
        }
    }

    /* Notify the client */
    // 回复客户端
    if (notify) {
        addReply(c,shared.mbulkhdr[3]);
        // "ubsubscribe" 字符串
        addReply(c,shared.unsubscribebulk);
        // 被退订的频道
        addReplyBulk(c,channel);
        // 退订频道之后客户端仍在订阅的频道和模式的总数
        addReplyLongLong(c,dictSize(c->pubsub_channels)+
                       listLength(c->pubsub_patterns));

    }

    decrRefCount(channel); /* it is finally safe to release it */

    return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x306f3
movq 0x158(%rbx), %rdi
movq %r14, %rsi
callq 0x22796
xorl %r15d, %r15d
testl %eax, %eax
jne 0x4b325
leaq 0x7ecdb(%rip), %r13     # 0xc9fa8
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x226ac
testq %rax, %rax
je 0x4b397
movq 0x8(%rax), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20729
testq %rax, %rax
je 0x4b3b2
movq %r12, %rdi
movq %rax, %rsi
callq 0x205b1
movl $0x1, %r15d
cmpq $0x0, 0x28(%r12)
jne 0x4b325
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x22796
testl %ebp, %ebp
je 0x4b37d
leaq 0x7f4d0(%rip), %r12     # 0xca800
movq 0x13a48(%r12), %rsi
movq %rbx, %rdi
callq 0x2d97c
movq 0x110(%r12), %rsi
movq %rbx, %rdi
callq 0x2d97c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e204
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %rcx
movq 0x48(%rax), %rsi
addq 0x28(%rax), %rsi
addq 0x28(%rcx), %rsi
movq %rbx, %rdi
callq 0x2e133
movq %r14, %rdi
callq 0x30b0c
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4c376(%rip), %rdx     # 0x97714
leaq 0x4fcaf(%rip), %rcx     # 0x9b054
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa6, %r8d
jmp 0x4b3cb
leaq 0x4dc3d(%rip), %rdx     # 0x98ff6
leaq 0x4fc94(%rip), %rcx     # 0x9b054
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa9, %r8d
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0