// File: ixchow[P]http-tweak/./http.hpp
// Function: http::server::server(unsigned short)
inline server::server(uint16_t port) {

	#ifdef _WIN32
	{ //init winsock:
		WSADATA info;
		if (WSAStartup((2 << 8) | 2, &info) != 0) {
			throw std::runtime_error("WSAStartup failed.");
		}
	}
	#endif

	//----------- socket setup -----------
	{ //create socket:
		listen_socket = ::socket(AF_INET, SOCK_STREAM, 0);
		if (listen_socket == -1) {
			throw std::system_error(errno, std::system_category(), "failed to create socket");
		}
	}
	{ //make it okay to reuse port:
		#ifdef _WIN32
		BOOL one = TRUE;
		int ret = setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast< const char * >(&one), sizeof(one));
		#else
		int one = 1;
		int ret = setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
		#endif
		if (ret != 0) {
			std::cerr << "[server::server] WARNING: Failed to set socket reuse address." << std::endl;
		}
	}
	{ //bind to address
		struct sockaddr_in addr;
		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
		addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
		int ret = bind(listen_socket, reinterpret_cast< sockaddr * >(&addr), sizeof(addr));
		if (ret < 0) {
			closesocket(listen_socket);
			throw std::system_error(errno, std::system_category(), "failed to bind socket");
		}
	}
	{ //listen on socket
		int ret = ::listen(listen_socket, 5);
		if (ret < 0) {
			closesocket(listen_socket);
			throw std::system_error(errno, std::system_category(), "failed to listen on socket");
		}
	}


	std::cout << "[server::server] Server started at localhost:" << port << "." << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x48(%rbp)
movl $0xffffffff, (%rdi)     # imm = 0xFFFFFFFF
addq $0x8, %rdi
movq %rdi, -0x58(%rbp)
callq 0x15290
movq -0x48(%rbp), %rdi
movb $0x0, 0x20(%rdi)
addq $0x28, %rdi
movq %rdi, -0x50(%rbp)
callq 0x152b0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4130
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, (%rax)
jne 0x14fe8
movl $0x20, %edi
callq 0x4250
movq %rax, -0x68(%rbp)
movq %rax, -0x70(%rbp)
callq 0x4080
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
callq 0x42a0
movq -0x68(%rbp), %rdi
movl -0x5c(%rbp), %esi
movq %rax, %rdx
leaq 0x369b(%rip), %rcx      # 0x18635
callq 0x152e0
jmp 0x14fa1
movq -0x70(%rbp), %rdi
movq 0xefc4(%rip), %rsi      # 0x23f70
movq 0xeff5(%rip), %rdx      # 0x23fa8
callq 0x46e0
jmp 0x15257
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x4390
jmp 0x1523c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1523c
movq -0x48(%rbp), %rax
movl $0x1, -0x20(%rbp)
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x20(%rbp), %rcx
movl $0x4, %r8d
callq 0x4730
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x15044
movq 0xefca(%rip), %rdi      # 0x23fe8
leaq 0x3628(%rip), %rsi      # 0x1864d
callq 0x4470
movq %rax, -0x78(%rbp)
jmp 0x15030
movq -0x78(%rbp), %rdi
movq 0xef4d(%rip), %rsi      # 0x23f88
callq 0x44d0
jmp 0x15042
jmp 0x15044
leaq -0x34(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4280
movw $0x2, -0x34(%rbp)
movzwl -0xa(%rbp), %edi
callq 0x4090
movw %ax, -0x32(%rbp)
movl $0x7f000001, %edi       # imm = 0x7F000001
callq 0x43c0
movl %eax, %ecx
movq -0x48(%rbp), %rax
movl %ecx, -0x30(%rbp)
movl (%rax), %edi
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
callq 0x40c0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x15122
movq -0x48(%rbp), %rax
movl (%rax), %edi
callq 0x47a0
jmp 0x150a4
movl $0x20, %edi
callq 0x4250
movq %rax, -0x88(%rbp)
movq %rax, -0x90(%rbp)
callq 0x4080
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
callq 0x42a0
movq -0x88(%rbp), %rdi
movl -0x7c(%rbp), %esi
movq %rax, %rdx
leaq 0x35ac(%rip), %rcx      # 0x1868b
callq 0x152e0
jmp 0x150e6
movq -0x90(%rbp), %rdi
movq 0xee7c(%rip), %rsi      # 0x23f70
movq 0xeead(%rip), %rdx      # 0x23fa8
callq 0x46e0
jmp 0x15257
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x4390
jmp 0x1523c
movq -0x48(%rbp), %rax
movl (%rax), %edi
movl $0x5, %esi
callq 0x4530
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x151cd
movq -0x48(%rbp), %rax
movl (%rax), %edi
callq 0x47a0
jmp 0x1514c
movl $0x20, %edi
callq 0x4250
movq %rax, -0xa0(%rbp)
movq %rax, -0xa8(%rbp)
callq 0x4080
movl (%rax), %eax
movl %eax, -0x94(%rbp)
callq 0x42a0
movq -0xa0(%rbp), %rdi
movl -0x94(%rbp), %esi
movq %rax, %rdx
leaq 0x3514(%rip), %rcx      # 0x186a1
callq 0x152e0
jmp 0x15194
movq -0xa8(%rbp), %rdi
movq 0xedce(%rip), %rsi      # 0x23f70
movq 0xedff(%rip), %rdx      # 0x23fa8
callq 0x46e0
jmp 0x15257
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x4390
jmp 0x1523c
movq 0xede4(%rip), %rdi      # 0x23fb8
leaq 0x34e1(%rip), %rsi      # 0x186bc
callq 0x4470
movq %rax, -0xb0(%rbp)
jmp 0x151e9
movq -0xb0(%rbp), %rdi
movzwl -0xa(%rbp), %esi
callq 0x46c0
movq %rax, -0xb8(%rbp)
jmp 0x15202
movq -0xb8(%rbp), %rdi
leaq 0x351e(%rip), %rsi      # 0x1872e
callq 0x4470
movq %rax, -0xc0(%rbp)
jmp 0x1521e
movq -0xc0(%rbp), %rdi
movq 0xed5c(%rip), %rsi      # 0x23f88
callq 0x44d0
jmp 0x15233
addq $0xc0, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0xe380
movq -0x58(%rbp), %rdi
callq 0xe3f0
movq -0x18(%rbp), %rdi
callq 0x4710
nopw (%rax,%rax)