// File: /workspace/github_c_makefile_star2_O2/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 0x3cb3
movq %rdi, %rbx
leaq 0x2003(%rip), %rbp      # 0x5aa4
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
pushq $0x12
popq %r12
movl 0x4(%rbx), %eax
cmpq $0x3, %rax
ja 0x3aaf
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
movl $0x10, 0x4(%rsp)
movl (%rbx), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x23a0
cmpl $-0x1, %eax
je 0x3c5a
movl %eax, %r13d
movl %eax, %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3c37
orl $0x800, %eax            # imm = 0x800
movl %r13d, %edi
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3c37
movq 0x48(%rbx), %rcx
movl %r13d, %edi
pushq $0x1
popq %rsi
movl %r12d, %edx
callq 0x320a
testq %rax, %rax
jne 0x3aaf
leaq 0x573d(%rip), %rax      # 0x9270
cmpl $0x0, 0x10(%rax)
jle 0x3c43
movq 0x54b4(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x205d(%rip), %rdi      # 0x5bab
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
jmp 0x3c43
movslq 0xbc(%rbx), %rdx
testq %rdx, %rdx
je 0x3c06
movl (%rbx), %edi
movq 0xb0(%rbx), %rsi
callq 0x20f0
testl %eax, %eax
jle 0x3c14
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0xb0(%rbx)
subl %eax, 0xbc(%rbx)
jmp 0x3aaf
movq %rbx, %rdi
callq 0x38ce
cmpl $0x2, %eax
je 0x3aaf
cmpl $0x1, %eax
je 0x3c78
movq %rbx, %rdi
callq 0x3947
testl %eax, %eax
jne 0x3aaf
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3704
testl %eax, %eax
jne 0x3c78
leaq 0x568e(%rip), %rax      # 0x9270
cmpl $0x0, 0x10(%rax)
jle 0x3c2b
movq 0x5409(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1fd2(%rip), %rdi      # 0x5bcb
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
jmp 0x3c2b
movl 0xc0(%rbx), %eax
movl %eax, 0x4(%rbx)
jmp 0x3aaf
cmpl $-0x1, %eax
jne 0x3c2b
callq 0x2040
cmpl $0xb, (%rax)
jne 0x3c2b
movq %rbx, %rdi
pushq $0x14
popq %rsi
jmp 0x3bce
movl $0x3, 0x4(%rbx)
jmp 0x3aaf
leaq 0x1f5a(%rip), %rdi      # 0x5b98
callq 0x2380
movl %r13d, %edi
callq 0x2190
jmp 0x3aaf
movq %rbx, %rdi
callq 0x344b
jmp 0x3c78
callq 0x2040
movl (%rax), %eax
cmpl $0xb, %eax
je 0x3c78
cmpl $0x18, %eax
jne 0x3c87
leaq 0x55fe(%rip), %rax      # 0x9270
cmpl $0x0, 0x10(%rax)
jg 0x3c95
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f01(%rip), %rdi      # 0x5b8f
callq 0x2380
jmp 0x3c78
movq 0x535c(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1ece(%rip), %rdi      # 0x5b74
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
jmp 0x3c78
leaq 0x1dfa(%rip), %rdi      # 0x5ab4
leaq 0x1dfd(%rip), %rsi      # 0x5abe
leaq 0x1e91(%rip), %rcx      # 0x5b59
movl $0x8a, %edx
callq 0x2180