// File: /workspace/github_c_makefile_star2_O2/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 0x2eacc
movq 0x158(%rbx), %rdi
movq %r14, %rsi
callq 0x21c36
xorl %r15d, %r15d
testl %eax, %eax
jne 0x47b49
leaq 0x7f4b5(%rip), %r13     # 0xc6fa8
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x21b64
testq %rax, %rax
je 0x47bbb
movq 0x8(%rax), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x20066
testq %rax, %rax
je 0x47bd6
movq %r12, %rdi
movq %rax, %rsi
callq 0x1ff28
pushq $0x1
popq %r15
cmpq $0x0, 0x28(%r12)
jne 0x47b49
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x21c36
testl %ebp, %ebp
je 0x47ba1
leaq 0x7fcac(%rip), %r12     # 0xc7800
movq 0x13a48(%r12), %rsi
movq %rbx, %rdi
callq 0x2c10d
movq 0x110(%r12), %rsi
movq %rbx, %rdi
callq 0x2c10d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2c84a
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %rcx
movq 0x48(%rax), %rsi
addq 0x28(%rax), %rsi
addq 0x28(%rcx), %rsi
movq %rbx, %rdi
callq 0x2c794
movq %r14, %rdi
callq 0x2ee1f
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45b34(%rip), %rdx     # 0x8d6f6
leaq 0x4944b(%rip), %rcx     # 0x91014
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa6, %r8d
jmp 0x47bef
leaq 0x473dd(%rip), %rdx     # 0x8efba
leaq 0x49430(%rip), %rcx     # 0x91014
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa9, %r8d
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0