// File: /workspace/github_c_makefile_star2_O3/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 $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, %r14
callq 0x2ffce
movq %rax, 0x68(%rsp)
callq 0x26420
leaq 0x6674d(%rip), %rdx     # 0xc57aa
leaq 0xf0(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x26120
leaq 0x61d4a(%rip), %rsi     # 0xc0dc7
movq %rbx, %rdi
callq 0x26ae0
testq %rax, %rax
je 0x5f46a
movq %rax, %rbx
callq 0x3628d
leaq 0xa280b(%rip), %r15     # 0x1018a8
movq %rax, 0x718(%r15)
leaq 0x88(%rsp), %rdi
movq %rbx, 0x10(%rsp)
movq %rbx, %rsi
callq 0x726c2
cmpl $0x0, 0x6e8(%r15)
je 0x5f0d5
leaq 0x88(%rsp), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x727a0
movq %r14, 0x58(%rsp)
cmpl $0x0, 0x610(%r15)
jle 0x5f4a5
xorl %r13d, %r13d
movapd 0x66c5d(%rip), %xmm0    # 0xc5d50
leaq 0x88(%rsp), %rbp
movq $0x0, 0x48(%rsp)
movapd %xmm0, 0x70(%rsp)
movb $0x0, 0x80(%rsp)
movq 0x28(%r15), %r14
movq %r13, %rbx
shlq $0x6, %rbx
movq (%r14,%rbx), %rdi
movq 0x28(%rdi), %rax
addq 0x48(%rdi), %rax
jne 0x5f13f
incq %r13
movslq 0x610(%r15), %rax
cmpq %rax, %r13
jl 0x5f104
jmp 0x5f4a5
callq 0x2efe5
testq %rax, %rax
je 0x5f6df
movq %rax, 0x8(%rsp)
addq %rbx, %r14
movl $0x10, %r12d
leaq 0x70(%rsp), %rbx
movq 0xa8(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq %r12, %rcx
movq %r12, %r15
cmovbq %rcx, %r15
testq %rcx, %rcx
cmoveq %r12, %r15
testq %rax, %rax
je 0x5f191
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *%rax
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0x90(%rsp)
testq %rax, %rax
je 0x5f496
addq %r15, %rbx
addq %r15, 0xb8(%rsp)
subq %r15, %r12
jne 0x5f160
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7296b
testq %rax, %rax
je 0x5f496
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x2f015
testq %rax, %rax
leaq 0xa26c3(%rip), %r15     # 0x1018a8
je 0x5f455
movq %r14, 0x60(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %r14
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4302f
movq %rax, %rbx
cmpq $-0x1, %rax
setne %al
cmpq 0x68(%rsp), %rbx
setl %cl
testb %cl, %al
jne 0x5f444
movl (%r14), %eax
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x5f722
leaq 0x65cde(%rip), %rcx     # 0xc4f28
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, 0x50(%rsp)
movq %rbx, 0x28(%rsp)
movabsq $0xa0d5445530a0d, %rax  # imm = 0xA0D5445530A0D
movq %rax, 0x36(%rsp)
movabsq $0xa0d33240a0d332a, %rax # imm = 0xA0D33240A0D332A
movq %rax, 0x30(%rsp)
movl $0xd, %r12d
leaq 0x30(%rsp), %r15
movq 0xa8(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq %r12, %rcx
movq %r12, %rbx
cmovbq %rcx, %rbx
testq %rcx, %rcx
cmoveq %r12, %rbx
testq %rax, %rax
je 0x5f2b7
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *%rax
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *0x90(%rsp)
testq %rax, %rax
je 0x5f496
addq %rbx, %r15
addq %rbx, 0xb8(%rsp)
subq %rbx, %r12
jne 0x5f286
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x5e739
testl %eax, %eax
je 0x5f496
movq %rbp, %rdi
movq 0x50(%rsp), %rsi
callq 0x5e739
testl %eax, %eax
leaq 0xa259d(%rip), %r15     # 0x1018a8
movq 0x28(%rsp), %rbx
jne 0x5f365
jmp 0x5f496
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x5edeb
jmp 0x5f35d
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x5e912
jmp 0x5f35d
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x5eb0e
jmp 0x5f35d
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x5e7c2
testl %eax, %eax
je 0x5f496
cmpq $-0x1, %rbx
je 0x5f41d
movq %rbx, 0x28(%rsp)
movapd 0x669f4(%rip), %xmm0    # 0xc5d70
movapd %xmm0, 0x30(%rsp)
movl $0xa0d54, 0x40(%rsp)    # imm = 0xA0D54
movl $0x13, %r12d
leaq 0x30(%rsp), %rbx
movq 0xa8(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq %r12, %rcx
movq %r12, %r15
cmovbq %rcx, %r15
testq %rcx, %rcx
cmoveq %r12, %r15
testq %rax, %rax
je 0x5f3c6
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *%rax
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0x90(%rsp)
testq %rax, %rax
je 0x5f496
addq %r15, %rbx
addq %r15, 0xb8(%rsp)
subq %r15, %r12
jne 0x5f395
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x5e739
testl %eax, %eax
je 0x5f496
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x7296b
testq %rax, %rax
leaq 0xa248d(%rip), %r15     # 0x1018a8
je 0x5f496
movq 0xb8(%rsp), %rbx
movq 0x48(%rsp), %rax
addq $0x2800, %rax           # imm = 0x2800
cmpq %rax, %rbx
movq 0x8(%rsp), %r12
jbe 0x5f444
callq 0x5efab
movq %rbx, 0x48(%rsp)
movq %r12, %rdi
callq 0x2f015
testq %rax, %rax
jne 0x5f1f0
movq %r12, %rdi
callq 0x2f0dd
movapd 0x668eb(%rip), %xmm0    # 0xc5d50
jmp 0x5f12b
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66344(%rip), %rsi     # 0xc57c1
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x5f6cb
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r12
jmp 0x5f682
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x26ce0
cmpl $-0x1, %eax
je 0x5f67f
movq %r14, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26600
cmpl $-0x1, %eax
je 0x5f67f
callq 0x2ffce
movq %rax, %rbx
callq 0x2ffce
subq %rbx, %rax
cmpq $0x3e7, %rax            # imm = 0x3E7
jg 0x5f527
xorl %ebp, %ebp
movl 0x6fc(%r15), %edi
movl $0x1, %edx
movl $0x1, %esi
callq 0x2d4e5
testl %eax, %eax
jle 0x5f510
callq 0x5efab
xorl %ebp, %ebp
jmp 0x5f512
incl %ebp
callq 0x2ffce
subq %rbx, %rax
cmpq $0x3e7, %rax            # imm = 0x3E7
jg 0x5f527
cmpl $0x14, %ebp
jl 0x5f4ed
movl 0x700(%r15), %edi
leaq 0x65fe0(%rip), %rsi     # 0xc5515
movl $0x1, %edx
callq 0x26bb0
cmpq $0x1, %rax
jne 0x5f67f
movl 0x70c(%r15), %esi
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x2d6a1
testl %eax, %eax
jne 0x5f682
movl 0x70c(%r15), %edi
leaq 0x30(%rsp), %rsi
movl $0x1, %edx
movl $0x1388, %ecx           # imm = 0x1388
callq 0x64f0e
xorl %r12d, %r12d
cmpq $0x1, %rax
jne 0x5f682
cmpb $0x21, 0x30(%rsp)
jne 0x5f682
leaq 0x66278(%rip), %rsi     # 0xc5814
xorl %r12d, %r12d
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
callq 0x5efab
movq 0x718(%r15), %rdi
callq 0x5daac
movq %rax, %xmm1
punpckldq 0x63397(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x6339f(%rip), %xmm1    # 0xc2970
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5cf63(%rip), %xmm0    # 0xbc548
leaq 0x6625f(%rip), %rsi     # 0xc584b
movl $0x2, %edi
movb $0x1, %al
callq 0x2fdad
movq 0x718(%r15), %rbx
movq %rbx, %rdi
callq 0x5daac
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x5f744
testq %rax, %rax
je 0x5f682
movq %r14, %rdi
callq 0x26ce0
cmpl $-0x1, %eax
je 0x5f67f
movq %r14, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26600
cmpl $-0x1, %eax
je 0x5f67f
movq %r14, %rdi
callq 0x26a10
cmpl $-0x1, %eax
je 0x5f67f
leaq 0xf0(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x5f6ee
leaq 0x66254(%rip), %rsi     # 0xc58c3
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x5f6cb
xorl %r12d, %r12d
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66256(%rip), %rsi     # 0xc58eb
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r14, %rdi
callq 0x26a10
leaq 0xf0(%rsp), %rdi
callq 0x263b0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x5f6cb
movq %r12, %rdi
callq 0x2f0dd
movl %ebx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x26a10
jmp 0x5f48c
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x66182(%rip), %rsi     # 0xc5883
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
leaq 0xf0(%rsp), %rdi
callq 0x263b0
jmp 0x5f48c
leaq 0x615fd(%rip), %rdi     # 0xc0d26
leaq 0x6584b(%rip), %rsi     # 0xc4f7b
movl $0x44f, %edx            # imm = 0x44F
callq 0x619b4
movl $0x1, %edi
callq 0x263c0