// File: /workspace/github_cpp_makefile_star2_O2/m1c0l[P]http-client-server/web-server.cpp
// Function: main
int main(int argc, char **argv) {
	if (argc > 5) {
		cerr << "usage: " << argv[0] << " [hostname] [port] [file-dir] [--sync]" << '\n';
		return 1;
	}

	string hostname, port, filedir, sync;

	// check argc and set default parameters if needed
	hostname = argc < 2 ? "localhost" : argv[1];
	port = argc < 3 ? "4000" : argv[2];
	filedir = argc < 4 ? "." : argv[3];
	sync = argc < 5 ? "" : argv[4];

	// create a socket using TCP IP
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);

	// allow others to reuse the address
	int yes = 1;
	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
		perror("setsockopt");
		return 1;
	}

	// bind address to socket
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(stoi(port));     // short, network byte order
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));

	if (bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		perror("bind");
		return 2;
	}

	// set socket to listen status
	if (listen(sockfd, 1) == -1) {
		perror("listen");
		return 3;
	}

	// accept a new connection
	struct sockaddr_in clientAddr;

	//int clientSockfd;

	socklen_t clientAddrSize = sizeof(clientAddr);
	//Start Multithreading Magic
	while(true){
		int clientSockfd= accept(sockfd, (struct sockaddr*)&clientAddr, &clientAddrSize);
		if (clientSockfd == -1) {
			perror("accept");
			return 4;
		}

		if (sync == "--sync") {
			thread_func(clientAddr, filedir, clientSockfd);
		}
		else {
			thread(thread_func, clientAddr, filedir, clientSockfd).detach();
		}

	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %rbx
cmpl $0x6, %edi
jl 0x618f
movq 0x7eab(%rip), %rdi      # 0xdff0
leaq 0x2fe4(%rip), %rsi      # 0x9130
callq 0x4350
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x4350
leaq 0x2fd5(%rip), %rsi      # 0x9138
movq %rax, %rdi
callq 0x4350
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x4400
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, %ebp
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x78(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
xorl %edx, %edx
movb %dl, (%rax)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x58(%rsp), %rsi
movq %rsi, -0x10(%rsi)
andq $0x0, -0x8(%rsi)
movb %dl, (%rcx)
movb %dl, (%rax)
movb %dl, (%rsi)
cmpl $0x2, %edi
jge 0x61e4
leaq 0x2f7d(%rip), %rsi      # 0x915f
jmp 0x61e8
movq 0x8(%rbx), %rsi
leaq 0x88(%rsp), %rdi
callq 0x4580
cmpl $0x3, %ebp
jge 0x6203
leaq 0x2f68(%rip), %rsi      # 0x9169
jmp 0x6207
movq 0x10(%rbx), %rsi
leaq 0x68(%rsp), %rdi
callq 0x4580
cmpl $0x4, %ebp
jge 0x621f
leaq 0x2f51(%rip), %rsi      # 0x916e
jmp 0x6223
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x4580
cmpl $0x5, %ebp
jne 0x6238
movq 0x20(%rbx), %rsi
jmp 0x623f
leaq 0x2ebf(%rip), %rsi      # 0x90fe
leaq 0x48(%rsp), %rdi
callq 0x4580
pushq $0x2
popq %rbx
pushq $0x1
popq %r14
movl %ebx, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x40b0
movl %eax, %ebp
leaq 0x14(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x4
popq %r8
movl %eax, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x4550
cmpl $-0x1, %eax
jne 0x628f
leaq 0x2eeb(%rip), %rdi      # 0x9170
callq 0x42b0
jmp 0x63d0
movw $0x2, 0xa8(%rsp)
leaq 0x68(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x67f0
rolw $0x8, %ax
leaq 0xa8(%rsp), %r14
movw %ax, 0x2(%r14)
leaq 0x2ebb(%rip), %rdi      # 0x917b
callq 0x4370
movl %eax, 0x4(%r14)
andq $0x0, 0x8(%r14)
pushq $0x10
popq %rdx
movl %ebp, %edi
movq %r14, %rsi
callq 0x4060
cmpl $-0x1, %eax
jne 0x62f4
leaq 0x2e9e(%rip), %rdi      # 0x9185
callq 0x42b0
movl %ebx, %r14d
jmp 0x63d0
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x43d0
cmpl $-0x1, %eax
jne 0x6316
leaq 0x2e80(%rip), %rdi      # 0x918a
callq 0x42b0
pushq $0x3
jmp 0x63ce
leaq 0x4(%rsp), %rax
movl $0x10, (%rax)
leaq 0x18(%rsp), %r15
leaq 0xb8(%rsp), %rbx
leaq 0x8(%rsp), %r12
movl %ebp, %edi
movq %r15, %rsi
leaq 0x4(%rsp), %rdx
callq 0x4100
movl %eax, (%rsp)
cmpl $-0x1, %eax
je 0x63c0
leaq 0x48(%rsp), %rdi
leaq 0x2e42(%rip), %rsi      # 0x9198
callq 0x56b7
testb %al, %al
je 0x6391
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %r14
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4480
movl (%rsp), %ecx
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5b01
movq %rbx, %rdi
callq 0x4170
jmp 0x6333
movq %r12, %rdi
leaq -0x89a(%rip), %rsi      # 0x5b01
movq %r15, %rdx
leaq 0x28(%rsp), %rcx
movq %rsp, %r8
callq 0x680c
movq %r12, %rdi
callq 0x4530
movq %r12, %rdi
callq 0x68a2
jmp 0x6333
leaq 0x2dca(%rip), %rdi      # 0x9191
callq 0x42b0
pushq $0x4
popq %r14
leaq 0x48(%rsp), %rdi
callq 0x4170
leaq 0x28(%rsp), %rdi
callq 0x4170
leaq 0x68(%rsp), %rdi
callq 0x4170
leaq 0x88(%rsp), %rdi
callq 0x4170
jmp 0x617a
jmp 0x6427
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4170
jmp 0x642a
jmp 0x6427
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x68a2
jmp 0x642a
jmp 0x6427
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4170
leaq 0x28(%rsp), %rdi
callq 0x4170
leaq 0x68(%rsp), %rdi
callq 0x4170
leaq 0x88(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x4540
nop