// File: /workspace/github_cpp_makefile_star2_O3/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 0x71ef
movq 0x5e9d(%rip), %r14      # 0xcff0
leaq 0x1fd6(%rip), %rsi      # 0x9130
movl $0x7, %edx
movq %r14, %rdi
callq 0x4400
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x718b
movq %rbx, %rdi
callq 0x4170
movq 0x5e72(%rip), %rdi      # 0xcff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x4400
jmp 0x71a3
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4520
movq 0x5e46(%rip), %rbx      # 0xcff0
leaq 0x1f87(%rip), %rsi      # 0x9138
movl $0x26, %edx
movq %rbx, %rdi
callq 0x4400
leaq 0x90(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x4400
movl $0x1, %ebp
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edi, %ebp
leaq 0xa0(%rsp), %r13
movq %r13, -0x10(%r13)
xorl %eax, %eax
movq %rax, -0x8(%r13)
movb %al, (%r13)
leaq 0x80(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x20(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x60(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
cmpl $0x2, %edi
jge 0x7245
leaq 0x1f1c(%rip), %r14      # 0x915f
jmp 0x7249
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x4170
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x44e0
cmpl $0x3, %ebp
jge 0x7276
leaq 0x1ef5(%rip), %r14      # 0x9169
jmp 0x727a
movq 0x10(%rbx), %r14
leaq 0x70(%rsp), %r15
movq 0x8(%r15), %r12
movq %r14, %rdi
callq 0x4170
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x44e0
cmpl $0x4, %ebp
jge 0x72ac
leaq 0x1ec4(%rip), %r14      # 0x916e
jmp 0x72b0
movq 0x18(%rbx), %r14
leaq 0x10(%rsp), %r15
movq 0x8(%r15), %r12
movq %r14, %rdi
callq 0x4170
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x44e0
cmpl $0x5, %ebp
jne 0x72df
movq 0x20(%rbx), %rbx
jmp 0x72e6
leaq 0x1e18(%rip), %rbx      # 0x90fe
leaq 0x50(%rsp), %r14
movq 0x8(%r14), %r15
movq %rbx, %rdi
callq 0x4170
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x44e0
movl $0x1, %ebp
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x40b0
movl %eax, %ebx
leaq 0xc4(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4560
cmpl $-0x1, %eax
jne 0x735d
leaq 0x1e1d(%rip), %rdi      # 0x9170
callq 0x42b0
jmp 0x7533
movw $0x2, 0xc8(%rsp)
movq 0x70(%rsp), %r15
callq 0x4040
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0xb0(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x4490
cmpq %r15, (%r12)
je 0x75b3
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x75bf
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x75bf
testl %ecx, %ecx
jne 0x73bd
movl %ebp, (%r14)
rolw $0x8, %ax
leaq 0xc8(%rsp), %r14
movw %ax, 0x2(%r14)
leaq 0x1da6(%rip), %rdi      # 0x917b
callq 0x4360
movl %eax, 0x4(%r14)
movq $0x0, 0x8(%r14)
movl %ebx, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x4070
cmpl $-0x1, %eax
jne 0x7410
leaq 0x1d84(%rip), %rdi      # 0x9185
callq 0x42b0
movl $0x2, %ebp
jmp 0x7533
movl %ebx, %edi
movl $0x1, %esi
callq 0x43d0
cmpl $-0x1, %eax
jne 0x7437
leaq 0x1d62(%rip), %rdi      # 0x918a
callq 0x42b0
movl $0x3, %ebp
jmp 0x7533
leaq 0x4(%rsp), %rax
movl $0x10, (%rax)
leaq 0xb0(%rsp), %r15
leaq 0x50(%rsp), %r13
leaq 0x8(%rsp), %rbp
movl %ebx, %edi
movq %r15, %rsi
leaq 0x4(%rsp), %rdx
callq 0x4110
movl %eax, (%rsp)
cmpl $-0x1, %eax
je 0x751a
movq %r13, %rdi
leaq 0x1d1f(%rip), %rsi      # 0x9198
callq 0x40e0
testl %eax, %eax
je 0x74b1
movq %rbp, %rdi
leaq -0xe9a(%rip), %rsi      # 0x65f2
movq %r15, %rdx
leaq 0x10(%rsp), %rcx
movq %rsp, %r8
callq 0x79e0
movq %rbp, %rdi
callq 0x4540
cmpq $0x0, 0x8(%rsp)
je 0x7454
jmp 0x7602
movq 0xb0(%rsp), %r14
movq 0xb8(%rsp), %r12
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x50d2
movl (%rsp), %ecx
movq %r14, %rdi
movq %r12, %rsi
leaq 0x30(%rsp), %rdx
callq 0x65f2
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x7454
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x7454
leaq 0x1c70(%rip), %rdi      # 0x9191
callq 0x42b0
movl $0x4, %ebp
leaq 0xa0(%rsp), %r13
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x754f
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x756b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x758d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x71db
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x71db
leaq 0x1be5(%rip), %rdi      # 0x919f
callq 0x4200
leaq 0x1bd9(%rip), %rdi      # 0x919f
callq 0x44a0
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x760e
movl %ebp, (%r14)
jmp 0x760e
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x760e
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x760e
cmpq $0x0, 0x8(%rsp)
je 0x760b
callq 0x40d0
jmp 0x760b
jmp 0x760b
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x762a
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7646
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x7668
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x768d
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x4370
movq %rbx, %rdi
callq 0x4550