// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xf8, %rsp
movl $0x0, 0xf4(%rsp)
movl %edi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
cmpl $0x2, 0xf0(%rsp)
je 0x19d8
movq 0x3646(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0xe8(%rsp), %rax
movq (%rax), %rdx
leaq 0x17a2(%rip), %rsi      # 0x3169
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movl $0x2, %edi
leaq -0x74(%rip), %rsi       # 0x1970
callq 0x1120
movq 0xe8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2430
movl %eax, 0xe4(%rsp)
movl 0xe4(%rsp), %edi
callq 0x2700
movl 0xe4(%rsp), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1140
movl %eax, 0xe0(%rsp)
cmpl $-0x1, %eax
jne 0x1b40
movq 0x35c3(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x177a(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x35ab(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1768(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x3593(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x170c(%rip), %rsi      # 0x317b
movb $0x0, %al
callq 0x1130
movq 0x357b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x15d4(%rip), %rsi      # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x3563(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x15bf(%rip), %rsi      # 0x305e
leaq 0x16f5(%rip), %rdx      # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x3544(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x15bc(%rip), %rsi      # 0x307a
movl $0x82, %edx
movb $0x0, %al
callq 0x1130
movq 0x3527(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1581(%rip), %rsi      # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x350f(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x16bf(%rip), %rsi      # 0x31b2
movb $0x0, %al
callq 0x1130
leaq 0x16c2(%rip), %rdi      # 0x31c3
callq 0x1170
movq 0x34eb(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x16a8(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x34d3(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1558(%rip), %rsi      # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x1b42
jmp 0x1b44
xorl %edi, %edi
callq 0x11e0
movl %eax, 0xdc(%rsp)
cmpl $-0x1, %eax
jne 0x1c6d
movq 0x3496(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x164d(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x347e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x163b(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x3466(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x162e(%rip), %rsi      # 0x31ca
movb $0x0, %al
callq 0x1130
movq 0x344e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x14a7(%rip), %rsi      # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x3436(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1492(%rip), %rsi      # 0x305e
leaq 0x15c8(%rip), %rdx      # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x3417(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x148f(%rip), %rsi      # 0x307a
movl $0x85, %edx
movb $0x0, %al
callq 0x1130
movq 0x33fa(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1454(%rip), %rsi      # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x33e2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1592(%rip), %rsi      # 0x31b2
movb $0x0, %al
callq 0x1130
leaq 0x15b5(%rip), %rdi      # 0x31e3
callq 0x1170
movq 0x33be(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x157b(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x33a6(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x142b(%rip), %rsi      # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x1c6f
movl 0xe4(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl $0x80000001, 0xd0(%rsp) # imm = 0x80000001
movl 0xdc(%rsp), %edi
movl 0xe4(%rsp), %edx
movl $0x1, %esi
leaq 0xd0(%rsp), %rcx
callq 0x10f0
movl %eax, 0xe0(%rsp)
cmpl $-0x1, %eax
jne 0x1dca
movq 0x3339(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x14f0(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x3321(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x14de(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x3309(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x14f8(%rip), %rsi      # 0x31f1
movb $0x0, %al
callq 0x1130
movq 0x32f1(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x134a(%rip), %rsi      # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x32d9(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1335(%rip), %rsi      # 0x305e
leaq 0x146b(%rip), %rdx      # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x32ba(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1332(%rip), %rsi      # 0x307a
movl $0x8c, %edx
movb $0x0, %al
callq 0x1130
movq 0x329d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x12f7(%rip), %rsi      # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x3285(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1435(%rip), %rsi      # 0x31b2
movb $0x0, %al
callq 0x1130
leaq 0x1489(%rip), %rdi      # 0x3214
callq 0x1170
movq 0x3261(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x141e(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x3249(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x12ce(%rip), %rsi      # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x1dcc
jmp 0x1dce
movl $0x40, %edi
movl $0xc, %esi
callq 0x1110
movq %rax, 0xc8(%rsp)
cmpq $0x0, %rax
jne 0x1ec5
movq 0x3202(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x13b9(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x31ea(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x13a7(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x31d2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x13ee(%rip), %rsi      # 0x321e
movb $0x0, %al
callq 0x1130
movq 0x31ba(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1213(%rip), %rsi      # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x31a2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11fe(%rip), %rsi      # 0x305e
leaq 0x1334(%rip), %rdx      # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x3183(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11fb(%rip), %rsi      # 0x307a
movl $0x8f, %edx
movb $0x0, %al
callq 0x1130
movq 0x3166(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11c0(%rip), %rsi      # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x314e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11d3(%rip), %rsi      # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x1ec7
jmp 0x1ec9
movl 0xdc(%rsp), %edi
movq 0xc8(%rsp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1150
movl %eax, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jge 0x242b
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x1f70
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x1f70
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x1fac
movq 0x3081(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x12c4(%rip), %rsi      # 0x3245
movb $0x0, %al
callq 0x1130
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0x10e0
jmp 0x2415
movl 0xe4(%rsp), %eax
movq 0xc8(%rsp), %rcx
movslq 0xc0(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x22c1
jmp 0x1fd5
movl $0x10, 0xac(%rsp)
movl 0xe4(%rsp), %edi
leaq 0xb0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rsi
leaq 0xac(%rsp), %rdx
callq 0x1190
movl %eax, 0xa8(%rsp)
cmpl $-0x1, 0xa8(%rsp)
jne 0x214e
callq 0x1040
cmpl $0xb, (%rax)
je 0x2035
callq 0x1040
cmpl $0xb, (%rax)
jne 0x203a
jmp 0x22bc
jmp 0x203c
movq 0x2fb5(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x116c(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x2f9d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x115a(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x2f85(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x11e0(%rip), %rsi      # 0x325d
movb $0x0, %al
callq 0x1130
movq 0x2f6d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xfc6(%rip), %rsi       # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x2f55(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xfb1(%rip), %rsi       # 0x305e
leaq 0x10e7(%rip), %rdx      # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x2f36(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xfae(%rip), %rsi       # 0x307a
movl $0xb6, %edx
movb $0x0, %al
callq 0x1130
movq 0x2f19(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xf73(%rip), %rsi       # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x2f01(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x10b1(%rip), %rsi      # 0x31b2
movb $0x0, %al
callq 0x1130
leaq 0x1174(%rip), %rdi      # 0x3283
callq 0x1170
movq 0x2edd(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x109a(%rip), %rsi      # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x2ec5(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xf4a(%rip), %rsi       # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
movl 0xa8(%rsp), %edi
callq 0x2700
movl 0xa8(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl $0x80000001, 0xd0(%rsp) # imm = 0x80000001
movl 0xdc(%rsp), %edi
movl 0xa8(%rsp), %edx
movl $0x1, %esi
leaq 0xd0(%rsp), %rcx
callq 0x10f0
movl %eax, 0xe0(%rsp)
cmpl $-0x1, %eax
jne 0x22b5
movq 0x2e4e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1005(%rip), %rsi      # 0x31b9
movb $0x0, %al
callq 0x1130
movq 0x2e36(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xff3(%rip), %rsi       # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x2e1e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x10a6(%rip), %rsi      # 0x328a
movb $0x0, %al
callq 0x1130
movq 0x2e06(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xe5f(%rip), %rsi       # 0x305b
movb $0x0, %al
callq 0x1130
movq 0x2dee(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xe4a(%rip), %rsi       # 0x305e
leaq 0xf80(%rip), %rdx       # 0x319b
movb $0x0, %al
callq 0x1130
movq 0x2dcf(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xe47(%rip), %rsi       # 0x307a
movl $0xc7, %edx
movb $0x0, %al
callq 0x1130
movq 0x2db2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xe0c(%rip), %rsi       # 0x305c
movb $0x0, %al
callq 0x1130
movq 0x2d9a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xf4a(%rip), %rsi       # 0x31b2
movb $0x0, %al
callq 0x1130
leaq 0xf9e(%rip), %rdi       # 0x3214
callq 0x1170
movq 0x2d76(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xf33(%rip), %rsi       # 0x31bf
movb $0x0, %al
callq 0x1130
movq 0x2d5e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xde3(%rip), %rsi       # 0x3087
movb $0x0, %al
callq 0x1130
movl $0x1, %edi
callq 0x11b0
jmp 0x22b7
jmp 0x1fd5
jmp 0x2415
movl $0x0, 0x9c(%rsp)
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
movq %rsp, %rsi
movl $0x80, %edx
movl $0x20, %ecx
callq 0x1030
movq %rax, 0x90(%rsp)
cmpq $-0x1, 0x90(%rsp)
jne 0x2331
callq 0x1040
cmpl $0xb, (%rax)
je 0x232c
leaq 0xf93(%rip), %rdi       # 0x32af
callq 0x1170
movl $0x1, 0x9c(%rsp)
jmp 0x23be
cmpq $0x0, 0x90(%rsp)
jne 0x2349
movl $0x1, 0x9c(%rsp)
jmp 0x23be
jmp 0x234b
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
movq 0x2db4(%rip), %rsi      # 0x5120
movq 0x2db5(%rip), %rdx      # 0x5128
callq 0x1080
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
leaq 0xf25(%rip), %rdi       # 0x32b4
movb $0x0, %al
callq 0x10b0
cmpq $-0x1, 0x88(%rsp)
jne 0x23b9
movq 0x2c50(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0xe93(%rip), %rsi       # 0x3245
movb $0x0, %al
callq 0x1130
jmp 0x22cc
cmpl $0x0, 0x9c(%rsp)
je 0x240f
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %esi
leaq 0xed9(%rip), %rdi       # 0x32c2
movb $0x0, %al
callq 0x10b0
movq 0xc8(%rsp), %rax
movslq 0xc0(%rsp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0x10e0
jmp 0x2411
jmp 0x2413
jmp 0x2415
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x1ef9
jmp 0x1ec9