// File: /workspace/github_c_makefile_star2_O2/yancanchen[P]mc-bloom-filter/mc_bloom_filter.c
// Function: process_command
void process_command(conn *c, char *command) {

    /* 
     * for commands set/add/replace, we build an item and read the data
     * directly into it, then continue in nread_complete().
     */ 

    if (my_srv.verbose > 1)
        fprintf(stderr, "<%d %s\n", c->sfd, command);

    /* All incoming commands will require a response, so we cork at the beginning,
       and uncork at the very end (usually by means of out_string)  */
    set_cork(c, 1);
    /* the function get bloom filter by key ,format: name_key*/
    if (strncmp(command, "get ", 4) == 0) {
        char key[256];
        char *subkey;
        char *start = command + 4;
        char *p = c->wbuf;
        char *end = c->wbuf + c->wsize - 6;
        int next;
        int res;

        while(sscanf(start, " %250s%n",key,&next) >= 1){    
            start += next;
             
            if((subkey = strchr(key, '|'))) {
                *subkey = '\0';
                subkey ++;
                res = blooms_get(key,subkey);
                if (res == 1) {
                    p += snprintf(p,end - p , "VALUE %s|%s 0 1\r\n%d\r\n", key,subkey,res); 
                }
            }
        }
        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;
    }

    //add create bloom filter 
    //add name 0 0 10
    //10000|0.0001 bit_chongtulv
    //set insert a new bloom_filter key
    if ((strncmp(command, "add ", 4) == 0 && (c->item_comm = NREAD_ADD)) ||
        (strncmp(command, "set ", 4) == 0 && (c->item_comm = NREAD_SET))) {
        char key[251];
        int flags;
        time_t expire;
        int len, res;

        res = sscanf(command, "%*s %250s %u %ld %d\n", key, &flags, &expire, &len);
        if (res!=4 || strlen(key)==0 || len < 0) {
            out_string(c, "CLIENT_ERROR bad command line format");
            return;
        }
        if (strlen(key) >= MAX_KEY_LEN) {
            // swallow the data line
            out_string(c, "NOT_STORED");
            c->write_and_go = conn_swallow;
            c->sbytes = len+2;
            return;
        }
        strcpy(c->key,key); 

        c->rlbytes = len+2;
        c->state = conn_nread;
        return;
    }

    //del key and free the bloom filter
    if (strncmp(command, "delete ", 7) == 0) {
        char key[MAX_KEY_LEN];
        char *start = command + 7;
        char *p = c->wbuf;
        
        if(sscanf(start, " %31s", key) >= 1) {
            if(blooms_delete(key) == 0){
                p += sprintf(p, "DELETED\r\n");
            }else{
                p += sprintf(p,"NOT_FOUND\r\n");    
            }
        }
        c->wbytes = p - c->wbuf;
        c->wcurr = c->wbuf;
        c->state = conn_write;
        c->write_and_go = conn_read;
        return;
    }

    if (strncmp(command, "stats", 5) == 0) {
        process_stat(c, command);
        return;
    }
    
    //calculate of the memory size,won't malloc memory
    if(strncmp(command, "try", 3) == 0){
        process_try(c,command);
        return;
    }

    //set the max memory limit
    //you can enlarge the max memory limit without restart service
    if(strncmp(command,"setmem",6) == 0){
        process_setmem(c,command);
        return;
    }

    if (strcmp(command, "quit") == 0) {
        c->state = conn_closing;
        return;
    }

    out_string(c, "ERROR");
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x2, 0x483b(%rip)      # 0x829c
jge 0x3d6f
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2db0
leaq 0x1d2e(%rip), %rsi      # 0x57a7
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3b30
leaq 0x1d38(%rip), %rsi      # 0x57cb
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3c24
leaq 0x1d23(%rip), %rsi      # 0x57d0
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3c28
leaq 0x1d48(%rip), %rsi      # 0x580f
pushq $0x7
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3cc6
leaq 0x1970(%rip), %rsi      # 0x5451
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3d92
cmpb $0x74, (%r14)
jne 0x3d1e
cmpb $0x72, 0x1(%r14)
jne 0x3d1e
cmpb $0x79, 0x2(%r14)
jne 0x3d1e
movq %r15, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3202
addq $0x4, %r14
movq 0xb0(%r15), %rcx
movq %r15, 0x20(%rsp)
movslq 0xc0(%r15), %rax
movq %rcx, 0x8(%rsp)
leaq -0x6(%rcx,%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x1c4f(%rip), %r12      # 0x57ac
leaq 0x30(%rsp), %r13
leaq 0x10(%rsp), %rbp
pushq $0x7c
popq %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2290
testl %eax, %eax
jle 0x3bdf
movslq 0x10(%rsp), %rax
addq %rax, %r14
movq %r13, %rdi
movl %ebx, %esi
callq 0x2160
testq %rax, %rax
je 0x3b6a
movq %rax, %r15
movb $0x0, (%rax)
incq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x298a
cmpl $0x1, %eax
jne 0x3b6a
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdi
subq %rdi, %rsi
leaq 0x1bf0(%rip), %rdx      # 0x57b5
movq %r13, %rcx
movq %r15, %r8
pushq $0x1
popq %r9
xorl %eax, %eax
callq 0x2190
cltq
addq %rax, 0x8(%rsp)
jmp 0x3b6a
movq 0x8(%rsp), %rdx
movw $0xa, 0x4(%rdx)
movl $0xd444e45, (%rdx)      # imm = 0xD444E45
movq 0x20(%rsp), %rcx
movq 0xb0(%rcx), %rax
subl %eax, %edx
addl $0x5, %edx
movl %edx, 0xc4(%rcx)
movq %rax, 0xb8(%rcx)
movl $0x2, 0x4(%rcx)
movl $0x1, 0xc8(%rcx)
jmp 0x3e30
movb $0x1, %al
jmp 0x3c2a
movb $0x2, %al
movb %al, 0xcd(%r15)
leaq 0x1b9d(%rip), %rsi      # 0x57d5
leaq 0x30(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x4(%rsp), %r9
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
cmpl $0x4, %eax
jne 0x3c7b
leaq 0x30(%rsp), %rdi
callq 0x2110
testq %rax, %rax
sete %cl
movl 0x4(%rsp), %ebx
testl %ebx, %ebx
sets %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x3c8f
leaq 0x1b68(%rip), %rsi      # 0x57ea
movq %r15, %rdi
callq 0x30ea
jmp 0x3e30
cmpq $0x20, %rax
jb 0x3dae
leaq 0x1ad9(%rip), %rsi      # 0x5779
movq %r15, %rdi
callq 0x30ea
movl $0x4, 0xc8(%r15)
movl 0x4(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xf0(%r15)
jmp 0x3e30
addq $0x7, %r14
movq 0xb0(%r15), %rbx
leaq 0x1b3f(%rip), %rsi      # 0x5817
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
testl %eax, %eax
jle 0x3e06
leaq 0x30(%rsp), %rdi
callq 0x2b82
testl %eax, %eax
je 0x3def
movabsq $0x4e554f465f544f4e, %rax # imm = 0x4E554F465F544F4E
movq %rax, (%rbx)
movl $0xa0d44, 0x8(%rbx)     # imm = 0xA0D44
addq $0xb, %rbx
jmp 0x3e06
leaq 0x1b0e(%rip), %rsi      # 0x5833
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3dd3
leaq 0x1afb(%rip), %rsi      # 0x583a
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x3e42
leaq 0x16f5(%rip), %rsi      # 0x544b
movq %r15, %rdi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30ea
movq 0x4282(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movl (%r15), %edx
leaq 0x1a1c(%rip), %rsi      # 0x579f
movq %r14, %rcx
xorl %eax, %eax
callq 0x2230
jmp 0x3a67
movq %r15, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3372
leaq 0xd0(%r15), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2080
addl $0x2, %ebx
movl %ebx, 0xa8(%r15)
movl $0x3, 0x4(%r15)
jmp 0x3e30
movq %r15, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3193
movabsq $0xd444554454c4544, %rax # imm = 0xD444554454C4544
movq %rax, (%rbx)
movw $0xa, 0x8(%rbx)
addq $0x9, %rbx
movq 0xb0(%r15), %rax
subl %eax, %ebx
movl %ebx, 0xc4(%r15)
movq %rax, 0xb8(%r15)
movl $0x2, 0x4(%r15)
movl $0x1, 0xc8(%r15)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x5, 0x4(%r15)
jmp 0x3e30