// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelResetMasterAndChangeAddress
int sentinelResetMasterAndChangeAddress(sentinelRedisInstance *master, char *ip, int port) {
    sentinelAddr *oldaddr, *newaddr;
    sentinelAddr **slaves = NULL;
    int numslaves = 0, j;
    dictIterator *di;
    dictEntry *de;

    // 根据 ip 和 port 参数，创建地址结构
    newaddr = createSentinelAddr(ip,port);
    if (newaddr == NULL) return REDIS_ERR;

    /* Make a list of slaves to add back after the reset.
     * Don't include the one having the address we are switching to. */
    // 创建一个包含原主服务器所有从服务器实例的数组
    // 用于在重置地址之后进行检查
    // 新主服务器（原主服务器的其中一个从服务器）的地址不会包含在这个数组中
    di = dictGetIterator(master->slaves);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *slave = dictGetVal(de);

        // 跳过新主服务器
        if (sentinelAddrIsEqual(slave->addr,newaddr)) continue;

        // 将从服务器保存到数组中
        slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1));
        slaves[numslaves++] = createSentinelAddr(slave->addr->ip,
                                                 slave->addr->port);
    }
    dictReleaseIterator(di);
    
    /* If we are switching to a different address, include the old address
     * as a slave as well, so that we'll be able to sense / reconfigure
     * the old master. */
    // 如果新地址和 master 的地址不相同，
    // 将 master 的地址也作为从服务器地址添加到保存了所有从服务器地址的数组中
    // （这样等于将下线主服务器设置为新主服务器的从服务器）
    if (!sentinelAddrIsEqual(newaddr,master->addr)) {
        slaves = zrealloc(slaves,sizeof(sentinelAddr*)*(numslaves+1));
        slaves[numslaves++] = createSentinelAddr(master->addr->ip,
                                                 master->addr->port);
    }

    /* Reset and switch address. */
    // 重置 master 实例结构
    sentinelResetMaster(master,SENTINEL_RESET_NO_SENTINELS);
    oldaddr = master->addr;
    // 为 master 实例设置新的地址
    master->addr = newaddr;
    master->o_down_since_time = 0;
    master->s_down_since_time = 0;

    /* Add slaves back. */
    // 为实例加回之前保存的所有从服务器
    for (j = 0; j < numslaves; j++) {
        sentinelRedisInstance *slave;

        slave = createSentinelRedisInstance(NULL,SRI_SLAVE,slaves[j]->ip,
                    slaves[j]->port, master->quorum, master);

        releaseSentinelAddr(slaves[j]);

        if (slave) {
            sentinelEvent(REDIS_NOTICE,"+slave",slave,"%@");
            sentinelFlushConfig();
        }
    }
    zfree(slaves);

    /* Release the old address at the end so we are safe even if the function
     * gets the master->addr->ip and master->addr->port as arguments. */
    // 释放旧地址
    releaseSentinelAddr(oldaddr);
    sentinelFlushConfig();
    return REDIS_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq %rsi, %rdi
movl %edx, %esi
callq 0x5b595
testq %rax, %rax
je 0x5c992
movq %rax, %r15
movq 0xc8(%rbx), %rdi
callq 0x228b5
movq %rax, %r12
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
movq %rbx, (%rsp)
je 0x5c99c
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x8(%rax), %rbp
movq 0x20(%rbp), %rax
movl 0x8(%rax), %ecx
cmpl 0x8(%r15), %ecx
jne 0x5c953
movq (%rax), %rdi
movq (%r15), %rsi
callq 0x1f2e0
testl %eax, %eax
je 0x5c983
movslq %r13d, %rbx
incl %r13d
leaq 0x8(,%rbx,8), %rsi
movq %r14, %rdi
callq 0x297b5
movq %rax, %r14
movq 0x20(%rbp), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x5b595
movq %rax, (%r14,%rbx,8)
movq (%rsp), %rbx
movq %r12, %rdi
callq 0x22913
testq %rax, %rax
jne 0x5c933
jmp 0x5c9a2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5cab2
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x229b9
movq 0x20(%rbx), %rax
movl 0x8(%r15), %ecx
cmpl 0x8(%rax), %ecx
jne 0x5c9c6
movq (%r15), %rdi
movq (%rax), %rsi
callq 0x1f2e0
testl %eax, %eax
je 0x5c9fa
movslq %r13d, %rbx
incl %r13d
leaq 0x8(,%rbx,8), %rsi
movq %r14, %rdi
callq 0x297b5
movq %rax, %r14
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x5b595
movq %rax, (%r14,%rbx,8)
movq (%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x5c71e
movq 0x20(%rbx), %rax
movq %rax, (%rsp)
movq %r15, 0x20(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x88(%rbx)
testl %r13d, %r13d
jle 0x5ca8f
movl %r13d, %r15d
xorl %r13d, %r13d
movq (%r14,%r13,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
movl 0xd0(%rbx), %r8d
xorl %edi, %edi
movl $0x2, %esi
movq %rbx, %r9
callq 0x5c139
movq %rax, %rbp
movq (%r14,%r13,8), %r12
movq (%r12), %rdi
callq 0x28204
movq %r12, %rdi
callq 0x2984b
testq %rbp, %rbp
je 0x5ca87
movl $0x2, %edi
leaq 0x42590(%rip), %rsi     # 0x9f001
movq %rbp, %rdx
leaq 0x42583(%rip), %rcx     # 0x9effe
xorl %eax, %eax
callq 0x5b66f
callq 0x5cac1
incq %r13
cmpq %r13, %r15
jne 0x5ca28
movq %r14, %rdi
callq 0x2984b
movq (%rsp), %rbx
movq (%rbx), %rdi
callq 0x28204
movq %rbx, %rdi
callq 0x2984b
callq 0x5cac1
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq