// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3a3f0
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x23c30
cmpl $0x0, %eax
jne 0x61b88
movl $0x1, -0x34(%rbp)
leaq 0xb1e3a(%rip), %rax     # 0x1138f8
movq 0x7a8(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x23aa0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x61adb
jmp 0x61b06
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x7fd2a(%rip), %rdx     # 0xe1814
leaq 0x835e3(%rip), %rcx     # 0xe50d4
movl $0xa6, %r8d
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x20480
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x61b2c
jmp 0x61b57
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x8153e(%rip), %rdx     # 0xe3079
leaq 0x83592(%rip), %rcx     # 0xe50d4
movl $0xa9, %r8d
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x20170
movq -0x28(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x61b86
leaq 0xb1d82(%rip), %rax     # 0x1138f8
movq 0x7a8(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x23c30
jmp 0x61b88
cmpl $0x0, -0x14(%rbp)
je 0x61bff
movq -0x8(%rbp), %rdi
leaq 0xb25b7(%rip), %rax     # 0x114150
movq 0x13a48(%rax), %rsi
callq 0x35a70
movq -0x8(%rbp), %rdi
leaq 0xb25a0(%rip), %rax     # 0x114150
movq 0x110(%rax), %rsi
callq 0x35a70
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x36620
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
movq 0x28(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
addq 0x48(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x160(%rax), %rax
addq 0x28(%rax), %rsi
callq 0x36480
movq -0x10(%rbp), %rdi
callq 0x3aaa0
movl -0x34(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)