// File: /workspace/github_c_makefile_star2_O1/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 $0xe8, %rsp
movq 0x987e(%rip), %rax      # 0xd380
movslq %edi, %rcx
shlq $0x5, %rcx
movq 0x10(%rax,%rcx), %r13
movl 0x40(%r13), %eax
cmpl 0x48(%r13), %eax
jge 0x40e1
movl %edi, %ebx
xorl %eax, %eax
cmpl $0x2, 0x9879(%rip)      # 0xd3a0
setge %al
leal (%rax,%rax,8), %ebp
movzwl %bx, %eax
movl %eax, 0x4(%rsp)
movl %edi, 0x8(%rsp)
movq %r13, 0x18(%rsp)
decl %ebp
je 0x40e1
movl $0x80, 0x14(%rsp)
movl %ebx, %edi
leaq 0x68(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x2360
cmpl $-0x1, %eax
je 0x3b7b
movl %eax, %r12d
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20e0
jmp 0x3b81
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $-0x1, %r12d
je 0x40e1
movq 0x97c6(%rip), %r15      # 0xd358
testq %r15, %r15
je 0x3ba3
movq (%r15), %rax
movq %rax, 0x97b7(%rip)      # 0xd358
jmp 0x3bb0
movl $0x178, %edi            # imm = 0x178
callq 0x2290
movq %rax, %r15
testq %r15, %r15
je 0x4018
movl %ebp, 0xc(%rsp)
movq 0x97ac(%rip), %rbp      # 0xd370
testq %rbp, %rbp
je 0x3bd6
movq (%rbp), %rax
movq %rax, 0x979c(%rip)      # 0xd370
jmp 0x3be3
movl $0x48, %edi
callq 0x2290
movq %rax, %rbp
testq %rbp, %rbp
je 0x4054
leaq 0x70(%r15), %rdi
movl $0x10, %ecx
leaq 0x68(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
cmpl 0x97ac(%rip), %r12d     # 0xd3b0
jge 0x408d
testb $0x4, 0x4e(%r13)
je 0x3c30
movl %r12d, %edi
movl $0x1, %esi
movl $0x9, %edx
leaq 0x54a7(%rip), %rcx      # 0x90cc
movl $0x4, %r8d
callq 0x20d0
cmpl $0x0, 0x96fd(%rip)      # 0xd334
jle 0x3c58
movl %r12d, %edi
movl $0x1, %esi
movl $0x7, %edx
leaq 0x96e7(%rip), %rcx      # 0xd334
movl $0x4, %r8d
callq 0x20d0
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
xorl %eax, %eax
movq %rax, 0x10(%rbp)
leaq 0x95d4(%rip), %rcx      # 0xd240
movq %rcx, 0x18(%rbp)
movl %eax, 0x20(%rbp)
leaq 0x47b(%rip), %rcx       # 0x40f5
movq %rcx, 0x38(%rbp)
movq %r15, 0x40(%rbp)
movq %rbp, (%r15)
movq %r13, 0x40(%r15)
movw $0x0, 0x38(%r15)
movups %xmm0, 0x4c(%r15)
movups %xmm0, 0x5c(%r15)
movl %eax, 0x6c(%r15)
movl %r12d, 0x48(%r15)
movq %rax, 0xf0(%r15)
movq %rax, 0x120(%r15)
movl %eax, 0x11c(%r15)
movq $-0x1, 0x128(%r15)
pcmpeqd %xmm1, %xmm1
movdqu %xmm1, 0x148(%r15)
movl $0xffffffff, 0x158(%r15) # imm = 0xFFFFFFFF
movq %rax, 0x130(%r15)
movups %xmm0, 0x15c(%r15)
movdqu %xmm1, 0x138(%r15)
movl %eax, 0x170(%r15)
movups 0x9700(%rip), %xmm0     # 0xd400
movups %xmm0, 0xf8(%r15)
movdqu %xmm1, 0x108(%r15)
movl 0x29711(%rip), %eax     # 0x2d428
movl %eax, 0x118(%r15)
incl 0x44(%r13)
movl 0x96a0(%rip), %eax      # 0xd3c8
testb $0x1, %al
sete %cl
andl $0x50, %eax
cmpl $0x10, %eax
sete %al
orb %cl, %al
jne 0x3df2
movl $0x10, 0x10(%rsp)
cmpw $0x2, 0x68(%rsp)
je 0x3d60
movl %r12d, %edi
leaq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x2370
cmpw $0x2, 0x70(%r15)
movzwl %r12w, %r14d
jne 0x3d81
leaq 0x74(%r15), %rsi
movl $0x2, %edi
leaq 0x20(%rsp), %rdx
movl $0x10, %ecx
jmp 0x3d94
leaq 0x78(%r15), %rsi
movl $0xa, %edi
leaq 0x20(%rsp), %rdx
movl $0x2e, %ecx
callq 0x2040
movl 0x118(%r15), %edx
movq 0x38(%r13), %rcx
movzwl 0x72(%r15), %eax
rolw $0x8, %ax
movzwl %ax, %r10d
leaq 0x29678(%rip), %rbx     # 0x2d430
movq %rbx, %rdi
leaq 0x64c2(%rip), %rsi      # 0xa284
movl 0x4(%rsp), %r8d
movl %r14d, %r9d
xorl %eax, %eax
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
callq 0x23a0
addq $0x10, %rsp
movslq %eax, %rdx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x20f0
movl 0x8(%rsp), %ebx
movq 0x9567(%rip), %rax      # 0xd360
testq %rax, %rax
je 0x3e0a
movq (%rax), %rcx
movq %rcx, 0x9558(%rip)      # 0xd360
jmp 0x3e14
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x58(%r15)
testq %rax, %rax
je 0x40b6
leaq 0x38(%rax), %rcx
movq %rcx, 0x30(%rax)
movl $0x0, (%rax)
movq $0x0, 0x28(%rax)
movq %rcx, 0x10(%rax)
movq %rcx, 0x20(%rax)
movq %rcx, 0x8(%rax)
movq %rcx, 0x18(%rax)
movq 0x9512(%rip), %rax      # 0xd360
testq %rax, %rax
je 0x3e5f
movq (%rax), %rcx
movq %rcx, 0x9503(%rip)      # 0xd360
jmp 0x3e69
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x60(%r15)
testq %rax, %rax
je 0x409d
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 0x94dd(%rip), %rax      # 0xd380
movslq %r12d, %rcx
shlq $0x5, %rcx
leaq -0xb1d(%rip), %rdx      # 0x3394
movq %rdx, (%rax,%rcx)
movq 0x94c4(%rip), %rax      # 0xd380
leaq -0x88b(%rip), %rdx      # 0x3638
movq %rdx, 0x8(%rax,%rcx)
movq 0x94b1(%rip), %rax      # 0xd380
movq %rbp, 0x10(%rax,%rcx)
movq 0x94a5(%rip), %rax      # 0xd380
movl $0x3, 0x18(%rax,%rcx)
movl %r12d, %edi
callq 0x3394
testl %eax, %eax
jns 0x3f0c
movl $0x1, %eax
movl %r12d, %ecx
shll %cl, %eax
movq 0x94f8(%rip), %rcx      # 0xd3f8
movl %r12d, %edx
sarl $0x5, %edx
movslq %edx, %rdx
orl %eax, (%rcx,%rdx,4)
movl %r12d, %edi
movl $0x6, %esi
movl $0x1, %edx
leaq 0x51ac(%rip), %rcx      # 0x90cc
movl $0x4, %r8d
callq 0x20d0
cmpl %r12d, 0x2a51a(%rip)    # 0x2e44c
jg 0x3f3f
leal 0x1(%r12), %eax
movl %eax, 0x2a50d(%rip)     # 0x2e44c
leaq 0x8(%r15), %r13
leaq 0x18(%r15), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x8(%r15)
movq 0x40(%r15), %rax
movl 0x30(%rax), %esi
testl %esi, %esi
je 0x3fac
movq 0x948d(%rip), %rax      # 0xd3f8
movl %r12d, %ecx
sarl $0x5, %ecx
movslq %ecx, %rbx
movl (%rax,%rbx,4), %eax
andl $0x1f, %r12d
btl %r12d, %eax
jae 0x3f89
movq %r13, %rdi
callq 0x3596
movq 0x29490(%rip), %rax     # 0x2d420
movl (%rax,%rbx,4), %eax
btl %r12d, %eax
movl 0x8(%rsp), %ebx
jae 0x3fac
movq 0x40(%r15), %rax
movl 0x30(%rax), %esi
movq %r14, %rdi
callq 0x3596
movq %r13, %rdi
movq %r14, %rsi
callq 0x3331
testl %eax, %eax
cmovleq %r13, %r14
movups (%r14), %xmm0
movups %xmm0, 0x28(%rbp)
movq %rbp, %rdi
callq 0x31e4
cmpl $0x1, 0x20(%rbp)
je 0x3fec
movq 0x93ae(%rip), %rax      # 0xd388
movq %rax, 0x10(%rbp)
movl $0x1, 0x20(%rbp)
movq %rbp, 0x939c(%rip)      # 0xd388
movq 0x18(%rsp), %r13
incl 0x40(%r13)
incl 0x2a435(%rip)           # 0x2e430
incl 0x29427(%rip)           # 0x2d428
movl 0x40(%r13), %eax
cmpl 0x48(%r13), %eax
movl 0xc(%rsp), %ebp
jl 0x3b3d
jmp 0x40e1
leaq 0x6201(%rip), %rdi      # 0xa220
xorl %eax, %eax
callq 0x2a00
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl %ebx, %ecx
roll %cl, %eax
movq 0x93c2(%rip), %rcx      # 0xd3f8
sarl $0x5, %ebx
movslq %ebx, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r13)
movl %r12d, %edi
callq 0x21b0
jmp 0x40e1
leaq 0x61c5(%rip), %rdi      # 0xa220
xorl %eax, %eax
callq 0x2a00
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl %ebx, %ecx
roll %cl, %eax
movq 0x9386(%rip), %rcx      # 0xd3f8
sarl $0x5, %ebx
movslq %ebx, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r13)
movl %r12d, %edi
callq 0x21b0
jmp 0x40d0
leaq 0x61ae(%rip), %rdi      # 0xa242
xorl %eax, %eax
callq 0x2a00
jmp 0x40b6
movq 0x92bc(%rip), %rax      # 0xd360
movq 0x58(%r15), %rcx
movq %rax, (%rcx)
movq 0x58(%r15), %rax
movq %rax, 0x92aa(%rip)      # 0xd360
movl %r12d, %edi
callq 0x21b0
movq 0x92ab(%rip), %rax      # 0xd370
movq %rax, (%rbp)
movq %rbp, 0x92a0(%rip)      # 0xd370
movq 0x9281(%rip), %rax      # 0xd358
movq %rax, (%r15)
movq %r15, 0x9277(%rip)      # 0xd358
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq