// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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
subq $0x18, %rsp
movq %rdi, %rbx
movq %rsi, %rdi
movl %edx, %esi
callq 0x6a6eb
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6bc73
movq 0x88(%rbx), %rdi
callq 0x2a99f
movq %rax, %r12
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x2a9d3
movl %r13d, %ebp
testq %rax, %rax
je 0x6bb96
movq 0x8(%rax), %r15
movq 0x20(%r15), %rdi
movq 0x8(%rsp), %rsi
callq 0x6a797
testl %eax, %eax
jne 0x6bb45
incl %r13d
leaq 0x8(,%rbp,8), %rsi
movq %r14, %rdi
callq 0x32863
movq %rax, %r14
movq 0x20(%r15), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x6a6eb
movq %rax, (%r14,%rbp,8)
jmp 0x6bb45
movq %r12, %rdi
callq 0x2aa94
movq 0x20(%rbx), %rsi
movq 0x8(%rsp), %rdi
callq 0x6a797
testl %eax, %eax
jne 0x6bbd9
incl %r13d
leaq 0x8(,%rbp,8), %rsi
movq %r14, %rdi
callq 0x32863
movq %rax, %r14
movq 0x20(%rbx), %rax
movq (%rax), %rdi
movl 0x8(%rax), %esi
callq 0x6a6eb
movq %rax, (%r14,%rbp,8)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6b94b
movq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rbx)
movl %r13d, %r15d
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rbx)
pushq $0x2
popq %rbp
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x6bc58
movq (%r14,%r13,8), %rax
movq (%rax), %rdx
movl 0x8(%rax), %ecx
movl 0x90(%rbx), %r8d
xorl %edi, %edi
movl %ebp, %esi
movq %rbx, %r9
callq 0x6b555
movq %rax, %r12
movq (%r14,%r13,8), %rdi
callq 0x6a782
testq %r12, %r12
je 0x6bc53
movl %ebp, %edi
leaq 0x5150d(%rip), %rsi     # 0xbd14f
movq %r12, %rdx
leaq 0x51500(%rip), %rcx     # 0xbd14c
xorl %eax, %eax
callq 0x6a7bb
incq %r13
jmp 0x6bc06
movq %r14, %rdi
callq 0x328f9
movq 0x10(%rsp), %rdi
callq 0x6a782
callq 0x6a604
xorl %eax, %eax
jmp 0x6bc76
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq