// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/networking.c
// Function: freeClient
void freeClient(redisClient *c) {
    listNode *ln;

    /* If this is marked as current client unset it */
    if (server.current_client == c) server.current_client = NULL;

    /* If it is our master that's beging disconnected we should make sure
     * to cache the state to try a partial resynchronization later.
     *
     * Note that before doing this we make sure that the client is not in
     * some unexpected state, by checking its flags. */
    if (server.master && c->flags & REDIS_MASTER) {
        redisLog(REDIS_WARNING,"Connection with master lost.");
        if (!(c->flags & (REDIS_CLOSE_AFTER_REPLY|
                          REDIS_CLOSE_ASAP|
                          REDIS_BLOCKED|
                          REDIS_UNBLOCKED)))
        {
            replicationCacheMaster(c);
            return;
        }
    }

    /* Log link disconnection with slave */
    if ((c->flags & REDIS_SLAVE) && !(c->flags & REDIS_MONITOR)) {
        char ip[REDIS_IP_STR_LEN];

        if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) {
            redisLog(REDIS_WARNING,"Connection with slave %s:%d lost.",
                ip, c->slave_listening_port);
        }
    }

    /* Free the query buffer */
    sdsfree(c->querybuf);
    c->querybuf = NULL;

    /* Deallocate structures used to block on blocking ops. */
    if (c->flags & REDIS_BLOCKED) unblockClient(c);
    dictRelease(c->bpop.keys);

    /* UNWATCH all the keys */
    // 清空 WATCH 信息
    unwatchAllKeys(c);
    listRelease(c->watched_keys);

    /* Unsubscribe from all the pubsub channels */
    // 退订所有频道和模式
    pubsubUnsubscribeAllChannels(c,0);
    pubsubUnsubscribeAllPatterns(c,0);
    dictRelease(c->pubsub_channels);
    listRelease(c->pubsub_patterns);

    /* Close socket, unregister events, and remove list of replies and
     * accumulated arguments. */
    // 关闭套接字，并从事件处理器中删除该套接字的事件
    if (c->fd != -1) {
        aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
        aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
        close(c->fd);
    }

    // 清空回复缓冲区
    listRelease(c->reply);

    // 清空命令参数
    freeClientArgv(c);

    /* Remove from the list of clients */
    // 从服务器的客户端链表中删除自身
    if (c->fd != -1) {
        ln = listSearchKey(server.clients,c);
        redisAssert(ln != NULL);
        listDelNode(server.clients,ln);
    }

    /* When client was just unblocked because of a blocking operation,
     * remove it from the list of unblocked clients. */
    // 删除客户端的阻塞信息
    if (c->flags & REDIS_UNBLOCKED) {
        ln = listSearchKey(server.unblocked_clients,c);
        redisAssert(ln != NULL);
        listDelNode(server.unblocked_clients,ln);
    }

    /* Master/slave cleanup Case 1:
     * we lost the connection with a slave. */
    if (c->flags & REDIS_SLAVE) {
        if (c->replstate == REDIS_REPL_SEND_BULK) {
            if (c->repldbfd != -1) close(c->repldbfd);
            if (c->replpreamble) sdsfree(c->replpreamble);
        }
        list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
        ln = listSearchKey(l,c);
        redisAssert(ln != NULL);
        listDelNode(l,ln);
        /* We need to remember the time when we started to have zero
         * attached slaves, as after some time we'll free the replication
         * backlog. */
        if (c->flags & REDIS_SLAVE && listLength(server.slaves) == 0)
            server.repl_no_slaves_since = server.unixtime;
        refreshGoodSlavesCount();
    }

    /* Master/slave cleanup Case 2:
     * we lost the connection with the master. */
    if (c->flags & REDIS_MASTER) replicationHandleMasterDisconnection();

    /* If this client was scheduled for async freeing we need to remove it
     * from the queue. */
    if (c->flags & REDIS_CLOSE_ASAP) {
        ln = listSearchKey(server.clients_to_close,c);
        redisAssert(ln != NULL);
        listDelNode(server.clients_to_close,ln);
    }

    /* Release other dynamically allocated client structure fields,
     * and finally release the client structure itself. */
    if (c->name) decrRefCount(c->name);
    // 清除参数空间
    zfree(c->argv);
    // 清除事务状态信息
    freeClientMultiState(c);
    sdsfree(c->peerid);
    // 释放客户端 redisClient 结构本身
    zfree(c);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
leaq 0x9a43f(%rip), %r15     # 0xc6fa8
cmpq %rdi, 0x1d0(%r15)
jne 0x2cb7a
andq $0x0, 0x1d0(%r15)
cmpq $0x0, 0x660(%r15)
sete %cl
movl 0x90(%rbx), %eax
testb $0x2, %al
sete %dl
orb %cl, %dl
jne 0x2cbb5
leaq 0x6027b(%rip), %rsi     # 0x8ce16
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movl 0x90(%rbx), %eax
testw $0x4d0, %ax             # imm = 0x4D0
je 0x2cdfd
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x2cbeb
movl (%rbx), %edi
movq %rsp, %rsi
pushq $0x2e
popq %rdx
xorl %ecx, %ecx
callq 0x21481
cmpl $-0x1, %eax
je 0x2cbeb
movl 0xfc(%rbx), %ecx
leaq 0x60255(%rip), %rsi     # 0x8ce33
pushq $0x3
popq %rdi
movq %rsp, %rdx
xorl %eax, %eax
callq 0x22613
movq 0x20(%rbx), %rdi
callq 0x271f7
andq $0x0, 0x20(%rbx)
testb $0x10, 0x90(%rbx)
je 0x2cc0a
movq %rbx, %rdi
callq 0x5ba5f
movq 0x128(%rbx), %rdi
callq 0x21e31
movq %rbx, %rdi
callq 0x484c6
movq 0x150(%rbx), %rdi
callq 0x1fdaa
movq %rbx, %rdi
xorl %esi, %esi
callq 0x47da9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x47e56
movq 0x158(%rbx), %rdi
callq 0x21e31
movq 0x160(%rbx), %rdi
callq 0x1fdaa
movl (%rbx), %esi
cmpl $-0x1, %esi
je 0x2cc7e
movq 0x28(%r15), %rdi
pushq $0x1
popq %rdx
callq 0x203cd
movq 0x28(%r15), %rdi
movl (%rbx), %esi
pushq $0x2
popq %rdx
callq 0x203cd
movl (%rbx), %edi
callq 0x1f170
movq 0x60(%rbx), %rdi
callq 0x1fdaa
movq %rbx, %rdi
callq 0x2cea3
cmpl $-0x1, (%rbx)
je 0x2ccbb
movq 0x1b0(%r15), %rdi
movq %rbx, %rsi
callq 0x20066
testq %rax, %rax
je 0x2ce0e
movq 0x1b0(%r15), %rdi
movq %rax, %rsi
callq 0x1ff28
movl 0x90(%rbx), %eax
testb %al, %al
jns 0x2ccf2
movq 0x738(%r15), %rdi
movq %rbx, %rsi
callq 0x20066
testq %rax, %rax
je 0x2ce23
movq 0x738(%r15), %rdi
movq %rax, %rsi
callq 0x1ff28
movl 0x90(%rbx), %eax
testb $0x1, %al
je 0x2cd84
cmpl $0x8, 0x98(%rbx)
jne 0x2cd24
movl 0x9c(%rbx), %edi
cmpl $-0x1, %edi
je 0x2cd13
callq 0x1f170
movq 0xb0(%rbx), %rdi
testq %rdi, %rdi
je 0x2cd24
callq 0x271f7
movl 0x90(%rbx), %eax
andl $0x4, %eax
movq 0x1c0(%r15,%rax,2), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20066
testq %rax, %rax
je 0x2ce38
movq %r14, %rdi
movq %rax, %rsi
callq 0x1ff28
testb $0x1, 0x90(%rbx)
je 0x2cd79
movq 0x1c0(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2cd79
movq 0x798(%r15), %rax
movq %rax, 0x630(%r15)
callq 0x31d7e
movl 0x90(%rbx), %eax
testb $0x2, %al
je 0x2cd93
callq 0x2ce6d
movl 0x90(%rbx), %eax
btl $0xa, %eax
jae 0x2cdc0
movq 0x1b8(%r15), %rdi
movq %rbx, %rsi
callq 0x20066
testq %rax, %rax
je 0x2ce4d
movq 0x1b8(%r15), %rdi
movq %rax, %rsi
callq 0x1ff28
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x2cdce
callq 0x2ee1f
movq 0x38(%rbx), %rdi
callq 0x283cd
movq %rbx, %rdi
callq 0x48398
movq 0x168(%rbx), %rdi
callq 0x271f7
movq %rbx, %rdi
callq 0x283cd
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq %rbx, %rdi
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x33712
leaq 0x621a5(%rip), %rdi     # 0x8efba
leaq 0x5ff52(%rip), %rsi     # 0x8cd6e
movl $0x3bf, %edx            # imm = 0x3BF
jmp 0x2ce60
leaq 0x62190(%rip), %rdi     # 0x8efba
leaq 0x5ff3d(%rip), %rsi     # 0x8cd6e
movl $0x3c8, %edx            # imm = 0x3C8
jmp 0x2ce60
leaq 0x6217b(%rip), %rdi     # 0x8efba
leaq 0x5ff28(%rip), %rsi     # 0x8cd6e
movl $0x3d5, %edx            # imm = 0x3D5
jmp 0x2ce60
leaq 0x62166(%rip), %rdi     # 0x8efba
leaq 0x5ff13(%rip), %rsi     # 0x8cd6e
movl $0x3e7, %edx            # imm = 0x3E7
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0