// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/replication.c
// Function: syncWithMaster
void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
    char tmpfile[256], *err = NULL;
    int dfd = -1, maxtries = 5;
    int sockerr = 0, psync_result;
    socklen_t errlen = sizeof(sockerr);
    UNUSED(el);
    UNUSED(privdata);
    UNUSED(mask);

    /* If this event fired after the user turned the instance into a master
     * with SLAVEOF NO ONE we must just return ASAP. */
    if (server.repl_state == REPL_STATE_NONE) {
        close(fd);
        return;
    }

    /* Check for errors in the socket. */
    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1)
        sockerr = errno;
    if (sockerr) {
        serverLog(LL_WARNING,"Error condition on socket for SYNC: %s",
            strerror(sockerr));
        goto error;
    }

    /* Send a PING to check the master is able to reply without errors. */
    if (server.repl_state == REPL_STATE_CONNECTING) {
        serverLog(LL_NOTICE,"Non blocking connect for SYNC fired the event.");
        /* Delete the writable event so that the readable event remains
         * registered and we can wait for the PONG reply. */
        aeDeleteFileEvent(server.el,fd,AE_WRITABLE);
        server.repl_state = REPL_STATE_RECEIVE_PONG;
        /* Send the PING, don't check for errors at all, we have the timeout
         * that will take care about this. */
        err = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"PING",NULL);
        if (err) goto write_error;
        return;
    }

    /* Receive the PONG command. */
    if (server.repl_state == REPL_STATE_RECEIVE_PONG) {
        err = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);

        /* We accept only two replies as valid, a positive +PONG reply
         * (we just check for "+") or an authentication error.
         * Note that older versions of Redis replied with "operation not
         * permitted" instead of using a proper error code, so we test
         * both. */
        if (err[0] != '+' &&
            strncmp(err,"-NOAUTH",7) != 0 &&
            strncmp(err,"-ERR operation not permitted",28) != 0)
        {
            serverLog(LL_WARNING,"Error reply to PING from master: '%s'",err);
            sdsfree(err);
            goto error;
        } else {
            serverLog(LL_NOTICE,
                "Master replied to PING, replication can continue...");
        }
        sdsfree(err);
        server.repl_state = REPL_STATE_SEND_AUTH;
    }

    /* AUTH with the master if required. */
    if (server.repl_state == REPL_STATE_SEND_AUTH) {
        if (server.masterauth) {
            err = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"AUTH",server.masterauth,NULL);
            if (err) goto write_error;
            server.repl_state = REPL_STATE_RECEIVE_AUTH;
            return;
        } else {
            server.repl_state = REPL_STATE_SEND_PORT;
        }
    }

    /* Receive AUTH reply. */
    if (server.repl_state == REPL_STATE_RECEIVE_AUTH) {
        err = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);
        if (err[0] == '-') {
            serverLog(LL_WARNING,"Unable to AUTH to MASTER: %s",err);
            sdsfree(err);
            goto error;
        }
        sdsfree(err);
        server.repl_state = REPL_STATE_SEND_PORT;
    }

    /* Set the slave port, so that Master's INFO command can list the
     * slave listening port correctly. */
    if (server.repl_state == REPL_STATE_SEND_PORT) {
        sds port = sdsfromlonglong(server.slave_announce_port ?
            server.slave_announce_port : server.port);
        err = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"REPLCONF",
                "listening-port",port, NULL);
        sdsfree(port);
        if (err) goto write_error;
        sdsfree(err);
        server.repl_state = REPL_STATE_RECEIVE_PORT;
        return;
    }

    /* Receive REPLCONF listening-port reply. */
    if (server.repl_state == REPL_STATE_RECEIVE_PORT) {
        err = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);
        /* Ignore the error if any, not all the Redis versions support
         * REPLCONF listening-port. */
        if (err[0] == '-') {
            serverLog(LL_NOTICE,"(Non critical) Master does not understand "
                                "REPLCONF listening-port: %s", err);
        }
        sdsfree(err);
        server.repl_state = REPL_STATE_SEND_IP;
    }

    /* Skip REPLCONF ip-address if there is no slave-announce-ip option set. */
    if (server.repl_state == REPL_STATE_SEND_IP &&
        server.slave_announce_ip == NULL)
    {
            server.repl_state = REPL_STATE_SEND_CAPA;
    }

    /* Set the slave ip, so that Master's INFO command can list the
     * slave IP address port correctly in case of port forwarding or NAT. */
    if (server.repl_state == REPL_STATE_SEND_IP) {
        err = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"REPLCONF",
                "ip-address",server.slave_announce_ip, NULL);
        if (err) goto write_error;
        sdsfree(err);
        server.repl_state = REPL_STATE_RECEIVE_IP;
        return;
    }

    /* Receive REPLCONF ip-address reply. */
    if (server.repl_state == REPL_STATE_RECEIVE_IP) {
        err = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);
        /* Ignore the error if any, not all the Redis versions support
         * REPLCONF listening-port. */
        if (err[0] == '-') {
            serverLog(LL_NOTICE,"(Non critical) Master does not understand "
                                "REPLCONF ip-address: %s", err);
        }
        sdsfree(err);
        server.repl_state = REPL_STATE_SEND_CAPA;
    }

    /* Inform the master of our capabilities. While we currently send
     * just one capability, it is possible to chain new capabilities here
     * in the form of REPLCONF capa X capa Y capa Z ...
     * The master will ignore capabilities it does not understand. */
    if (server.repl_state == REPL_STATE_SEND_CAPA) {
        err = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"REPLCONF",
                "capa","eof",NULL);
        if (err) goto write_error;
        sdsfree(err);
        server.repl_state = REPL_STATE_RECEIVE_CAPA;
        return;
    }

    /* Receive CAPA reply. */
    if (server.repl_state == REPL_STATE_RECEIVE_CAPA) {
        err = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);
        /* Ignore the error if any, not all the Redis versions support
         * REPLCONF capa. */
        if (err[0] == '-') {
            serverLog(LL_NOTICE,"(Non critical) Master does not understand "
                                  "REPLCONF capa: %s", err);
        }
        sdsfree(err);
        server.repl_state = REPL_STATE_SEND_PSYNC;
    }

    /* Try a partial resynchonization. If we don't have a cached master
     * slaveTryPartialResynchronization() will at least try to use PSYNC
     * to start a full resynchronization so that we get the master run id
     * and the global offset, to try a partial resync at the next
     * reconnection attempt. */
    if (server.repl_state == REPL_STATE_SEND_PSYNC) {
        if (slaveTryPartialResynchronization(fd,0) == PSYNC_WRITE_ERROR) {
            err = sdsnew("Write error sending the PSYNC command.");
            goto write_error;
        }
        server.repl_state = REPL_STATE_RECEIVE_PSYNC;
        return;
    }

    /* If reached this point, we should be in REPL_STATE_RECEIVE_PSYNC. */
    if (server.repl_state != REPL_STATE_RECEIVE_PSYNC) {
        serverLog(LL_WARNING,"syncWithMaster(): state machine error, "
                             "state should be RECEIVE_PSYNC but is %d",
                             server.repl_state);
        goto error;
    }

    psync_result = slaveTryPartialResynchronization(fd,1);
    if (psync_result == PSYNC_WAIT_REPLY) return; /* Try again later... */

    /* Note: if PSYNC does not return WAIT_REPLY, it will take care of
     * uninstalling the read handler from the file descriptor. */

    if (psync_result == PSYNC_CONTINUE) {
        serverLog(LL_NOTICE, "MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.");
        return;
    }

    /* PSYNC failed or is not supported: we want our slaves to resync with us
     * as well, if we have any (chained replication case). The mater may
     * transfer us an entirely different data set and we have no way to
     * incrementally feed our slaves after that. */
    disconnectSlaves(); /* Force our slaves to resync with us as well. */
    freeReplicationBacklog(); /* Don't allow our chained slaves to PSYNC. */

    /* Fall back to SYNC if needed. Otherwise psync_result == PSYNC_FULLRESYNC
     * and the server.repl_master_runid and repl_master_initial_offset are
     * already populated. */
    if (psync_result == PSYNC_NOT_SUPPORTED) {
        serverLog(LL_NOTICE,"Retrying with SYNC...");
        if (syncWrite(fd,"SYNC\r\n",6,server.repl_syncio_timeout*1000) == -1) {
            serverLog(LL_WARNING,"I/O error writing to MASTER: %s",
                strerror(errno));
            goto error;
        }
    }

    /* Prepare a suitable temp file for bulk transfer */
    while(maxtries--) {
        snprintf(tmpfile,256,
            "temp-%d.%ld.rdb",(int)server.unixtime,(long int)getpid());
        dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
        if (dfd != -1) break;
        sleep(1);
    }
    if (dfd == -1) {
        serverLog(LL_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
        goto error;
    }

    /* Setup the non blocking download of the bulk file. */
    if (aeCreateFileEvent(server.el,fd, AE_READABLE,readSyncBulkPayload,NULL)
            == AE_ERR)
    {
        serverLog(LL_WARNING,
            "Can't create readable event for SYNC: %s (fd=%d)",
            strerror(errno),fd);
        goto error;
    }

    server.repl_state = REPL_STATE_TRANSFER;
    server.repl_transfer_size = -1;
    server.repl_transfer_read = 0;
    server.repl_transfer_last_fsync_off = 0;
    server.repl_transfer_fd = dfd;
    server.repl_transfer_lastio = server.unixtime;
    server.repl_transfer_tmpfile = zstrdup(tmpfile);
    return;

error:
    aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
    if (dfd != -1) close(dfd);
    close(fd);
    server.repl_transfer_s = -1;
    server.repl_state = REPL_STATE_CONNECT;
    return;

write_error: /* Handle sendSynchronousCommand(SYNC_CMD_WRITE) errors. */
    serverLog(LL_WARNING,"Sending command to master in replication handshake: %s", err);
    sdsfree(err);
    goto error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %esi, %ebx
movl $0x0, 0x8(%rsp)
movl $0x4, 0xc(%rsp)
leaq 0xbb481(%rip), %r12     # 0x1018a8
cmpl $0x0, 0x84c(%r12)
je 0x46458
leaq 0x8(%rsp), %rcx
leaq 0xc(%rsp), %r8
movl %ebx, %edi
movl $0x1, %esi
movl $0x4, %edx
callq 0x26700
cmpl $-0x1, %eax
je 0x46470
movl 0x8(%rsp), %edi
jmp 0x4647b
movl %ebx, %edi
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x26180
callq 0x26880
movl (%rax), %edi
movl %edi, 0x8(%rsp)
testl %edi, %edi
je 0x4649f
callq 0x26680
leaq 0x7bb90(%rip), %rsi     # 0xc201b
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
jmp 0x46787
movl 0x84c(%r12), %edx
leal -0x2(%rdx), %eax
cmpl $0x5, %eax
ja 0x465e7
leaq 0x7ad0e(%rip), %rcx     # 0xc11c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7bb78(%rip), %rsi     # 0xc2042
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x40(%r12), %rdi
movl %ebx, %esi
movl $0x2, %edx
callq 0x2cf0e
movl $0x3, 0x84c(%r12)
leaq 0x7bb77(%rip), %rdx     # 0xc2071
movl $0x2, %edi
movl %ebx, %esi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x45d04
testq %rax, %rax
jne 0x466ff
jmp 0x467b7
movl $0x1, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
cmpb $0x2d, (%rax)
jne 0x46704
leaq 0x7bbba(%rip), %rsi     # 0xc20f5
jmp 0x46770
movl $0x1, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
cmpb $0x2b, (%rax)
je 0x466aa
leaq 0x7bb13(%rip), %rsi     # 0xc2076
movl $0x7, %edx
movq %r14, %rdi
callq 0x26350
testl %eax, %eax
je 0x466aa
leaq 0x7baff(%rip), %rsi     # 0xc207e
movl $0x1c, %edx
movq %r14, %rdi
callq 0x26350
testl %eax, %eax
je 0x466aa
leaq 0x7bb00(%rip), %rsi     # 0xc209b
jmp 0x46770
movl $0x1, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
cmpb $0x2d, (%rax)
jne 0x465ce
leaq 0x7bb53(%rip), %rsi     # 0xc2112
movl $0x2, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x36330
movl $0x8, 0x84c(%r12)
movl $0x8, %edx
cmpl $0x8, %edx
sete %al
movq 0x8a0(%r12), %r8
testq %r8, %r8
sete %cl
testb %cl, %al
je 0x46652
movl $0xa, 0x84c(%r12)
leaq 0x765d9(%rip), %rdx     # 0xbcbeb
leaq 0x7b239(%rip), %rcx     # 0xc1852
leaq 0x76285(%rip), %r8      # 0xbc8a5
movl $0x2, %edi
movl %ebx, %esi
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x45d04
testq %rax, %rax
jne 0x466ff
xorl %edi, %edi
callq 0x36330
movl $0xb, 0x84c(%r12)
jmp 0x467b7
leal -0x8(%rdx), %eax
cmpl $0x5, %eax
ja 0x468bf
leaq 0x7ab7b(%rip), %rcx     # 0xc11e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x76576(%rip), %rdx     # 0xbcbeb
leaq 0x7b185(%rip), %rcx     # 0xc1801
movl $0x2, %edi
movl %ebx, %esi
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x45d04
testq %rax, %rax
jne 0x466ff
xorl %edi, %edi
callq 0x36330
movl $0x9, 0x84c(%r12)
jmp 0x467b7
leaq 0x7ba10(%rip), %rsi     # 0xc20c1
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x36330
movl $0x4, 0x84c(%r12)
movq 0x820(%r12), %rcx
testq %rcx, %rcx
je 0x4670c
leaq 0x7b994(%rip), %rdx     # 0xc2079
movl $0x2, %edi
movl %ebx, %esi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x45d04
testq %rax, %rax
je 0x467de
movq %rax, %r14
jmp 0x46769
movq %r14, %rdi
callq 0x36330
movl $0x6, 0x84c(%r12)
movl 0x898(%r12), %eax
testl %eax, %eax
jne 0x4672c
movl 0xa0(%r12), %eax
movslq %eax, %rdi
callq 0x36e1a
movq %rax, %r15
leaq 0x764ad(%rip), %rdx     # 0xbcbeb
leaq 0x7b0ad(%rip), %rcx     # 0xc17f2
movl $0x2, %edi
movl %ebx, %esi
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
movq %r15, %rdi
callq 0x36330
testq %r14, %r14
je 0x467c9
leaq 0x7bbe4(%rip), %rsi     # 0xc2354
movl $0x3, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x36330
movq 0x40(%r12), %rdi
movl %ebx, %esi
movl $0x3, %edx
callq 0x2cf0e
movl %ebx, %edi
callq 0x26180
movl $0xffffffff, 0x868(%r12) # imm = 0xFFFFFFFF
movl $0x1, 0x84c(%r12)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x36330
movl $0x7, 0x84c(%r12)
jmp 0x467b7
movl $0x5, 0x84c(%r12)
jmp 0x467b7
movl $0x1, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
cmpb $0x2d, (%rax)
jne 0x4681a
leaq 0x7b98f(%rip), %rsi     # 0xc219a
movl $0x2, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x36330
movl $0xc, 0x84c(%r12)
movl %ebx, %edi
xorl %esi, %esi
callq 0x45f6f
testl %eax, %eax
je 0x468d7
movl $0xd, 0x84c(%r12)
jmp 0x467b7
movl $0x1, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %r14
cmpb $0x2d, (%rax)
jne 0x4687e
leaq 0x7b8e9(%rip), %rsi     # 0xc2158
movl $0x2, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x36330
jmp 0x465ff
movl %ebx, %edi
movl $0x1, %esi
callq 0x45f6f
cmpl $0x1, %eax
je 0x467b7
movl %eax, %ebp
cmpl $0x2, %eax
jne 0x468e8
leaq 0x7b99e(%rip), %rsi     # 0xc224c
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x467b7
leaq 0x7b937(%rip), %rsi     # 0xc21fd
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x46787
leaq 0x7b8f8(%rip), %rdi     # 0xc21d6
callq 0x362b0
jmp 0x466ff
callq 0x3d715
callq 0x4400c
cmpl $0x4, %ebp
jne 0x46936
leaq 0x7b992(%rip), %rsi     # 0xc2290
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movslq 0x848(%r12), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
leaq 0x7b986(%rip), %rsi     # 0xc22a6
movl $0x6, %edx
movl %ebx, %edi
callq 0x64e6c
cmpq $-0x1, %rax
je 0x46a21
movl $0xfffffffb, %r13d      # imm = 0xFFFFFFFB
leaq 0x7b98a(%rip), %r14     # 0xc22cd
leaq 0x10(%rsp), %r15
movl 0x978(%r12), %ebp
callq 0x26420
movslq %eax, %r8
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x26120
movq %r15, %rdi
movl $0xc1, %esi
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x26690
cmpl $-0x1, %eax
jne 0x469ac
movl $0x1, %edi
callq 0x265f0
incl %r13d
jne 0x46948
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7b936(%rip), %rsi     # 0xc22dd
jmp 0x4648b
movl %eax, %ebp
movq 0x40(%r12), %rdi
leaq -0x1246(%rip), %rcx     # 0x45774
movl %ebx, %esi
movl $0x1, %edx
xorl %r8d, %r8d
callq 0x2ce46
cmpl $-0x1, %eax
je 0x46a39
movl $0xe, 0x84c(%r12)
movq $-0x1, 0x850(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x858(%r12)
movl %ebp, 0x86c(%r12)
movq 0x978(%r12), %rax
movq %rax, 0x878(%r12)
leaq 0x10(%rsp), %rdi
callq 0x3844d
movq %rax, 0x870(%r12)
jmp 0x467b7
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7b879(%rip), %rsi     # 0xc22ad
jmp 0x4648b
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7b8d7(%rip), %rsi     # 0xc2323
movl $0x3, %edi
movq %rax, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2fdad
movq 0x40(%r12), %rdi
movl %ebx, %esi
movl $0x3, %edx
callq 0x2cf0e
movl %ebp, %edi
callq 0x26180
jmp 0x46798