// File: /workspace/github_c_makefile_star2_O1/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 0x406f
movq %rdi, %rbx
movb $0x1, %r12b
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
xorl %r13d, %r13d
movl 0x4(%rbx), %eax
cmpl $0x1, %eax
jne 0x3e88
movq %rbx, %rdi
callq 0x3c2b
cmpl $0x2, %eax
je 0x3e7d
movl %eax, %ebp
cmpl $0x1, %eax
je 0x403a
movq %rbx, %rdi
callq 0x3ca6
testl %eax, %eax
je 0x3f82
testb $0x1, %r12b
jne 0x3e4d
jmp 0x4060
cmpl $0x3, %eax
je 0x3f30
cmpl $0x2, %eax
je 0x3f42
testl %eax, %eax
jne 0x4037
movl $0x10, 0x4(%rsp)
movl (%rbx), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x23b0
cmpl $-0x1, %eax
je 0x3fb0
movl %eax, %ebp
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4024
orl $0x800, %eax            # imm = 0x800
movl %ebp, %edi
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x4024
movq 0x48(%rbx), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0x12, %edx
callq 0x3571
testq %rax, %rax
jne 0x4037
leaq 0x534c(%rip), %rax      # 0x9268
cmpl $0x0, 0x10(%rax)
jle 0x4030
callq 0x25ac
jmp 0x4030
movq %rbx, %rdi
callq 0x37cf
movl $0x1, %ebp
jmp 0x403a
movslq 0xbc(%rbx), %rdx
testq %rdx, %rdx
je 0x3fde
movl (%rbx), %edi
movq 0xb0(%rbx), %rsi
callq 0x20f0
testl %eax, %eax
jle 0x3fe9
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq %rcx, 0xb0(%rbx)
subl %eax, 0xbc(%rbx)
jmp 0x4037
movq %rbx, %rdi
movl $0x12, %esi
callq 0x3a6c
movl $0x1, %ebp
testl %eax, %eax
jne 0x403a
leaq 0x52c5(%rip), %rax      # 0x9268
cmpl $0x0, 0x10(%rax)
jle 0x401b
callq 0x258a
jmp 0x401b
callq 0x2040
movl (%rax), %eax
movl $0x1, %ebp
cmpl $0xb, %eax
je 0x403a
cmpl $0x18, %eax
jne 0x404b
leaq 0x5297(%rip), %rax      # 0x9268
cmpl $0x0, 0x10(%rax)
jle 0x403a
callq 0x25ce
jmp 0x403a
movl 0xc0(%rbx), %eax
movl %eax, 0x4(%rbx)
jmp 0x4037
cmpl $-0x1, %eax
jne 0x401b
callq 0x2040
cmpl $0xb, (%rax)
jne 0x401b
movq %rbx, %rdi
movl $0x14, %esi
callq 0x3a6c
movl $0x1, %ebp
testl %eax, %eax
jne 0x403a
leaq 0x5253(%rip), %rax      # 0x9268
cmpl $0x0, 0x10(%rax)
jg 0x4059
movl $0x3, 0x4(%rbx)
jmp 0x4037
leaq 0x1b5d(%rip), %rdi      # 0x5b88
callq 0x2390
movl %ebp, %edi
callq 0x21a0
movl %r13d, %ebp
movl %ebp, %r13d
testl %ebp, %ebp
sete %r12b
je 0x3e4d
jmp 0x4060
leaq 0x1b2d(%rip), %rdi      # 0x5b7f
callq 0x2390
jmp 0x403a
callq 0x2568
jmp 0x401b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a2e(%rip), %rdi      # 0x5aa4
leaq 0x1a31(%rip), %rsi      # 0x5aae
leaq 0x1ac5(%rip), %rcx      # 0x5b49
movl $0x8a, %edx
callq 0x2180