// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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;

    newaddr = createSentinelAddr(ip,port);
    if (newaddr == NULL) return C_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. */
    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. */
    sentinelResetMaster(master,SENTINEL_RESET_NO_SENTINELS);
    oldaddr = master->addr;
    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(LL_NOTICE,"+slave",slave,"%@");
    }
    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 C_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 0x7a436
testq %rax, %rax
je 0x7bb1f
movq %rax, %r15
movq 0x88(%rbx), %rdi
callq 0x320bb
movq %rax, %r12
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
movq %rbx, (%rsp)
je 0x7bb29
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x8(%rax), %rbp
movq 0x20(%rbp), %rax
movl 0x8(%rax), %ecx
cmpl 0x8(%r15), %ecx
jne 0x7bae0
movq (%rax), %rdi
movq (%r15), %rsi
callq 0x26300
testl %eax, %eax
je 0x7bb10
movslq %r13d, %rbx
incl %r13d
leaq 0x8(,%rbx,8), %rsi
movq %r14, %rdi
callq 0x3b349
movq %rax, %r14
movq 0x20(%rbp), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x7a436
movq %rax, (%r14,%rbx,8)
movq (%rsp), %rbx
movq %r12, %rdi
callq 0x32116
testq %rax, %rax
jne 0x7bac0
jmp 0x7bb2f
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x7bc37
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x321d3
movq 0x20(%rbx), %rax
movl 0x8(%r15), %ecx
cmpl 0x8(%rax), %ecx
jne 0x7bb53
movq (%r15), %rdi
movq (%rax), %rsi
callq 0x26300
testl %eax, %eax
je 0x7bb87
movslq %r13d, %rbx
incl %r13d
leaq 0x8(,%rbx,8), %rsi
movq %r14, %rdi
callq 0x3b349
movq %rax, %r14
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x7a436
movq %rax, (%r14,%rbx,8)
movq (%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7b8a9
movq 0x20(%rbx), %rax
movq %rax, (%rsp)
movq %r15, 0x20(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rbx)
testl %r13d, %r13d
jle 0x7bc14
movl %r13d, %r15d
xorl %r13d, %r13d
movq (%r14,%r13,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
movl 0x90(%rbx), %r8d
xorl %edi, %edi
movl $0x2, %esi
movq %rbx, %r9
callq 0x7b48e
movq %rax, %rbp
movq (%r14,%r13,8), %r12
movq (%r12), %rdi
callq 0x392b6
movq %r12, %rdi
callq 0x3b3df
testq %rbp, %rbp
je 0x7bc0c
movl $0x2, %edi
leaq 0x53808(%rip), %rsi     # 0xcf403
movq %rbp, %rdx
leaq 0x537fb(%rip), %rcx     # 0xcf400
xorl %eax, %eax
callq 0x7a50c
incq %r13
cmpq %r13, %r15
jne 0x7bbb2
movq %r14, %rdi
callq 0x3b3df
movq (%rsp), %rbx
movq (%rbx), %rdi
callq 0x392b6
movq %rbx, %rdi
callq 0x3b3df
callq 0x7a33b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq