// File: /workspace/github_c_makefile_star2_O2/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 0x615c(%rip), %r13      # 0x9384
movl (%r13), %eax
testl %eax, %eax
jle 0x3249
leaq 0x6141(%rip), %rcx      # 0x9378
movq (%rcx), %rcx
decl %eax
movl %eax, (%r13)
movq (%rcx,%rax,8), %rbx
jmp 0x32d7
movl $0xe8, %edi
callq 0x22b0
testq %rax, %rax
je 0x341c
movq %rax, %rbx
movl $0x2000, %edi           # imm = 0x2000
callq 0x22b0
movq %rax, %r13
movq %rax, 0x90(%rbx)
movl $0x2000, %edi           # imm = 0x2000
callq 0x22b0
movq %rax, %r14
movq %rax, 0xa8(%rbx)
testq %r13, %r13
je 0x3425
testq %r14, %r14
je 0x3425
movl $0x2000, %eax           # imm = 0x2000
movl %eax, 0xb8(%rbx)
movl %eax, 0xa0(%rbx)
movq %r13, 0x98(%rbx)
leaq 0x60d0(%rip), %r13      # 0x9388
leaq 0x28(%r13), %r14
movq %r14, %rdi
callq 0x2460
incl 0x4(%r13)
movq %r14, %rdi
callq 0x22a0
leaq 0x60ad(%rip), %r13      # 0x9384
leaq 0x5f92(%rip), %rax      # 0x9270
cmpl $0x2, 0x10(%rax)
jge 0x33ef
movl %ebp, (%rbx)
movl %r12d, 0x4(%rbx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0xbc(%rbx)
andl $0x0, 0xa4(%rbx)
movq 0xa8(%rbx), %rax
movq %rax, 0xb0(%rbx)
leaq 0x8(%rbx), %r14
movswl %r15w, %r15d
leaq 0x487(%rip), %rcx       # 0x37aa
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
movq %rbx, %r8
callq 0x2350
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x2060
movw %r15w, 0x88(%rbx)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2220
cmpl $-0x1, %eax
je 0x3376
leaq 0x602b(%rip), %r15      # 0x9388
leaq 0x28(%r15), %r14
movq %r14, %rdi
callq 0x2460
incl (%r15)
movq %r14, %rdi
callq 0x22a0
jmp 0x33dd
movslq (%r13), %rax
leaq 0x5fff(%rip), %rcx      # 0x9380
cmpl (%rcx), %eax
jge 0x339c
leaq 0x5fec(%rip), %rcx      # 0x9378
movq (%rcx), %rcx
leal 0x1(%rax), %edx
movl %edx, (%r13)
movq %rbx, (%rcx,%rax,8)
jmp 0x33db
movq 0x90(%rbx), %rdi
callq 0x2030
movq 0xa8(%rbx), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x5fc5(%rip), %r14      # 0x9388
leaq 0x28(%r14), %rbx
movq %rbx, %rdi
callq 0x2460
decl 0x4(%r14)
movq %rbx, %rdi
callq 0x22a0
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 0x5bff(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x25fb(%rip), %rax      # 0x59fe
leaq 0x260a(%rip), %rsi      # 0x5a14
cmoveq %rax, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2230
jmp 0x32e8
leaq 0x25c5(%rip), %rdi      # 0x59e8
jmp 0x3444
movq %r13, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x25b1(%rip), %rdi      # 0x59f5
callq 0x2380
jmp 0x33db