// File: /workspace/github_c_makefile_star2_O1/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, %r12
cmpl $0x2, 0x53d5(%rip)      # 0x929c
jge 0x41e3
movq %r12, %rdi
movl $0x1, %esi
callq 0x31c9
leaq 0x28c6(%rip), %rsi      # 0x67a7
movl $0x4, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x3fd5
leaq 0x28ce(%rip), %rsi      # 0x67cb
movl $0x4, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x40e5
leaq 0x28b7(%rip), %rsi      # 0x67d0
movl $0x4, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x40e9
leaq 0x28da(%rip), %rsi      # 0x680f
movl $0x7, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x418a
leaq 0x2500(%rip), %rsi      # 0x6451
movl $0x5, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x4207
leaq 0x28c6(%rip), %rsi      # 0x6833
movl $0x3, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x424b
leaq 0x28ae(%rip), %rsi      # 0x6837
movl $0x6, %edx
movq %r14, %rdi
callq 0x2060
testl %eax, %eax
je 0x4267
leaq 0x2899(%rip), %rsi      # 0x683e
movq %r14, %rdi
callq 0x2200
testl %eax, %eax
je 0x42db
leaq 0x248f(%rip), %rsi      # 0x644b
movq %r12, %rdi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3500
addq $0x4, %r14
movq 0xb0(%r12), %r15
movq %r12, 0x18(%rsp)
movslq 0xc0(%r12), %rbx
leaq 0x27b7(%rip), %rsi      # 0x67ac
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
testl %eax, %eax
jle 0x40a0
leaq (%r15,%rbx), %rax
addq $-0x6, %rax
movq %rax, 0x20(%rsp)
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %rbp
movslq 0x10(%rsp), %r13
movq %r12, %rdi
movl $0x7c, %esi
callq 0x2160
testq %rax, %rax
je 0x4082
movq %rax, %rbx
movb $0x0, (%rax)
incq %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2d39
cmpl $0x1, %eax
jne 0x4082
movq 0x20(%rsp), %rsi
subq %r15, %rsi
movq %r15, %rdi
leaq 0x274b(%rip), %rdx      # 0x67b5
movq %r12, %rcx
movq %rbx, %r8
movl $0x1, %r9d
xorl %eax, %eax
callq 0x2190
cltq
addq %rax, %r15
addq %r13, %r14
movq %r14, %rdi
leaq 0x271d(%rip), %rsi      # 0x67ac
movq %r12, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2290
testl %eax, %eax
jg 0x4028
movw $0xa, 0x4(%r15)
movl $0xd444e45, (%r15)      # imm = 0xD444E45
movq 0x18(%rsp), %rcx
movq 0xb0(%rcx), %rax
subl %eax, %r15d
addl $0x5, %r15d
movl %r15d, 0xc4(%rcx)
movq %rax, 0xb8(%rcx)
movl $0x2, 0x4(%rcx)
movl $0x1, 0xc8(%rcx)
jmp 0x42c9
movb $0x1, %al
jmp 0x40eb
movb $0x2, %al
movb %al, 0xcd(%r12)
leaq 0x26db(%rip), %rsi      # 0x67d5
leaq 0x30(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
cmpl $0x4, %eax
jne 0x413d
leaq 0x30(%rsp), %rdi
callq 0x2110
testq %rax, %rax
sete %cl
movl 0xc(%rsp), %ebx
testl %ebx, %ebx
sets %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x4151
leaq 0x26a6(%rip), %rsi      # 0x67ea
movq %r12, %rdi
callq 0x3500
jmp 0x42c9
cmpq $0x20, %rax
jb 0x4223
leaq 0x2617(%rip), %rsi      # 0x6779
movq %r12, %rdi
callq 0x3500
movl $0x4, 0xc8(%r12)
movl 0xc(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xf0(%r12)
jmp 0x42c9
addq $0x7, %r14
movq 0xb0(%r12), %rbx
leaq 0x267a(%rip), %rsi      # 0x6817
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2290
testl %eax, %eax
jle 0x429a
leaq 0x30(%rsp), %rdi
callq 0x2f3c
testl %eax, %eax
je 0x4283
movabsq $0x4e554f465f544f4e, %rax # imm = 0x4E554F465F544F4E
movq %rax, (%rbx)
movl $0xa0d44, 0x8(%rbx)     # imm = 0xA0D44
addq $0xb, %rbx
jmp 0x429a
movq 0x4e0e(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movl (%r12), %edx
leaq 0x25a7(%rip), %rsi      # 0x679f
movq %r14, %rcx
xorl %eax, %eax
callq 0x2230
jmp 0x3ecd
movq %r12, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3793
leaq 0xd0(%r12), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2080
addl $0x2, %ebx
movl %ebx, 0xa8(%r12)
movl $0x3, 0x4(%r12)
jmp 0x42c9
movq %r12, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x361a
movq %r12, %rdi
movq %r14, %rsi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x35ab
movabsq $0xd444554454c4544, %rax # imm = 0xD444554454C4544
movq %rax, (%rbx)
movw $0xa, 0x8(%rbx)
addq $0x9, %rbx
movq 0xb0(%r12), %rax
subl %eax, %ebx
movl %ebx, 0xc4(%r12)
movq %rax, 0xb8(%r12)
movl $0x2, 0x4(%r12)
movl $0x1, 0xc8(%r12)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x5, 0x4(%r12)
jmp 0x42c9