// File: m1c0l[P]http-client-server/web-client.cpp
// Function: main
int main(int argc, char **argv) {
	if (argc < 2) {
		cerr << "usage: " << argv[0] << " [URL] [URL] ..." << '\n';
		return 1;
	}

	Url *url = new Url[argc];
	thread *t = new thread[argc];
	HttpRequest **req = new HttpRequest*[argc];
	sockaddr_in *serverAddr = new sockaddr_in[argc];

	// send a request for each url
	for (int i = 1; i < argc; i++) {

		url[i] = parseUrl(argv[i]);
		serverAddr[i] = getServerAddr(url[i].host, url[i].port);

		// set up HTTP request
		req[i] = new HttpRequest();
		req[i]->setMethod("GET");
		req[i]->setUrl(url[i].path);
		req[i]->setVersion("HTTP/1.0");
		req[i]->setHeader("Host", url[i].host);

		// send the request and retrieve the file
		t[i] = thread(processRequest, (sockaddr*)&serverAddr[i], req[i],
			url[i].path);
	}

	// join threads
	for (int i = 1; i < argc; i++) {
		t[i].join();
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0xa0b9
movq 0x1cf7a(%rip), %rdi     # 0x26ff0
leaq 0xf10d(%rip), %rsi      # 0x1918a
callq 0x5390
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x5390
movq %rax, %rdi
leaq 0xf0f7(%rip), %rsi      # 0x19192
callq 0x5390
movq %rax, %rdi
movl $0xa, %esi
callq 0x5460
movl $0x1, -0x4(%rbp)
jmp 0xa86f
movslq -0x8(%rbp), %rax
movq %rax, -0x200(%rbp)
movl $0x60, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
addq $0x8, %rdi
setb %al
orb %al, %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x5030
movq %rax, %rcx
movq -0x200(%rbp), %rax
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x1f8(%rbp)
cmpq $0x0, %rax
je 0xa163
movq -0x1f8(%rbp), %rax
movq -0x200(%rbp), %rcx
imulq $0x60, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x210(%rbp)
movq %rax, -0x208(%rbp)
movq -0x208(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0xa8e0
movq -0x218(%rbp), %rax
movq -0x210(%rbp), %rcx
addq $0x60, %rax
cmpq %rcx, %rax
movq %rax, -0x208(%rbp)
jne 0xa132
movq -0x1f8(%rbp), %rax
movq %rax, -0x18(%rbp)
movslq -0x8(%rbp), %rax
movq %rax, -0x228(%rbp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
addq $0x8, %rdi
setb %al
orb %al, %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x5030
movq %rax, %rcx
movq -0x228(%rbp), %rax
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x220(%rbp)
cmpq $0x0, %rax
je 0xa218
movq -0x220(%rbp), %rax
movq -0x228(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x238(%rbp)
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0xb200
movq -0x240(%rbp), %rax
movq -0x238(%rbp), %rcx
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x230(%rbp)
jne 0xa1e7
movq -0x220(%rbp), %rax
movq %rax, -0x20(%rbp)
movslq -0x8(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x5030
movq %rax, -0x28(%rbp)
movslq -0x8(%rbp), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x5030
movq %rax, -0x30(%rbp)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0xa82b
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x250(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x5630
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rdx
leaq -0xb8(%rbp), %rdi
callq 0x6180
jmp 0xa2ca
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x8c00
jmp 0xa2df
movq -0x18(%rbp), %rdi
movslq -0x34(%rbp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rdi
leaq -0x98(%rbp), %rsi
movq %rsi, -0x258(%rbp)
callq 0xb220
movq -0x258(%rbp), %rdi
callq 0xa920
leaq -0xb8(%rbp), %rdi
callq 0x5240
leaq -0xb9(%rbp), %rdi
callq 0x5420
movq -0x18(%rbp), %rsi
movslq -0x34(%rbp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x100(%rbp), %rdi
callq 0x5170
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x5, %rcx
leaq 0x20(%rax,%rcx), %rsi
leaq -0x120(%rbp), %rdi
callq 0x5170
jmp 0xa36b
leaq -0x100(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x9060
movq %rdx, -0x268(%rbp)
movq %rax, -0x260(%rbp)
jmp 0xa38e
movq -0x268(%rbp), %rax
movq -0x260(%rbp), %rcx
movq %rcx, -0xdc(%rbp)
movq %rax, -0xd4(%rbp)
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
movups -0xdc(%rbp), %xmm0
movups %xmm0, (%rax,%rcx)
leaq -0x120(%rbp), %rdi
callq 0x5240
leaq -0x100(%rbp), %rdi
callq 0x5240
movl $0xa0, %edi
callq 0x53a0
movq %rax, %rdi
movq %rdi, -0x280(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x290(%rbp)
movaps %xmm0, 0x90(%rdi)
movaps %xmm0, 0x80(%rdi)
movaps %xmm0, 0x70(%rdi)
movaps %xmm0, 0x60(%rdi)
movaps %xmm0, 0x50(%rdi)
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0xb280
movq -0x280(%rbp), %rdx
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x278(%rbp)
leaq -0x141(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x5630
movq -0x270(%rbp), %rdx
leaq 0xebc9(%rip), %rsi      # 0x19039
leaq -0x140(%rbp), %rdi
callq 0x6180
jmp 0xa47e
movq -0x278(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x7e60
jmp 0xa493
leaq -0x140(%rbp), %rdi
callq 0x5240
leaq -0x141(%rbp), %rdi
callq 0x5420
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x2a0(%rbp)
movq -0x18(%rbp), %rax
leaq (%rcx,%rcx,2), %rcx
shlq $0x5, %rcx
leaq 0x40(%rax,%rcx), %rsi
leaq -0x168(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x5170
movq -0x2a0(%rbp), %rdi
movq -0x298(%rbp), %rsi
callq 0x7ed0
jmp 0xa4f7
leaq -0x168(%rbp), %rdi
callq 0x5240
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x2b0(%rbp)
leaq -0x189(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x5630
movq -0x2a8(%rbp), %rdx
leaq 0xeb0f(%rip), %rsi      # 0x19046
leaq -0x188(%rbp), %rdi
callq 0x6180
jmp 0xa545
movq -0x2b0(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x5860
jmp 0xa55a
leaq -0x188(%rbp), %rdi
callq 0x5240
leaq -0x189(%rbp), %rdi
callq 0x5420
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x2c0(%rbp)
leaq -0x1b1(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0x5630
movq -0x2b8(%rbp), %rdx
leaq 0xebd0(%rip), %rsi      # 0x19176
leaq -0x1b0(%rbp), %rdi
callq 0x6180
jmp 0xa5b4
movq -0x18(%rbp), %rsi
movslq -0x34(%rbp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rsi
leaq -0x1d8(%rbp), %rdi
callq 0x5170
jmp 0xa5d5
movq -0x2c0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
leaq -0x1d8(%rbp), %rdx
callq 0x58c0
jmp 0xa5f1
leaq -0x1d8(%rbp), %rdi
callq 0x5240
leaq -0x1b0(%rbp), %rdi
callq 0x5240
leaq -0x1b1(%rbp), %rdi
callq 0x5420
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x1e8(%rbp)
movq -0x28(%rbp), %rcx
movslq -0x34(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movq -0x18(%rbp), %r8
movslq -0x34(%rbp), %rax
imulq $0x60, %rax, %rax
addq %rax, %r8
addq $0x40, %r8
leaq -0x1e0(%rbp), %rdi
leaq -0x145b(%rip), %rsi     # 0x9200
leaq -0x1e8(%rbp), %rdx
callq 0xb2d0
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x1e0(%rbp), %rsi
callq 0xb3a0
leaq -0x1e0(%rbp), %rdi
callq 0xb3e0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xa27c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa6ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x5240
leaq -0xb9(%rbp), %rdi
callq 0x5420
jmp 0xa87b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa711
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x5240
leaq -0x100(%rbp), %rdi
callq 0x5240
jmp 0xa87b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa754
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5240
leaq -0x141(%rbp), %rdi
callq 0x5420
jmp 0xa87b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x5240
jmp 0xa87b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa7ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x5240
leaq -0x189(%rbp), %rdi
callq 0x5420
jmp 0xa87b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa81d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xa811
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x5240
leaq -0x1b0(%rbp), %rdi
callq 0x5240
leaq -0x1b1(%rbp), %rdi
callq 0x5420
jmp 0xa87b
movl $0x1, -0x1ec(%rbp)
movl -0x1ec(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0xa868
movq -0x20(%rbp), %rdi
movslq -0x1ec(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
callq 0x56e8
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
jmp 0xa835
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x5620
nopw (%rax,%rax)