// File: /workspace/github_c_makefile_star2_O0/zuocheng-liu[P]ae-memcached/src/memcached.c
// Function: drive_machine
void drive_machine(conn *c) {

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

    while (!exit) {
        switch(c->state) {
        case conn_listening:
            addrlen = sizeof(addr);
            if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) {
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    exit = 1;
                    break;
                } else {
                    perror("accept()");
                }
                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, AE_READABLE, DATA_BUFFER_SIZE, 0);
            if (!newc) {
                LOG_INFO("Couldn't create new connection\n");
                close(sfd);
                break;
            }

            break;

        case conn_read:
            if (try_read_command(c)) {
                continue;
            }
            if (c->udp ? try_read_udp(c) : try_read_network(c)) {
                continue;
            }
            /* we have no command line and no data to read from network */
            if (!update_event(c, AE_READABLE)) {
                LOG_INFO("Couldn't update event\n");
                conn_set_state(c, conn_closing);
                break;
            }
            exit = 1;
            break;

        case conn_nread:
            /* we are reading rlbytes into ritem; */
            if (c->rlbytes == 0) {
                complete_nread(c);
                break;
            }
            /* first check if we have leftovers in the conn_read buffer */
            if (c->rbytes > 0) {
                int tocopy = c->rbytes > c->rlbytes ? c->rlbytes : c->rbytes;
                memcpy(c->ritem, c->rcurr, tocopy);
                c->ritem += tocopy;
                c->rlbytes -= tocopy;
                c->rcurr += tocopy;
                c->rbytes -= tocopy;
                break;
            }

            /*  now try reading from the socket */
            res = read(c->sfd, c->ritem, c->rlbytes);
            if (res > 0) {
                stats.bytes_read += res;
                c->ritem += res;
                c->rlbytes -= res;
                break;
            }
            if (res == 0) { /* end of stream */
                conn_set_state(c, conn_closing);
                break;
            }
            if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
                if (!update_event(c, AE_READABLE)) {
                    LOG_INFO("Couldn't update event\n");
                    conn_set_state(c, conn_closing);
                    break;
                }
                exit = 1;
                break;
            }
            /* otherwise we have a real error, on which we close the connection */
            LOG_INFO("Failed to read, and not due to blocking\n");
            conn_set_state(c, conn_closing);
            break;

        case conn_swallow:
            /* we are reading sbytes and throwing them away */
            if (c->sbytes == 0) {
                conn_set_state(c, 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 */
                conn_set_state(c, conn_closing);
                break;
            }
            if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
                if (!update_event(c, AE_READABLE)) {
                    LOG_INFO("Couldn't update event\n");
                    conn_set_state(c, conn_closing);
                    break;
                }
                exit = 1;
                break;
            }
            /* otherwise we have a real error, on which we close the connection */
            LOG_INFO("Failed to read, and not due to blocking\n");
            conn_set_state(c, conn_closing);
            break;

        case conn_write:
            /*
             * We want to write out a simple response. If we haven't already,
             * assemble it into a msgbuf list (this will be a single-entry
             * list for TCP or a two-entry list for UDP).
             */
            if (c->iovused == 0) {
                if (add_iov(c, c->wcurr, c->wbytes) ||
                        (c->udp && build_udp_headers(c))) {
                    LOG_INFO("Couldn't build response\n");
                    conn_set_state(c, conn_closing);
                    break;
                }
            }

            /* fall through... */

        case conn_mwrite:
            switch (transmit(c)) {
            case TRANSMIT_COMPLETE:
                if (c->state == conn_mwrite) {
                    while (c->ileft > 0) {
                        item *it = *(c->icurr);
                        assert((it->it_flags & ITEM_SLABBED) == 0);
                        item_remove(it);
                        c->icurr++;
                        c->ileft--;
                    }
                    conn_set_state(c, conn_read);
                } else if (c->state == conn_write) {
                    if (c->write_and_free) {
                        free(c->write_and_free);
                        c->write_and_free = 0;
                    }
                    conn_set_state(c, c->write_and_go);
                } else {
                    LOG_INFO_F1("Unexpected state %d\n", c->state);
                    conn_set_state(c, conn_closing);
                }
                break;

            case TRANSMIT_INCOMPLETE:
            case TRANSMIT_HARD_ERROR:
                break;                   /* Continue in state machine. */

            case TRANSMIT_SOFT_ERROR:
                exit = 1;
                break;
            }
            break;

        case conn_closing:
            if (c->udp)
                conn_cleanup(c);
            else
                conn_close(c);
            exit = 1;
            break;
        }

    }

    return;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x1, 0x64(%rsp)
