// File: /workspace/github_c_makefile_star2_O2/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
movl %edi, %r13d
movq 0xa9f5(%rip), %rax      # 0xe380
movslq %edi, %rcx
movzwl %cx, %edx
movl %edx, 0x4(%rsp)
shlq $0x5, %rcx
movq 0x10(%rax,%rcx), %r12
xorl %eax, %eax
cmpl $0x2, 0xa9f9(%rip)      # 0xe3a0
setge %al
leal (%rax,%rax,8), %ebx
decl %ebx
movl 0x40(%r12), %eax
movl %edi, 0x8(%rsp)
movq %r12, 0x18(%rsp)
cmpl 0x48(%r12), %eax
jge 0x3f1f
subl $0x1, %ebx
jb 0x3f1f
movl $0x80, 0x14(%rsp)
movl %r13d, %edi
leaq 0x68(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x2360
cmpl $-0x1, %eax
je 0x3f1f
movl %eax, %r15d
movl %eax, %edi
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20e0
movq 0xa949(%rip), %rbp      # 0xe358
testq %rbp, %rbp
je 0x3a21
movq (%rbp), %rax
movq %rax, 0xa939(%rip)      # 0xe358
jmp 0x3a37
movl $0x178, %edi            # imm = 0x178
callq 0x2290
movq %rax, %rbp
testq %rax, %rax
je 0x3e71
movl %ebx, 0xc(%rsp)
movq 0xa92e(%rip), %r14      # 0xe370
testq %r14, %r14
je 0x3a53
movq (%r14), %rax
movq %rax, 0xa91f(%rip)      # 0xe370
jmp 0x3a67
pushq $0x48
popq %rdi
callq 0x2290
movq %rax, %r14
testq %rax, %rax
je 0x3eab
leaq 0x70(%rbp), %rdi
pushq $0x10
popq %rcx
leaq 0x68(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
cmpl 0xa933(%rip), %r15d     # 0xe3b0
jge 0x3e5e
testb $0x4, 0x4e(%r12)
je 0x3aa4
movl %r15d, %edi
pushq $0x1
popq %rsi
pushq $0x9
popq %rdx
leaq 0x5631(%rip), %rcx      # 0x90cc
pushq $0x4
popq %r8
callq 0x20d0
cmpl $0x0, 0xa889(%rip)      # 0xe334
jle 0x3ac6
movl %r15d, %edi
pushq $0x1
popq %rsi
pushq $0x7
popq %rdx
leaq 0xa877(%rip), %rcx      # 0xe334
pushq $0x4
popq %r8
callq 0x20d0
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
andq $0x0, 0x10(%r14)
leaq 0xa767(%rip), %rax      # 0xe240
movq %rax, 0x18(%r14)
andl $0x0, 0x20(%r14)
leaq 0x44a(%rip), %rax       # 0x3f33
movq %rax, 0x38(%r14)
movq %rbp, 0x40(%r14)
movq %r14, (%rbp)
movq %r12, 0x40(%rbp)
andw $0x0, 0x38(%rbp)
movups %xmm0, 0x4c(%rbp)
movups %xmm0, 0x5c(%rbp)
andl $0x0, 0x6c(%rbp)
movl %r15d, 0x48(%rbp)
andq $0x0, 0xf0(%rbp)
andq $0x0, 0x120(%rbp)
andl $0x0, 0x11c(%rbp)
orq $-0x1, 0x128(%rbp)
pcmpeqd %xmm1, %xmm1
movdqu %xmm1, 0x148(%rbp)
orl $-0x1, 0x158(%rbp)
andq $0x0, 0x130(%rbp)
movups %xmm0, 0x15c(%rbp)
movdqu %xmm1, 0x138(%rbp)
andl $0x0, 0x170(%rbp)
movups 0xa89b(%rip), %xmm0     # 0xe400
movups %xmm0, 0xf8(%rbp)
movdqu %xmm1, 0x108(%rbp)
movl 0x2a8ae(%rip), %eax     # 0x2e428
movl %eax, 0x118(%rbp)
incl 0x44(%r12)
movl 0xa83d(%rip), %eax      # 0xe3c8
testb $0x1, %al
sete %cl
andl $0x50, %eax
cmpl $0x10, %eax
sete %al
orb %cl, %al
jne 0x3c49
movl $0x10, 0x10(%rsp)
cmpw $0x2, 0x68(%rsp)
je 0x3bc3
movl %r15d, %edi
leaq 0x58(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x2370
cmpw $0x2, 0x70(%rbp)
movzwl %r15w, %ebx
jne 0x3bde
leaq 0x74(%rbp), %rsi
pushq $0x2
popq %rdi
leaq 0x20(%rsp), %rdx
pushq $0x10
jmp 0x3bec
leaq 0x78(%rbp), %rsi
pushq $0xa
popq %rdi
leaq 0x20(%rsp), %rdx
pushq $0x2e
popq %rcx
callq 0x2040
movl 0x118(%rbp), %edx
movq 0x38(%r12), %rcx
movzwl 0x72(%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %r10d
leaq 0x2a820(%rip), %r13     # 0x2e430
movq %r13, %rdi
leaq 0x666a(%rip), %rsi      # 0xa284
movl 0x4(%rsp), %r8d
movl %ebx, %r9d
xorl %eax, %eax
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
callq 0x23a0
addq $0x10, %rsp
movslq %eax, %rdx
pushq $0x1
popq %rdi
movq %r13, %rsi
callq 0x20f0
movl 0x8(%rsp), %r13d
movq 0xa710(%rip), %rbx      # 0xe360
testq %rbx, %rbx
je 0x3c65
movq (%rbx), %rax
movq %rax, 0xa701(%rip)      # 0xe360
movq %rbx, 0x58(%rbp)
jmp 0x3c81
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x58(%rbp)
testq %rax, %rax
je 0x3ef4
movq %rax, %rbx
xorl %eax, %eax
leaq 0x38(%rbx), %rcx
movq %rcx, 0x30(%rbx)
andl $0x0, (%rbx)
andq $0x0, 0x28(%rbx)
movq %rcx, 0x10(%rbx)
movq %rcx, 0x20(%rbx)
movq %rcx, 0x8(%rbx)
movq %rcx, 0x18(%rbx)
testq %rax, %rax
je 0x3cb6
movq (%rax), %rcx
movq %rcx, 0xa6b0(%rip)      # 0xe360
movq %rax, 0x60(%rbp)
jmp 0x3ccd
movl $0x1038, %edi           # imm = 0x1038
callq 0x2290
movq %rax, 0x60(%rbp)
testq %rax, %rax
je 0x3ee5
andl $0x0, (%rax)
andq $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 0xa68c(%rip), %rax      # 0xe380
movslq %r15d, %rcx
shlq $0x5, %rcx
leaq -0xa43(%rip), %rdx      # 0x32bf
movq %rdx, (%rax,%rcx)
movq 0xa673(%rip), %rax      # 0xe380
leaq -0x81d(%rip), %rdx      # 0x34f7
movq %rdx, 0x8(%rax,%rcx)
movq 0xa660(%rip), %rax      # 0xe380
movq %r14, 0x10(%rax,%rcx)
movq 0xa654(%rip), %rax      # 0xe380
movl $0x3, 0x18(%rax,%rcx)
movl %r15d, %edi
callq 0x32bf
testl %eax, %eax
jns 0x3d5b
pushq $0x1
popq %rax
movl %r15d, %ecx
shll %cl, %eax
movq 0xa6a9(%rip), %rcx      # 0xe3f8
movl %r15d, %edx
sarl $0x5, %edx
movslq %edx, %rdx
orl %eax, (%rcx,%rdx,4)
movl %r15d, %edi
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
leaq 0x5361(%rip), %rcx      # 0x90cc
pushq $0x4
popq %r8
callq 0x20d0
cmpl %r15d, 0x2b6d1(%rip)    # 0x2f44c
jg 0x3d87
leal 0x1(%r15), %eax
movl %eax, 0x2b6c5(%rip)     # 0x2f44c
leaq 0x8(%rbp), %rdx
leaq 0x18(%rbp), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbp)
movups %xmm0, 0x18(%rbp)
movups %xmm0, 0x8(%rbp)
movq 0x40(%rbp), %rax
movl 0x30(%rax), %esi
testl %esi, %esi
je 0x3e00
movq 0xa648(%rip), %rax      # 0xe3f8
movl %r15d, %ecx
sarl $0x5, %ecx
movslq %ecx, %r13
movl (%rax,%r13,4), %eax
pushq $0x1
popq %rbx
movl %r15d, %ecx
shll %cl, %ebx
btl %r15d, %eax
jae 0x3dd9
movq %rdx, %rdi
movq %rdx, %r15
callq 0x34ab
movq %r15, %rdx
movq 0x2a640(%rip), %rax     # 0x2e420
testl %ebx, (%rax,%r13,4)
movl 0x8(%rsp), %r13d
je 0x3e00
movq 0x40(%rbp), %rax
movl 0x30(%rax), %esi
movq %r12, %rdi
movq %rdx, %rbx
callq 0x34ab
movq %rbx, %rdx
leaq 0x28(%r14), %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq 0x40f0
movq %r14, %rdi
callq 0x3120
cmpl $0x1, 0x20(%r14)
je 0x3e38
movq 0xa563(%rip), %rax      # 0xe388
movq %rax, 0x10(%r14)
movl $0x1, 0x20(%r14)
movq %r14, 0xa550(%rip)      # 0xe388
movq 0x18(%rsp), %r12
movl 0x40(%r12), %eax
incl %eax
movl %eax, 0x40(%r12)
incl 0x2b5e1(%rip)           # 0x2f430
incl 0x2a5d3(%rip)           # 0x2e428
movl 0xc(%rsp), %ebx
jmp 0x39bd
leaq 0x63dd(%rip), %rdi      # 0xa242
xorl %eax, %eax
callq 0x29aa
jmp 0x3ef4
leaq 0x63a8(%rip), %rdi      # 0xa220
xorl %eax, %eax
callq 0x29aa
pushq $-0x2
popq %rax
movl %r13d, %ecx
roll %cl, %eax
movq 0xa56a(%rip), %rcx      # 0xe3f8
sarl $0x5, %r13d
movslq %r13d, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r12)
movl %r15d, %edi
callq 0x21b0
jmp 0x3f1f
leaq 0x636e(%rip), %rdi      # 0xa220
xorl %eax, %eax
callq 0x29aa
pushq $-0x2
popq %rax
movl %r13d, %ecx
roll %cl, %eax
movq 0xa530(%rip), %rcx      # 0xe3f8
sarl $0x5, %r13d
movslq %r13d, %rdx
andl %eax, (%rcx,%rdx,4)
movl $0x1, 0x8(%r12)
movl %r15d, %edi
callq 0x21b0
jmp 0x3f0d
andq $0x0, (%rbx)
movq 0x58(%rbp), %rax
movq %rax, 0xa46c(%rip)      # 0xe360
movl %r15d, %edi
callq 0x21b0
movq 0xa46d(%rip), %rax      # 0xe370
movq %rax, (%r14)
movq %r14, 0xa463(%rip)      # 0xe370
movq 0xa444(%rip), %rax      # 0xe358
movq %rax, (%rbp)
movq %rbp, 0xa439(%rip)      # 0xe358
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq