// File: JohnWoodman[P]SHellNo/listener.cpp
// Function: listener::listener(int)
listener::listener(int port){
	this->port = port;
	string portString = to_string(port);
	const char* PORT= portString.c_str();
	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE; // use my IP

	if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return;
	}

	// loop through all the results and bind to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
			perror("server: socket");
			continue;
		}

		if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
			perror("setsockopt");
			return;
		}

		if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("server: bind");
			continue;
		}
		break;
	}

	freeaddrinfo(servinfo); // all done with this structure

	if (p == NULL)  {
		fprintf(stderr, "server: failed to bind\n");
		return;
	}

	if (listen(sockfd, 1) == -1) {
		perror("listen");
		return;
	}

	sa.sa_handler = sigchld_handler; // reap all dead processes
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGCHLD, &sa, NULL) == -1) {
		perror("sigaction");
		return;
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, -0x58(%rbp)
movl $0x1, 0x180(%rax)
movl $0x0, 0x184(%rax)
movl -0xc(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0xc(%rbp), %esi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x31530
movq -0x68(%rbp), %rdi
callq 0x6210
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq %rax, -0x38(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rcx)
movups %xmm0, 0x30(%rcx)
movups %xmm0, 0x20(%rcx)
movl $0x0, 0x24(%rcx)
movl $0x1, 0x28(%rcx)
movl $0x1, 0x20(%rcx)
movq -0x38(%rbp), %rsi
addq $0x50, %rcx
xorl %eax, %eax
movl %eax, %edi
callq 0x6600
movl %eax, -0x4c(%rbp)
jmp 0x304db
movl -0x4c(%rbp), %eax
movq -0x60(%rbp), %rcx
movl %eax, 0x1b8(%rcx)
cmpl $0x0, %eax
je 0x30545
movq -0x60(%rbp), %rax
movq 0x1cab8(%rip), %rcx     # 0x4cfb0
movq (%rcx), %rcx
movq %rcx, -0x70(%rbp)
movl 0x1b8(%rax), %edi
callq 0x66a0
movq -0x70(%rbp), %rdi
movq %rax, %rdx
leaq 0x4bd9(%rip), %rsi      # 0x350f1
movb $0x0, %al
callq 0x66f0
movl $0x1, -0x48(%rbp)
jmp 0x30705
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6330
jmp 0x30714
movq -0x60(%rbp), %rax
movq 0x50(%rax), %rcx
movq %rcx, 0x58(%rax)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x30634
movq -0x60(%rbp), %rax
movq 0x58(%rax), %rcx
movl 0x4(%rcx), %edi
movq 0x58(%rax), %rcx
movl 0x8(%rcx), %esi
movq 0x58(%rax), %rax
movl 0xc(%rax), %edx
callq 0x6120
movq -0x60(%rbp), %rcx
movl %eax, 0x10(%rcx)
cmpl $-0x1, %eax
jne 0x3059d
leaq 0x4b71(%rip), %rdi      # 0x35102
callq 0x6420
jmp 0x30598
jmp 0x3061f
movq -0x60(%rbp), %rcx
movl 0x10(%rcx), %edi
addq $0x180, %rcx            # imm = 0x180
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x6870
cmpl $-0x1, %eax
jne 0x305df
leaq 0x4b45(%rip), %rdi      # 0x35111
callq 0x6420
jmp 0x305d3
movl $0x1, -0x48(%rbp)
jmp 0x30705
movq -0x60(%rbp), %rax
movl 0x10(%rax), %edi
movq 0x58(%rax), %rcx
movq 0x18(%rcx), %rsi
movq 0x58(%rax), %rax
movl 0x10(%rax), %edx
callq 0x60b0
cmpl $-0x1, %eax
jne 0x3061d
movq -0x60(%rbp), %rax
movl 0x10(%rax), %edi
callq 0x6920
jmp 0x3060d
leaq 0x4b08(%rip), %rdi      # 0x3511c
callq 0x6420
jmp 0x3061b
jmp 0x3061f
jmp 0x30634
movq -0x60(%rbp), %rax
movq 0x58(%rax), %rcx
movq 0x28(%rcx), %rcx
movq %rcx, 0x58(%rax)
jmp 0x30551
movq -0x60(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0x6650
movq -0x60(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x30670
movq 0x1c95d(%rip), %rax     # 0x4cfb0
movq (%rax), %rdi
leaq 0x4acc(%rip), %rsi      # 0x35129
movb $0x0, %al
callq 0x66f0
movl $0x1, -0x48(%rbp)
jmp 0x30705
movq -0x60(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x1, %esi
callq 0x6570
cmpl $-0x1, %eax
jne 0x3069d
leaq 0x4ab4(%rip), %rdi      # 0x35141
callq 0x6420
jmp 0x30694
movl $0x1, -0x48(%rbp)
jmp 0x30705
movq -0x60(%rbp), %rdi
leaq -0x2f8(%rip), %rax      # 0x303b0
movq %rax, 0xe8(%rdi)
addq $0xe8, %rdi
addq $0x8, %rdi
callq 0x65e0
movq -0x60(%rbp), %rsi
movl $0x10000000, 0x170(%rsi) # imm = 0x10000000
addq $0xe8, %rsi
movl $0x11, %edi
xorl %eax, %eax
movl %eax, %edx
callq 0x65f0
cmpl $-0x1, %eax
jne 0x306fe
leaq 0x4a5a(%rip), %rdi      # 0x35148
callq 0x6420
jmp 0x306f5
movl $0x1, -0x48(%rbp)
jmp 0x30705
movl $0x0, -0x48(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6330
addq $0x70, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x6850
nopl (%rax)