// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x79990
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x7b605
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b80f
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdi
callq 0x241d0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x24270
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x7b6a3
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x79ab0
cmpl $0x0, %eax
je 0x7b650
jmp 0x7b619
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x2e270
movq %rax, -0x38(%rbp)
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %esi
callq 0x79990
movq %rax, %rdx
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x7b619
movq -0x48(%rbp), %rdi
callq 0x243d0
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x79ab0
cmpl $0x0, %eax
jne 0x7b710
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x2e270
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %esi
callq 0x79990
movq %rax, %rdx
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x7b310
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x88(%rax)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x7b7f1
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movl 0xd0(%rax), %r8d
movq -0x10(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
movl $0x2, %esi
callq 0x7a9e0
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x79a80
cmpq $0x0, -0x60(%rbp)
je 0x7b7e1
movq -0x60(%rbp), %rdx
movl $0x2, %edi
leaq 0x6d867(%rip), %rsi     # 0xe9035
leaq 0x6d85d(%rip), %rcx     # 0xe9032
movb $0x0, %al
callq 0x79b10
callq 0x7b820
jmp 0x7b7e3
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x7b75b
movq -0x38(%rbp), %rdi
callq 0x2e3a0
movq -0x28(%rbp), %rdi
callq 0x79a80
callq 0x7b820
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)