// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdi, %rbx
movq %rsi, %rdi
movl %edx, %esi
callq 0x566bb
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x57a13
movq 0xc8(%rbx), %rdi
callq 0x21ef6
movq %rax, %r12
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x21f2f
movl %r13d, %ebp
testq %rax, %rax
je 0x5792e
movq 0x8(%rax), %r15
movq 0x20(%r15), %rdi
movq 0x8(%rsp), %rsi
callq 0x5676b
testl %eax, %eax
jne 0x578dd
incl %r13d
leaq 0x8(,%rbp,8), %rsi
movq %r14, %rdi
callq 0x28337
movq %rax, %r14
movq 0x20(%r15), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x566bb
movq %rax, (%r14,%rbp,8)
jmp 0x578dd
movq %r12, %rdi
callq 0x21fe6
movq 0x20(%rbx), %rsi
movq 0x8(%rsp), %rdi
callq 0x5676b
testl %eax, %eax
jne 0x57971
incl %r13d
leaq 0x8(,%rbp,8), %rsi
movq %r14, %rdi
callq 0x28337
movq %rax, %r14
movq 0x20(%rbx), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x566bb
movq %rax, (%r14,%rbp,8)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x576e6
movq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rbx)
movl %r13d, %r15d
xorps %xmm0, %xmm0
movups %xmm0, 0x88(%rbx)
pushq $0x2
popq %rbp
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x579f8
movq (%r14,%r13,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
movl 0xd0(%rbx), %r8d
xorl %edi, %edi
movl %ebp, %esi
movq %rbx, %r9
callq 0x5715c
movq %rax, %r12
movq (%r14,%r13,8), %rdi
callq 0x56756
testq %r12, %r12
je 0x579f3
movl %ebp, %edi
leaq 0x3d608(%rip), %rsi     # 0x94fe5
movq %r12, %rdx
leaq 0x3d5fb(%rip), %rcx     # 0x94fe2
xorl %eax, %eax
callq 0x5678f
callq 0x57a25
incq %r13
jmp 0x579a1
movq %r14, %rdi
callq 0x283cd
movq 0x10(%rsp), %rdi
callq 0x56756
callq 0x57a25
xorl %eax, %eax
jmp 0x57a16
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq