// File: /workspace/github_c_makefile_star2_O3/desion[P]terminal/src/connection.c
// Function: conn_new
conn *conn_new(int sfd, int init_state, int event_flags, struct event_base *base) {
    conn *c;
    if (freecurr > 0) {
        c = freeconns[--freecurr];
    } else {
        if (!(c = (conn *)malloc(sizeof(conn)))) {
            perror("malloc(conn)");
            return NULL;
        }

        c->rbuf = c->wbuf = 0;
        c->rbuf = (char *) malloc(DATA_BUFFER_SIZE);
        c->wbuf = (char *) malloc(DATA_BUFFER_SIZE);

        if (c->rbuf == 0 || c->wbuf == 0) {
            if (c->rbuf != 0) free(c->rbuf);
            if (c->wbuf != 0) free(c->wbuf);
            free(c);
            perror("malloc()");
            return NULL;
        }
        c->rsize = c->wsize = DATA_BUFFER_SIZE;
        c->rcurr = c->rbuf;

        STATS_LOCK(stats);
        stats.conn_structs++;
        STATS_UNLOCK(stats);
    }

    if (settings.verbose > 1) {
        if (init_state == conn_listening)
            fprintf(stderr, "<%d server listening\n", sfd);
        else
            fprintf(stderr, "<%d new client connection\n", sfd);
    }

    c->sfd = sfd;
    c->state = init_state;
    c->rbytes = c->wbytes = 0;
    c->wcurr = c->wbuf;
    c->write_and_go = conn_read;
    event_set(&c->event, sfd, event_flags, event_handler, (void *)c);
    event_base_set(base, &c->event);
    c->ev_flags = event_flags;
    if (event_add(&c->event, 0) == -1) {
        if (freecurr < freetotal) {
            freeconns[freecurr++] = c;
        } else {
            free (c->rbuf);
            free (c->wbuf);
            free (c);

            STATS_LOCK(stats);
            stats.conn_structs--;
            STATS_UNLOCK(stats);
        }
        return NULL;
    }

    STATS_LOCK(stats);
    stats.curr_conns++;
    STATS_UNLOCK(stats);
    return c;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, (%rsp)
movl %edx, %r15d
movl %esi, %r12d
movl %edi, %ebp
leaq 0x5e49(%rip), %r13      # 0x9384
movl (%r13), %eax
testl %eax, %eax
jle 0x355c
leaq 0x5e2e(%rip), %rcx      # 0x9378
movq (%rcx), %rcx
decl %eax
movl %eax, (%r13)
movq (%rcx,%rax,8), %rbx
jmp 0x35ea
movl $0xe8, %edi
callq 0x22c0
testq %rax, %rax
je 0x3732
movq %rax, %rbx
movl $0x2000, %edi           # imm = 0x2000
callq 0x22c0
movq %rax, %r13
movq %rax, 0x90(%rbx)
movl $0x2000, %edi           # imm = 0x2000
callq 0x22c0
movq %rax, %r14
movq %rax, 0xa8(%rbx)
testq %r13, %r13
je 0x373b
testq %r14, %r14
je 0x373b
movl $0x2000, %eax           # imm = 0x2000
movl %eax, 0xb8(%rbx)
movl %eax, 0xa0(%rbx)
movq %r13, 0x98(%rbx)
leaq 0x5dbd(%rip), %r13      # 0x9388
leaq 0x28(%r13), %r14
movq %r14, %rdi
callq 0x2470
incl 0x4(%r13)
movq %r14, %rdi
callq 0x22b0
leaq 0x5d9a(%rip), %r13      # 0x9384
leaq 0x5c7f(%rip), %rax      # 0x9270
cmpl $0x2, 0x10(%rax)
jge 0x3705
movl %ebp, (%rbx)
movl %r12d, 0x4(%rbx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0xbc(%rbx)
movl $0x0, 0xa4(%rbx)
movq 0xa8(%rbx), %rax
movq %rax, 0xb0(%rbx)
leaq 0x8(%rbx), %r14
movswl %r15w, %r15d
leaq 0x477(%rip), %rcx       # 0x3ab0
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movq %rbx, %r8
callq 0x2360
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x2060
movw %r15w, 0x88(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2230
cmpl $-0x1, %eax
je 0x368c
leaq 0x5d15(%rip), %r15      # 0x9388
leaq 0x28(%r15), %r14
movq %r14, %rdi
callq 0x2470
incl (%r15)
movq %r14, %rdi
callq 0x22b0
jmp 0x36f3
movslq (%r13), %rax
leaq 0x5ce9(%rip), %rcx      # 0x9380
cmpl (%rcx), %eax
jge 0x36b2
leaq 0x5cd6(%rip), %rcx      # 0x9378
movq (%rcx), %rcx
leal 0x1(%rax), %edx
movl %edx, (%r13)
movq %rbx, (%rcx,%rax,8)
jmp 0x36f1
movq 0x90(%rbx), %rdi
callq 0x2030
movq 0xa8(%rbx), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x5caf(%rip), %r14      # 0x9388
leaq 0x28(%r14), %rbx
movq %rbx, %rdi
callq 0x2470
decl 0x4(%r14)
movq %rbx, %rdi
callq 0x22b0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
movq 0x58e9(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x22e5(%rip), %rax      # 0x59fe
leaq 0x22f4(%rip), %rsi      # 0x5a14
cmoveq %rax, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x35fb
leaq 0x22af(%rip), %rdi      # 0x59e8
jmp 0x3764
testq %r13, %r13
je 0x3748
movq %r13, %rdi
callq 0x2030
testq %r14, %r14
je 0x3755
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x2291(%rip), %rdi      # 0x59f5
callq 0x2390
jmp 0x36f1