// File: /workspace/github_c_makefile_star2_O2/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 0x1c70
pushq $0x1
popq %rbp
leaq -0x2f(%rip), %rsi       # 0x175e
pushq $0x2
popq %rdi
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 %ebp, 0x8(%rdx)
movl %ebp, (%rdx)
leaq 0x8(%rsp), %r15
xorl %edi, %edi
movq %r14, %rsi
movq %r15, %rcx
callq 0x11e0
testl %eax, %eax
jne 0x1c90
movq (%r15), %r15
testq %r15, %r15
je 0x1bd4
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x1230
cmpl $-0x1, %eax
je 0x1809
movl %eax, %ebx
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x1170
testl %eax, %eax
je 0x180f
movl %ebx, %edi
callq 0x10e0
addq $0x28, %r15
jmp 0x17cb
movq 0x8(%rsp), %rdi
callq 0x1220
movl %ebx, %edi
callq 0x1fe6
movl %ebx, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1150
cmpl $-0x1, %eax
je 0x1d22
xorl %edi, %edi
callq 0x1200
cmpl $-0x1, %eax
je 0x1de9
movl %eax, %ebp
leaq 0x8(%rsp), %rcx
movl %ebx, 0x4(%rcx)
movl $0x80000001, (%rcx)     # imm = 0x80000001
pushq $0x1
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x1100
cmpl $-0x1, %eax
je 0x1eb0
pushq $0x40
popq %rdi
pushq $0xc
popq %rsi
callq 0x1120
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x1f47
leaq 0x30(%rsp), %r13
leaq 0x14(%rsp), %r15
movl %ebp, %edi
movq 0x18(%rsp), %rsi
pushq $0x40
popq %rdx
pushq $-0x1
popq %rcx
callq 0x1160
movl %eax, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovlel %eax, %ecx
movq %rcx, 0x20(%rsp)
xorl %eax, %eax
cmpq 0x20(%rsp), %rax
je 0x188d
movq %rax, 0x28(%rsp)
imulq $0xc, %rax, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r14
movl (%rcx,%rax), %eax
andl $0x19, %eax
cmpl $0x1, %eax
jne 0x19f8
movl 0x4(%r14), %edi
cmpl %edi, %ebx
jne 0x1937
pushq $0x1
popq %r14
movl $0x10, 0x14(%rsp)
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x11a0
cmpl $-0x1, %eax
je 0x19ab
movl %eax, %r12d
movl %eax, %edi
callq 0x1fe6
movl %r12d, 0xc(%rsp)
movl $0x80000001, 0x8(%rsp)  # imm = 0x80000001
movl %ebp, %edi
movl %r14d, %esi
movl %r12d, %edx
leaq 0x8(%rsp), %rcx
callq 0x1100
cmpl $-0x1, %eax
jne 0x18e6
jmp 0x1a16
movl $0x80, %edx
movq %r13, %rsi
pushq $0x20
popq %rcx
callq 0x1030
testq %rax, %rax
je 0x19d0
cmpq $-0x1, %rax
je 0x19ba
movl 0x4(%r14), %edi
movq 0x37cf(%rip), %rsi      # 0x5130
movq 0x37d0(%rip), %rdx      # 0x5138
callq 0x1080
movq %rax, %r12
leaq 0x1925(%rip), %rdi      # 0x329c
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
cmpq $-0x1, %r12
je 0x198d
movl 0x4(%r14), %edi
jmp 0x1937
movq 0x3664(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x188f(%rip), %rdi      # 0x322d
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x1987
callq 0x1040
cmpl $0xb, (%rax)
je 0x19eb
jmp 0x1b0d
callq 0x1040
cmpl $0xb, (%rax)
je 0x19eb
leaq 0x18cc(%rip), %rdi      # 0x3297
callq 0x1180
movl 0x4(%r14), %esi
leaq 0x18cf(%rip), %rdi      # 0x32aa
xorl %eax, %eax
callq 0x10b0
movl 0x4(%r14), %edi
callq 0x10e0
movq 0x28(%rsp), %rax
incq %rax
jmp 0x18b2
movq 0x35f9(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1824(%rip), %rdi      # 0x322d
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x19e2
movq 0x35db(%rip), %r12      # 0x4ff8
movq (%r12), %rcx
leaq 0x1779(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1766(%rip), %r14      # 0x31a7
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1814(%rip), %rdi      # 0x3272
pushq $0x24
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x15cf(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rdi
leaq 0x15bc(%rip), %rsi      # 0x3046
leaq 0x16f2(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r12), %rdi
leaq 0x15bf(%rip), %rsi      # 0x3062
movl $0xc7, %edx
xorl %eax, %eax
callq 0x1140
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r12), %rcx
leaq 0x16d4(%rip), %rdi      # 0x319a
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x11d0
leaq 0x1724(%rip), %rdi      # 0x31fc
callq 0x1180
movq (%r12), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1575(%rip), %rdi      # 0x306f
pushq $0xa
popq %rsi
movq %rbx, %rdx
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x11c0
movq 0x34e4(%rip), %r12      # 0x4ff8
movq (%r12), %rcx
leaq 0x1682(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x166f(%rip), %r14      # 0x31a7
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x16f0(%rip), %rdi      # 0x3245
pushq $0x25
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x14d8(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rdi
leaq 0x14c5(%rip), %rsi      # 0x3046
leaq 0x15fb(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r12), %rdi
leaq 0x14c8(%rip), %rsi      # 0x3062
movl $0xb6, %edx
xorl %eax, %eax
callq 0x1140
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r12), %rcx
leaq 0x15dd(%rip), %rdi      # 0x319a
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x11d0
leaq 0x169c(%rip), %rdi      # 0x326b
jmp 0x1ad8
movq 0x341d(%rip), %r15      # 0x4ff8
movq (%r15), %rcx
leaq 0x15bc(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %rcx
leaq 0x15aa(%rip), %rdi      # 0x31a7
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %rdi
leaq 0x16d8(%rip), %rsi      # 0x32ea
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rcx
leaq 0x141d(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %rdi
leaq 0x140b(%rip), %rsi      # 0x3046
leaq 0x1541(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r15), %rdi
leaq 0x140f(%rip), %rsi      # 0x3062
pushq $0x57
popq %rdx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r15), %rcx
jmp 0x1af3
movq 0x3381(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x14cd(%rip), %rsi      # 0x3151
xorl %eax, %eax
callq 0x1140
jmp 0x1b05
movl %eax, %ebp
movq 0x335f(%rip), %r15      # 0x4ff8
movq (%r15), %rcx
leaq 0x14fe(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %rcx
leaq 0x14ec(%rip), %rdi      # 0x31a7
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %r14
movl %ebp, %edi
callq 0x10c0
leaq 0x15f7(%rip), %rsi      # 0x32ce
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rcx
leaq 0x1355(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r15), %rdi
leaq 0x1343(%rip), %rsi      # 0x3046
leaq 0x1479(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r15), %rdi
leaq 0x1347(%rip), %rsi      # 0x3062
pushq $0x47
jmp 0x1c55
movq 0x32cf(%rip), %r12      # 0x4ff8
movq (%r12), %rcx
leaq 0x146d(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x145a(%rip), %r14      # 0x31a7
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x13f9(%rip), %rdi      # 0x3163
pushq $0x1f
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x12c3(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rdi
leaq 0x12b0(%rip), %rsi      # 0x3046
leaq 0x13e6(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r12), %rdi
leaq 0x12b3(%rip), %rsi      # 0x3062
movl $0x82, %edx
xorl %eax, %eax
callq 0x1140
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r12), %rcx
leaq 0x13c8(%rip), %rdi      # 0x319a
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x11d0
leaq 0x13c7(%rip), %rdi      # 0x31ab
jmp 0x1ad8
movq 0x3208(%rip), %r12      # 0x4ff8
movq (%r12), %rcx
leaq 0x13a6(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1393(%rip), %r14      # 0x31a7
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1381(%rip), %rdi      # 0x31b2
pushq $0x18
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x11fc(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rdi
leaq 0x11e9(%rip), %rsi      # 0x3046
leaq 0x131f(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r12), %rdi
leaq 0x11ec(%rip), %rsi      # 0x3062
movl $0x85, %edx
xorl %eax, %eax
callq 0x1140
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r12), %rcx
leaq 0x1301(%rip), %rdi      # 0x319a
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x11d0
leaq 0x1320(%rip), %rdi      # 0x31cb
jmp 0x1ad8
movq 0x3141(%rip), %r12      # 0x4ff8
movq (%r12), %rcx
leaq 0x12df(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x12cc(%rip), %r14      # 0x31a7
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x12e1(%rip), %rdi      # 0x31d9
pushq $0x22
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1135(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rdi
leaq 0x1122(%rip), %rsi      # 0x3046
leaq 0x1258(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r12), %rdi
leaq 0x1125(%rip), %rsi      # 0x3062
movl $0x8c, %edx
jmp 0x1aa8
movq 0x30aa(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x1249(%rip), %rdi      # 0x31a1
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1237(%rip), %rdi      # 0x31a7
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1281(%rip), %rdi      # 0x3206
pushq $0x26
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r14), %rcx
leaq 0x10a9(%rip), %rdi      # 0x3043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r14), %rdi
leaq 0x1097(%rip), %rsi      # 0x3046
leaq 0x11cd(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x109b(%rip), %rsi      # 0x3062
movl $0x8f, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x10f0
movq (%r14), %rcx
jmp 0x1af3