// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x1, -0x14(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x4b33
jmp 0x4b52
leaq 0x275e(%rip), %rdi      # 0x7298
leaq 0x2761(%rip), %rsi      # 0x72a2
movl $0x8a, %edx
leaq 0x27f0(%rip), %rcx      # 0x733d
callq 0x2190
jmp 0x4b54
cmpl $0x0, -0xc(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4b66
jmp 0x4e6c
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x48(%rbp)
subq $0x3, %rax
ja 0x4e67
movq -0x48(%rbp), %rax
leaq 0x2702(%rip), %rcx      # 0x7288
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x10, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %edi
leaq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x23d0
movl %eax, -0x10(%rbp)
cmpl $-0x1, %eax
jne 0x4c28
callq 0x2040
cmpl $0xb, (%rax)
je 0x4bcd
callq 0x2040
cmpl $0xb, (%rax)
jne 0x4bd6
movl $0x1, -0xc(%rbp)
jmp 0x4c23
callq 0x2040
cmpl $0x18, (%rax)
jne 0x4c0e
leaq 0x5e91(%rip), %rax      # 0xaa78
cmpl $0x0, 0x10(%rax)
jle 0x4c05
movq 0x5404(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x275a(%rip), %rsi      # 0x7358
movb $0x0, %al
callq 0x2260
movl $0x1, -0xc(%rbp)
jmp 0x4c21
leaq 0x275e(%rip), %rdi      # 0x7373
callq 0x23b0
movl $0x1, -0xc(%rbp)
jmp 0x4c23
jmp 0x4e67
movl -0x10(%rbp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x20d0
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jl 0x4c5e
movl -0x10(%rbp), %edi
movl -0x14(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x20d0
cmpl $0x0, %eax
jge 0x4c77
leaq 0x2717(%rip), %rdi      # 0x737c
callq 0x23b0
movl -0x10(%rbp), %edi
callq 0x21c0
jmp 0x4e67
movl -0x10(%rbp), %edi
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
movl $0x1, %esi
movl $0x12, %edx
callq 0x3d20
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4cc9
leaq 0x5dd5(%rip), %rax      # 0xaa78
cmpl $0x0, 0x10(%rax)
jle 0x4cc1
movq 0x5348(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x26d5(%rip), %rsi      # 0x738f
movb $0x0, %al
callq 0x2260
movl -0x10(%rbp), %edi
callq 0x21c0
jmp 0x4e67
movq -0x8(%rbp), %rdi
callq 0x47f0
movl %eax, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jne 0x4ce5
jmp 0x4b54
cmpl $0x1, -0x34(%rbp)
jne 0x4cf7
movl $0x1, -0xc(%rbp)
jmp 0x4e67
jmp 0x4cf9
movq -0x8(%rbp), %rdi
callq 0x48f0
cmpl $0x0, %eax
je 0x4d0c
jmp 0x4b54
movq -0x8(%rbp), %rdi
movl $0x12, %esi
callq 0x4530
cmpl $0x0, %eax
jne 0x4d54
leaq 0x5d52(%rip), %rax      # 0xaa78
cmpl $0x0, 0x10(%rax)
jle 0x4d44
movq 0x52c5(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x2672(%rip), %rsi      # 0x73af
movb $0x0, %al
callq 0x2260
movq -0x8(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x4e67
movl $0x1, -0xc(%rbp)
jmp 0x4e67
movq -0x8(%rbp), %rax
cmpl $0x0, 0xbc(%rax)
jne 0x4d83
movq -0x8(%rbp), %rax
movl 0xc0(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x4e67
movq -0x8(%rbp), %rax
movl (%rax), %edi
movq -0x8(%rbp), %rax
movq 0xb0(%rax), %rsi
movq -0x8(%rbp), %rax
movslq 0xbc(%rax), %rdx
callq 0x20e0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x4de2
movl -0x34(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0xb0(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0xb0(%rax)
movl -0x34(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0xbc(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0xbc(%rax)
jmp 0x4e67
cmpl $-0x1, -0x34(%rbp)
jne 0x4e4a
callq 0x2040
cmpl $0xb, (%rax)
je 0x4dfc
callq 0x2040
cmpl $0xb, (%rax)
jne 0x4e4a
movq -0x8(%rbp), %rdi
movl $0x14, %esi
callq 0x4530
cmpl $0x0, %eax
jne 0x4e41
leaq 0x5c62(%rip), %rax      # 0xaa78
cmpl $0x0, 0x10(%rax)
jle 0x4e34
movq 0x51d5(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x2582(%rip), %rsi      # 0x73af
movb $0x0, %al
callq 0x2260
movq -0x8(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x4e67
movl $0x1, -0xc(%rbp)
jmp 0x4e67
movq -0x8(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x4e67
movq -0x8(%rbp), %rdi
callq 0x40c0
movl $0x1, -0xc(%rbp)
jmp 0x4b54
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)