// File: /workspace/github_c_makefile_star2_O1/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 0x9bd0e(%rip), %r15     # 0xc9fa8
cmpq %rdi, 0x1d0(%r15)
jne 0x2e2ae
movq $0x0, 0x1d0(%r15)
cmpq $0x0, 0x660(%r15)
je 0x2e2e6
testb $0x2, 0x90(%rbx)
je 0x2e2e6
leaq 0x68b1e(%rip), %rsi     # 0x96de6
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movzwl 0x90(%rbx), %eax
testl $0x4d0, %eax            # imm = 0x4D0
je 0x2e57e
movl 0x90(%rbx), %eax
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x2e326
movl (%rbx), %edi
movq %rsp, %rsi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x21d05
cmpl $-0x1, %eax
je 0x2e326
movl 0xfc(%rbx), %ecx
leaq 0x68aec(%rip), %rsi     # 0x96e03
movq %rsp, %rdx
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movq 0x20(%rbx), %rdi
callq 0x28204
movq $0x0, 0x20(%rbx)
testb $0x10, 0x90(%rbx)
je 0x2e348
movq %rbx, %rdi
callq 0x60e97
movq 0x128(%rbx), %rdi
callq 0x227ed
movq %rbx, %rdi
callq 0x4c349
movq 0x150(%rbx), %rdi
callq 0x203b0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4bb9d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4bc53
movq 0x158(%rbx), %rdi
callq 0x227ed
movq 0x160(%rbx), %rdi
callq 0x203b0
movl (%rbx), %esi
cmpl $-0x1, %esi
je 0x2e3c0
movq 0x28(%r15), %rdi
movl $0x1, %edx
callq 0x20a9d
movq 0x28(%r15), %rdi
movl (%rbx), %esi
movl $0x2, %edx
callq 0x20a9d
movl (%rbx), %edi
callq 0x1f170
movq 0x60(%rbx), %rdi
callq 0x203b0
movq %rbx, %rdi
callq 0x2e629
cmpl $-0x1, (%rbx)
je 0x2e3fd
movq 0x1b0(%r15), %rdi
movq %rbx, %rsi
callq 0x206d5
testq %rax, %rax
je 0x2e58f
movq 0x1b0(%r15), %rdi
movq %rax, %rsi
callq 0x20544
testb $-0x80, 0x90(%rbx)
je 0x2e42d
movq 0x738(%r15), %rdi
movq %rbx, %rsi
callq 0x206d5
testq %rax, %rax
je 0x2e5a4
movq 0x738(%r15), %rdi
movq %rax, %rsi
callq 0x20544
testb $0x1, 0x90(%rbx)
je 0x2e4be
cmpl $0x8, 0x98(%rbx)
jne 0x2e464
movl 0x9c(%rbx), %edi
cmpl $-0x1, %edi
je 0x2e453
callq 0x1f170
movq 0xb0(%rbx), %rdi
testq %rdi, %rdi
je 0x2e464
callq 0x28204
movl 0x90(%rbx), %eax
andl $0x4, %eax
movq 0x1c0(%r15,%rax,2), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x206d5
testq %rax, %rax
je 0x2e5b9
movq %r14, %rdi
movq %rax, %rsi
callq 0x20544
testb $0x1, 0x90(%rbx)
je 0x2e4b9
movq 0x1c0(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2e4b9
movq 0x798(%r15), %rax
movq %rax, 0x630(%r15)
callq 0x33a99
testb $0x2, 0x90(%rbx)
je 0x2e511
movq $0x0, 0x660(%r15)
movl $0x1, 0x674(%r15)
movq 0x798(%r15), %rax
movq %rax, 0x6b0(%r15)
cmpq $0x0, 0x650(%r15)
jne 0x2e503
jmp 0x2e511
movq (%rax), %rax
movq 0x10(%rax), %rdi
callq 0x2e287
movq 0x1c0(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2e4f7
testb $0x4, 0x91(%rbx)
je 0x2e541
movq 0x1b8(%r15), %rdi
movq %rbx, %rsi
callq 0x206d5
testq %rax, %rax
je 0x2e5ce
movq 0x1b8(%r15), %rdi
movq %rax, %rsi
callq 0x20544
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x2e54f
callq 0x30834
movq 0x38(%rbx), %rdi
callq 0x2984b
movq %rbx, %rdi
callq 0x4c202
movq 0x168(%rbx), %rdi
callq 0x28204
movq %rbx, %rdi
callq 0x2984b
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq %rbx, %rdi
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x3557f
leaq 0x6aa17(%rip), %rdi     # 0x98fad
leaq 0x687a1(%rip), %rsi     # 0x96d3e
movl $0x3bf, %edx            # imm = 0x3BF
jmp 0x2e5e1
leaq 0x6aa02(%rip), %rdi     # 0x98fad
leaq 0x6878c(%rip), %rsi     # 0x96d3e
movl $0x3c8, %edx            # imm = 0x3C8
jmp 0x2e5e1
leaq 0x6a9ed(%rip), %rdi     # 0x98fad
leaq 0x68777(%rip), %rsi     # 0x96d3e
movl $0x3d5, %edx            # imm = 0x3D5
jmp 0x2e5e1
leaq 0x6a9d8(%rip), %rdi     # 0x98fad
leaq 0x68762(%rip), %rsi     # 0x96d3e
movl $0x3e7, %edx            # imm = 0x3E7
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0