// File: /workspace/github_c_makefile_star2_O1/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 0x5ded(%rip), %r13      # 0x937c
movl (%r13), %eax
testl %eax, %eax
jle 0x35b0
leaq 0x5dd2(%rip), %rcx      # 0x9370
movq (%rcx), %rcx
decl %eax
movl %eax, (%r13)
movq (%rcx,%rax,8), %rbx
jmp 0x364e
movl $0xe8, %edi
callq 0x22c0
testq %rax, %rax
je 0x3796
movq %rax, %rbx
xorl %eax, %eax
movq %rax, 0xa8(%rbx)
movq %rax, 0x90(%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 0x379f
testq %r14, %r14
je 0x379f
movl $0x2000, %eax           # imm = 0x2000
movl %eax, 0xb8(%rbx)
movl %eax, 0xa0(%rbx)
movq %r13, 0x98(%rbx)
leaq 0x5d51(%rip), %r13      # 0x9380
leaq 0x28(%r13), %r14
movq %r14, %rdi
callq 0x2470
incl 0x4(%r13)
movq %r14, %rdi
callq 0x22b0
leaq 0x5d2e(%rip), %r13      # 0x937c
leaq 0x5c13(%rip), %rax      # 0x9268
cmpl $0x2, 0x10(%rax)
jge 0x3769
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       # 0x3b14
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 0x36f0
leaq 0x5ca9(%rip), %r15      # 0x9380
leaq 0x28(%r15), %r14
movq %r14, %rdi
callq 0x2470
incl (%r15)
movq %r14, %rdi
callq 0x22b0
jmp 0x3757
movslq (%r13), %rax
leaq 0x5c7d(%rip), %rcx      # 0x9378
cmpl (%rcx), %eax
jge 0x3716
leaq 0x5c6a(%rip), %rcx      # 0x9370
movq (%rcx), %rcx
leal 0x1(%rax), %edx
movl %edx, (%r13)
movq %rbx, (%rcx,%rax,8)
jmp 0x3755
movq 0x90(%rbx), %rdi
callq 0x2030
movq 0xa8(%rbx), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x5c43(%rip), %r14      # 0x9380
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 0x5885(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x2271(%rip), %rax      # 0x59ee
leaq 0x2280(%rip), %rsi      # 0x5a04
cmoveq %rax, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x365f
leaq 0x223b(%rip), %rdi      # 0x59d8
jmp 0x37c8
testq %r13, %r13
je 0x37ac
movq %r13, %rdi
callq 0x2030
testq %r14, %r14
je 0x37b9
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x221d(%rip), %rdi      # 0x59e5
callq 0x2390
jmp 0x3755