// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movq %rsi, %rbx
cmpl $0x6, %edi
jl 0x71de
movq 0x5e51(%rip), %r14      # 0xcff0
leaq 0x1f8a(%rip), %rsi      # 0x9130
movl $0x7, %edx
movq %r14, %rdi
callq 0x4400
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x7585
movq %rbx, %rdi
callq 0x4170
movq 0x5e22(%rip), %rdi      # 0xcff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x4400
jmp 0x759d
movl %edi, %ebp
leaq 0xb0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x90(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x30(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x70(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
cmpl $0x2, %edi
jge 0x7232
leaq 0x1f2f(%rip), %r14      # 0x915f
jmp 0x7236
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x4170
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x44e0
cmpl $0x3, %ebp
jge 0x7263
leaq 0x1f08(%rip), %r14      # 0x9169
jmp 0x7267
movq 0x10(%rbx), %r14
leaq 0x80(%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 0x729c
leaq 0x1ed4(%rip), %r14      # 0x916e
jmp 0x72a0
movq 0x18(%rbx), %r14
leaq 0x20(%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 0x72cf
movq 0x20(%rbx), %rbx
jmp 0x72d6
leaq 0x1e28(%rip), %rbx      # 0x90fe
leaq 0x60(%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, %ebx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x40b0
movl %eax, %ebp
leaq 0xd4(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4560
cmpl $-0x1, %eax
je 0x75e9
movw $0x2, 0xd8(%rsp)
movq 0x80(%rsp), %rbx
callq 0x4040
movq %rax, %r14
movl (%rax), %r12d
movl $0x0, (%rax)
leaq 0xc0(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4490
cmpq %rbx, (%r15)
je 0x7626
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x7632
movl (%r14), %ecx
cmpl $0x22, %ecx
je 0x7632
testl %ecx, %ecx
jne 0x73a3
movl %r12d, (%r14)
rolw $0x8, %ax
leaq 0xd8(%rsp), %rbx
movw %ax, 0x2(%rbx)
leaq 0x1dc1(%rip), %rdi      # 0x917b
callq 0x4360
movl %eax, 0x4(%rbx)
movq $0x0, 0x8(%rbx)
movl %ebp, %edi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x4070
cmpl $-0x1, %eax
je 0x75fa
movl %ebp, %edi
movl $0x1, %esi
callq 0x43d0
cmpl $-0x1, %eax
je 0x7610
leaq 0x14(%rsp), %rax
movl $0x10, (%rax)
movl $0x0, 0xc(%rsp)
leaq 0xc0(%rsp), %r15
leaq 0x18(%rsp), %rbx
movl %ebp, %edi
movq %r15, %rsi
leaq 0x14(%rsp), %rdx
callq 0x4110
movl %eax, %r13d
movl %eax, 0x10(%rsp)
cmpl $-0x1, %eax
je 0x74df
leaq 0x60(%rsp), %rdi
leaq 0x1d56(%rip), %rsi      # 0x9198
callq 0x40e0
testl %eax, %eax
je 0x747c
movq %rbx, %rdi
leaq -0xe37(%rip), %rsi      # 0x661e
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
leaq 0x10(%rsp), %r8
callq 0x7a6c
movq %rbx, %rdi
callq 0x4540
cmpq $0x0, 0x18(%rsp)
je 0x74f3
jmp 0x7679
movq 0xc0(%rsp), %r12
movq 0xc8(%rsp), %r14
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x5100
movl 0x10(%rsp), %ecx
movq %r12, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x661e
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x74f3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x74f3
leaq 0x1cab(%rip), %rdi      # 0x9191
callq 0x42b0
movl $0x4, 0xc(%rsp)
cmpl $-0x1, %r13d
jne 0x7417
movl 0xc(%rsp), %ebx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x751d
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x7539
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x755e
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x75d5
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x75d5
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4520
movq 0x5a4c(%rip), %rbx      # 0xcff0
leaq 0x1b8d(%rip), %rsi      # 0x9138
movl $0x26, %edx
movq %rbx, %rdi
callq 0x4400
leaq 0xa0(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x4400
movl $0x1, %ebx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b80(%rip), %rdi      # 0x9170
callq 0x42b0
jmp 0x7501
leaq 0x1b84(%rip), %rdi      # 0x9185
callq 0x42b0
movl $0x2, %ebx
jmp 0x7501
leaq 0x1b73(%rip), %rdi      # 0x918a
callq 0x42b0
movl $0x3, %ebx
jmp 0x7501
leaq 0x1b72(%rip), %rdi      # 0x919f
callq 0x4200
leaq 0x1b66(%rip), %rdi      # 0x919f
callq 0x44a0
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x7681
movl %r12d, (%r14)
jmp 0x7681
jmp 0x767e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7681
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x7681
jmp 0x767e
cmpq $0x0, 0x18(%rsp)
je 0x767e
callq 0x40d0
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x769d
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x76b9
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x76de
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rax
cmpq %rax, %rdi
je 0x7703
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
movq %rbx, %rdi
callq 0x4550