// File: /workspace/github_c_makefile_star2_O3/yancanchen[P]mc-bloom-filter/mc_bloom_filter.c
// Function: process_stat
void process_stat(conn *c, char *command) {
    time_t now = time(0);
    if (strcmp(command, "stats") == 0) {
        char temp[1024];
        pid_t pid = getpid();
        char *pos = temp;
        struct rusage usage;

        getrusage(RUSAGE_SELF, &usage);

        pos += sprintf(pos, "STAT pid %u\r\n", pid);
        pos += sprintf(pos, "STAT uptime %lu\r\n", now - stats.started);
        pos += sprintf(pos, "STAT time %ld\r\n", now);
        //bloom status
        pos += sprintf(pos, "STAT max_memory %lu\r\n", (unsigned long)blooms->max_bytes);
        pos += sprintf(pos, "STAT used_memory %ld\r\n", (unsigned long)blooms->bytes);
        pos += sprintf(pos, "STAT bloom_count %d\r\n", blooms->count);
        pos += sprintf(pos, "STAT bloom_power %d\r\n", blooms->power);

        pos += sprintf(pos, "STAT pointer_size %lu\r\n", (unsigned long)(8 * sizeof(void*)));
        pos += sprintf(pos, "STAT rusage_user %ld.%06ld\r\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
        pos += sprintf(pos, "STAT rusage_system %ld.%06ld\r\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
        pos += sprintf(pos, "STAT curr_connections %u\r\n", stats.curr_conns - 1); /* ignore listening conn */
        pos += sprintf(pos, "STAT total_connections %u\r\n", stats.total_conns);
        pos += sprintf(pos, "STAT connection_structures %u\r\n", stats.conn_structs);
        pos += sprintf(pos, "STAT bytes_read %llu\r\n", stats.bytes_read);
        pos += sprintf(pos, "STAT bytes_written %llu\r\n", stats.bytes_written);
        pos += sprintf(pos, "END");
        out_string(c, temp);
        return;
    }
    
    //show all the blooms info    
    if(strcmp(command,"stats blooms") == 0){

        char *p = c->wbuf;
        char *end  = c->wbuf + c->wsize - 6;
        if(!list_empty(&blooms->list)) {
            bloom_t *bl;
            list_for_each_entry(bl, &blooms->list, list) {
                //32+1+21+2
                if(end - p <= 55) {
                    char *new_wbuf = realloc(c->wbuf, c->wsize*2);    
                    if(!new_wbuf){
                        fprintf(stderr, "Couldn't realloc input buffer\n");
                        out_string(c, "SERVER_ERROR out of memory");
                        return;
                    }
                    c->wbuf = new_wbuf;
                    c->wcurr = c->wbuf;
                    c->wsize *= 2;
                    p = c->wbuf + c->wbytes;
                    end  = c->wbuf + c->wsize - 6;
                }
                p += snprintf(p,end - p, "%s %lu\r\n", bl->key,(unsigned long)bl->bytes);
                c->wbytes = p - c->wbuf;
            }
        }
        p += sprintf(p, "END\r\n");
        c->wbytes = p - c->wbuf;
        c->wcurr = c->wbuf;
        c->state = conn_write;
        c->write_and_go = conn_read;
        
        return;
    }

    //stats bloom key
    if(strncmp(command,"stats bloom ",12) == 0){
        char key[MAX_KEY_LEN];
        int res;
        res = sscanf(command + 12, " %32s\n", key);
        if (res!=1 || strlen(key)==0 ) {
            out_string(c, "CLIENT_ERROR bad command line format,'stats bloom key'");    
            return;
        }

        char buff[1024];
        char *pos1 = buff;
        bloom_t *bloom = blooms_search(key);

        if(!bloom){
            pos1 += sprintf(pos1,"key %s not exists\r\n", key);
        }else{
            pos1 += sprintf(pos1, "STAT key %s\r\n", bloom->key);
            pos1 += sprintf(pos1, "STAT elements %llu\r\n", bloom->n);
            pos1 += sprintf(pos1, "STAT table_size %llu\r\n", bloom->m);
            pos1 += sprintf(pos1, "STAT table_size_bytes %lu\r\n", (unsigned long)bloom->bytes);
            pos1 += sprintf(pos1, "STAT false_positive_rate %f\r\n", bloom->e);
            pos1 += sprintf(pos1, "STAT hash_functions_count %d\r\n", bloom->k);
            pos1 += sprintf(pos1, "STAT get_count %llu\r\n", bloom->get_count);
            pos1 += sprintf(pos1, "STAT get_miss_count %llu\r\n", bloom->get_miss_count);
            pos1 += sprintf(pos1, "STAT set_count %llu\r\n", bloom->set_count);

        }
        pos1 += sprintf(pos1, "END");
        
        out_string(c,buff);    
        return;    
    }

    if (strcmp(command, "stats reset") == 0) {
        stats_reset();
        out_string(c, "RESET");
        return;
    }

    out_string(c, "ERROR");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x498, %rsp            # imm = 0x498
movq %rsi, %r14
movq %rdi, %rbx
xorl %edi, %edi
callq 0x2270
movq %rax, %r12
leaq 0x2ce6(%rip), %rsi      # 0x6451
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x37cd
leaq 0x2e3e(%rip), %rsi      # 0x65bc
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x39c2
leaq 0x2e7d(%rip), %rsi      # 0x6612
movl $0xc, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3a93
leaq 0x2f9a(%rip), %rsi      # 0x674b
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x3bcb
leaq 0x2c83(%rip), %rsi      # 0x644b
jmp 0x3bf8
callq 0x20e0
movl %eax, %ebp
movq %rsp, %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x2350
leaq 0x2c6f(%rip), %rsi      # 0x6457
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x23a0
cltq
leaq (%rsp,%rax), %r13
addq $0x90, %r13
movq %r12, %rdx
subq 0x5a5d(%rip), %rdx      # 0x9270
leaq 0x2c4b(%rip), %rsi      # 0x6465
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %rbp
addq %r13, %rbp
leaq 0x2c46(%rip), %rsi      # 0x6477
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %rbp, %r12
leaq 0x59ed(%rip), %rbp      # 0x9238
movq (%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x2c2d(%rip), %rsi      # 0x6487
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x2c24(%rip), %rsi      # 0x649d
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
movq (%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x2c1d(%rip), %rsi      # 0x64b4
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%rbp), %rax
movl 0x24(%rax), %edx
leaq 0x2c15(%rip), %rsi      # 0x64ca
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
leaq 0x2c14(%rip), %rsi      # 0x64e0
movl $0x40, %edx
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%r15), %rdx
movq 0x8(%r15), %rcx
leaq 0x2c09(%rip), %rsi      # 0x64f8
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rcx
leaq 0x2c07(%rip), %rsi      # 0x6515
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x5924(%rip), %edx      # 0x9248
decl %edx
leaq 0x2c07(%rip), %rsi      # 0x6534
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movl 0x5909(%rip), %edx      # 0x924c
leaq 0x2c05(%rip), %rsi      # 0x654f
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x58f0(%rip), %edx      # 0x9250
leaq 0x2c04(%rip), %rsi      # 0x656b
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x58fa(%rip), %rdx      # 0x9278
leaq 0x2c06(%rip), %rsi      # 0x658b
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x58e4(%rip), %rdx      # 0x9280
leaq 0x2bff(%rip), %rsi      # 0x65a2
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
cltq
movl $0x444e45, (%rax,%r15)  # imm = 0x444E45
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x3c8b
movq 0xb0(%rbx), %rdi
leaq 0x5868(%rip), %r12      # 0x9238
movq (%r12), %rax
movq (%rax), %r13
cmpq %rax, %r13
je 0x3c11
movslq 0xc0(%rbx), %rax
leaq (%rdi,%rax), %rbp
addq $-0x6, %rbp
leaq 0x2c0d(%rip), %r14      # 0x6603
movq %rdi, %r15
movq %rbp, %rsi
subq %r15, %rsi
cmpq $0x37, %rsi
jg 0x3a54
movslq 0xc0(%rbx), %rsi
addq %rsi, %rsi
callq 0x22c0
testq %rax, %rax
je 0x3ca2
movq %rax, 0xb0(%rbx)
movq %rax, 0xb8(%rbx)
movslq 0xc0(%rbx), %rcx
leaq (%rcx,%rcx), %rdx
movl %edx, 0xc0(%rbx)
movslq 0xc4(%rbx), %r15
addq %rax, %r15
leaq (%rax,%rcx,2), %rbp
addq $-0x6, %rbp
movq %rbp, %rsi
subq %r15, %rsi
leaq 0x20(%r13), %rcx
movq 0x18(%r13), %r8
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2190
cltq
addq %rax, %r15
movq 0xb0(%rbx), %rdi
movl %r15d, %eax
subl %edi, %eax
movl %eax, 0xc4(%rbx)
movq (%r13), %r13
cmpq (%r12), %r13
jne 0x39f9
jmp 0x3c14
addq $0xc, %r14
leaq 0x2b81(%rip), %rsi      # 0x661f
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
cmpl $0x1, %eax
jne 0x3c51
cmpb $0x0, (%rsp)
je 0x3c51
movq %rsp, %rdi
callq 0x2bb0
testq %rax, %rax
je 0x3c5a
movq %rax, %r14
movq %rax, %rdx
addq $0x20, %rdx
leaq 0x2b91(%rip), %rsi      # 0x6671
leaq 0x90(%rsp), %rdi
xorl %eax, %eax
callq 0x23a0
cltq
leaq (%rsp,%rax), %r15
addq $0x90, %r15
movq 0x40(%r14), %rdx
leaq 0x2b78(%rip), %rsi      # 0x667f
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x48(%r14), %rdx
leaq 0x2b72(%rip), %rsi      # 0x6694
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x18(%r14), %rdx
leaq 0x2b6e(%rip), %rsi      # 0x66ab
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movsd 0x68(%r14), %xmm0
leaq 0x2b6d(%rip), %rsi      # 0x66c7
movq %r12, %rdi
movb $0x1, %al
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x78(%r14), %edx
leaq 0x2b70(%rip), %rsi      # 0x66e5
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x50(%r14), %rdx
leaq 0x2b74(%rip), %rsi      # 0x6704
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq 0x60(%r14), %rdx
leaq 0x2b6f(%rip), %rsi      # 0x671a
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r13, %r15
movq 0x58(%r14), %rdx
leaq 0x2b6f(%rip), %rsi      # 0x6735
jmp 0x3c6c
movl $0x0, 0x5677(%rip)      # 0x924c
xorps %xmm0, %xmm0
movups %xmm0, 0x5679(%rip)     # 0x9258
movq $0x0, 0x567e(%rip) # 0x9268
movups %xmm0, 0x5687(%rip)     # 0x9278
leaq 0x2b5f(%rip), %rsi      # 0x6757
movq %rbx, %rdi
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x34b0
movq %rdi, %r15
movw $0xa, 0x4(%r15)
movl $0xd444e45, (%r15)      # imm = 0xD444E45
movq 0xb0(%rbx), %rax
subl %eax, %r15d
addl $0x5, %r15d
movl %r15d, 0xc4(%rbx)
movq %rax, 0xb8(%rbx)
movl $0x2, 0x4(%rbx)
movl $0x1, 0xc8(%rbx)
jmp 0x3c90
leaq 0x29ce(%rip), %rsi      # 0x6626
jmp 0x3c88
leaq 0x29fc(%rip), %rsi      # 0x665d
leaq 0x90(%rsp), %r15
movq %rsp, %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
cltq
movl $0x444e45, (%r15,%rax)  # imm = 0x444E45
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0x34b0
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2489
jmp 0x3c90