// File: /workspace/github_c_makefile_star2_O2/zuocheng-liu[P]ae-memcached/src/connection.c
// Function: conn_new
conn *conn_new(int sfd, int init_state, int event_flags, int read_buffer_size,
                int is_udp) {
    conn *c;

    /* do we have a free conn structure from a previous close? */
    if (freecurr > 0) {
        c = freeconns[--freecurr];
    } else { /* allocate a new one */
        if (!(c = (conn *)malloc(sizeof(conn)))) {
            perror("malloc()");
            return 0;
        }
        c->rbuf = c->wbuf = 0;
        c->ilist = 0;
        c->iov = 0;
        c->msglist = 0;
        c->hdrbuf = 0;

        c->rsize = read_buffer_size;
        c->wsize = DATA_BUFFER_SIZE;
        c->isize = ITEM_LIST_INITIAL;
        c->iovsize = IOV_LIST_INITIAL;
        c->msgsize = MSG_LIST_INITIAL;
        c->hdrsize = 0;

        c->rbuf = (char *) malloc(c->rsize);
        c->wbuf = (char *) malloc(c->wsize);
        c->ilist = (item **) malloc(sizeof(item *) * c->isize);
        c->iov = (struct iovec *) malloc(sizeof(struct iovec) * c->iovsize);
        c->msglist = (struct msghdr *) malloc(sizeof(struct msghdr) * c->msgsize);

        if (c->rbuf == 0 || c->wbuf == 0 || c->ilist == 0 || c->iov == 0 ||
                c->msglist == 0) {
            if (c->rbuf != 0) free(c->rbuf);
            if (c->wbuf != 0) free(c->wbuf);
            if (c->ilist !=0) free(c->ilist);
            if (c->iov != 0) free(c->iov);
            if (c->msglist != 0) free(c->msglist);
            free(c);
            perror("malloc()");
            return 0;
        }

        stats.conn_structs++;
    }

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

    c->sfd = sfd;
    c->udp = is_udp;
    c->state = init_state;
    c->rlbytes = 0;
    c->rbytes = c->wbytes = 0;
    c->wcurr = c->wbuf;
    c->rcurr = c->rbuf;
    c->ritem = 0;
    c->icurr = c->ilist;
    c->ileft = 0;
    c->iovused = 0;
    c->msgcurr = 0;
    c->msgused = 0;

    c->write_and_go = conn_read;
    c->write_and_free = 0;
    c->item = 0;
    c->bucket = -1;
    c->gen = 0;
    
    c->ev_flags = event_flags;
    if (aeCreateFileEvent(g_el, sfd, event_flags, event_handler, (void *)c) == AE_ERR) {
        if (freecurr < freetotal) {
            freeconns[freecurr++] = c;
        } else {
            if (c->hdrbuf) {
                free (c->hdrbuf);
            }
            free (c->msglist);
            free (c->rbuf);
            free (c->wbuf);
            free (c->ilist);
            free (c->iov);
            free (c);
        }
        return 0;
    }

    stats.curr_conns++;
    stats.total_conns++;

    return c;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r12d
movl %edx, %ebp
movl %esi, %r13d
movl %edi, %r14d
movl 0x9ea4(%rip), %eax      # 0xd264
testl %eax, %eax
jle 0x33dc
movq 0x9e9d(%rip), %rcx      # 0xd268
decl %eax
movl %eax, 0x9e91(%rip)      # 0xd264
movq (%rcx,%rax,8), %rbx
jmp 0x34e3
movl %ecx, %r15d
movl $0xf0, %edi
callq 0x22c0
testq %rax, %rax
je 0x3664
movq %rax, %rbx
movl %r14d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
movl %r13d, 0x10(%rsp)
movl %r12d, 0x14(%rsp)
andq $0x0, 0xd8(%rax)
movl %r15d, 0x28(%rax)
movl $0x800, 0x40(%rax)      # imm = 0x800
movl $0xc8, 0xa8(%rax)
movl $0x190, 0x80(%rax)      # imm = 0x190
movl $0xa, 0x90(%rax)
andl $0x0, 0xe0(%rax)
movslq %r15d, %rdi
callq 0x22c0
movq %rax, %r14
movq %rax, 0x18(%rbx)
movl $0x800, %edi            # imm = 0x800
callq 0x22c0
movq %rax, %rbp
movq %rax, 0x30(%rbx)
movl $0x640, %edi            # imm = 0x640
callq 0x22c0
movq %rax, %r12
movq %rax, 0xa0(%rbx)
movl $0x1900, %edi           # imm = 0x1900
callq 0x22c0
movq %rax, %r13
movq %rax, 0x78(%rbx)
movl $0x230, %edi            # imm = 0x230
callq 0x22c0
movq %rax, %r15
movq %rax, 0x88(%rbx)
testq %r14, %r14
je 0x3634
testq %rbp, %rbp
je 0x3634
testq %r12, %r12
je 0x3634
testq %r13, %r13
je 0x3634
testq %r15, %r15
je 0x3634
leaq 0x9e13(%rip), %rax      # 0xd2e0
incl 0x18(%rax)
movl 0x14(%rsp), %r12d
movl 0x10(%rsp), %r13d
movl 0xc(%rsp), %ebp
movl 0x8(%rsp), %r14d
leaq 0x9d9e(%rip), %rax      # 0xd288
cmpl $0x2, 0x18(%rax)
jge 0x361c
movl %r14d, (%rbx)
movl %r12d, 0xbc(%rbx)
movl %r13d, 0x4(%rbx)
andl $0x0, 0x60(%rbx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x44(%rbx)
andl $0x0, 0x2c(%rbx)
movq 0x18(%rbx), %rax
movq 0x30(%rbx), %rcx
movq %rcx, 0x38(%rbx)
movq %rax, 0x20(%rbx)
movq 0xa0(%rbx), %rax
movq %rax, 0xb0(%rbx)
andl $0x0, 0xb8(%rbx)
andl $0x0, 0x84(%rbx)
andq $0x0, 0x94(%rbx)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x50(%rbx)
andq $0x0, 0x68(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0xe8(%rbx)
movw %bp, 0x10(%rbx)
leaq 0x9d07(%rip), %rax      # 0xd278
movq (%rax), %rdi
leaq 0x2580(%rip), %rcx      # 0x5afb
movl %r14d, %esi
movl %ebp, %edx
movq %rbx, %r8
callq 0x2766
cmpl $-0x1, %eax
je 0x35ab
leaq 0x9d4c(%rip), %rax      # 0xd2e0
movq 0x10(%rax), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x10(%rax)
jmp 0x3672
movslq 0x9cb2(%rip), %rax      # 0xd264
cmpl 0x9ca8(%rip), %eax      # 0xd260
jge 0x35d3
movq 0x9ca7(%rip), %rcx      # 0xd268
leal 0x1(%rax), %edx
movl %edx, 0x9c9a(%rip)      # 0xd264
movq %rbx, (%rcx,%rax,8)
jmp 0x3670
movq 0xd8(%rbx), %rdi
callq 0x2030
movq 0x88(%rbx), %rdi
callq 0x2030
movq 0x18(%rbx), %rdi
callq 0x2030
movq 0x30(%rbx), %rdi
callq 0x2030
movq 0xa0(%rbx), %rdi
callq 0x2030
movq 0x78(%rbx), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
jmp 0x3670
movq 0x99d5(%rip), %rax      # 0xcff8
testl %r13d, %r13d
jne 0x3684
movq (%rax), %rdi
leaq 0x4b62(%rip), %rsi      # 0x8194
jmp 0x369c
movq %r14, %rdi
callq 0x2030
movq %rbp, %rdi
callq 0x2030
movq %r12, %rdi
callq 0x2030
movq %r13, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x4b20(%rip), %rdi      # 0x818b
callq 0x23b0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rax), %rdi
testl %r12d, %r12d
jne 0x3695
leaq 0x4b33(%rip), %rsi      # 0x81c6
jmp 0x369c
leaq 0x4b0e(%rip), %rsi      # 0x81aa
movl %r14d, %edx
xorl %eax, %eax
callq 0x2260
jmp 0x34f4