// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x28, %rsp
movl %edi, 0x1c(%rsp)
movl %esi, 0x18(%rsp)
movl %edx, 0x14(%rsp)
movl %ecx, 0x10(%rsp)
movl %r8d, 0xc(%rsp)
cmpl $0x0, 0xcc64(%rip)      # 0x11274
jle 0x4638
movq 0xcc5f(%rip), %rax      # 0x11278
movl 0xcc55(%rip), %ecx      # 0x11274
addl $-0x1, %ecx
movl %ecx, 0xcc4c(%rip)      # 0x11274
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, (%rsp)
jmp 0x4897
movl $0xf0, %edi
callq 0x22e0
movq %rax, (%rsp)
cmpq $0x0, %rax
jne 0x4666
leaq 0x7b38(%rip), %rdi      # 0xc18b
callq 0x23d0
movq $0x0, 0x20(%rsp)
jmp 0x4b28
movq (%rsp), %rax
movq $0x0, 0x30(%rax)
movq (%rsp), %rax
movq $0x0, 0x18(%rax)
movq (%rsp), %rax
movq $0x0, 0xa0(%rax)
movq (%rsp), %rax
movq $0x0, 0x78(%rax)
movq (%rsp), %rax
movq $0x0, 0x88(%rax)
movq (%rsp), %rax
movq $0x0, 0xd8(%rax)
movl 0x10(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, 0x28(%rax)
movq (%rsp), %rax
movl $0x800, 0x40(%rax)      # imm = 0x800
movq (%rsp), %rax
movl $0xc8, 0xa8(%rax)
movq (%rsp), %rax
movl $0x190, 0x80(%rax)      # imm = 0x190
movq (%rsp), %rax
movl $0xa, 0x90(%rax)
movq (%rsp), %rax
movl $0x0, 0xe0(%rax)
movq (%rsp), %rax
movslq 0x28(%rax), %rdi
callq 0x22e0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x18(%rax)
movq (%rsp), %rax
movslq 0x40(%rax), %rdi
callq 0x22e0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x30(%rax)
movq (%rsp), %rax
movslq 0xa8(%rax), %rdi
shlq $0x3, %rdi
callq 0x22e0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0xa0(%rax)
movq (%rsp), %rax
movslq 0x80(%rax), %rdi
shlq $0x4, %rdi
callq 0x22e0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x78(%rax)
movq (%rsp), %rax
movslq 0x90(%rax), %rax
imulq $0x38, %rax, %rdi
callq 0x22e0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x88(%rax)
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x47d9
movq (%rsp), %rax
cmpq $0x0, 0x30(%rax)
je 0x47d9
movq (%rsp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x47d9
movq (%rsp), %rax
cmpq $0x0, 0x78(%rax)
je 0x47d9
movq (%rsp), %rax
cmpq $0x0, 0x88(%rax)
jne 0x4880
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x47f1
movq (%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
cmpq $0x0, 0x30(%rax)
je 0x4809
movq (%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
cmpq $0x0, 0xa0(%rax)
je 0x4827
movq (%rsp), %rax
movq 0xa0(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
cmpq $0x0, 0x78(%rax)
je 0x483f
movq (%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
cmpq $0x0, 0x88(%rax)
je 0x485d
movq (%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x2030
movq (%rsp), %rdi
callq 0x2030
leaq 0x791e(%rip), %rdi      # 0xc18b
callq 0x23d0
movq $0x0, 0x20(%rsp)
jmp 0x4b28
leaq 0xca69(%rip), %rax      # 0x112f0
movl 0x18(%rax), %ecx
addl $0x1, %ecx
leaq 0xca5c(%rip), %rax      # 0x112f0
movl %ecx, 0x18(%rax)
leaq 0xc9fa(%rip), %rax      # 0x11298
cmpl $0x1, 0x18(%rax)
jle 0x490e
cmpl $0x0, 0x18(%rsp)
jne 0x48c9
movq 0xc746(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl 0x1c(%rsp), %edx
leaq 0x78d4(%rip), %rsi      # 0xc194
movb $0x0, %al
callq 0x2280
jmp 0x490c
cmpl $0x0, 0xc(%rsp)
je 0x48ee
movq 0xc721(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl 0x1c(%rsp), %edx
leaq 0x78c5(%rip), %rsi      # 0xc1aa
movb $0x0, %al
callq 0x2280
jmp 0x490a
movq 0xc703(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
movl 0x1c(%rsp), %edx
leaq 0x78c3(%rip), %rsi      # 0xc1c6
movb $0x0, %al
callq 0x2280
jmp 0x490c
jmp 0x490e
movl 0x1c(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, (%rax)
movl 0xc(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, 0xbc(%rax)
movl 0x18(%rsp), %ecx
movq (%rsp), %rax
movl %ecx, 0x4(%rax)
movq (%rsp), %rax
movl $0x0, 0x60(%rax)
movq (%rsp), %rax
movl $0x0, 0x44(%rax)
movq (%rsp), %rax
movl $0x0, 0x2c(%rax)
movq (%rsp), %rax
movq 0x30(%rax), %rcx
movq (%rsp), %rax
movq %rcx, 0x38(%rax)
movq (%rsp), %rax
movq 0x18(%rax), %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
movq (%rsp), %rax
movq $0x0, 0x58(%rax)
movq (%rsp), %rax
movq 0xa0(%rax), %rcx
movq (%rsp), %rax
movq %rcx, 0xb0(%rax)
movq (%rsp), %rax
movl $0x0, 0xb8(%rax)
movq (%rsp), %rax
movl $0x0, 0x84(%rax)
movq (%rsp), %rax
movl $0x0, 0x98(%rax)
movq (%rsp), %rax
movl $0x0, 0x94(%rax)
movq (%rsp), %rax
movl $0x1, 0x48(%rax)
movq (%rsp), %rax
movq $0x0, 0x50(%rax)
movq (%rsp), %rax
movq $0x0, 0x68(%rax)
movq (%rsp), %rax
movl $0xffffffff, 0xe8(%rax) # imm = 0xFFFFFFFF
movq (%rsp), %rax
movl $0x0, 0xec(%rax)
movl 0x14(%rsp), %eax
movw %ax, %cx
movq (%rsp), %rax
movw %cx, 0x10(%rax)
leaq 0xc867(%rip), %rax      # 0x11288
movq (%rax), %rdi
movl 0x1c(%rsp), %esi
movl 0x14(%rsp), %edx
movq (%rsp), %r8
leaq 0x4759(%rip), %rcx      # 0x9190
callq 0x29b0
cmpl $-0x1, %eax
jne 0x4af1
movl 0xc829(%rip), %eax      # 0x11274
cmpl 0xc81f(%rip), %eax      # 0x11270
jge 0x4a78
movq (%rsp), %rdx
movq 0xc81a(%rip), %rax      # 0x11278
movl 0xc810(%rip), %ecx      # 0x11274
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0xc805(%rip)      # 0x11274
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x4ae6
movq (%rsp), %rax
cmpq $0x0, 0xd8(%rax)
je 0x4a96
movq (%rsp), %rax
movq 0xd8(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
movq 0x88(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
movq 0x30(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
movq 0xa0(%rax), %rdi
callq 0x2030
movq (%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x2030
movq (%rsp), %rdi
callq 0x2030
movq $0x0, 0x20(%rsp)
jmp 0x4b28
leaq 0xc7f8(%rip), %rax      # 0x112f0
movl 0x10(%rax), %ecx
addl $0x1, %ecx
leaq 0xc7eb(%rip), %rax      # 0x112f0
movl %ecx, 0x10(%rax)
leaq 0xc7e1(%rip), %rax      # 0x112f0
movl 0x14(%rax), %ecx
addl $0x1, %ecx
leaq 0xc7d4(%rip), %rax      # 0x112f0
movl %ecx, 0x14(%rax)
movq (%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)