// File: /workspace/github_c_makefile_star2_O1/cirocosta[P]stress/src/epoll-tcp-server.c
// Function: main
int
main(int argc, char* argv[])
{
	int sfd, s;
	int efd;
	struct epoll_event event;
	struct epoll_event* events;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s [port]\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	signal(SIGINT, terminate);

	sfd = create_and_bind(argv[1]);
	make_socket_non_blocking(sfd);

	_STRESS_MUST_P((s = listen(sfd, SOMAXCONN)) != -1,
	               "listen",
	               "couldn't make socket fd listen.");
	_STRESS_MUST_P((efd = epoll_create1(0)) != -1,
	               "epoll_create1",
	               "Couldn't create epoll fd");

	event.data.fd = sfd;
	event.events = EPOLLIN | EPOLLET;

	_STRESS_MUST_P((s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event)) != -1,
	               "epoll_ctl",
	               "Couldn't set events data for epoll");

	_STRESS_MUST((events = calloc(MAXEVENTS, sizeof event)),
	             "couldn't allocate memory for epoll evs");

	while (1) {
		int n;
		int i;

		n = epoll_wait(efd, events, MAXEVENTS, -1);
		for (i = 0; i < n; i++) {
			if ((events[i].events & EPOLLERR) ||
			    (events[i].events & EPOLLHUP) ||
			    (!(events[i].events & EPOLLIN))) {
				fprintf(stderr, "Unexpected epoll error\n");
				close(events[i].data.fd);
				continue;
			}

			// Notification on the listening socket.
			// - one or more incoming connections.
			else if (sfd == events[i].data.fd) {
				while (1) {
					struct sockaddr in_addr;
					socklen_t in_len;
					int infd;

					in_len = sizeof in_addr;
					infd = accept(sfd, &in_addr, &in_len);
					if (infd == -1) {
						// All incoming
						// connections
						// have been processed.
						if ((errno == EAGAIN) ||
						    (errno == EWOULDBLOCK)) {
							break;
						}

						_STRESS_MUST_P(
						  0,
						  "accept",
						  "Unexpected error accepting "
						  "connection");
					}

					// Make the incoming socket non-blocking
					// and add it to the list of fds to
					// monitor.
					make_socket_non_blocking(infd);

					event.data.fd = infd;
					event.events = EPOLLIN | EPOLLET;
					_STRESS_MUST_P(
					  (s = epoll_ctl(efd,
					                 EPOLL_CTL_ADD,
					                 infd,
					                 &event)) != -1,
					  "epoll_ctl",
					  "Couldn't make client "
					  "fd be monitored");
				}

				continue;
			}

			// data on a fd waiting to be read.
			// - read all of the data that is
			//   available in it (we won't get
			//   another notification for this
			//   same data as we're running
			//   edge-triggered mode).
			else {
				int done = 0;

				while (1) {
					ssize_t count;
					ssize_t written;
					char buf[128]; // dumb buffer

					// TODO use recv such that we can
					// count but drop the entirity of the
					// content. This allows us to avoid
					// copying from kernel space to
					// userspace.
					count = recv(events[i].data.fd,
					             buf,
					             sizeof buf,
					             MSG_TRUNC);
					if (count == -1) {
						// We have read all data.
						// Go back to the main loop.
						if (errno != EAGAIN) {
							perror("read");
							done = 1;
						}
						break;
					} else if (count == 0) {
						// EOF
						done = 1;
						break;
					}

					written =
					  write(events[i].data.fd,
					        stress_http_response,
					        stress_http_response_len);
					printf("written: %ld\n", written);
					if (written == -1) {
						fprintf(
						  stderr,
						  "Unexpected epoll error\n");
					}
				}

				if (done) {
					printf("Closed connection on "
					       "descriptor %d\n",
					       events[i].data.fd);

					/* Closing the descriptor will make
					   epoll remove it
					   from the set of descriptors which are
					   monitored. */
					close(events[i].data.fd);
				}
			}
		}
	}

	free(events);
	close(sfd);

	return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
cmpl $0x2, %edi
jne 0x1ed0
leaq -0x2c(%rip), %rsi       # 0x19fb
movl $0x2, %edi
callq 0x1130
movq 0x8(%rbx), %r14
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, %eax
movl %eax, 0x8(%rdx)
movl %eax, (%rdx)
movq %rsp, %rcx
xorl %edi, %edi
movq %r14, %rsi
callq 0x11e0
testl %eax, %eax
jne 0x1eed
movq (%rsp), %r15
testq %r15, %r15
sete %bpl
je 0x1ab5
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x1230
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x1aa6
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %ebx, %edi
callq 0x1170
testl %eax, %eax
je 0x1ab5
movl %ebx, %edi
callq 0x10e0
movq 0x28(%r15), %r15
testq %r15, %r15
sete %bpl
jne 0x1a74
jmp 0x1ab5
testb $0x1, %bpl
jne 0x1fa2
movq (%rsp), %rdi
callq 0x1220
movl %ebx, %edi
callq 0x2318
movl %ebx, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1150
cmpl $-0x1, %eax
je 0x2034
xorl %edi, %edi
callq 0x1200
cmpl $-0x1, %eax
je 0x2102
movl %eax, %ebp
movq %rsp, %rcx
movl %ebx, 0x4(%rcx)
movl $0x80000001, (%rcx)     # imm = 0x80000001
movl %eax, %edi
movl $0x1, %esi
movl %ebx, %edx
callq 0x1100
cmpl $-0x1, %eax
je 0x21d0
movl $0x40, %edi
movl $0xc, %esi
callq 0x1120
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x226a
leaq 0x30(%rsp), %r15
movl %ebp, 0x10(%rsp)
movl %ebp, %edi
movq 0x18(%rsp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1160
testl %eax, %eax
jle 0x1b3f
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x28(%rsp)
leaq (%rax,%rax,2), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,4), %r12
movl (%rcx,%rax,4), %eax
andl $0x19, %eax
cmpl $0x1, %eax
jne 0x1cd6
cmpl 0x4(%r12), %ebx
jne 0x1bf2
movl $0x10, 0x14(%rsp)
movl %ebx, %edi
movq %r15, %rsi
leaq 0x14(%rsp), %rdx
callq 0x11a0
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x1bd9
movl %r14d, %edi
callq 0x2318
movl %r14d, 0x4(%rsp)
movl $0x80000001, (%rsp)     # imm = 0x80000001
movl %ebp, %edi
movl $0x1, %esi
movl %r14d, %edx
movq %rsp, %rcx
callq 0x1100
cmpl $-0x1, %eax
jne 0x1be7
jmp 0x1cfc
callq 0x1040
cmpl $0xb, (%rax)
jne 0x1dca
cmpl $-0x1, %r14d
jne 0x1b8a
jmp 0x1cbe
addq $0x4, %r12
xorl %ebp, %ebp
movl (%r12), %edi
movl $0x80, %edx
movq %r15, %rsi
movl $0x20, %ecx
callq 0x1030
testq %rax, %rax
je 0x1c8a
cmpq $-0x1, %rax
jne 0x1c28
callq 0x1040
cmpl $0xb, (%rax)
jne 0x1c7e
xorl %r13d, %r13d
jmp 0x1c92
movl (%r12), %edi
movq 0x34fd(%rip), %rsi      # 0x5130
movq 0x34fe(%rip), %rdx      # 0x5138
callq 0x1080
movq %rax, %r14
leaq 0x1653(%rip), %rdi      # 0x329c
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
movb $0x1, %r13b
cmpq $-0x1, %r14
jne 0x1c92
movq 0x3395(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
movl $0x17, %esi
movl $0x1, %edx
leaq 0x15b6(%rip), %rdi      # 0x322d
callq 0x11d0
jmp 0x1c92
leaq 0x1612(%rip), %rdi      # 0x3297
callq 0x1180
xorl %r13d, %r13d
movl $0x1, %ebp
testb %r13b, %r13b
jne 0x1bf8
testl %ebp, %ebp
movl 0x10(%rsp), %ebp
je 0x1cbe
movl (%r12), %esi
leaq 0x15fc(%rip), %rdi      # 0x32aa
xorl %eax, %eax
callq 0x10b0
movl (%r12), %edi
callq 0x10e0
movq 0x28(%rsp), %rax
incq %rax
cmpq 0x20(%rsp), %rax
jne 0x1b62
jmp 0x1b3f
movq 0x331b(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
movl $0x17, %esi
movl $0x1, %edx
leaq 0x153c(%rip), %rdi      # 0x322d
callq 0x11d0
addq $0x4, %r12
jmp 0x1cb5
movq 0x32f5(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x1494(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1481(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x1530(%rip), %rdi      # 0x3272
movl $0x24, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x12e8(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0x12d2(%rip), %rsi      # 0x3046
leaq 0x1408(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x12d6(%rip), %rsi      # 0x3062
movl $0xc7, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x13eb(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x1437(%rip), %rdi      # 0x31fc
jmp 0x1e93
movq 0x3227(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x13c6(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x13b3(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x1435(%rip), %rdi      # 0x3245
movl $0x25, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x121a(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0x1204(%rip), %rsi      # 0x3046
leaq 0x133a(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x1208(%rip), %rsi      # 0x3062
movl $0xb6, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x131d(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x13d8(%rip), %rdi      # 0x326b
callq 0x1180
movq (%r14), %rcx
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x11b8(%rip), %rdi      # 0x306f
movl $0xa, %esi
movl $0x1, %edx
callq 0x11d0
movl $0x1, %edi
callq 0x11c0
movq 0x3121(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x126d(%rip), %rsi      # 0x3151
xorl %eax, %eax
callq 0x1140
jmp 0x1ec6
movl %eax, %ebx
movq 0x3102(%rip), %r15      # 0x4ff8
movq (%r15), %rcx
leaq 0x12a1(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %rcx
leaq 0x128e(%rip), %rdi      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %r14
movl %ebx, %edi
callq 0x10c0
leaq 0x1395(%rip), %rsi      # 0x32ce
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rcx
leaq 0x10f3(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %rdi
leaq 0x10dd(%rip), %rsi      # 0x3046
leaq 0x1213(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r15), %rdi
leaq 0x10e1(%rip), %rsi      # 0x3062
movl $0x47, %edx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r15), %rcx
jmp 0x1eb0
movq 0x304f(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x11ee(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0x11db(%rip), %rdi      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0x1305(%rip), %rsi      # 0x32ea
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rcx
leaq 0x104a(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0x1034(%rip), %rsi      # 0x3046
leaq 0x116a(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rdi
leaq 0x1038(%rip), %rsi      # 0x3062
movl $0x57, %edx
jmp 0x22fc
movq 0x2fbd(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x115c(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1149(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x10e9(%rip), %rdi      # 0x3163
movl $0x1f, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xfb0(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xf9a(%rip), %rsi       # 0x3046
leaq 0x10d0(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xf9e(%rip), %rsi       # 0x3062
movl $0x82, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x10b3(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x10ae(%rip), %rdi      # 0x31ab
jmp 0x1e93
movq 0x2eef(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x108e(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x107b(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x106a(%rip), %rdi      # 0x31b2
movl $0x18, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xee2(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xecc(%rip), %rsi       # 0x3046
leaq 0x1002(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xed0(%rip), %rsi       # 0x3062
movl $0x85, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0xfe5(%rip), %rdi       # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x1000(%rip), %rdi      # 0x31cb
jmp 0x1e93
movq 0x2e21(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0xfc0(%rip), %rdi       # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xfad(%rip), %rbx       # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0xfc3(%rip), %rdi       # 0x31d9
movl $0x22, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xe14(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xdfe(%rip), %rsi       # 0x3046
leaq 0xf34(%rip), %rdx       # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xe02(%rip), %rsi       # 0x3062
movl $0x8c, %edx
jmp 0x1d91
movq 0x2d87(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0xf26(%rip), %rdi       # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xf13(%rip), %rdi       # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xf59(%rip), %rdi       # 0x3206
movl $0x26, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xd7d(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0xd67(%rip), %rsi       # 0x3046
leaq 0xe9d(%rip), %rdx       # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rdi
leaq 0xd6b(%rip), %rsi       # 0x3062
movl $0x8f, %edx
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%rbx), %rcx
jmp 0x1eb0