// File: parth-shel[P]xpac/client_socket.cpp
// Function: recv_file(int, char*)
int recv_file(int sock, char* file_name) {
 	char send_str [MAX_SEND_BUF]; // message to be sent to server
 	int fd; // file descriptor for receiving file
 	ssize_t sent_bytes, rcvd_bytes, rcvd_file_size;
 	int recv_count; // count of recv() calls
 	char recv_str[MAX_RECV_BUF]; // buffer to hold received data 
 	size_t send_strlen; // length of transmitted string

 	sprintf(send_str, "%s\n", file_name); 
 	send_strlen = strlen(send_str); /* length of message to be transmitted */
 	if( (sent_bytes = send(sock, send_str, send_strlen, 0)) < 0 ) {
 		perror("send error");
 		return -1;
 	}
 

 	// create file to save received data
 	if ( (fd = open(file_name, O_WRONLY|O_CREAT, 0666)) < 0 ) {
 		perror("error creating file");
 		return -1;
 	}

 	recv_count = 0; // number of recv() calls required to receive the file
 	rcvd_file_size = 0; // size of received file

 	// continue receiving file
 	while ( (rcvd_bytes = recv(sock, recv_str, MAX_RECV_BUF, 0)) > 0 ) {
 		recv_count++;
 		rcvd_file_size += rcvd_bytes;

		if(!strcmp(recv_str,errmsg_notfound)){
			printf("File not found on xpac repo!\nPlease check file name and try again!");
			return -1;
		}
 		if (write(fd, recv_str, rcvd_bytes) < 0 ) {
 			perror("error writing to file");
 			return -1;
 		}
 	}
 	
	close(fd); // close file
 	printf("Client Received: %zu bytes in %d recv(s)\n", rcvd_file_size, recv_count);
 	return rcvd_file_size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x487a(%rip), %rsi      # 0x1575e
movb $0x0, %al
callq 0x40d0
leaq -0x110(%rbp), %rdi
callq 0x41e0
movq %rax, -0x248(%rbp)
movl -0x8(%rbp), %edi
leaq -0x110(%rbp), %rsi
movq -0x248(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4250
movq %rax, -0x120(%rbp)
cmpq $0x0, %rax
jge 0x10f3b
leaq 0x4838(%rip), %rdi      # 0x15762
callq 0x4380
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1106b
movq -0x10(%rbp), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x4330
movl %eax, -0x114(%rbp)
cmpl $0x0, %eax
jge 0x10f73
leaq 0x480b(%rip), %rdi      # 0x1576d
callq 0x4380
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1106b
movl $0x0, -0x134(%rbp)
movq $0x0, -0x130(%rbp)
movl -0x8(%rbp), %edi
leaq -0x240(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
xorl %ecx, %ecx
callq 0x4180
movq %rax, -0x128(%rbp)
cmpq $0x0, %rax
jle 0x1103b
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
movq -0x128(%rbp), %rax
addq -0x130(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x240(%rbp), %rdi
movq 0xd427(%rip), %rsi      # 0x1e408
callq 0x4590
cmpl $0x0, %eax
jne 0x11002
leaq 0x478f(%rip), %rdi      # 0x15781
movb $0x0, %al
callq 0x4070
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1106b
movl -0x114(%rbp), %edi
leaq -0x240(%rbp), %rsi
movq -0x128(%rbp), %rdx
callq 0x4190
cmpq $0x0, %rax
jge 0x11036
leaq 0x479c(%rip), %rdi      # 0x157c4
callq 0x4380
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1106b
jmp 0x10f88
movl -0x114(%rbp), %edi
callq 0x47a0
movq -0x130(%rbp), %rsi
movl -0x134(%rbp), %edx
leaq 0x4780(%rip), %rdi      # 0x157da
movb $0x0, %al
callq 0x4070
movq -0x130(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
nopw (%rax,%rax)