// File: /workspace/github_c_makefile_star2_O3/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 0x1f58
leaq -0x2c(%rip), %rsi       # 0x19fe
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)
leaq 0x10(%rsp), %rcx
xorl %edi, %edi
movq %r14, %rsi
callq 0x11e0
testl %eax, %eax
jne 0x1f78
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x1eaf
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x1230
cmpl $-0x1, %eax
je 0x1aac
movl %eax, %ebx
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x1170
testl %eax, %eax
je 0x1ab2
movl %ebx, %edi
callq 0x10e0
movq 0x28(%r15), %r15
jmp 0x1a71
movq 0x10(%rsp), %rdi
callq 0x1220
movl %ebx, %edi
callq 0x22fa
movl %ebx, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1150
cmpl $-0x1, %eax
je 0x202d
xorl %edi, %edi
callq 0x1200
cmpl $-0x1, %eax
je 0x20fb
movl %eax, %ebp
leaq 0x10(%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 0x21c9
movl $0x40, %edi
movl $0xc, %esi
callq 0x1120
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x2263
leaq 0x30(%rsp), %r15
movl %ebp, %edi
movq 0x20(%rsp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1160
testl %eax, %eax
jle 0x1b31
movl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,4), %r12
movl (%rcx,%rax,4), %eax
andl $0x19, %eax
cmpl $0x1, %eax
jne 0x1cb9
movl 0x4(%r12), %edi
cmpl %edi, %ebx
jne 0x1be8
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
movq %r15, %rsi
leaq 0xc(%rsp), %r12
movq %r12, %rdx
callq 0x11a0
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x1c64
movl %r14d, %edi
callq 0x22fa
movl %r14d, 0x14(%rsp)
movl $0x80000001, 0x10(%rsp) # imm = 0x80000001
movl %ebp, %edi
movl $0x1, %esi
movl %r14d, %edx
leaq 0x10(%rsp), %rcx
callq 0x1100
cmpl $-0x1, %eax
je 0x1cdb
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
movq %r15, %rsi
jmp 0x1b93
movl $0x80, %edx
movq %r15, %rsi
movl $0x20, %ecx
callq 0x1030
testq %rax, %rax
je 0x1c89
cmpq $-0x1, %rax
je 0x1c73
movl 0x4(%r12), %edi
movq 0x351b(%rip), %rsi      # 0x5130
movq 0x351c(%rip), %rdx      # 0x5138
callq 0x1080
movq %rax, %r14
leaq 0x1671(%rip), %rdi      # 0x329c
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
cmpq $-0x1, %r14
je 0x1c42
movl 0x4(%r12), %edi
jmp 0x1be8
movq 0x33af(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
movl $0x17, %esi
movl $0x1, %edx
leaq 0x15d0(%rip), %rdi      # 0x322d
callq 0x11d0
jmp 0x1c3b
callq 0x1040
cmpl $0xb, (%rax)
je 0x1ca6
jmp 0x1de1
callq 0x1040
cmpl $0xb, (%rax)
je 0x1ca6
leaq 0x1613(%rip), %rdi      # 0x3297
callq 0x1180
movl 0x4(%r12), %esi
leaq 0x1615(%rip), %rdi      # 0x32aa
xorl %eax, %eax
callq 0x10b0
movl 0x4(%r12), %edi
callq 0x10e0
incq %r13
cmpq 0x28(%rsp), %r13
jne 0x1b55
jmp 0x1b31
movq 0x3338(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
movl $0x17, %esi
movl $0x1, %edx
leaq 0x1559(%rip), %rdi      # 0x322d
callq 0x11d0
jmp 0x1c9c
movq 0x3316(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x14b5(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x14a2(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x1551(%rip), %rdi      # 0x3272
movl $0x24, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1309(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0x12f3(%rip), %rsi      # 0x3046
leaq 0x1429(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x12f7(%rip), %rsi      # 0x3062
movl $0xc7, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x140c(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x1458(%rip), %rdi      # 0x31fc
callq 0x1180
movq (%r14), %rcx
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x12a7(%rip), %rdi      # 0x306f
movl $0xa, %esi
movl $0x1, %edx
callq 0x11d0
movl $0x1, %edi
callq 0x11c0
movq 0x3210(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x13af(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x139c(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x141e(%rip), %rdi      # 0x3245
movl $0x25, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1203(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0x11ed(%rip), %rsi      # 0x3046
leaq 0x1323(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0x11f1(%rip), %rsi      # 0x3062
movl $0xb6, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x1306(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x13c1(%rip), %rdi      # 0x326b
jmp 0x1da4
movq 0x3142(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x12e1(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0x12ce(%rip), %rdi      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0x13f8(%rip), %rsi      # 0x32ea
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rcx
leaq 0x113d(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0x1127(%rip), %rsi      # 0x3046
leaq 0x125d(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rdi
leaq 0x112b(%rip), %rsi      # 0x3062
movl $0x57, %edx
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%rbx), %rcx
jmp 0x1dc1
movq 0x3099(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x11e5(%rip), %rsi      # 0x3151
xorl %eax, %eax
callq 0x1140
jmp 0x1dd7
movl %eax, %ebx
movq 0x3077(%rip), %r15      # 0x4ff8
movq (%r15), %rcx
leaq 0x1216(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %rcx
leaq 0x1203(%rip), %rdi      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %r14
movl %ebx, %edi
callq 0x10c0
leaq 0x130a(%rip), %rsi      # 0x32ce
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rcx
leaq 0x1068(%rip), %rdi      # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r15), %rdi
leaq 0x1052(%rip), %rsi      # 0x3046
leaq 0x1188(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r15), %rdi
leaq 0x1056(%rip), %rsi      # 0x3062
movl $0x47, %edx
xorl %eax, %eax
callq 0x1140
movq (%r15), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r15), %rcx
jmp 0x1dc1
movq 0x2fc4(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x1163(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1150(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x10f0(%rip), %rdi      # 0x3163
movl $0x1f, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xfb7(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xfa1(%rip), %rsi       # 0x3046
leaq 0x10d7(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xfa5(%rip), %rsi       # 0x3062
movl $0x82, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0x10ba(%rip), %rdi      # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x10b5(%rip), %rdi      # 0x31ab
jmp 0x1da4
movq 0x2ef6(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x1095(%rip), %rdi      # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0x1082(%rip), %rbx      # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0x1071(%rip), %rdi      # 0x31b2
movl $0x18, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xee9(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xed3(%rip), %rsi       # 0x3046
leaq 0x1009(%rip), %rdx      # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xed7(%rip), %rsi       # 0x3062
movl $0x85, %edx
xorl %eax, %eax
callq 0x1140
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10f0
movq (%r14), %rcx
leaq 0xfec(%rip), %rdi       # 0x319a
movl $0x10, %esi
movl $0x1, %edx
callq 0x11d0
leaq 0x1007(%rip), %rdi      # 0x31cb
jmp 0x1da4
movq 0x2e28(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0xfc7(%rip), %rdi       # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xfb4(%rip), %rbx       # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x11d0
movq (%r14), %rcx
leaq 0xfca(%rip), %rdi       # 0x31d9
movl $0x22, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rcx
leaq 0xe1b(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%r14), %rdi
leaq 0xe05(%rip), %rsi       # 0x3046
leaq 0xf3b(%rip), %rdx       # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%r14), %rdi
leaq 0xe09(%rip), %rsi       # 0x3062
movl $0x8c, %edx
jmp 0x1d70
movq 0x2d8e(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0xf2d(%rip), %rdi       # 0x31a1
movl $0x9, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xf1a(%rip), %rdi       # 0x31a7
movl $0x3, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xf60(%rip), %rdi       # 0x3206
movl $0x26, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rcx
leaq 0xd84(%rip), %rdi       # 0x3043
movl $0x2, %esi
movl $0x1, %edx
callq 0x11d0
movq (%rbx), %rdi
leaq 0xd6e(%rip), %rsi       # 0x3046
leaq 0xea4(%rip), %rdx       # 0x3183
xorl %eax, %eax
callq 0x1140
movq (%rbx), %rdi
leaq 0xd72(%rip), %rsi       # 0x3062
movl $0x8f, %edx
jmp 0x1f3c