// File: /workspace/github_c_makefile_star2_O3/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 0x9ba7e(%rip), %r15     # 0xc9fa8
cmpq %rdi, 0x1d0(%r15)
jne 0x2e53e
movq $0x0, 0x1d0(%r15)
cmpq $0x0, 0x660(%r15)
sete %cl
movl 0x90(%rbx), %eax
testb $0x2, %al
sete %dl
orb %cl, %dl
jne 0x2e57c
leaq 0x688e7(%rip), %rsi     # 0x96e46
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl 0x90(%rbx), %eax
testl $0x4d0, %eax            # imm = 0x4D0
je 0x2e820
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x2e5b6
movl (%rbx), %edi
movq %rsp, %rsi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x21ee7
cmpl $-0x1, %eax
je 0x2e5b6
movl 0xfc(%rbx), %ecx
leaq 0x688bc(%rip), %rsi     # 0x96e63
movq %rsp, %rdx
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movq 0x20(%rbx), %rdi
callq 0x2850d
movq $0x0, 0x20(%rbx)
testb $0x10, 0x90(%rbx)
je 0x2e5d8
movq %rbx, %rdi
callq 0x60977
movq 0x128(%rbx), %rdi
callq 0x229a9
movq %rbx, %rdi
callq 0x4bd35
movq 0x150(%rbx), %rdi
callq 0x2041c
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4b58d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4b643
movq 0x158(%rbx), %rdi
callq 0x229a9
movq 0x160(%rbx), %rdi
callq 0x2041c
movl (%rbx), %esi
cmpl $-0x1, %esi
je 0x2e650
movq 0x28(%r15), %rdi
movl $0x1, %edx
callq 0x20ce8
movq 0x28(%r15), %rdi
movl (%rbx), %esi
movl $0x2, %edx
callq 0x20ce8
movl (%rbx), %edi
callq 0x1f170
movq 0x60(%rbx), %rdi
callq 0x2041c
movq %rbx, %rdi
callq 0x2e8cb
cmpl $-0x1, (%rbx)
je 0x2e68d
movq 0x1b0(%r15), %rdi
movq %rbx, %rsi
callq 0x20729
testq %rax, %rax
je 0x2e831
movq 0x1b0(%r15), %rdi
movq %rax, %rsi
callq 0x205b1
movl 0x90(%rbx), %eax
testb %al, %al
jns 0x2e6c4
movq 0x738(%r15), %rdi
movq %rbx, %rsi
callq 0x20729
testq %rax, %rax
je 0x2e846
movq 0x738(%r15), %rdi
movq %rax, %rsi
callq 0x205b1
movl 0x90(%rbx), %eax
testb $0x1, %al
je 0x2e756
cmpl $0x8, 0x98(%rbx)
jne 0x2e6f6
movl 0x9c(%rbx), %edi
cmpl $-0x1, %edi
je 0x2e6e5
callq 0x1f170
movq 0xb0(%rbx), %rdi
testq %rdi, %rdi
je 0x2e6f6
callq 0x2850d
movl 0x90(%rbx), %eax
andl $0x4, %eax
movq 0x1c0(%r15,%rax,2), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20729
testq %rax, %rax
je 0x2e85b
movq %r14, %rdi
movq %rax, %rsi
callq 0x205b1
testb $0x1, 0x90(%rbx)
je 0x2e74b
movq 0x1c0(%r15), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2e74b
movq 0x798(%r15), %rax
movq %rax, 0x630(%r15)
callq 0x33f0b
movl 0x90(%rbx), %eax
testb $0x2, %al
je 0x2e7b6
movq $0x0, 0x660(%r15)
movl $0x1, 0x674(%r15)
movq 0x798(%r15), %rcx
movq %rcx, 0x6b0(%r15)
cmpq $0x0, 0x650(%r15)
je 0x2e7b6
movq 0x1c0(%r15), %rcx
cmpq $0x0, 0x28(%rcx)
je 0x2e7b6
movq (%rcx), %rax
movq 0x10(%rax), %rdi
callq 0x2e517
movq 0x1c0(%r15), %rcx
cmpq $0x0, 0x28(%rcx)
jne 0x2e796
movl 0x90(%rbx), %eax
btl $0xa, %eax
jae 0x2e7e3
movq 0x1b8(%r15), %rdi
movq %rbx, %rsi
callq 0x20729
testq %rax, %rax
je 0x2e870
movq 0x1b8(%r15), %rdi
movq %rax, %rsi
callq 0x205b1
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x2e7f1
callq 0x30b0c
movq 0x38(%rbx), %rdi
callq 0x29af7
movq %rbx, %rdi
callq 0x4bbee
movq 0x168(%rbx), %rdi
callq 0x2850d
movq %rbx, %rdi
callq 0x29af7
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq %rbx, %rdi
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x359a6
leaq 0x6a7be(%rip), %rdi     # 0x98ff6
leaq 0x6855f(%rip), %rsi     # 0x96d9e
movl $0x3bf, %edx            # imm = 0x3BF
jmp 0x2e883
leaq 0x6a7a9(%rip), %rdi     # 0x98ff6
leaq 0x6854a(%rip), %rsi     # 0x96d9e
movl $0x3c8, %edx            # imm = 0x3C8
jmp 0x2e883
leaq 0x6a794(%rip), %rdi     # 0x98ff6
leaq 0x68535(%rip), %rsi     # 0x96d9e
movl $0x3d5, %edx            # imm = 0x3D5
jmp 0x2e883
leaq 0x6a77f(%rip), %rdi     # 0x98ff6
leaq 0x68520(%rip), %rsi     # 0x96d9e
movl $0x3e7, %edx            # imm = 0x3E7
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0