// File: /workspace/github_c_makefile_star2_O3/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 0x96b1(%rip), %eax      # 0xd264
testl %eax, %eax
jle 0x3bcf
movq 0x96aa(%rip), %rcx      # 0xd268
decl %eax
movl %eax, 0x969e(%rip)      # 0xd264
movq (%rcx,%rax,8), %rbx
jmp 0x3cce
movl %ecx, %r15d
movl $0xf0, %edi
callq 0x22c0
testq %rax, %rax
je 0x3e88
movq %rax, %rbx
movl %r13d, 0x8(%rsp)
movl %r12d, 0xc(%rsp)
movq $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)
movl $0x0, 0xe0(%rax)
movslq %r15d, %rdi
callq 0x22c0
movq %rax, %r15
movq %rax, 0x18(%rbx)
movl $0x800, %edi            # imm = 0x800
callq 0x22c0
movq %rax, %r13
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, 0x10(%rsp)
movq %rax, 0x78(%rbx)
movl $0x230, %edi            # imm = 0x230
callq 0x22c0
movq %rax, 0x88(%rbx)
testq %r15, %r15
je 0x3e38
testq %r13, %r13
movq 0x10(%rsp), %rcx
je 0x3e2a
testq %r12, %r12
je 0x3e2a
testq %rcx, %rcx
je 0x3e2a
testq %rax, %rax
je 0x3e2a
leaq 0x961f(%rip), %rax      # 0xd2e0
incl 0x18(%rax)
movl 0xc(%rsp), %r12d
movl 0x8(%rsp), %r13d
leaq 0x95b3(%rip), %rax      # 0xd288
cmpl $0x2, 0x18(%rax)
jge 0x3e0b
movl %r14d, (%rbx)
movl %r12d, 0xbc(%rbx)
movl %r13d, 0x4(%rbx)
xorl %eax, %eax
movl %eax, 0x60(%rbx)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x44(%rbx)
movl %eax, 0x2c(%rbx)
movq 0x18(%rbx), %rcx
movq 0x30(%rbx), %rdx
movq %rdx, 0x38(%rbx)
movq %rcx, 0x20(%rbx)
movq 0xa0(%rbx), %rcx
movq %rcx, 0xb0(%rbx)
movl %eax, 0xb8(%rbx)
movl %eax, 0x84(%rbx)
movq %rax, 0x94(%rbx)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x50(%rbx)
movq %rax, 0x68(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0xe8(%rbx)
movw %bp, 0x10(%rbx)
leaq 0x9520(%rip), %rax      # 0xd278
movq (%rax), %rdi
leaq 0x279d(%rip), %rcx      # 0x64ff
movl %r14d, %esi
movl %ebp, %edx
movq %rbx, %r8
callq 0x2ec6
cmpl $-0x1, %eax
je 0x3d92
leaq 0x9565(%rip), %rax      # 0xd2e0
movq 0x10(%rax), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x10(%rax)
jmp 0x3e96
movslq 0x94cb(%rip), %rax      # 0xd264
cmpl 0x94c1(%rip), %eax      # 0xd260
jge 0x3dba
movq 0x94c0(%rip), %rcx      # 0xd268
leal 0x1(%rax), %edx
movl %edx, 0x94b3(%rip)      # 0xd264
movq %rbx, (%rcx,%rax,8)
jmp 0x3e94
movq 0xd8(%rbx), %rdi
testq %rdi, %rdi
je 0x3dcb
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 0x3e94
movq 0x91e6(%rip), %rax      # 0xcff8
testl %r13d, %r13d
jne 0x3ea8
movq (%rax), %rdi
leaq 0x43bb(%rip), %rsi      # 0x81e0
jmp 0x3ec0
movq %r15, %rdi
movq %rax, %r14
callq 0x2030
movq %r14, %rax
testq %r13, %r13
je 0x3e4b
movq %r13, %rdi
movq %rax, %r14
callq 0x2030
movq %r14, %rax
testq %r12, %r12
je 0x3e5e
movq %r12, %rdi
movq %rax, %r14
callq 0x2030
movq %r14, %rax
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e73
movq %rax, %r14
callq 0x2030
movq %r14, %rax
testq %rax, %rax
je 0x3e80
movq %rax, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x4348(%rip), %rdi      # 0x81d7
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 0x3eb9
leaq 0x435b(%rip), %rsi      # 0x8212
jmp 0x3ec0
leaq 0x4336(%rip), %rsi      # 0x81f6
movl %r14d, %edx
xorl %eax, %eax
callq 0x2260
jmp 0x3cdf