// File: /workspace/github_c_makefile_star2_O0/panux[P]linit/src/linitd.c
// Function: handle_conn
void handle_conn(struct conn *c, uint32_t events) {
    ref(c); //make sure that c isn't freed in the middle
    if(EPOLL_MODE(events, EPOLLIN)) {
        if(!readBuf(&c->rx, c->fd)) {
            fprintf(stderr, "[ERROR] Failed to read from connection %d\n", c->fd);
            close_conn(c);
        }
        if(conn_has_cmd(c)) {
            handle_scmd(c);
            if(c->closed) { //command closed connection
                deref(c);
                return;
            }
            if(c->txoff && (c->tx.len > 0)) {  //tx buffer was just populated
                //enable write in epoll
                struct epoll_event ev = {.events = EPOLLIN | EPOLLOUT, .data = {.ptr = &c->e}};
                if(epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) {   //failed to enable - close and forget
                    fprintf(stderr, "[ERROR] Failed to enable writing on conn %d\n", c->fd);
                    close_conn(c);
                } else {
                    c->txoff = false;
                }
            }
        }
    } else if(EPOLL_MODE(events, EPOLLOUT)) {   //available for writing
        if(!writeBuf(&c->tx, c->fd)) {
            fprintf(stderr, "[ERROR] Failed to write to connection %d\n", c->fd);
            close_conn(c);
        } else {
            if(c->tx.len == 0) {    //write done - disable it in epoll
                struct epoll_event ev = {.events = EPOLLIN, .data = {.ptr = &c->e}};
                if(epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) {   //failed to disable - close and forget
                    fprintf(stderr, "[ERROR] Failed to disable writing on conn %d\n", c->fd);
                    close_conn(c);
                } else {
                    c->txoff = true;
                }
            }
        }
    } else if(EPOLL_MODE(events, EPOLLRDHUP) || EPOLL_MODE(events, EPOLLHUP)) {
        printf("[INFO] Connection %d closed by peer\n", c->fd);
        close_conn(c);
    } else {
        fprintf(stderr, "[FATAL] Unrecognized epoll event %d on fd %d\n", events, c->fd);
        exit(65);
    }
    deref(c);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl -0xc(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2081
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x1b40
testb $0x1, %al
jne 0x1fbb
movq 0x505e(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x2362(%rip), %rsi      # 0x430d
movb $0x0, %al
callq 0x1140
movq -0x8(%rbp), %rdi
callq 0x1620
movq -0x8(%rbp), %rdi
callq 0x2500
testb $0x1, %al
jne 0x1fcd
jmp 0x207c
movq -0x8(%rbp), %rdi
callq 0x2590
movq -0x8(%rbp), %rax
testb $0x1, 0x18(%rax)
je 0x2002
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %eax
movl %eax, %edx
addl $-0x1, %edx
movl %edx, 0x10(%rcx)
cmpl $0x0, %eax
jne 0x1ffd
movq -0x8(%rbp), %rdi
callq 0x1030
jmp 0x21c9
movq -0x8(%rbp), %rax
testb $0x1, 0x19(%rax)
je 0x207a
movq -0x8(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x207a
movl $0x5, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x14(%rbp)
movl 0x5124(%rip), %edi      # 0x7150
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
movl $0x3, %esi
leaq -0x18(%rbp), %rcx
callq 0x1100
cmpl $-0x1, %eax
jne 0x2070
movq 0x4fab(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x1fe4(%rip), %rsi      # 0x4042
movb $0x0, %al
callq 0x1140
movq -0x8(%rbp), %rdi
callq 0x1620
jmp 0x2078
movq -0x8(%rbp), %rax
movb $0x0, 0x19(%rax)
jmp 0x207a
jmp 0x207c
jmp 0x21ac
movl -0xc(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x2144
movq -0x8(%rbp), %rdi
addq $0x30, %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %esi
callq 0x1c40
testb $0x1, %al
jne 0x20d2
movq 0x4f49(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x2278(%rip), %rsi      # 0x4338
movb $0x0, %al
callq 0x1140
movq -0x8(%rbp), %rdi
callq 0x1620
jmp 0x2142
movq -0x8(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x2140
movl $0x1, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movl 0x505e(%rip), %edi      # 0x7150
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
movl $0x3, %esi
leaq -0x24(%rbp), %rcx
callq 0x1100
cmpl $-0x1, %eax
jne 0x2136
movq 0x4ee5(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x223e(%rip), %rsi      # 0x4362
movb $0x0, %al
callq 0x1140
movq -0x8(%rbp), %rdi
callq 0x1620
jmp 0x213e
movq -0x8(%rbp), %rax
movb $0x1, 0x19(%rax)
jmp 0x2140
jmp 0x2142
jmp 0x21aa
movl -0xc(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x215c
movl -0xc(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x217c
movq -0x8(%rbp), %rax
movl 0x14(%rax), %esi
leaq 0x2226(%rip), %rdi      # 0x4390
movb $0x0, %al
callq 0x10c0
movq -0x8(%rbp), %rdi
callq 0x1620
jmp 0x21a8
movq 0x4e75(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x14(%rax), %ecx
leaq 0x221e(%rip), %rsi      # 0x43b5
movb $0x0, %al
callq 0x1140
movl $0x41, %edi
callq 0x1210
jmp 0x21aa
jmp 0x21ac
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %eax
movl %eax, %edx
addl $-0x1, %edx
movl %edx, 0x10(%rcx)
cmpl $0x0, %eax
jne 0x21c9
movq -0x8(%rbp), %rdi
callq 0x1030
addq $0x30, %rsp
popq %rbp
retq
nop