// File: /workspace/github_c_makefile_star2_O2/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 0x20b7(%rip), %rsi      # 0x5451
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x33fa
leaq 0x220f(%rip), %rsi      # 0x55bc
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x35ed
leaq 0x224e(%rip), %rsi      # 0x5612
pushq $0xc
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x36c1
leaq 0x236d(%rip), %rsi      # 0x574b
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x3834
leaq 0x2056(%rip), %rsi      # 0x544b
jmp 0x385b
callq 0x20e0
movl %eax, %ebp
movq %rsp, %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x2350
leaq 0x2042(%rip), %rsi      # 0x5457
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 0x4e30(%rip), %rdx      # 0x8270
leaq 0x201e(%rip), %rsi      # 0x5465
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %rbp
addq %r13, %rbp
leaq 0x2019(%rip), %rsi      # 0x5477
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %rbp, %r12
leaq 0x4dc0(%rip), %rbp      # 0x8238
movq (%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x2000(%rip), %rsi      # 0x5487
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x1ff7(%rip), %rsi      # 0x549d
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
movq (%rbp), %rax
movl 0x20(%rax), %edx
leaq 0x1ff0(%rip), %rsi      # 0x54b4
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%rbp), %rax
movl 0x24(%rax), %edx
leaq 0x1fe8(%rip), %rsi      # 0x54ca
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
leaq 0x1fe7(%rip), %rsi      # 0x54e0
pushq $0x40
popq %rdx
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq (%r15), %rdx
movq 0x8(%r15), %rcx
leaq 0x1fde(%rip), %rsi      # 0x54f8
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rcx
leaq 0x1fdc(%rip), %rsi      # 0x5515
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x4cf9(%rip), %edx      # 0x8248
decl %edx
leaq 0x1fdc(%rip), %rsi      # 0x5534
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movl 0x4cde(%rip), %edx      # 0x824c
leaq 0x1fda(%rip), %rsi      # 0x554f
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x4cc5(%rip), %edx      # 0x8250
leaq 0x1fd9(%rip), %rsi      # 0x556b
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x4ccf(%rip), %rdx      # 0x8278
leaq 0x1fdb(%rip), %rsi      # 0x558b
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x4cb9(%rip), %rdx      # 0x8280
leaq 0x1fd4(%rip), %rsi      # 0x55a2
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
cltq
movl $0x444e45, (%rax,%r15)  # imm = 0x444E45
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x38ae
movq 0xb0(%rbx), %rdi
leaq 0x4c3d(%rip), %r12      # 0x8238
movq (%r12), %rax
movq (%rax), %r13
cmpq %rax, %r13
je 0x37fc
movslq 0xc0(%rbx), %rcx
leaq (%rdi,%rcx), %rbp
addq $-0x6, %rbp
leaq 0x1fe2(%rip), %r14      # 0x5603
movq %rdi, %r15
cmpq %rax, %r13
je 0x37f9
movq %rbp, %rsi
subq %r15, %rsi
cmpq $0x37, %rsi
jg 0x3688
movslq 0xc0(%rbx), %rsi
addq %rsi, %rsi
callq 0x22c0
testq %rax, %rax
je 0x38c5
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
movq (%r12), %rax
jmp 0x3624
addq $0xc, %r14
leaq 0x1f53(%rip), %rsi      # 0x561f
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
cmpl $0x1, %eax
jne 0x3874
cmpb $0x0, (%rsp)
je 0x3874
movq %rsp, %rdi
callq 0x282e
testq %rax, %rax
je 0x387d
movq %rax, %r14
movq %rax, %rdx
addq $0x20, %rdx
leaq 0x1f63(%rip), %rsi      # 0x5671
leaq 0x90(%rsp), %rdi
xorl %eax, %eax
callq 0x23a0
cltq
leaq (%rsp,%rax), %r15
addq $0x90, %r15
movq 0x40(%r14), %rdx
leaq 0x1f4a(%rip), %rsi      # 0x567f
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x48(%r14), %rdx
leaq 0x1f44(%rip), %rsi      # 0x5694
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x18(%r14), %rdx
leaq 0x1f40(%rip), %rsi      # 0x56ab
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movsd 0x68(%r14), %xmm0
leaq 0x1f3f(%rip), %rsi      # 0x56c7
movq %r12, %rdi
movb $0x1, %al
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x78(%r14), %edx
leaq 0x1f42(%rip), %rsi      # 0x56e5
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x50(%r14), %rdx
leaq 0x1f46(%rip), %rsi      # 0x5704
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq 0x60(%r14), %rdx
leaq 0x1f41(%rip), %rsi      # 0x571a
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r13, %r15
movq 0x58(%r14), %rdx
leaq 0x1f41(%rip), %rsi      # 0x5735
jmp 0x388f
movq %r15, %rdi
movw $0xa, 0x4(%rdi)
movl $0xd444e45, (%rdi)      # imm = 0xD444E45
movq 0xb0(%rbx), %rax
subl %eax, %edi
addl $0x5, %edi
movl %edi, 0xc4(%rbx)
movq %rax, 0xb8(%rbx)
movl $0x2, 0x4(%rbx)
movl $0x1, 0xc8(%rbx)
jmp 0x38b3
andl $0x0, 0x4a11(%rip)      # 0x824c
xorps %xmm0, %xmm0
movups %xmm0, 0x4a13(%rip)     # 0x8258
andq $0x0, 0x4a1b(%rip)      # 0x8268
movups %xmm0, 0x4a24(%rip)     # 0x8278
leaq 0x1efc(%rip), %rsi      # 0x5757
movq %rbx, %rdi
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30ea
leaq 0x1dab(%rip), %rsi      # 0x5626
jmp 0x38ab
leaq 0x1dd9(%rip), %rsi      # 0x565d
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 0x30ea
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x472c(%rip), %rax      # 0x7ff8
movq (%rax), %rcx
leaq 0x1cf3(%rip), %rdi      # 0x55c9
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x23c0
leaq 0x1d00(%rip), %rsi      # 0x55e8
jmp 0x385b