// File: /workspace/github_c_makefile_star2_O3/wtarreau[P]httpterm/httpterm.c
// Function: event_accept
int event_accept(int fd) {
    struct proxy *p = (struct proxy *)fdtab[fd].owner;
    struct session *s;
    struct task *t;
    int cfd = -1;
    int max_accept;

#ifdef ENABLE_ACCEPT4
    static int use_accept = 0;
#else
    static int use_accept = 1;
#endif

    if (global.nbproc > 1)
	    max_accept = 8; /* let other processes catch some connections too */
    else
	    max_accept = -1;

    while (p->nbconn < p->maxconn && max_accept--) {
	struct sockaddr_storage addr;
	socklen_t laddr = sizeof(addr);

#ifdef ENABLE_ACCEPT4
	if (!use_accept) {
	    cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK);
	    if (cfd == -1 && errno == ENOSYS)
		use_accept = 1;
	}
#endif
	if (use_accept) {
	    cfd = accept(fd, (struct sockaddr *)&addr, &laddr);
	    if (cfd != -1)
		fcntl(cfd, F_SETFL, O_NONBLOCK);
	}

	if (cfd == -1) {
	    switch (errno) {
	    case EAGAIN:
	    case EINTR:
	    case ECONNABORTED:
		return 0;	    /* nothing more to accept */
	    case ENFILE:
		return 0;
	    case EMFILE:
		return 0;
	    case ENOBUFS:
	    case ENOMEM:
		return 0;
	    default:
		return 0;
	    }
	}

	if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */
	    Alert("out of memory in event_accept().\n");
	    my_fd_clr(fd, StaticReadEvent);
	    p->state = PR_STIDLE;
	    close(cfd);
	    return 0;
	}

	if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */
	    Alert("out of memory in event_accept().\n");
	    my_fd_clr(fd, StaticReadEvent);
	    p->state = PR_STIDLE;
	    close(cfd);
	    pool_free(session, s);
	    return 0;
	}

	s->cli_addr = addr;
	if (cfd >= global.maxsock) {
	    Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
	    close(cfd);
	    pool_free(task, t);
	    pool_free(session, s);
	    return 0;
	}

	if (p->options & PR_O_TCP_CLI_KA)
	    setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));

	if (cfg_sndbuf > 0)
	    setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &cfg_sndbuf, sizeof(cfg_sndbuf));

	t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
	t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
	t->state = TASK_IDLE;
	t->process = process_session;
	t->context = s;

	s->task = t;
	s->proxy = p;
	s->cli_state = CL_STHEADERS;
	s->sock_st = 0;
	s->ka = 0;
	s->req = s->rep = NULL; /* will be allocated later */
        s->to_write = 0;

	s->res_cr = s->res_cw  = RES_SILENT;
	s->cli_fd = cfd;
	s->srv = NULL;
	s->uri = NULL;
	s->etag = 0;
	s->req_code = s->req_size = s->req_cache = s->req_time = -1;
	s->ka_time = s->pause_time = -1;
	s->req_body = 0;
	s->req_chunked = 0;
	s->req_nosplice = 0;
	s->req_random = 0;
	s->req_pieces = 0;
	s->req_bodylen = -1; // default: advertise the correct size
	s->req_maxbody = -1;
	s->req_verbose = 0;

	s->logs.tv_accept = now;
	s->logs.t_request = -1;
	s->logs.t_queue = -1;

	s->uniq_id = totalconn;
	p->cum_conn++;

	if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
	    struct sockaddr_in sockname;
	    socklen_t namelen = sizeof(sockname);
	    int len;
	    if (addr.ss_family != AF_INET)
		getsockname(cfd, (struct sockaddr *)&sockname, &namelen);

	    if (s->cli_addr.ss_family == AF_INET) {
		char pn[INET_ADDRSTRLEN];
		inet_ntop(AF_INET,
			  (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
			  pn, sizeof(pn));

		len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
			      s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
			      pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
	    }
	    else {
		char pn[INET6_ADDRSTRLEN];
		inet_ntop(AF_INET6,
			  (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
			  pn, sizeof(pn));

		len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
			      s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
			      pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
	    }

	    (void)!write(1, trash, len);
	}

	if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */
	    close(cfd); /* nothing can be done for this fd without memory */
	    pool_free(task, t);
	    pool_free(session, s);
	    return 0;
	}

	s->req->data = s->req->data_buf;
	s->req->l = 0;
	s->req->total = 0;
	s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data;	/* r and w will be reset further */

	if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */
	    pool_free(buffer, s->req);
	    close(cfd); /* nothing can be done for this fd without memory */
	    pool_free(task, t);
	    pool_free(session, s);
	    return 0;
	}
	s->rep->l = 0;
	s->rep->total = 0;
	s->rep->data = s->rep->data_buf;
	s->rep->h = s->rep->r = s->rep->lr = s->rep->w = s->rep->data;

	fdtab[cfd].read  = &event_cli_read;
	fdtab[cfd].write = &event_cli_write;
	fdtab[cfd].owner = t;
	fdtab[cfd].state = FD_STREADY;

	if (event_cli_read(cfd) < 0)
	    my_fd_set(cfd, StaticReadEvent);

	setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one));

	fd_insert(cfd);

	tv_eternity(&s->cnexpire);
	tv_eternity(&s->crexpire);
	tv_eternity(&s->cwexpire);

	if (s->proxy->clitimeout) {
	    if (my_fd_isset(cfd, StaticReadEvent))
		tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout);
	    if (my_fd_isset(cfd, StaticWriteEvent))
		tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout);
	}

	tv_min(&t->expire, &s->crexpire, &s->cwexpire);

	task_queue(t);
	task_wakeup(&rq, t);

	p->nbconn++;
	actconn++;
	totalconn++;

	// fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
    } /* end of while (p->nbconn < p->maxconn) */
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x98b5(%rip), %rax      # 0xd380
movslq %edi, %rcx
shlq $0x5, %rcx
movq 0x10(%rax,%rcx), %r15
movl 0x40(%r15), %eax
cmpl 0x48(%r15), %eax
jge 0x40e9
movl %edi, %ebx
xorl %eax, %eax
cmpl $0x2, 0x98b0(%rip)      # 0xd3a0
setge %al
leal (%rax,%rax,8), %r13d
movzwl %bx, %eax
movl %eax, 0x14(%rsp)
leaq 0x78(%rsp), %r14
movl %edi, 0x18(%rsp)
movq %r15, 0x28(%rsp)
decl %r13d
je 0x40e9
movl $0x80, 0x24(%rsp)
movl %ebx, %edi
movq %r14, %rsi
leaq 0x24(%rsp), %rdx
callq 0x2360
cmpl $-0x1, %eax
je 0x40e9
movq %rax, 0x8(%rsp)
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20e0
movq 0x9804(%rip), %r12      # 0xd358
testq %r12, %r12
je 0x3b66
movq (%r12), %rax
movq %rax, 0x97f4(%rip)      # 0xd358
jmp 0x3b7c
movl $0x178, %edi            # imm = 0x178
callq 0x2290
movq %rax, %r12
testq %rax, %rax
je 0x4032
movq 0x97ed(%rip), %rbp      # 0xd370
testq %rbp, %rbp
je 0x3b95
movq (%rbp), %rax
movq %rax, 0x97dd(%rip)      # 0xd370
jmp 0x3bab
movl $0x48, %edi
callq 0x2290
movq %rax, %rbp
testq %rax, %rax
je 0x406d
leaq 0x70(%r12), %rdi
movl $0x10, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x8(%rsp), %r14
cmpl 0x97e9(%rip), %r14d     # 0xd3b0
jge 0x401c
testb $0x4, 0x4e(%r15)
je 0x3bf3
movl %r14d, %edi
movl $0x1, %esi
movl $0x9, %edx
leaq 0x55d4(%rip), %rcx      # 0x91bc
movl $0x4, %r8d
callq 0x20d0
cmpl $0x0, 0x973a(%rip)      # 0xd334
jle 0x3c1b
movl %r14d, %edi
movl $0x1, %esi
movl $0x7, %edx
leaq 0x9724(%rip), %rcx      # 0xd334
movl $0x4, %r8d
callq 0x20d0
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
xorl %eax, %eax
movq %rax, 0x10(%rbp)
leaq 0x9611(%rip), %rcx      # 0xd240
movq %rcx, 0x18(%rbp)
movl %eax, 0x20(%rbp)
leaq 0x4c0(%rip), %rcx       # 0x40fd
movq %rcx, 0x38(%rbp)
movq %r12, 0x40(%rbp)
movq %rbp, (%r12)
movq %r15, 0x40(%r12)
movw $0x0, 0x38(%r12)
movups %xmm0, 0x4c(%r12)
movups %xmm0, 0x5c(%r12)
movl %eax, 0x6c(%r12)
movl %r14d, 0x48(%r12)
movq %rax, 0xf0(%r12)
movq %rax, 0x120(%r12)
movl %eax, 0x11c(%r12)
movq $-0x1, 0x128(%r12)
pcmpeqd %xmm1, %xmm1
movdqu %xmm1, 0x148(%r12)
movl $0xffffffff, 0x158(%r12) # imm = 0xFFFFFFFF
movq %rax, 0x130(%r12)
movups %xmm0, 0x15c(%r12)
movdqu %xmm1, 0x138(%r12)
movl %eax, 0x170(%r12)
movups 0x972c(%rip), %xmm0     # 0xd400
movups %xmm0, 0xf8(%r12)
movdqu %xmm1, 0x108(%r12)
movl 0x2973b(%rip), %eax     # 0x2d428
movl %eax, 0x118(%r12)
incl 0x44(%r15)
movl 0x96c9(%rip), %eax      # 0xd3c8
testb $0x1, %al
sete %cl
andl $0x50, %eax
cmpl $0x10, %eax
sete %al
orb %cl, %al
jne 0x3dce
movl $0x10, 0x20(%rsp)
cmpw $0x2, 0x78(%rsp)
je 0x3d37
movl %r14d, %edi
leaq 0x68(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x2370
cmpw $0x2, 0x70(%r12)
movzwl %r14w, %r14d
jne 0x3d5a
leaq 0x74(%r12), %rsi
movl $0x2, %edi
leaq 0x30(%rsp), %rdx
movl $0x10, %ecx
jmp 0x3d6e
leaq 0x78(%r12), %rsi
movl $0xa, %edi
leaq 0x30(%rsp), %rdx
movl $0x2e, %ecx
callq 0x2040
movl 0x118(%r12), %edx
movq 0x38(%r15), %rcx
movzwl 0x72(%r12), %eax
rolw $0x8, %ax
movzwl %ax, %r10d
leaq 0x2969c(%rip), %rbx     # 0x2d430
movq %rbx, %rdi
leaq 0x65d6(%rip), %rsi      # 0xa374
movl 0x14(%rsp), %r8d
movl %r14d, %r9d
xorl %eax, %eax
pushq %r10
leaq 0x38(%rsp), %r10
pushq %r10
callq 0x23a0
addq $0x10, %rsp
movslq %eax, %rdx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x20f0
movl 0x18(%rsp), %ebx
movq 0x958b(%rip), %r14      # 0xd360
testq %r14, %r14
je 0x3deb
movq (%r14), %rax
movq %rax, 0x957c(%rip)      # 0xd360
movq %r14, 0x58(%r12)
jmp 0x3e08
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x58(%r12)
testq %rax, %rax
je 0x40bb
movq %rax, %r14
xorl %eax, %eax
leaq 0x38(%r14), %rcx
movq %rcx, 0x30(%r14)
movl $0x0, (%r14)
movq $0x0, 0x28(%r14)
movq %rcx, 0x10(%r14)
movq %rcx, 0x20(%r14)
movq %rcx, 0x8(%r14)
movq %rcx, 0x18(%r14)
testq %rax, %rax
je 0x3e45
movq (%rax), %rcx
movq %rcx, 0x9522(%rip)      # 0xd360
movq %rax, 0x60(%r12)
jmp 0x3e5d
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x60(%r12)
testq %rax, %rax
je 0x40a8
movl $0x0, (%rax)
movq $0x0, 0x28(%rax)
leaq 0x38(%rax), %rcx
movq %rcx, 0x30(%rax)
movq %rcx, 0x10(%rax)
movq %rcx, 0x20(%rax)
movq %rcx, 0x8(%rax)
movq %rcx, 0x18(%rax)
movq 0x94f6(%rip), %rax      # 0xd380
movq 0x8(%rsp), %r14
movslq %r14d, %rcx
shlq $0x5, %rcx
leaq -0xb49(%rip), %rdx      # 0x3354
movq %rdx, (%rax,%rcx)
movq 0x94d8(%rip), %rax      # 0xd380
leaq -0x8c7(%rip), %rdx      # 0x35e8
movq %rdx, 0x8(%rax,%rcx)
movq 0x94c5(%rip), %rax      # 0xd380
movq %rbp, 0x10(%rax,%rcx)
movq 0x94b9(%rip), %rax      # 0xd380
movl $0x3, 0x18(%rax,%rcx)
movl %r14d, %edi
callq 0x3354
testl %eax, %eax
jns 0x3ef8
movl $0x1, %eax
movl %r14d, %ecx
shll %cl, %eax
movq 0x950c(%rip), %rcx      # 0xd3f8
movl %r14d, %edx
sarl $0x5, %edx
movslq %edx, %rdx
orl %eax, (%rcx,%rdx,4)
movl %r13d, 0x1c(%rsp)
movl %r14d, %edi
movl $0x6, %esi
movl $0x1, %edx
leaq 0x52ab(%rip), %rcx      # 0x91bc
movl $0x4, %r8d
callq 0x20d0
cmpl %r14d, 0x2a529(%rip)    # 0x2e44c
jg 0x3f2f
leal 0x1(%r14), %eax
movl %eax, 0x2a51d(%rip)     # 0x2e44c
leaq 0x8(%r12), %r13
leaq 0x18(%r12), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r12)
movups %xmm0, 0x18(%r12)
movups %xmm0, 0x8(%r12)
movq 0x40(%r12), %rax
movl 0x30(%rax), %esi
testl %esi, %esi
je 0x3fad
movq 0x9497(%rip), %rax      # 0xd3f8
movq 0x8(%rsp), %rcx
movl %ecx, %edx
sarl $0x5, %edx
movslq %edx, %rbx
movl (%rax,%rbx,4), %eax
movl $0x1, %r15d
shll %cl, %r15d
btl %ecx, %eax
jae 0x3f87
movq %r13, %rdi
callq 0x3545
movq 0x29492(%rip), %rax     # 0x2d420
testl %r15d, (%rax,%rbx,4)
movl 0x18(%rsp), %ebx
movq 0x28(%rsp), %r15
je 0x3fad
movq 0x40(%r12), %rax
movl 0x30(%rax), %esi
movq %r14, %rdi
callq 0x3545
movq %r13, %rdi
movq %r14, %rsi
callq 0x32f1
testl %eax, %eax
cmovleq %r13, %r14
movups (%r14), %xmm0
movups %xmm0, 0x28(%rbp)
movq %rbp, %rdi
callq 0x31a6
cmpl $0x1, 0x20(%rbp)
je 0x3fed
movq 0x93ad(%rip), %rax      # 0xd388
movq %rax, 0x10(%rbp)
movl $0x1, 0x20(%rbp)
movq %rbp, 0x939b(%rip)      # 0xd388
movl 0x40(%r15), %eax
incl %eax
movl %eax, 0x40(%r15)
incl 0x2a433(%rip)           # 0x2e430
incl 0x29425(%rip)           # 0x2d428
cmpl 0x48(%r15), %eax
movl 0x1c(%rsp), %r13d
leaq 0x78(%rsp), %r14
jl 0x3b0c
jmp 0x40e9
leaq 0x630f(%rip), %rdi      # 0xa332
xorl %eax, %eax
callq 0x29fa
movl %r14d, %edi
jmp 0x40c0
leaq 0x62d7(%rip), %rdi      # 0xa310
xorl %eax, %eax
callq 0x29fa
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl %ebx, %ecx
roll %cl, %eax
movq 0x93a8(%rip), %rcx      # 0xd3f8
sarl $0x5, %ebx
movslq %ebx, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r15)
movq 0x8(%rsp), %rdi
callq 0x21b0
jmp 0x40e9
leaq 0x629c(%rip), %rdi      # 0xa310
xorl %eax, %eax
callq 0x29fa
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl %ebx, %ecx
roll %cl, %eax
movq 0x936d(%rip), %rcx      # 0xd3f8
sarl $0x5, %ebx
movslq %ebx, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r15)
movq 0x8(%rsp), %rdi
callq 0x21b0
jmp 0x40d7
movq $0x0, (%r14)
movq 0x58(%r12), %rax
movq %rax, 0x92a5(%rip)      # 0xd360
movq 0x8(%rsp), %rdi
callq 0x21b0
movq 0x92a4(%rip), %rax      # 0xd370
movq %rax, (%rbp)
movq %rbp, 0x9299(%rip)      # 0xd370
movq 0x927a(%rip), %rax      # 0xd358
movq %rax, (%r12)
movq %r12, 0x926f(%rip)      # 0xd358
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq