// File: /workspace/github_c_makefile_star2_O1/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 $0x4a8, %rsp            # imm = 0x4A8
movq %rsi, %r14
movq %rdi, %rbx
xorl %edi, %edi
callq 0x2270
movq %rax, %r12
leaq 0x2c96(%rip), %rsi      # 0x6451
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x381d
leaq 0x2dee(%rip), %rsi      # 0x65bc
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x3a14
leaq 0x2e2d(%rip), %rsi      # 0x6612
movl $0xc, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3b1e
leaq 0x2f4a(%rip), %rsi      # 0x674b
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x3ca5
leaq 0x2c33(%rip), %rsi      # 0x644b
jmp 0x3cd2
callq 0x20e0
movl %eax, %ebp
leaq 0x10(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x2350
leaq 0x2c1d(%rip), %rsi      # 0x6457
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x23a0
cltq
leaq (%rsp,%rax), %r13
addq $0xa0, %r13
movq %r12, %rdx
subq 0x5a0b(%rip), %rdx      # 0x9270
leaq 0x2bf9(%rip), %rsi      # 0x6465
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %rbp
addq %r13, %rbp
leaq 0x2bf4(%rip), %rsi      # 0x6477
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %rbp, %r12
leaq 0x599b(%rip), %rbp      # 0x9238
movq (%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x2bdb(%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 0x2bd2(%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 0x2bcb(%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 0x2bc3(%rip), %rsi      # 0x64ca
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r13, %r12
leaq 0x2bc2(%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 0x2bb7(%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 0x2bb5(%rip), %rsi      # 0x6515
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x58d2(%rip), %edx      # 0x9248
decl %edx
leaq 0x2bb5(%rip), %rsi      # 0x6534
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movl 0x58b7(%rip), %edx      # 0x924c
leaq 0x2bb3(%rip), %rsi      # 0x654f
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x589e(%rip), %edx      # 0x9250
leaq 0x2bb2(%rip), %rsi      # 0x656b
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x58a8(%rip), %rdx      # 0x9278
leaq 0x2bb4(%rip), %rsi      # 0x658b
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x5892(%rip), %rdx      # 0x9280
leaq 0x2bad(%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 0x3d27
movq 0xb0(%rbx), %r14
leaq 0x5816(%rip), %r12      # 0x9238
movq (%r12), %rax
cmpq %rax, (%rax)
je 0x3c65
movq (%rax), %rbp
cmpq (%r12), %rbp
sete %r13b
je 0x3c5b
movslq 0xc0(%rbx), %rax
leaq (%r14,%rax), %r15
addq $-0x6, %r15
movq %rbx, 0x8(%rsp)
movq %r15, %rax
subq %r14, %rax
cmpq $0x37, %rax
jg 0x3ac8
movq 0xb0(%rbx), %rdi
movslq 0xc0(%rbx), %rsi
addq %rsi, %rsi
callq 0x22c0
movq %rax, %r12
testq %rax, %rax
je 0x3b14
movq %r12, 0xb0(%rbx)
movq %r12, 0xb8(%rbx)
movslq 0xc0(%rbx), %rax
leaq (%rax,%rax), %rcx
movl %ecx, 0xc0(%rbx)
movslq 0xc4(%rbx), %r14
addq %r12, %r14
leaq (%r12,%rax,2), %r15
addq $-0x6, %r15
testq %r12, %r12
leaq 0x577b(%rip), %r12      # 0x9238
movq 0x8(%rsp), %rbx
je 0x3c5b
movq %r15, %rsi
subq %r14, %rsi
leaq 0x20(%rbp), %rcx
movq 0x18(%rbp), %r8
movq %r14, %rdi
leaq 0x2b23(%rip), %rdx      # 0x6603
xorl %eax, %eax
callq 0x2190
cltq
addq %rax, %r14
movl 0xb0(%rbx), %eax
movl %r14d, %ecx
subl %eax, %ecx
movl %ecx, 0xc4(%rbx)
movq (%rbp), %rbp
cmpq (%r12), %rbp
sete %r13b
jne 0x3a54
jmp 0x3c5b
movq %rbx, %rdi
callq 0x2489
jmp 0x3ab3
addq $0xc, %r14
leaq 0x2af6(%rip), %rsi      # 0x661f
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
cmpl $0x1, %eax
jne 0x3ceb
cmpb $0x0, 0x10(%rsp)
je 0x3ceb
leaq 0x10(%rsp), %rdi
callq 0x2bb8
testq %rax, %rax
je 0x3cf4
movq %rax, %r14
movq %rax, %rdx
addq $0x20, %rdx
leaq 0x2b01(%rip), %rsi      # 0x6671
leaq 0xa0(%rsp), %rdi
xorl %eax, %eax
callq 0x23a0
cltq
leaq (%rsp,%rax), %r15
addq $0xa0, %r15
movq 0x40(%r14), %rdx
leaq 0x2ae8(%rip), %rsi      # 0x667f
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x48(%r14), %rdx
leaq 0x2ae2(%rip), %rsi      # 0x6694
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movq 0x18(%r14), %rdx
leaq 0x2ade(%rip), %rsi      # 0x66ab
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movsd 0x68(%r14), %xmm0
leaq 0x2add(%rip), %rsi      # 0x66c7
movq %r12, %rdi
movb $0x1, %al
callq 0x23a0
movslq %eax, %r15
addq %r12, %r15
movl 0x78(%r14), %edx
leaq 0x2ae0(%rip), %rsi      # 0x66e5
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r12
addq %r15, %r12
movq 0x50(%r14), %rdx
leaq 0x2ae4(%rip), %rsi      # 0x6704
movq %r12, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r13
addq %r12, %r13
movq 0x60(%r14), %rdx
leaq 0x2adf(%rip), %rsi      # 0x671a
movq %r13, %rdi
xorl %eax, %eax
callq 0x23a0
movslq %eax, %r15
addq %r13, %r15
movq 0x58(%r14), %rdx
leaq 0x2adf(%rip), %rsi      # 0x6735
jmp 0x3d08
testb $0x1, %r13b
je 0x3d2c
movw $0xa, 0x4(%r14)
movl $0xd444e45, (%r14)      # imm = 0xD444E45
movq 0xb0(%rbx), %rax
subl %eax, %r14d
addl $0x5, %r14d
movl %r14d, 0xc4(%rbx)
movq %rax, 0xb8(%rbx)
movl $0x2, 0x4(%rbx)
movl $0x1, 0xc8(%rbx)
jmp 0x3d2c
movl $0x0, 0x559d(%rip)      # 0x924c
xorps %xmm0, %xmm0
movups %xmm0, 0x559f(%rip)     # 0x9258
movq $0x0, 0x55a4(%rip) # 0x9268
movups %xmm0, 0x55ad(%rip)     # 0x9278
leaq 0x2a85(%rip), %rsi      # 0x6757
movq %rbx, %rdi
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3500
leaq 0x2934(%rip), %rsi      # 0x6626
jmp 0x3d24
leaq 0x2962(%rip), %rsi      # 0x665d
leaq 0xa0(%rsp), %r15
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x23a0
cltq
movl $0x444e45, (%r15,%rax)  # imm = 0x444E45
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0x3500
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq