// File: /workspace/github_cpp_makefile_star2_O2/jasmeet13n[P]HTTP-Proxy-Server-Client/client.cpp
// Function: main
int main(int argc, char *argv[]){
	int error;
	if(argc!=4){									// show error if number of command line arguments are not equal to 4
		fprintf(stderr,"Enter PROXY SERVER IP, PROXY SERVER PORT, URL\n");
		return 1;
	}
	struct addrinfo hints;
	struct addrinfo *servinfo, *p;
	//char ipstr[INET_ADDRSTRLEN];	//INET6_ADDRSTRLEN for IPv6

	memset(&hints,0,sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;

	if( (error = getaddrinfo(argv[1], argv[2], &hints, &servinfo)) != 0){	// get address info of server for given IP address and PORT number
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(error));
		exit(1);
	}

	int sockfd;
	for(p=servinfo; p!=NULL; p=p->ai_next){
		if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol))== -1){	// create client socket
			perror("client: socket");
			continue;
		}
		break;
	}
	if(p==NULL){
		fprintf(stderr, "my sock fd: failed to connect\n");
		return 2;
	}
	int yes=1;

	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
    	perror("setsockopt");
    	exit(1);
	}

	int size = 2048, num_bytes;
	char *buf = (char *)malloc((size+1)*sizeof(char));
	unsigned short int len;
	if( (error = connect(sockfd,p->ai_addr,p->ai_addrlen)) == -1){			// connect to server IP Address and PORT number
		perror("client: connect");
	}
	freeaddrinfo(servinfo);
	
	char *query = get_http_request(argv[3]);
	/*char *query = "GET / HTTP/1.0\r\n"
        "Host: www.google.com\r\n"
        "\r\n";*/
	//cout << query << endl;
    len = strlen(query);
    cout << "CLIENT: GET " << argv[3] << endl;
	if( (error = send(sockfd,query,len,0)) == -1){			// send the message to the server
		perror("client: send");
	}
	ofstream myfile;
	char *filename = get_filename(argv[3]);
	stringstream ss;
	ss << filename;

	myfile.open(ss.str().c_str(),ios::out | ios::binary);
	if(!myfile.is_open()){
		cout << "Could not open file in filesystem" << endl;
		exit(0);
	}
	bool header = true;
	bool first=true;
	while(1){
		num_bytes = recv(sockfd,buf,2000,0);			// receive message from server
		//printf("%d\n",num_bytes);
		if(first){
			stringstream tmp;
			tmp << buf;
			string line;
			getline(tmp,line);
			if(line.find("404")!=string::npos)
				cout << "SERVER: **ERROR** " << line << endl;
			else
				cout << "SERVER: " << line << endl;
			first = false;
		}
		int i=0;
		if(header){
			if(num_bytes>=4){
				for(i=3; i<num_bytes; i++){
					if(buf[i]=='\n' && buf[i-1]=='\r'){
						if(buf[i-2]=='\n' && buf[i-3]=='\r'){
							header=false;				// Detect the end of header and start storing data
							i++;
							break;
						}
					}
				}	
			}
		}
		if(!header)
			myfile.write(buf+i,num_bytes-i);			// Store received data in file 
		if(num_bytes == -1){
			perror("client: recv");
		}
		else if(num_bytes == 0){						// if number of bytes received is zero, it means server has closed the connection
			close(sockfd);
			cout << "CLIENT: File Saved at: " << filename << endl;	// Close the connection when server closes the connection
			printf("CLIENT: Connection closed by server\n");
			myfile.flush();
			myfile.close();
			break;							
		}
		myfile.flush();				// Flush the output file buffers
	}
	myfile.close();					// Close the output file
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x588, %rsp            # imm = 0x588
cmpl $0x4, %edi
jne 0x291f
movq %rsi, %r15
xorps %xmm0, %xmm0
leaq -0x220(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movabsq $0x100000002, %rax      # imm = 0x100000002
movq %rax, 0x4(%rdx)
movq 0x8(%rsi), %rdi
movq 0x10(%rsi), %rsi
leaq -0x40(%rbp), %r12
movq %r12, %rcx
callq 0x2160
testl %eax, %eax
jne 0x2983
leaq 0xa8d(%rip), %r14       # 0x3066
movq (%r12), %r12
testq %r12, %r12
je 0x2962
movl 0x4(%r12), %edi
movl 0x8(%r12), %esi
movl 0xc(%r12), %edx
callq 0x2060
cmpl $-0x1, %eax
jne 0x260d
movq %r14, %rdi
callq 0x2110
addq $0x28, %r12
jmp 0x25d9
movl %eax, %ebx
leaq -0x44(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x2230
cmpl $-0x1, %eax
je 0x29aa
movl $0x801, %edi            # imm = 0x801
callq 0x2190
movq %rax, %r14
movq 0x18(%r12), %rsi
movl 0x10(%r12), %edx
movl %ebx, %edi
callq 0x2100
cmpl $-0x1, %eax
je 0x2940
movq -0x40(%rbp), %rdi
callq 0x2170
movq 0x18(%r15), %rdi
callq 0x23ea
movq %rax, %r12
movq %rax, %rdi
callq 0x2080
movq %rax, %r13
movq 0x294f(%rip), %rdi      # 0x4fd0
leaq 0xa27(%rip), %rsi       # 0x30af
callq 0x2140
movq 0x18(%r15), %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
callq 0x20a0
movzwl %r13w, %edx
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x20b0
cmpl $-0x1, %eax
je 0x2951
leaq -0x420(%rbp), %rdi
callq 0x2210
movq 0x18(%r15), %rdi
callq 0x24c4
movq %rax, %r15
leaq -0x5a8(%rbp), %rdi
callq 0x20d0
leaq -0x598(%rbp), %rdi
movq %r15, %rsi
callq 0x2140
leaq -0x590(%rbp), %rsi
leaq -0x1f0(%rbp), %rdi
callq 0x2200
movq -0x1f0(%rbp), %rsi
leaq -0x420(%rbp), %rdi
pushq $0x14
popq %rdx
callq 0x2030
movq %r15, -0x38(%rbp)
movl %ebx, -0x2c(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x2090
leaq -0x3b0(%rbp), %rdi
callq 0x2050
testb %al, %al
je 0x29be
movb $0x1, %r12b
leaq -0x68(%rbp), %r15
movb $0x1, %bl
movl $0x7d0, %edx            # imm = 0x7D0
movl -0x2c(%rbp), %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2070
movq %rax, %r13
testb $0x1, %bl
je 0x27f9
leaq -0x1f0(%rbp), %rdi
callq 0x20d0
leaq -0x1e0(%rbp), %rdi
movq %r14, %rsi
callq 0x2140
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
leaq -0x1f0(%rbp), %rdi
movq %r15, %rsi
callq 0x2040
movq %r15, %rdi
leaq 0x942(%rip), %rsi       # 0x30eb
xorl %edx, %edx
callq 0x2260
cmpq $-0x1, %rax
leaq 0x934(%rip), %rsi       # 0x30ef
leaq 0x940(%rip), %rax       # 0x3102
cmoveq %rax, %rsi
movq 0x2803(%rip), %rdi      # 0x4fd0
callq 0x2140
movq %rax, %rdi
movq %r15, %rsi
callq 0x2130
movq %rax, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x2090
leaq -0x1f0(%rbp), %rdi
callq 0x20e0
xorl %eax, %eax
testb $0x1, %r12b
je 0x283f
cmpl $0x4, %r13d
jl 0x283f
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pushq $0x3
popq %rax
cmpq %rax, %rcx
je 0x283c
cmpb $0xa, (%r14,%rax)
jne 0x2837
cmpb $0xd, -0x1(%r14,%rax)
jne 0x2837
cmpb $0xa, -0x2(%r14,%rax)
jne 0x2837
cmpb $0xd, -0x3(%r14,%rax)
je 0x288b
incq %rax
jmp 0x2813
movl %r13d, %eax
testb $0x1, %r12b
jne 0x285f
cltq
leaq (%r14,%rax), %rsi
movl %r13d, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
leaq -0x420(%rbp), %rdi
callq 0x2180
cmpl $-0x1, %r13d
je 0x287d
testl %r13d, %r13d
je 0x2892
xorl %ebx, %ebx
leaq -0x420(%rbp), %rdi
callq 0x20c0
jmp 0x2746
leaq 0x887(%rip), %rdi       # 0x310b
callq 0x2110
jmp 0x286a
incl %eax
xorl %r12d, %r12d
jmp 0x2845
movl -0x2c(%rbp), %edi
callq 0x2250
movq 0x272f(%rip), %rdi      # 0x4fd0
leaq 0x870(%rip), %rsi       # 0x3118
callq 0x2140
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x2140
movq %rax, %rdi
callq 0x20a0
leaq 0x868(%rip), %rdi       # 0x3130
callq 0x21f0
leaq -0x420(%rbp), %rdi
callq 0x20c0
leaq -0x420(%rbp), %rdi
callq 0x21b0
leaq -0x420(%rbp), %rdi
callq 0x21b0
leaq -0x5a8(%rbp), %rdi
callq 0x20e0
leaq -0x420(%rbp), %rdi
callq 0x21d0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x588, %rsp            # imm = 0x588
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x26b2(%rip), %rax      # 0x4fd8
movq (%rax), %rcx
leaq 0x6f0(%rip), %rdi       # 0x3020
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
pushq $0x1
popq %rbx
jmp 0x290b
leaq 0x758(%rip), %rdi       # 0x309f
callq 0x2110
jmp 0x265a
leaq 0x764(%rip), %rdi       # 0x30bc
callq 0x2110
jmp 0x26ba
pushq $0x2
popq %rbx
movq 0x266c(%rip), %rax      # 0x4fd8
movq (%rax), %rcx
leaq 0x6ff(%rip), %rdi       # 0x3075
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
jmp 0x290b
movq 0x264e(%rip), %rcx      # 0x4fd8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x21a0
leaq 0x6b4(%rip), %rsi       # 0x304f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
jmp 0x29b6
leaq 0x6e3(%rip), %rdi       # 0x3094
callq 0x2110
pushq $0x1
popq %rdi
callq 0x2150
movq 0x260b(%rip), %rdi      # 0x4fd0
leaq 0x6fd(%rip), %rsi       # 0x30c9
callq 0x2140
movq %rax, %rdi
callq 0x20a0
xorl %edi, %edi
callq 0x2150
jmp 0x2a07
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
callq 0x2090
jmp 0x2a24
jmp 0x2a07
movq %rax, %rbx
jmp 0x2a30
jmp 0x2a07
jmp 0x2a07
jmp 0x2a07
movq %rax, %rbx
jmp 0x2a18
jmp 0x2a07
movq %rax, %rbx
jmp 0x2a24
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x2090
leaq -0x1f0(%rbp), %rdi
callq 0x20e0
leaq -0x5a8(%rbp), %rdi
callq 0x20e0
leaq -0x420(%rbp), %rdi
callq 0x21d0
movq %rbx, %rdi
callq 0x2220