cmpl $0x0, 0x6c(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x94ac
jmp 0x9c9a
movq 0x70(%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, 0x28(%rsp)
subq $0x6, %rax
ja 0x9c95
movq 0x28(%rsp), %rax
leaq 0x2e39(%rip), %rcx      # 0xc308
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x10, 0x60(%rsp)
movq 0x70(%rsp), %rax
movl (%rax), %edi
leaq 0x50(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x23f0
movl %eax, 0x68(%rsp)
cmpl $-0x1, %eax
jne 0x9531
callq 0x2040
cmpl $0xb, (%rax)
je 0x9513
callq 0x2040
cmpl $0xb, (%rax)
jne 0x9520
movl $0x1, 0x6c(%rsp)
jmp 0x9c95
leaq 0x33c9(%rip), %rdi      # 0xc8f0
callq 0x23d0
jmp 0x9c95
movl 0x68(%rsp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x20e0
movl %eax, 0x64(%rsp)
cmpl $0x0, %eax
jl 0x956b
movl 0x68(%rsp), %edi
movl 0x64(%rsp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x20e0
cmpl $0x0, %eax
jge 0x9585
leaq 0x3387(%rip), %rdi      # 0xc8f9
callq 0x23d0
movl 0x68(%rsp), %edi
callq 0x21d0
jmp 0x9c95
movl 0x68(%rsp), %edi
movl $0x1, %edx
movl $0x800, %ecx            # imm = 0x800
xorl %r8d, %r8d
movl %edx, %esi
callq 0x45f0
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x95e1
jmp 0x95ac
leaq 0x7ce5(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x95d1
movq 0x7a38(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x3342(%rip), %rsi      # 0xc90c
movb $0x0, %al
callq 0x2280
jmp 0x95d3
movl 0x68(%rsp), %edi
callq 0x21d0
jmp 0x9c95
jmp 0x9c95
movq 0x70(%rsp), %rdi
callq 0x8c90
cmpl $0x0, %eax
je 0x95fa
jmp 0x9499
movq 0x70(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
je 0x9619
movq 0x70(%rsp), %rdi
callq 0x8d90
cmpl $0x0, %eax
jne 0x9628
jmp 0x962d
movq 0x70(%rsp), %rdi
callq 0x8ee0
cmpl $0x0, %eax
je 0x962d
jmp 0x9499
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x9110
cmpl $0x0, %eax
jne 0x967e
jmp 0x9643
leaq 0x7c4e(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9668
movq 0x79a1(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x324e(%rip), %rsi      # 0xc8af
movb $0x0, %al
callq 0x2280
jmp 0x966a
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
movl $0x1, 0x6c(%rsp)
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0x60(%rax)
jne 0x96a5
movq 0x70(%rsp), %rdi
callq 0x6b80
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0x9759
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0x60(%rcx), %eax
jle 0x96d4
movq 0x70(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x24(%rsp)
jmp 0x96e0
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rdi
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rsi
movslq 0x40(%rsp), %rdx
callq 0x22a0
movl 0x40(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x58(%rax)
movl 0x40(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x60(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x60(%rax)
movl 0x40(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x20(%rax)
movl 0x40(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x9c95
movq 0x70(%rsp), %rax
movl (%rax), %edi
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rsi
movq 0x70(%rsp), %rax
movslq 0x60(%rax), %rdx
callq 0x2210
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
jle 0x97ca
movslq 0x44(%rsp), %rcx
leaq 0x7b62(%rip), %rax      # 0x112f0
addq 0x48(%rax), %rcx
leaq 0x7b57(%rip), %rax      # 0x112f0
movq %rcx, 0x48(%rax)
movl 0x44(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x58(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x58(%rax)
movl 0x44(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x60(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x60(%rax)
jmp 0x9c95
cmpl $0x0, 0x44(%rsp)
jne 0x97e5
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
cmpl $-0x1, 0x44(%rsp)
jne 0x985e
callq 0x2040
cmpl $0xb, (%rax)
je 0x9800
callq 0x2040
cmpl $0xb, (%rax)
jne 0x985e
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x9110
cmpl $0x0, %eax
jne 0x9851
jmp 0x9816
leaq 0x7a7b(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x983b
movq 0x77ce(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x307b(%rip), %rsi      # 0xc8af
movb $0x0, %al
callq 0x2280
jmp 0x983d
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
movl $0x1, 0x6c(%rsp)
jmp 0x9c95
jmp 0x9860
leaq 0x7a31(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9885
movq 0x7784(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x30ae(%rip), %rsi      # 0xc92c
movb $0x0, %al
callq 0x2280
jmp 0x9887
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0x74(%rax)
jne 0x98ba
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x4f80
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0x9937
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0x74(%rcx), %eax
jle 0x98e5
movq 0x70(%rsp), %rax
movl 0x74(%rax), %eax
movl %eax, 0x20(%rsp)
jmp 0x98f1
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x74(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x74(%rax)
movl 0x3c(%rsp), %edx
movq 0x70(%rsp), %rax
movq 0x20(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x20(%rax)
movl 0x3c(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x2c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x9c95
movq 0x70(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x70(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x70(%rsp), %rax
movl 0x28(%rax), %eax
movq 0x70(%rsp), %rcx
cmpl 0x74(%rcx), %eax
jle 0x9970
movq 0x70(%rsp), %rax
movl 0x74(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x997c
movq 0x70(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rsi
movl 0x14(%rsp), %edi
movl 0x10(%rsp), %eax
movslq %eax, %rdx
callq 0x2210
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x44(%rsp)
jle 0x99cd
movslq 0x44(%rsp), %rcx
leaq 0x7948(%rip), %rax      # 0x112f0
addq 0x48(%rax), %rcx
leaq 0x793d(%rip), %rax      # 0x112f0
movq %rcx, 0x48(%rax)
movl 0x44(%rsp), %edx
movq 0x70(%rsp), %rax
movl 0x74(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x74(%rax)
jmp 0x9c95
cmpl $0x0, 0x44(%rsp)
jne 0x99e8
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
cmpl $-0x1, 0x44(%rsp)
jne 0x9a61
callq 0x2040
cmpl $0xb, (%rax)
je 0x9a03
callq 0x2040
cmpl $0xb, (%rax)
jne 0x9a61
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x9110
cmpl $0x0, %eax
jne 0x9a54
jmp 0x9a19
leaq 0x7878(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9a3e
movq 0x75cb(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x2e78(%rip), %rsi      # 0xc8af
movb $0x0, %al
callq 0x2280
jmp 0x9a40
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
movl $0x1, 0x6c(%rsp)
jmp 0x9c95
jmp 0x9a63
leaq 0x782e(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9a88
movq 0x7581(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x2eab(%rip), %rsi      # 0xc92c
movb $0x0, %al
callq 0x2280
jmp 0x9a8a
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0x84(%rax)
jne 0x9b28
movq 0x70(%rsp), %rdi
movq 0x70(%rsp), %rax
movq 0x38(%rax), %rsi
movq 0x70(%rsp), %rax
movl 0x44(%rax), %edx
callq 0x50b0
cmpl $0x0, %eax
jne 0x9ae9
movq 0x70(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
je 0x9b26
movq 0x70(%rsp), %rdi
callq 0x6920
cmpl $0x0, %eax
je 0x9b26
jmp 0x9aeb
leaq 0x77a6(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9b10
movq 0x74f9(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x2e4c(%rip), %rsi      # 0xc955
movb $0x0, %al
callq 0x2280
jmp 0x9b12
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c95
jmp 0x9b28
jmp 0x9b2a
movq 0x70(%rsp), %rdi
callq 0x9200
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0x3, %eax
ja 0x9c67
movq 0x8(%rsp), %rax
leaq 0x27d4(%rip), %rcx      # 0xc324
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x70(%rsp), %rax
cmpl $0x6, 0x4(%rax)
jne 0x9bd3
jmp 0x9b66
movq 0x70(%rsp), %rax
cmpl $0x0, 0xb8(%rax)
jle 0x9bbf
movq 0x70(%rsp), %rax
movq 0xb0(%rax), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x5c80
movq 0x70(%rsp), %rax
movq 0xb0(%rax), %rcx
addq $0x8, %rcx
movq %rcx, 0xb0(%rax)
movq 0x70(%rsp), %rax
movl 0xb8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xb8(%rax)
jmp 0x9b66
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x4f80
jmp 0x9c5b
movq 0x70(%rsp), %rax
cmpl $0x2, 0x4(%rax)
jne 0x9c19
movq 0x70(%rsp), %rax
cmpq $0x0, 0x50(%rax)
je 0x9c05
movq 0x70(%rsp), %rax
movq 0x50(%rax), %rdi
callq 0x2030
movq 0x70(%rsp), %rax
movq $0x0, 0x50(%rax)
movq 0x70(%rsp), %rdi
movq 0x70(%rsp), %rax
movl 0x48(%rax), %esi
callq 0x4f80
jmp 0x9c59
jmp 0x9c1b
leaq 0x7676(%rip), %rax      # 0x11298
cmpl $0x0, 0x18(%rax)
jle 0x9c48
movq 0x73c9(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0x2d2d(%rip), %rsi      # 0xc96e
movb $0x0, %al
callq 0x2280
jmp 0x9c4a
movq 0x70(%rsp), %rdi
movl $0x5, %esi
callq 0x4f80
jmp 0x9c5b
jmp 0x9c67
jmp 0x9c67
movl $0x1, 0x6c(%rsp)
jmp 0x9c95
movq 0x70(%rsp), %rax
cmpl $0x0, 0xbc(%rax)
je 0x9c83
movq 0x70(%rsp), %rdi
callq 0x4b40
jmp 0x9c8d
movq 0x70(%rsp), %rdi
callq 0x4bf0
movl $0x1, 0x6c(%rsp)
jmp 0x9499
addq $0x78, %rsp
retq
nop