// File: /workspace/github_c_makefile_star2_O3/yancanchen[P]mc-bloom-filter/mc_bloom_filter.c
// Function: drive_machine
void drive_machine(conn *c) {

    int exit = 0;
    int sfd, flags = 1;
    socklen_t addrlen;
    struct sockaddr_in addr;
    conn *newc;
    int res;

    while (!exit) {
        /* printf("state %d\n", c->state);*/
        switch(c->state) {
            case conn_listening:
                addrlen = sizeof(addr);
                if ((sfd = accept(c->sfd, (struct sockaddr*)(&addr), &addrlen)) == -1) {
                    if (errno == EAGAIN || errno == EWOULDBLOCK) {
                        exit = 1;
                        break;
                    } else {
                        perror("accept()");
                        exit = 1;
                        break;
                    }
                    break;
                }
                if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 ||
                        fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
                    perror("setting O_NONBLOCK");
                    close(sfd);
                    break;
                }        
                newc = conn_new(sfd, conn_read, EV_READ | EV_PERSIST);
                if (!newc) {
                    if (my_srv.verbose > 0)
                        fprintf(stderr, "couldn't create new connection\n");
                    close(sfd);
                    break;
                }

                break;

            case conn_read:
                if (try_read_command(c)) {
                    continue;
                }
                if (try_read_network(c)) {
                    continue;
                }
                /* we have no command line and no data to read from network */
                if (!update_event(c, EV_READ | EV_PERSIST)) {
                    if (my_srv.verbose > 0)
                        fprintf(stderr, "Couldn't update event\n");
                    c->state = conn_closing;
                    break;
                }
                exit = 1;
                break;

            case conn_nread:
                /* we are reading rlbytes into ritem; */
                if (c->rbytes >= c->rlbytes) {
                    complete_nread(c);
                    c->rcurr += c->rlbytes;
                    c->rbytes -= c->rlbytes;
                    c->rlbytes = 0;
                    continue;
                }

                if (try_read_network(c)) {
                    continue;
                }

                /* we have no command line and no data to read from network */
                if (!update_event(c, EV_READ | EV_PERSIST)) {
                    if (my_srv.verbose > 0)
                        fprintf(stderr, "Couldn't update event\n");
                    c->state = conn_closing;
                    break;
                }
                exit = 1;
                break;

            case conn_swallow:
                /* we are reading sbytes and throwing them away */
                if (c->sbytes == 0) {
                    c->state = conn_read;
                    break;
                }

                /* first check if we have leftovers in the conn_read buffer */
                if (c->rbytes > 0) {
                    int tocopy = c->rbytes > c->sbytes ? c->sbytes : c->rbytes;
                    c->sbytes -= tocopy;
                    c->rcurr += tocopy;
                    c->rbytes -= tocopy;
                    break;
                }

                /*  now try reading from the socket */
                res = read(c->sfd, c->rbuf, c->rsize > c->sbytes ? c->sbytes : c->rsize);
                if (res > 0) {
                    stats.bytes_read += res;
                    c->sbytes -= res;
                    break;
                }
                if (res == 0) { /* end of stream */
                    c->state = conn_closing;
                    break;
                }
                if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
                    if (!update_event(c, EV_READ | EV_PERSIST)) {
                        if (my_srv.verbose > 0)
                            fprintf(stderr, "Couldn't update event\n");
                        c->state = conn_closing;
                        break;
                    }
                    exit = 1;
                    break;
                }
                /* otherwise we have a real error, on which we close the connection */
                if (my_srv.verbose > 0)
                    fprintf(stderr, "Failed to read, and not due to blocking\n");
                c->state = conn_closing;
                break;

            case conn_write:
                /* we are writing wbytes bytes starting from wcurr */
                if (c->wbytes == 0) {
                    c->state = c->write_and_go;
                    if (c->state == conn_read)
                        set_cork(c, 0);
                    break;
                }
                res = write(c->sfd, c->wcurr, c->wbytes);
                if (res > 0) {
                    stats.bytes_written += res;
                    c->wcurr  += res;
                    c->wbytes -= res;
                    break;
                }
                if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
                    if (!update_event(c, EV_WRITE | EV_PERSIST)) {
                        if (my_srv.verbose > 0)
                            fprintf(stderr, "Couldn't update event\n");
                        c->state = conn_closing;
                        break;
                    }        
                    exit = 1;
                    break;
                }
                /* if res==0 or res==-1 and error is not EAGAIN or EWOULDBLOCK,
                   we have a real error, on which we close the connection */
                if (my_srv.verbose > 0)
                    fprintf(stderr, "Failed to write, and not due to blocking\n");
                c->state = conn_closing;
                break;

            case conn_closing:
                conn_close(c);
                exit = 1;
                break;
        }

    }

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x1de8(%rip), %r13      # 0x625c
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
leaq 0x23c3(%rip), %r12      # 0x6848
movl 0x4(%rbx), %eax
cmpq $0x5, %rax
ja 0x4485
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movl $0x10, 0x4(%rsp)
movl (%rbx), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2380
cmpl $-0x1, %eax
je 0x4783
movl %eax, %ebp
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20c0
testl %eax, %eax
js 0x4718
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20c0
testl %eax, %eax
js 0x4718
movl %ebp, %edi
movl $0x1, %esi
movl $0x12, %edx
callq 0x31e6
testq %rax, %rax
jne 0x4485
cmpl $0x0, 0x4d92(%rip)      # 0x929c
jle 0x4720
callq 0x25a1
jmp 0x4720
movl 0xf0(%rbx), %eax
testl %eax, %eax
je 0x4668
movl 0xa4(%rbx), %ecx
testl %ecx, %ecx
jle 0x4695
cmpl %eax, %ecx
movl %eax, %edx
cmovll %ecx, %edx
subl %edx, %eax
movl %eax, 0xf0(%rbx)
movslq %edx, %rax
addq %rax, 0x98(%rbx)
subl %eax, %ecx
movl %ecx, 0xa4(%rbx)
jmp 0x4485
movslq 0xc4(%rbx), %rdx
testq %rdx, %rdx
je 0x4674
movl (%rbx), %edi
movq 0xb8(%rbx), %rsi
callq 0x20d0
testl %eax, %eax
jle 0x46cf
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x4cef(%rip)      # 0x9280
addq %rcx, 0xb8(%rbx)
subl %eax, 0xc4(%rbx)
jmp 0x4485
movl 0xa4(%rbx), %eax
cmpl 0xa8(%rbx), %eax
jge 0x463d
movq %rbx, %rdi
callq 0x42ce
testl %eax, %eax
jne 0x4485
movq %rbx, %rdi
movl $0x12, %esi
callq 0x43f5
testl %eax, %eax
jne 0x478d
cmpl $0x0, 0x4cbb(%rip)      # 0x929c
jle 0x470c
callq 0x255d
jmp 0x470c
movq %rbx, %rdi
callq 0x4247
testl %eax, %eax
jne 0x4485
movq %rbx, %rdi
callq 0x42ce
testl %eax, %eax
jne 0x4485
movq %rbx, %rdi
movl $0x12, %esi
callq 0x43f5
testl %eax, %eax
jne 0x478d
cmpl $0x0, 0x4c6f(%rip)      # 0x929c
jle 0x470c
callq 0x257f
jmp 0x470c
movq %rbx, %rdi
callq 0x3cac
movslq 0xa8(%rbx), %rax
addq %rax, 0x98(%rbx)
subl %eax, 0xa4(%rbx)
movl $0x0, 0xa8(%rbx)
jmp 0x4485
movl $0x1, 0x4(%rbx)
jmp 0x4485
movl 0xc8(%rbx), %eax
movl %eax, 0x4(%rbx)
cmpl $0x1, %eax
jne 0x4485
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3189
jmp 0x4485
movl (%rbx), %edi
movq 0x90(%rbx), %rsi
movl 0xa0(%rbx), %ecx
cmpl %eax, %ecx
cmovll %ecx, %eax
movslq %eax, %rdx
callq 0x21e0
testl %eax, %eax
jle 0x472c
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0x4bb4(%rip)      # 0x9278
subl %eax, 0xf0(%rbx)
jmp 0x4485
cmpl $-0x1, %eax
jne 0x4703
callq 0x2040
cmpl $0xb, (%rax)
jne 0x4703
movq %rbx, %rdi
movl $0x14, %esi
callq 0x43f5
testl %eax, %eax
jne 0x478d
cmpl $0x0, 0x4ba2(%rip)      # 0x929c
jle 0x470c
callq 0x24f7
jmp 0x470c
cmpl $0x0, 0x4b92(%rip)      # 0x929c
jg 0x4772
movl $0x5, 0x4(%rbx)
jmp 0x4485
movq %r12, %rdi
callq 0x2360
movl %ebp, %edi
callq 0x21b0
jmp 0x4485
cmpl $-0x1, %eax
je 0x4737
testl %eax, %eax
je 0x470c
jmp 0x4762
callq 0x2040
cmpl $0xb, (%rax)
jne 0x4762
movq %rbx, %rdi
movl $0x12, %esi
callq 0x43f5
testl %eax, %eax
jne 0x478d
cmpl $0x0, 0x4b43(%rip)      # 0x929c
jle 0x470c
callq 0x2519
jmp 0x470c
cmpl $0x0, 0x4b33(%rip)      # 0x929c
jle 0x470c
callq 0x253b
jmp 0x470c
callq 0x24d5
jmp 0x470c
movq %rbx, %rdi
callq 0x3401
jmp 0x478d
callq 0x2040
cmpl $0xb, (%rax)
jne 0x479c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x209c(%rip), %rdi      # 0x683f
callq 0x2360
jmp 0x478d