// File: /workspace/github_c_makefile_star2_O3/desion[P]terminal/src/network.c
// Function: drive_machine
void drive_machine(conn *c) {
    int stop = 0;
    int sfd, flags = 1;
    socklen_t addrlen;
    struct sockaddr_in addr;
    conn *newc;
    int res;

    assert(c != NULL);

    while (!stop) {
        switch(c->state) {
        case conn_listening:
            addrlen = sizeof(addr);
            if ((sfd = accept(c->sfd, (struct sockaddr *)&addr, &addrlen)) == -1) {
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    /* these are transient, so don't log anything */
                    stop = 1;
                } else if (errno == EMFILE) {
                    if (settings.verbose > 0)
                        fprintf(stderr, "Too many open connections\n");
                    stop = 1;
                } else {
                    perror("accept()");
                    stop = 1;
                }
                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, c->event.ev_base);
            if (!newc) {
                if (settings.verbose > 0)
                    fprintf(stderr, "couldn't create new connection\n");
                close(sfd);
            }
            break;

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

        case conn_write:
            if (c->wbytes == 0) {
                c->state = c->write_and_go;
                break;
            }

            res = write(c->sfd, c->wcurr, c->wbytes);
            if (res > 0) {
                c->wcurr  += res;
                c->wbytes -= res;
                break;
            }
            if (res == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
                if (!update_event(c, EV_WRITE | EV_PERSIST)) {
                    if (settings.verbose > 0)
                        fprintf(stderr, "Couldn't update event\n");
                    c->state = conn_closing;
                    break;
                }
                stop = 1;
                break;
            }
            c->state = conn_closing;
            break;

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

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x3f55
movq %rdi, %rbx
leaq 0x1d25(%rip), %r12      # 0x5aa4
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
leaq 0x54e0(%rip), %r13      # 0x9270
movl 0x4(%rbx), %eax
cmpq $0x3, %rax
ja 0x3d90
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movl $0x10, 0x4(%rsp)
movl (%rbx), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x23b0
cmpl $-0x1, %eax
je 0x3f19
movl %eax, %ebp
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3ef0
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3ef0
movq 0x48(%rbx), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0x12, %edx
callq 0x351d
testq %rax, %rax
jne 0x3d90
cmpl $0x0, 0x10(%r13)
jle 0x3efc
callq 0x2565
jmp 0x3efc
movslq 0xbc(%rbx), %rdx
testq %rdx, %rdx
je 0x3eaf
movl (%rbx), %edi
movq 0xb0(%rbx), %rsi
callq 0x20f0
testl %eax, %eax
jle 0x3ebd
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0xb0(%rbx)
subl %eax, 0xbc(%rbx)
jmp 0x3d90
movq %rbx, %rdi
callq 0x3bc7
cmpl $0x2, %eax
je 0x3d90
cmpl $0x1, %eax
je 0x3f31
movq %rbx, %rdi
callq 0x3c42
testl %eax, %eax
jne 0x3d90
movq %rbx, %rdi
movl $0x12, %esi
callq 0x3a08
testl %eax, %eax
jne 0x3f31
cmpl $0x0, 0x10(%r13)
jle 0x3ee4
callq 0x2543
jmp 0x3ee4
movl 0xc0(%rbx), %eax
movl %eax, 0x4(%rbx)
jmp 0x3d90
cmpl $-0x1, %eax
jne 0x3ee4
callq 0x2040
cmpl $0xb, (%rax)
jne 0x3ee4
movq %rbx, %rdi
movl $0x14, %esi
callq 0x3a08
testl %eax, %eax
jne 0x3f31
cmpl $0x0, 0x10(%r13)
jg 0x3f08
movl $0x3, 0x4(%rbx)
jmp 0x3d90
leaq 0x1ca1(%rip), %rdi      # 0x5b98
callq 0x2390
movl %ebp, %edi
callq 0x21a0
jmp 0x3d90
callq 0x2521
jmp 0x3ee4
movq %rbx, %rdi
callq 0x376b
jmp 0x3f31
callq 0x2040
movl (%rax), %eax
cmpl $0xb, %eax
je 0x3f31
cmpl $0x18, %eax
jne 0x3f40
cmpl $0x0, 0x10(%r13)
jg 0x3f4e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c48(%rip), %rdi      # 0x5b8f
callq 0x2390
jmp 0x3f31
callq 0x2587
jmp 0x3f31
leaq 0x1b58(%rip), %rdi      # 0x5ab4
leaq 0x1b5b(%rip), %rsi      # 0x5abe
leaq 0x1bef(%rip), %rcx      # 0x5b59
movl $0x8a, %edx
callq 0x2180