// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rdi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
callq 0x2ff70
movq %rax, -0x1a0(%rbp)
movq $0x0, -0x1b0(%rbp)
leaq -0x190(%rbp), %rax
movq %rax, -0x248(%rbp)
callq 0x26420
movq -0x248(%rbp), %rdi
movl %eax, %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0xcc520(%rip), %rdx     # 0x13fba6
movb $0x0, %al
callq 0x26120
leaq -0x190(%rbp), %rdi
leaq 0xc8166(%rip), %rsi     # 0x13b801
callq 0x26ae0
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x736df
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xcc4f1(%rip), %rsi     # 0x13fbbd
movb $0x0, %al
callq 0x2fc60
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73e76
callq 0x384f0
movq %rax, %rcx
leaq 0x104aea(%rip), %rax    # 0x1781d8
movq %rcx, 0x718(%rax)
movq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8e620
leaq 0x104ac9(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x6e8(%rax)
je 0x73729
leaq -0x88(%rbp), %rdi
movl $0x2000000, %esi        # imm = 0x2000000
callq 0x8e7c0
movl $0x0, -0x194(%rbp)
movl -0x194(%rbp), %eax
leaq 0x104a98(%rip), %rcx    # 0x1781d8
cmpl 0x610(%rcx), %eax
jge 0x73b75
movq 0xcc9fd(%rip), %rax     # 0x140150
movq %rax, -0x1d0(%rbp)
movq 0xcc9f7(%rip), %rax     # 0x140158
movq %rax, -0x1c8(%rbp)
movb 0xcc9f2(%rip), %al      # 0x140160
movb %al, -0x1c0(%rbp)
leaq 0x104a5d(%rip), %rax    # 0x1781d8
movq 0x28(%rax), %rax
movslq -0x194(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x1e0(%rbp), %rcx
addq 0x48(%rcx), %rax
cmpq $0x0, %rax
jne 0x737c6
jmp 0x73b61
movq -0x1e0(%rbp), %rdi
callq 0x2e470
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x737f5
movq -0x90(%rbp), %rdi
callq 0x26a10
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73e76
leaq -0x1d0(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x10, %edx
callq 0x73e90
cmpq $0x0, %rax
jne 0x73818
jmp 0x73e25
movslq -0x194(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8ea70
cmpq $0x0, %rax
jne 0x73836
jmp 0x73e25
jmp 0x73838
movq -0x18(%rbp), %rdi
callq 0x2e4a0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x73b50
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1e8(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x200(%rbp)
movl $0x1, -0x1f4(%rbp)
movl -0x1f8(%rbp), %eax
andl $-0x10, %eax
orl $0x0, %eax
movl %eax, -0x1f8(%rbp)
movl -0x1f8(%rbp), %eax
andl $0xffffff0f, %eax       # imm = 0xFFFFFF0F
orl $0x0, %eax
movl %eax, -0x1f8(%rbp)
movq -0x1e8(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movq -0x1d8(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x4c430
movq %rax, -0x208(%rbp)
cmpq $-0x1, -0x208(%rbp)
je 0x738e3
movq -0x208(%rbp), %rax
cmpq -0x1a0(%rbp), %rax
jge 0x738e3
jmp 0x73838
movq -0x200(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x73982
movq 0xcc303(%rip), %rax     # 0x13fc02
movq %rax, -0x216(%rbp)
movl 0xcc2fe(%rip), %eax     # 0x13fc0a
movl %eax, -0x20e(%rbp)
movw 0xcc2f5(%rip), %ax      # 0x13fc0e
movw %ax, -0x20a(%rbp)
leaq -0x216(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0xd, %edx
callq 0x73e90
cmpq $0x0, %rax
jne 0x73943
jmp 0x73e25
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x729c0
cmpl $0x0, %eax
jne 0x73960
jmp 0x73e25
movq -0x200(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x729c0
cmpl $0x0, %eax
jne 0x7397d
jmp 0x73e25
jmp 0x73a8e
movq -0x200(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x1, %eax
jne 0x739bc
movq -0x200(%rbp), %rdx
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x72a80
cmpl $0x0, %eax
jne 0x739b7
jmp 0x73e25
jmp 0x73a8c
movq -0x200(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x739f6
movq -0x200(%rbp), %rdx
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x72c40
cmpl $0x0, %eax
jne 0x739f1
jmp 0x73e25
jmp 0x73a8a
movq -0x200(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x3, %eax
jne 0x73a2d
movq -0x200(%rbp), %rdx
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x72f00
cmpl $0x0, %eax
jne 0x73a2b
jmp 0x73e25
jmp 0x73a88
movq -0x200(%rbp), %rax
movl (%rax), %eax
andl $0xf, %eax
cmpl $0x4, %eax
jne 0x73a64
movq -0x200(%rbp), %rdx
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x73340
cmpl $0x0, %eax
jne 0x73a62
jmp 0x73e25
jmp 0x73a86
leaq 0xc7cdf(%rip), %rdi     # 0x13b74a
leaq 0xcb905(%rip), %rsi     # 0x13f377
movl $0x44f, %edx            # imm = 0x44F
callq 0x76d80
movl $0x1, %edi
callq 0x263c0
jmp 0x73a88
jmp 0x73a8a
jmp 0x73a8c
jmp 0x73a8e
cmpq $-0x1, -0x208(%rbp)
je 0x73b24
movq 0xcc6cd(%rip), %rax     # 0x140170
movq %rax, -0x230(%rbp)
movq 0xcc6c7(%rip), %rax     # 0x140178
movq %rax, -0x228(%rbp)
movl 0xcc6c2(%rip), %eax     # 0x140180
movl %eax, -0x220(%rbp)
leaq -0x230(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x13, %edx
callq 0x73e90
cmpq $0x0, %rax
jne 0x73ae7
jmp 0x73e25
leaq -0x88(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x729c0
cmpl $0x0, %eax
jne 0x73b04
jmp 0x73e25
movq -0x208(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8ea70
cmpq $0x0, %rax
jne 0x73b22
jmp 0x73e25
jmp 0x73b24
movq -0x58(%rbp), %rax
movq -0x1b0(%rbp), %rcx
addq $0x2800, %rcx           # imm = 0x2800
cmpq %rcx, %rax
jbe 0x73b4b
movq -0x58(%rbp), %rax
movq %rax, -0x1b0(%rbp)
callq 0x73590
jmp 0x73838
movq -0x18(%rbp), %rdi
callq 0x2e600
movq $0x0, -0x18(%rbp)
movl -0x194(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x194(%rbp)
jmp 0x73733
movq -0x90(%rbp), %rdi
callq 0x26cf0
cmpl $-0x1, %eax
jne 0x73b8b
jmp 0x73e25
movq -0x90(%rbp), %rdi
callq 0x26070
movl %eax, %edi
callq 0x26610
cmpl $-0x1, %eax
jne 0x73ba8
jmp 0x73e25
movl $0x0, -0x234(%rbp)
callq 0x2ff70
movq %rax, -0x240(%rbp)
callq 0x2ff70
movq %rax, %rcx
subq -0x240(%rbp), %rcx
xorl %eax, %eax
cmpq $0x3e8, %rcx            # imm = 0x3E8
movb %al, -0x249(%rbp)
jge 0x73bee
cmpl $0x14, -0x234(%rbp)
setl %al
movb %al, -0x249(%rbp)
movb -0x249(%rbp), %al
testb $0x1, %al
jne 0x73bfa
jmp 0x73c3d
leaq 0x1045d7(%rip), %rax    # 0x1781d8
movl 0x6fc(%rax), %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x2b540
cmpl $0x0, %eax
jg 0x73c2c
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
jmp 0x73bbe
movl $0x0, -0x234(%rbp)
callq 0x73590
jmp 0x73bbe
leaq 0x104594(%rip), %rax    # 0x1781d8
movl 0x700(%rax), %edi
leaq 0xcbcc0(%rip), %rsi     # 0x13f911
movl $0x1, %edx
callq 0x26bc0
cmpq $0x1, %rax
je 0x73c66
jmp 0x73e25
leaq 0x10456b(%rip), %rax    # 0x1781d8
movl 0x70c(%rax), %esi
xorl %eax, %eax
movl %eax, %edi
callq 0x2b860
cmpl $0x0, %eax
je 0x73c86
jmp 0x73e25
leaq 0x10454b(%rip), %rax    # 0x1781d8
movl 0x70c(%rax), %edi
leaq -0x1a1(%rbp), %rsi
movl $0x1, %edx
movl $0x1388, %ecx           # imm = 0x1388
callq 0x7bbf0
cmpq $0x1, %rax
jne 0x73cbb
movsbl -0x1a1(%rbp), %eax
cmpl $0x21, %eax
je 0x73cc0
jmp 0x73e25
leaq 0xcbf49(%rip), %rsi     # 0x13fc10
xorl %eax, %eax
movl $0x2, %edi
callq 0x2fc60
callq 0x73590
leaq 0x1044f9(%rip), %rax    # 0x1781d8
movq 0x718(%rax), %rdi
callq 0x71850
movq %rax, %xmm0
movaps 0xc37a9(%rip), %xmm1    # 0x1374a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xc37ad(%rip), %xmm1    # 0x1374b0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xc376e(%rip), %xmm1    # 0x137488
divsd %xmm1, %xmm0
movl $0x2, %edi
leaq 0xcbf1d(%rip), %rsi     # 0x13fc47
movb $0x1, %al
callq 0x2fc60
leaq 0x1044a0(%rip), %rax    # 0x1781d8
movq 0x718(%rax), %rax
movq %rax, -0x258(%rbp)
leaq 0x10448b(%rip), %rax    # 0x1781d8
movq 0x718(%rax), %rdi
callq 0x71850
movq -0x258(%rbp), %rsi
movq %rax, %rdx
leaq -0x88(%rbp), %rdi
callq 0x73e90
cmpq $0x0, %rax
jne 0x73d7a
jmp 0x73e25
movq -0x90(%rbp), %rdi
callq 0x26cf0
cmpl $-0x1, %eax
jne 0x73d90
jmp 0x73e25
movq -0x90(%rbp), %rdi
callq 0x26070
movl %eax, %edi
callq 0x26610
cmpl $-0x1, %eax
jne 0x73daa
jmp 0x73e25
movq -0x90(%rbp), %rdi
callq 0x26a10
cmpl $-0x1, %eax
jne 0x73dbd
jmp 0x73e25
leaq -0x190(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x26cb0
cmpl $-0x1, %eax
jne 0x73e09
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xcbe92(%rip), %rsi     # 0x13fc7f
movb $0x0, %al
callq 0x2fc60
leaq -0x190(%rbp), %rdi
callq 0x263b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x73e76
movl $0x2, %edi
leaq 0xcbeaa(%rip), %rsi     # 0x13fcbf
movb $0x0, %al
callq 0x2fc60
movl $0x0, -0x4(%rbp)
jmp 0x73e76
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xcbea7(%rip), %rsi     # 0x13fce7
movb $0x0, %al
callq 0x2fc60
movq -0x90(%rbp), %rdi
callq 0x26a10
leaq -0x190(%rbp), %rdi
callq 0x263b0
cmpq $0x0, -0x18(%rbp)
je 0x73e6f
movq -0x18(%rbp), %rdi
callq 0x2e600
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw %cs:(%rax,%rax)