// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 0x330c8
movq %rax, %r15
callq 0x26430
leaq 0x65b90(%rip), %rdx     # 0xc8729
leaq 0xf0(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x26120
leaq 0x611be(%rip), %rsi     # 0xc3d77
movq %rbx, %rdi
callq 0x26af0
testq %rax, %rax
je 0x6305f
movq %rax, %r13
callq 0x39222
leaq 0xa3c7f(%rip), %rbx     # 0x106858
movq %rax, 0x708(%rbx)
leaq 0x88(%rsp), %rdi
movq %r13, %rsi
callq 0x76172
cmpl $0x0, 0x6d8(%rbx)
je 0x62c0b
leaq 0x88(%rsp), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x76250
movq %r14, 0x68(%rsp)
cmpl $0x0, 0x600(%rbx)
jle 0x63086
xorl %r14d, %r14d
leaq 0x88(%rsp), %rbp
movq $0x0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
movq %r13, 0x28(%rsp)
movq %r15, 0x8(%rsp)
movapd 0x66084(%rip), %xmm0    # 0xc8cd0
movapd %xmm0, 0x70(%rsp)
movb $0x0, 0x80(%rsp)
movq 0x28(%rbx), %rax
movq %r14, 0x58(%rsp)
shlq $0x6, %r14
movq %rax, 0x60(%rsp)
movq (%rax,%r14), %rdi
movq 0x28(%rdi), %rax
movl $0x4, %ebx
addq 0x48(%rdi), %rax
je 0x6302e
callq 0x320e5
testq %rax, %rax
je 0x63011
movq %rax, 0x10(%rsp)
addq %r14, 0x60(%rsp)
movl $0x10, %ecx
leaq 0x70(%rsp), %r14
testq %rcx, %rcx
je 0x62d1c
movq %rcx, %r12
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 0x62cde
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *%rax
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *0x90(%rsp)
testq %rax, %rax
je 0x62d06
addq %r15, %r14
movq %r12, %rcx
subq %r15, %rcx
addq %r15, 0xb8(%rsp)
jmp 0x62d09
movq %r12, %rcx
testq %rax, %rax
jne 0x62ca5
movl $0x5, %ebx
testq %r12, %r12
jne 0x6302e
movq %rbp, %rdi
movq 0x58(%rsp), %rsi
callq 0x7643f
testq %rax, %rax
je 0x63029
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x32116
testq %rax, %rax
movq 0x8(%rsp), %r15
je 0x63005
movq (%rax), %rcx
movq 0x8(%rax), %r12
movl $0x1, 0x1c(%rsp)
movb $0x0, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x46484
movq %rax, %r14
cmpq $-0x1, %rax
setne %al
cmpq %r15, %r14
setl %cl
movl $0x6, %ebx
testb %cl, %al
jne 0x62fe6
movl (%r12), %eax
andl $0xf, %eax
cmpl $0x4, %eax
ja 0x632f8
leaq 0x65113(%rip), %rcx     # 0xc7ebc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0xa0d5445530a0d, %rax  # imm = 0xA0D5445530A0D
movq %rax, 0x36(%rsp)
movabsq $0xa0d33240a0d332a, %rax # imm = 0xA0D33240A0D332A
movq %rax, 0x30(%rsp)
movl $0xd, %ecx
leaq 0x30(%rsp), %r15
testq %rcx, %rcx
je 0x62e4c
movq %rcx, %r13
movq 0xa8(%rsp), %rax
movq 0xc0(%rsp), %rcx
cmpq %r13, %rcx
movq %r13, %rbx
cmovbq %rcx, %rbx
testq %rcx, %rcx
cmoveq %r13, %rbx
testq %rax, %rax
je 0x62e13
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 0x62e3b
addq %rbx, %r15
movq %r13, %rcx
subq %rbx, %rcx
addq %rbx, 0xb8(%rsp)
jmp 0x62e3e
movq %r13, %rcx
testq %rax, %rax
jne 0x62dda
testq %r13, %r13
jne 0x62fcd
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x6225d
testl %eax, %eax
je 0x62fcd
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6225d
xorl %ecx, %ecx
testl %eax, %eax
setne %al
sete %cl
leal (%rcx,%rcx,4), %ebx
jmp 0x62fd4
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x62929
jmp 0x62ec4
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x62434
jmp 0x62ec4
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x62638
jmp 0x62ec4
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x622e6
testl %eax, %eax
je 0x62f90
cmpq $-0x1, %r14
je 0x62fa7
movapd 0x65e12(%rip), %xmm0    # 0xc8cf0
movapd %xmm0, 0x30(%rsp)
movl $0xa0d54, 0x40(%rsp)    # imm = 0xA0D54
movl $0x13, %ecx
leaq 0x30(%rsp), %r15
testq %rcx, %rcx
je 0x62f64
movq %rcx, %r12
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 0x62f2f
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 0x62f57
addq %rbx, %r15
movq %r12, %rcx
subq %rbx, %rcx
addq %rbx, 0xb8(%rsp)
jmp 0x62f5a
movq %r12, %rcx
testq %rax, %rax
jne 0x62ef6
testq %r12, %r12
jne 0x62f97
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x6225d
testl %eax, %eax
je 0x62f97
movq %rbp, %rdi
movq %r14, %rsi
callq 0x7643f
xorl %ecx, %ecx
testq %rax, %rax
setne %al
sete %cl
leal (%rcx,%rcx,4), %ebx
jmp 0x62f9e
movl $0x5, %ebx
jmp 0x62fe6
movl $0x5, %ebx
xorl %eax, %eax
movq 0x8(%rsp), %r15
testb %al, %al
je 0x62fe6
movq 0xb8(%rsp), %r14
movq 0x50(%rsp), %rax
addq $0x2800, %rax           # imm = 0x2800
xorl %ebx, %ebx
cmpq %rax, %r14
jbe 0x62fe6
callq 0x62ae9
movq %r14, 0x50(%rsp)
jmp 0x62fe6
movl $0x5, %ebx
xorl %eax, %eax
movq 0x28(%rsp), %r13
movq 0x8(%rsp), %r15
testb %al, %al
jne 0x62ecc
cmpl $0x6, %ebx
je 0x62fef
testl %ebx, %ebx
jne 0x6302e
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x32116
testq %rax, %rax
jne 0x62d4d
movq %rbx, %rdi
callq 0x321d3
xorl %ebx, %ebx
jmp 0x6301e
movq %r13, %rdi
callq 0x26a20
movl $0x1, %ebx
movq $0x0, 0x10(%rsp)
jmp 0x6302e
movl $0x5, %ebx
testl %ebx, %ebx
movq 0x58(%rsp), %rax
je 0x63040
cmpl $0x4, %ebx
jne 0x6325e
incq %rax
leaq 0xa380e(%rip), %rbx     # 0x106858
movq %rax, %r14
movslq 0x600(%rbx), %rax
cmpq %rax, %r14
jl 0x62c44
jmp 0x6308f
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x656ce(%rip), %rsi     # 0xc8740
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
jmp 0x632df
movq $0x0, 0x10(%rsp)
movq %r13, %rdi
callq 0x26cf0
cmpl $-0x1, %eax
je 0x63263
movq %r13, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26610
cmpl $-0x1, %eax
je 0x63263
movq %rbx, %r14
callq 0x330c8
movq %rax, %rbx
callq 0x330c8
subq %rbx, %rax
cmpq $0x3e7, %rax            # imm = 0x3E7
jg 0x6310f
xorl %ebp, %ebp
movl 0x6ec(%r14), %edi
movl $0x1, %edx
movl $0x1, %esi
callq 0x305c8
testl %eax, %eax
jle 0x630f8
callq 0x62ae9
xorl %ebp, %ebp
jmp 0x630fa
incl %ebp
callq 0x330c8
subq %rbx, %rax
cmpq $0x3e7, %rax            # imm = 0x3E7
jg 0x6310f
cmpl $0x14, %ebp
jl 0x630d5
movq %r14, %rbx
movl 0x6f0(%r14), %edi
leaq 0x65374(%rip), %rsi     # 0xc8494
movl $0x1, %edx
callq 0x26bc0
cmpq $0x1, %rax
jne 0x63263
movl 0x6fc(%rbx), %esi
xorl %edi, %edi
callq 0x30779
testl %eax, %eax
jne 0x63263
movl 0x6fc(%rbx), %edi
leaq 0x30(%rsp), %rsi
movl $0x1, %edx
movl $0x1388, %ecx           # imm = 0x1388
callq 0x68b45
cmpq $0x1, %rax
jne 0x63263
cmpb $0x21, 0x30(%rsp)
jne 0x63263
leaq 0x65614(%rip), %rsi     # 0xc8793
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
callq 0x62ae9
movq 0x708(%rbx), %rdi
callq 0x616bb
movq %rax, %xmm1
punpckldq 0x62737(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x6273f(%rip), %xmm1    # 0xc58f0
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
mulsd 0x5c333(%rip), %xmm0    # 0xbf4f8
leaq 0x655fe(%rip), %rsi     # 0xc87ca
movl $0x2, %edi
movb $0x1, %al
callq 0x32ea6
movq 0x708(%rbx), %rbx
movq %rbx, %rdi
callq 0x616bb
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x6331a
testq %rax, %rax
je 0x63263
movq %r13, %rdi
callq 0x26cf0
cmpl $-0x1, %eax
je 0x63263
movq %r13, %rdi
callq 0x26070
movl %eax, %edi
callq 0x26610
cmpl $-0x1, %eax
je 0x63263
movq %r13, %rdi
callq 0x26a20
cmpl $-0x1, %eax
je 0x63263
leaq 0xf0(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x26cb0
cmpl $-0x1, %eax
je 0x632b0
leaq 0x655f7(%rip), %rsi     # 0xc8842
xorl %ebx, %ebx
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x632e4
cmpl $0x5, %ebx
jne 0x632df
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x655f4(%rip), %rsi     # 0xc886a
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
movq %r13, %rdi
callq 0x26a20
leaq 0xf0(%rsp), %rdi
callq 0x263c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x632e4
callq 0x321d3
jmp 0x632e4
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x6553f(%rip), %rsi     # 0xc8802
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
leaq 0xf0(%rsp), %rdi
callq 0x263c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x609d7(%rip), %rdi     # 0xc3cd6
leaq 0x64c09(%rip), %rsi     # 0xc7f0f
movl $0x478, %edx            # imm = 0x478
callq 0x6558d
movl $0x1, %edi
callq 0x263d0