// File: /workspace/github_c_makefile_star2_O1/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 0x96c5(%rip), %eax      # 0xd264
testl %eax, %eax
jle 0x3bbb
movq 0x96be(%rip), %rcx      # 0xd268
decl %eax
movl %eax, 0x96b2(%rip)      # 0xd264
movq (%rcx,%rax,8), %rbx
jmp 0x3cd9
movl %ecx, %r15d
movl $0xf0, %edi
callq 0x22c0
testq %rax, %rax
je 0x3ea2
movq %rax, %rbx
movl %ebp, 0xc(%rsp)
movl %r13d, 0x10(%rsp)
movl %r12d, 0x14(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rbx)
movq %rax, 0x18(%rbx)
movq %rax, 0xa0(%rbx)
movq %rax, 0x78(%rbx)
movq %rax, 0x88(%rbx)
movq %rax, 0xd8(%rbx)
movl %r15d, 0x28(%rbx)
movl $0x800, 0x40(%rbx)      # imm = 0x800
movl $0xc8, 0xa8(%rbx)
movl $0x190, 0x80(%rbx)      # imm = 0x190
movl $0xa, 0x90(%rbx)
movl $0x0, 0xe0(%rbx)
movslq %r15d, %rdi
callq 0x22c0
movq %rax, %r15
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, %r13
movq %rax, 0xa0(%rbx)
movl $0x1900, %edi           # imm = 0x1900
callq 0x22c0
movq %rax, %r12
movq %rax, 0x78(%rbx)
movl $0x230, %edi            # imm = 0x230
callq 0x22c0
movq %r13, %rcx
movq %r12, %rdi
movq %rax, 0x88(%rbx)
testq %r15, %r15
je 0x3e35
testq %rbp, %rbp
je 0x3e35
testq %rcx, %rcx
je 0x3e35
testq %rdi, %rdi
je 0x3e35
testq %rax, %rax
je 0x3e35
leaq 0x9618(%rip), %rax      # 0xd2e0
incl 0x18(%rax)
movl 0x14(%rsp), %r12d
movl 0x10(%rsp), %r13d
movl 0xc(%rsp), %ebp
leaq 0x95a8(%rip), %rax      # 0xd288
cmpl $0x2, 0x18(%rax)
jge 0x3e16
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 0x9515(%rip), %rax      # 0xd278
movq (%rax), %rdi
leaq 0x280a(%rip), %rcx      # 0x6577
movl %r14d, %esi
movl %ebp, %edx
movq %rbx, %r8
callq 0x2eb8
cmpl $-0x1, %eax
je 0x3d9d
leaq 0x955a(%rip), %rax      # 0xd2e0
movq 0x10(%rax), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x10(%rax)
jmp 0x3eb0
movslq 0x94c0(%rip), %rax      # 0xd264
cmpl 0x94b6(%rip), %eax      # 0xd260
jge 0x3dc5
movq 0x94b5(%rip), %rcx      # 0xd268
leal 0x1(%rax), %edx
movl %edx, 0x94a8(%rip)      # 0xd264
movq %rbx, (%rcx,%rax,8)
jmp 0x3eae
movq 0xd8(%rbx), %rdi
testq %rdi, %rdi
je 0x3dd6
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 0x3eae
movq 0x91db(%rip), %rax      # 0xcff8
testl %r13d, %r13d
jne 0x3ec2
movq (%rax), %rdi
leaq 0x43b0(%rip), %rsi      # 0x81e0
jmp 0x3eda
testq %r15, %r15
movq %rax, %r14
movq %rdi, %r12
je 0x3e54
movq %r15, %rdi
movq %rcx, %r15
callq 0x2030
movq %r15, %rcx
movq %r12, %rdi
movq %r14, %rax
testq %rbp, %rbp
je 0x3e6d
movq %rbp, %rdi
movq %rcx, %r15
callq 0x2030
movq %r15, %rcx
movq %r12, %rdi
movq %r14, %rax
testq %rcx, %rcx
je 0x3e80
movq %rcx, %rdi
callq 0x2030
movq %r12, %rdi
movq %r14, %rax
testq %rdi, %rdi
je 0x3e8d
callq 0x2030
movq %r14, %rax
testq %rax, %rax
je 0x3e9a
movq %rax, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
leaq 0x432e(%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 0x3ed3
leaq 0x4341(%rip), %rsi      # 0x8212
jmp 0x3eda
leaq 0x431c(%rip), %rsi      # 0x81f6
movl %r14d, %edx
xorl %eax, %eax
callq 0x2260
jmp 0x3cea