// File: /workspace/github_c_makefile_star2_O1/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 0x3041b
movq 0x158(%rbx), %rdi
movq %r14, %rsi
callq 0x225d3
xorl %r15d, %r15d
testl %eax, %eax
jne 0x4b935
leaq 0x7e6cb(%rip), %r13     # 0xc9fa8
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x224dc
testq %rax, %rax
je 0x4b9a7
movq 0x8(%rax), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x206d5
testq %rax, %rax
je 0x4b9c2
movq %r12, %rdi
movq %rax, %rsi
callq 0x20544
movl $0x1, %r15d
cmpq $0x0, 0x28(%r12)
jne 0x4b935
movq 0x7a8(%r13), %rdi
movq %r14, %rsi
callq 0x225d3
testl %ebp, %ebp
je 0x4b98d
leaq 0x7eec0(%rip), %r12     # 0xca800
movq 0x13a48(%r12), %rsi
movq %rbx, %rdi
callq 0x2d6f0
movq 0x110(%r12), %rsi
movq %rbx, %rdi
callq 0x2d6f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2df74
movq 0x158(%rbx), %rax
movq 0x160(%rbx), %rcx
movq 0x48(%rax), %rsi
addq 0x28(%rax), %rsi
addq 0x28(%rcx), %rsi
movq %rbx, %rdi
callq 0x2dea3
movq %r14, %rdi
callq 0x30834
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4bcf4(%rip), %rdx     # 0x976a2
leaq 0x4f64f(%rip), %rcx     # 0x9b004
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa6, %r8d
jmp 0x4b9db
leaq 0x4d5e4(%rip), %rdx     # 0x98fad
leaq 0x4f634(%rip), %rcx     # 0x9b004
movq %rbx, %rdi
xorl %esi, %esi
movl $0xa9, %r8d
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0