// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/aof.c
// Function: rewriteAppendOnlyFile
int rewriteAppendOnlyFile(char *filename) {
    dictIterator *di = NULL;
    dictEntry *de;
    rio aof;
    FILE *fp;
    char tmpfile[256];
    int j;
    long long now = mstime();
    char byte;
    size_t processed = 0;

    /* Note that we have to use a different temp name here compared to the
     * one used by rewriteAppendOnlyFileBackground() function. */
    snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
    fp = fopen(tmpfile,"w");
    if (!fp) {
        serverLog(LL_WARNING, "Opening the temp file for AOF rewrite in rewriteAppendOnlyFile(): %s", strerror(errno));
        return C_ERR;
    }

    server.aof_child_diff = sdsempty();
    rioInitWithFile(&aof,fp);
    if (server.aof_rewrite_incremental_fsync)
        rioSetAutoSync(&aof,AOF_AUTOSYNC_BYTES);
    for (j = 0; j < server.dbnum; j++) {
        char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
        redisDb *db = server.db+j;
        dict *d = db->dict;
        if (dictSize(d) == 0) continue;
        di = dictGetSafeIterator(d);
        if (!di) {
            fclose(fp);
            return C_ERR;
        }

        /* SELECT the new DB */
        if (rioWrite(&aof,selectcmd,sizeof(selectcmd)-1) == 0) goto werr;
        if (rioWriteBulkLongLong(&aof,j) == 0) goto werr;

        /* Iterate this DB writing every entry */
        while((de = dictNext(di)) != NULL) {
            sds keystr;
            robj key, *o;
            long long expiretime;

            keystr = dictGetKey(de);
            o = dictGetVal(de);
            initStaticStringObject(key,keystr);

            expiretime = getExpire(db,&key);

            /* If this key is already expired skip it */
            if (expiretime != -1 && expiretime < now) continue;

            /* Save the key and associated value */
            if (o->type == OBJ_STRING) {
                /* Emit a SET command */
                char cmd[]="*3\r\n$3\r\nSET\r\n";
                if (rioWrite(&aof,cmd,sizeof(cmd)-1) == 0) goto werr;
                /* Key and value */
                if (rioWriteBulkObject(&aof,&key) == 0) goto werr;
                if (rioWriteBulkObject(&aof,o) == 0) goto werr;
            } else if (o->type == OBJ_LIST) {
                if (rewriteListObject(&aof,&key,o) == 0) goto werr;
            } else if (o->type == OBJ_SET) {
                if (rewriteSetObject(&aof,&key,o) == 0) goto werr;
            } else if (o->type == OBJ_ZSET) {
                if (rewriteSortedSetObject(&aof,&key,o) == 0) goto werr;
            } else if (o->type == OBJ_HASH) {
                if (rewriteHashObject(&aof,&key,o) == 0) goto werr;
            } else {
                serverPanic("Unknown object type");
            }
            /* Save the expire time */
            if (expiretime != -1) {
                char cmd[]="*3\r\n$9\r\nPEXPIREAT\r\n";
                if (rioWrite(&aof,cmd,sizeof(cmd)-1) == 0) goto werr;
                if (rioWriteBulkObject(&aof,&key) == 0) goto werr;
                if (rioWriteBulkLongLong(&aof,expiretime) == 0) goto werr;
            }
            /* Read some diff from the parent process from time to time. */
            if (aof.processed_bytes > processed+1024*10) {
                processed = aof.processed_bytes;
                aofReadDiffFromParent();
            }
        }
        dictReleaseIterator(di);
        di = NULL;
    }

    /* Do an initial slow fsync here while the parent is still sending
     * data, in order to make the next final fsync faster. */
    if (fflush(fp) == EOF) goto werr;
    if (fsync(fileno(fp)) == -1) goto werr;

    /* Read again a few times to get more data from the parent.
     * We can't read forever (the server may receive data from clients
     * faster than it is able to send data to the child), so we try to read
     * some more data in a loop as soon as there is a good chance more data
     * will come. If it looks like we are wasting time, we abort (this
     * happens after 20 ms without new data). */
    int nodata = 0;
    mstime_t start = mstime();
    while(mstime()-start < 1000 && nodata < 20) {
        if (aeWait(server.aof_pipe_read_data_from_parent, AE_READABLE, 1) <= 0)
        {
            nodata++;
            continue;
        }
        nodata = 0; /* Start counting from zero, we stop on N *contiguous*
                       timeouts. */
        aofReadDiffFromParent();
    }

    /* Ask the master to stop sending diffs. */
    if (write(server.aof_pipe_write_ack_to_parent,"!",1) != 1) goto werr;
    if (anetNonBlock(NULL,server.aof_pipe_read_ack_from_parent) != ANET_OK)
        goto werr;
    /* We read the ACK from the server using a 10 seconds timeout. Normally
     * it should reply ASAP, but just in case we lose its reply, we are sure
     * the child will eventually get terminated. */
    if (syncRead(server.aof_pipe_read_ack_from_parent,&byte,1,5000) != 1 ||
        byte != '!') goto werr;
    serverLog(LL_NOTICE,"Parent agreed to stop sending diffs. Finalizing AOF...");

    /* Read the final diff if any. */
    aofReadDiffFromParent();

    /* Write the received diff to the file. */
    serverLog(LL_NOTICE,
        "Concatenating %.2f MB of AOF diff received from parent.",
        (double) sdslen(server.aof_child_diff) / (1024*1024));
    if (rioWrite(&aof,server.aof_child_diff,sdslen(server.aof_child_diff)) == 0)
        goto werr;

    /* Make sure data will not remain on the OS's output buffers */
    if (fflush(fp) == EOF) goto werr;
    if (fsync(fileno(fp)) == -1) goto werr;
    if (fclose(fp) == EOF) goto werr;

    /* Use RENAME to make sure the DB file is changed atomically only
     * if the generate DB file is ok. */
    if (rename(tmpfile,filename) == -1) {
        serverLog(LL_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
        unlink(tmpfile);
        return C_ERR;
    }
    serverLog(LL_NOTICE,"SYNC append only file rewrite performed");
    return C_OK;

werr:
    serverLog(LL_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
    fclose(fp);
    unlink(tmpfile);
    if (di) dictReleaseIterator(di);
    return C_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, %r14
callq 0x2b8ae
movq %rax, 0x48(%rsp)
callq 0x26420
leaq 0x61363(%rip), %rdx     # 0xb6a96
leaq 0xe0(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x26120
leaq 0x5cf4c(%rip), %rsi     # 0xb269f
movq %rbx, %rdi
callq 0x26ae0
testq %rax, %rax
je 0x559f3
movq %rax, %rbx
movq %r14, 0x40(%rsp)
callq 0x3114c
leaq 0xa4130(%rip), %r15     # 0xf98a8
movq %rax, 0x718(%r15)
leaq 0x78(%rsp), %rdi
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
callq 0x66ab5
cmpl $0x0, 0x6e8(%r15)
je 0x557aa
leaq 0x78(%rsp), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x66b8f
xorl %ecx, %ecx
leaq 0x78(%rsp), %r12
leaq 0x50(%rsp), %r14
movq $0x0, 0x38(%rsp)
movslq 0x610(%r15), %rax
cmpq %rax, %rcx
jge 0x55a1b
movapd 0x61869(%rip), %xmm0    # 0xb7040
movapd %xmm0, 0x60(%rsp)
movb $0x0, 0x70(%rsp)
movq 0x28(%r15), %rbx
movq %rcx, %r13
shlq $0x6, %r13
movq (%rbx,%r13), %rdi
movq 0x28(%rdi), %rax
addq 0x48(%rdi), %rax
je 0x559eb
movq %rcx, 0x30(%rsp)
callq 0x2a9c4
testq %rax, %rax
je 0x55a9b
movq %rax, %r15
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x55cbc
testq %rax, %rax
je 0x55c0c
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x66ced
testq %rax, %rax
je 0x55c0c
addq %r13, %rbx
movq %r15, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x559d7
movq (%rax), %rcx
movq 0x8(%rax), %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3c0d3
movq %rax, %r13
cmpq $-0x1, %rax
setne %al
cmpq 0x48(%rsp), %r13
setl %cl
testb %cl, %al
jne 0x55847
movl (%rbp), %eax
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x55c9c
leaq 0x6096a(%rip), %rcx     # 0xb6214
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0xa0d5445530a0d, %rax  # imm = 0xA0D5445530A0D
movq %rax, 0x16(%rsp)
movabsq $0xa0d33240a0d332a, %rax # imm = 0xA0D33240A0D332A
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
pushq $0xd
popq %rdx
callq 0x55cbc
testq %rax, %rax
je 0x55c0c
movq %r12, %rdi
movq %r14, %rsi
callq 0x54ef3
testl %eax, %eax
je 0x55c0c
movq %r12, %rdi
movq %rbp, %rsi
callq 0x54ef3
jmp 0x55948
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x55505
jmp 0x55948
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x5508a
jmp 0x55948
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x55244
jmp 0x55948
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x54f5e
testl %eax, %eax
je 0x55c0c
cmpq $-0x1, %r13
je 0x559ac
movapd 0x61702(%rip), %xmm0    # 0xb7060
movapd %xmm0, 0x10(%rsp)
movl $0xa0d54, 0x20(%rsp)    # imm = 0xA0D54
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
pushq $0x13
popq %rdx
callq 0x55cbc
testq %rax, %rax
je 0x55c0c
movq %r12, %rdi
movq %r14, %rsi
callq 0x54ef3
testl %eax, %eax
je 0x55c0c
movq %r12, %rdi
movq %r13, %rsi
callq 0x66ced
testq %rax, %rax
je 0x55c0c
movq 0xa8(%rsp), %r13
movq 0x38(%rsp), %rax
addq $0x2800, %rax           # imm = 0x2800
cmpq %rax, %r13
jbe 0x55847
callq 0x556a0
movq %r13, 0x38(%rsp)
jmp 0x55847
movq %r15, %rdi
callq 0x2aa94
leaq 0xa3ec2(%rip), %r15     # 0xf98a8
movq 0x30(%rsp), %rcx
incq %rcx
jmp 0x557bf
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x610a7(%rip), %rsi     # 0xb6aad
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
pushq $-0x1
popq %rbx
jmp 0x55c56
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x26ce0
cmpl $-0x1, %eax
je 0x55c04
movq %r14, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26600
xorl %r15d, %r15d
cmpl $-0x1, %eax
je 0x55c11
callq 0x2b8ae
movq %rax, %rbx
pushq $0x1
popq %rbp
pushq $0x1
popq %r14
callq 0x2b8ae
subq %rbx, %rax
cmpq $0x3e7, %rax            # imm = 0x3E7
jg 0x55aaa
cmpl $0x13, %r15d
jg 0x55aaa
leaq 0xa3e30(%rip), %rax     # 0xf98a8
movl 0x6fc(%rax), %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x290e1
testl %eax, %eax
jle 0x55a96
callq 0x556a0
xorl %r15d, %r15d
jmp 0x55a5b
incl %r15d
jmp 0x55a5b
movq 0x8(%rsp), %rdi
callq 0x26a10
jmp 0x55a13
leaq 0xa3df7(%rip), %rbx     # 0xf98a8
movl 0x700(%rbx), %edi
leaq 0x60d43(%rip), %rsi     # 0xb6801
pushq $0x1
popq %rdx
callq 0x26bb0
cmpq $0x1, %rax
jne 0x55c09
movl 0x70c(%rbx), %esi
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x2929c
testl %eax, %eax
movq 0x8(%rsp), %r14
jne 0x55c11
movl 0x70c(%rbx), %edi
leaq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
movl $0x1388, %ecx           # imm = 0x1388
callq 0x5aa82
xorl %r15d, %r15d
cmpq $0x1, %rax
jne 0x55c11
cmpb $0x21, 0x10(%rsp)
leaq 0xa3d8a(%rip), %r12     # 0xf98a8
jne 0x55c11
leaq 0x60fd5(%rip), %rsi     # 0xb6b00
xorl %r15d, %r15d
pushq $0x2
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x2b699
callq 0x556a0
movq 0x718(%r12), %rdi
callq 0x543cb
movq %rax, %xmm1
punpckldq 0x5e4c7(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x5e4cf(%rip), %xmm1    # 0xb4030
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x588c3(%rip), %xmm0    # 0xae438
leaq 0x60fbb(%rip), %rsi     # 0xb6b37
movl %ebx, %edi
movb $0x1, %al
callq 0x2b699
movq 0x718(%r12), %rbx
movq %rbx, %rdi
callq 0x543cb
leaq 0x78(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x55cbc
testq %rax, %rax
je 0x55c11
movq %r14, %rdi
callq 0x26ce0
cmpl $-0x1, %eax
je 0x55c04
movq %r14, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26600
cmpl $-0x1, %eax
je 0x55c04
movq %r14, %rdi
callq 0x26a10
cmpl $-0x1, %eax
je 0x55c04
leaq 0xe0(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x55c6a
leaq 0x60fb9(%rip), %rsi     # 0xb6baf
xorl %ebx, %ebx
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x55c56
xorl %r15d, %r15d
jmp 0x55c11
xorl %r15d, %r15d
movq 0x8(%rsp), %r14
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x60fb3(%rip), %rsi     # 0xb6bd7
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
movq %r14, %rdi
callq 0x26a10
leaq 0xe0(%rsp), %rdi
callq 0x263b0
pushq $-0x1
popq %rbx
testq %r15, %r15
je 0x55c56
movq %r15, %rdi
callq 0x2aa94
movl %ebx, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x60ef2(%rip), %rsi     # 0xb6b6f
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
leaq 0xe0(%rsp), %rdi
callq 0x263b0
jmp 0x55a13
leaq 0x5c95b(%rip), %rdi     # 0xb25fe
leaq 0x605bd(%rip), %rsi     # 0xb6267
movl $0x44f, %edx            # imm = 0x44F
callq 0x57b45
pushq $0x1
popq %rdi
callq 0x263c0