// File: /workspace/github_cpp_makefile_star2_O1/akw0088[P]openssl/rsync_upload.c
// Function: rsync_file_upload
int rsync_file_upload(char *file, unsigned short port)
{
	int			connfd;
	unsigned int		size = sizeof(struct sockaddr_in);
	struct sockaddr_in	servaddr, client;
	time_t			ticks;
	int listenfd;


	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd == -1)
	{
		perror("socket error");
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(port);

	int ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	while (ret == -1)
	{
		perror("bind error");
		printf("Retrying in 5 seconds\r\n");
#ifdef WIN32
		Sleep(5000);
#else
		sleep(5);
#endif
		ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	}

	printf("Server listening on: %s:%d\n", inet_ntoa(servaddr.sin_addr), htons(servaddr.sin_port));

	if (listen(listenfd, 3) == -1)
	{
		perror("listen error");
		return -1;
	}

	for (;;)
	{
		char response[1024] = { 0 };

		printf("listening for connections...\n");
		connfd = accept(listenfd, (struct sockaddr *)&client, (socklen_t *)&size);
		if (connfd == INVALID_SOCKET)
			continue;

		ticks = time(NULL);
		snprintf(response, sizeof(response), "%.24s\r\n", ctime(&ticks));
		printf("Client: %s - %s", inet_ntoa(client.sin_addr), response);

		unsigned int file_size = 0;
		char *data = get_file(file, &file_size);
		if (data == NULL)
		{
			printf("Unable to open %s\r\n", file);
			closesocket(connfd);
			continue;
		}

		char file_name[PATH_SIZE] = { 0 };
		unsigned char file_hash[MD5_SIZE + 1] = {0};
		unsigned char rfile_hash[MD5_SIZE + 1] = {0};

		md5sum((char *)&data[0], file_size, (char *)file_hash);

		unsigned int rnum_block = 0;
		memcpy(file_name, file, MIN(PATH_SIZE - 1, strlen(file)));
		Send(connfd, (char *)&file_size, sizeof(int), 0);
		Send(connfd, (char *)&file_name, PATH_SIZE, 0);
		Send(connfd, (char *)&file_hash, MD5_SIZE, 0);
		if ( Recv(connfd, (char *)&rfile_hash, MD5_SIZE, 0) == -1)
		{
			printf("recv failed\r\n");
			closesocket(connfd);
			continue;
		}

		if (strcmp((char *)rfile_hash, (char *)file_hash) == 0)
		{
			printf("Files already match\r\n");
			closesocket(connfd);
			continue;
		}

		if ( Recv(connfd, (char *)&rnum_block, sizeof(int), 0) == -1)
		{
			printf("recv failed\r\n");
			closesocket(connfd);
			continue;
		}

		unsigned int rblock_size = 0;
		if ( Recv(connfd, (char *)&rblock_size, sizeof(int), 0) == -1)
		{
			printf("recv failed\r\n");
			closesocket(connfd);
			continue;
		}


		unsigned int *rchecksum_array = (unsigned int *)malloc(rnum_block * sizeof(int));
		if (rchecksum_array == NULL)
		{
			perror("malloc failed");
			closesocket(connfd);
			continue;
		}

		for (unsigned int i = 0; i < rnum_block; i++)
		{
			rchecksum_array[i] = -1;
		}

		printf("Received %d checksum array from client\r\n", rnum_block);
		if (rnum_block == 0)
		{
			printf("Dropping client as we got no blocks\r\n");
			closesocket(connfd);
			continue;
		}

		if ( Recv(connfd, (char *)rchecksum_array, rnum_block * sizeof(int), 0) == -1)
		{
			printf("recv failed\r\n");
			closesocket(connfd);
			continue;
		}

		char **rmd5_array = (char **)malloc(rnum_block * sizeof(char *));
		if (rmd5_array == NULL)
		{
			perror("malloc failed");
			closesocket(connfd);
			continue;
		}

		for (unsigned int i = 0; i < rnum_block; i++)
		{
			rmd5_array[i] = (char *)malloc(MD5_SIZE + 1 * sizeof(char));
			memset(rmd5_array[i], 0, MD5_SIZE + 1);

			if ( Recv(connfd, (char *)rmd5_array[i], MD5_SIZE, 0) == -1)
			{
				printf("recv failed\r\n");
				closesocket(connfd);
				continue;
			}

		}

		block_t *block = (block_t *)malloc(sizeof(block_t) * rnum_block);
		if (block == NULL)
		{
			perror("malloc failed");
			closesocket(connfd);
			continue;
		}
		printf("Searching for matches in %d blocks\r\n", rnum_block);
		
		unsigned int null_count = 0;
		for (unsigned int i = 0; i < rnum_block; i++)
		{
			block[i].offset = -1;
			block[i].length = 0;

			if (strcmp((char *)rmd5_array, "") == 0)
			{
				null_count++;
			}
		}

		int ret = 0;
		if (null_count != rnum_block)
		{
			// we dont send the last partial block as we can only scan blocks of a single length at a time
			ret = adler32_scan((unsigned char *)&data[0], file_size, rblock_size, rchecksum_array, rnum_block - 1, block, rmd5_array);
		}

		// set length of last block which is always transferred
		block[rnum_block - 1].offset = -1;
		block[rnum_block - 1].length = file_size - (rnum_block - 1) * rblock_size;


		printf("Sending block offsets\r\n");
		Send(connfd, (char *)block, rnum_block * sizeof(block_t), 0);

		// Remote side now knows where it's blocks go in new file
		// going to send all bytes in order of whats left starting from 0 and skipping whats in the block report
		
		printf("Determining data client is missing\r\n");
		for (unsigned int i = 0; i < rnum_block; i++)
		{
			if (block[i].offset == -1)
			{
				printf("\tClient missing block %d [%d bytes]\r\n", i, block[i].length);
			}
		}

		unsigned char *send_file = (unsigned char *)malloc(file_size);
		if (send_file == NULL)
		{
			perror("malloc failed");
			closesocket(connfd);
			continue;
		}

		int send_file_pos = 0;

		memset(send_file, 0, file_size);
		for(unsigned int i = 0; i < file_size; i++)
		{
			int skip = 0;


			for(unsigned int j = 0; j < rnum_block; j++)
			{
				if (block[j].length == 0)
				{
					continue;
				}

				if ( i >= block[j].offset && i < block[j].offset + rblock_size )
				{
					// we are in this block, skip to the end of this block
					i = block[j].offset + rblock_size - 1;
					skip = 1;
					break;
				}
			}
			
			if (skip)
			{
				// restart again as we move passed a block
				continue;
			}
			
			// This byte needs to be sent, move into transport block
			send_file[send_file_pos++] = data[i];
		}
		
		printf("Sending client missing chunk of size %d\r\n", send_file_pos);
		printf("Sending data\r\n");
		Send(connfd, (char *)&send_file_pos, sizeof(unsigned int), 0);		
		Send(connfd, (char *)send_file, send_file_pos, 0);
		closesocket(connfd);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x558, %rsp            # imm = 0x558
movl %esi, %ebp
movq %rdi, %r14
movl $0x10, 0x44(%rsp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1230
cmpl $-0x1, %eax
je 0x2c81
movl %eax, %r12d
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %bp
movw %bp, 0x2(%rsi)
movl %eax, %edi
movl $0x10, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x2c8a
movl 0x54(%rsp), %edi
callq 0x1090
movzwl 0x52(%rsp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x1cfa(%rip), %rdi      # 0x41fc
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e0
movl %r12d, %edi
movl $0x3, %esi
callq 0x1180
cmpl $-0x1, %eax
je 0x2c9c
leaq 0x150(%rsp), %r13
leaq 0x1fdc(%rip), %rbx      # 0x450d
leaq 0x34(%rsp), %rbp
movq %r14, 0x20(%rsp)
movl %r12d, 0x14(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x1100
movq %rbx, %rdi
callq 0x1070
movl %r12d, %edi
movq %rbp, %rsi
leaq 0x44(%rsp), %rdx
callq 0x11d0
cmpl $-0x1, %eax
je 0x2540
movl %eax, %r15d
xorl %edi, %edi
callq 0x1160
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x10b0
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdi
leaq 0x1c91(%rip), %rdx      # 0x4225
movq %rax, %rcx
xorl %eax, %eax
callq 0x10f0
movl 0x38(%rsp), %edi
callq 0x1090
leaq 0x1c7f(%rip), %rdi      # 0x422d
movq %rax, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x0, 0x10(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x191f
testq %rax, %rax
je 0x2762
xorps %xmm0, %xmm0
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
xorl %ebp, %ebp
movb %bpl, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movb %bpl, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movl 0x10(%rsp), %esi
movq %rax, 0x48(%rsp)
movq %rax, %rdi
leaq 0x70(%rsp), %rdx
callq 0x2d0c
movl $0x0, 0xc(%rsp)
movq %r14, %rdi
callq 0x10c0
cmpq $0x7f, %rax
movl $0x7f, %ecx
cmovaeq %rcx, %rax
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1150
cmpl $0x3, %ebp
ja 0x26c1
movl %ebp, %eax
leaq (%rsp,%rax), %rsi
addq $0x10, %rsi
movl $0x4, %edx
subl %ebp, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebp
testl %eax, %eax
jg 0x2691
xorl %ebx, %ebx
cmpl $0x7f, %ebx
ja 0x26f6
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0xd0, %rsi
movl $0x80, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebx
testl %eax, %eax
jg 0x26c3
xorl %ebx, %ebx
cmpl $0x1f, %ebx
ja 0x2728
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0x70, %rsi
movl $0x20, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebx
testl %eax, %eax
jg 0x26f8
xorl %ebp, %ebp
cmpl $0x1f, %ebp
ja 0x2780
movl %ebp, %eax
leaq (%rsp,%rax), %rsi
addq $0xa0, %rsi
movl $0x20, %edx
subl %ebp, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebp
testl %eax, %eax
cmovlel %eax, %ebx
jg 0x272a
jmp 0x2782
leaq 0x1959(%rip), %rdi      # 0x40c2
movq %r14, %rsi
xorl %eax, %eax
callq 0x10e0
movl %r15d, %edi
callq 0x1110
jmp 0x2540
movl %ebp, %ebx
cmpl $-0x1, %ebx
je 0x2bbe
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1130
testl %eax, %eax
je 0x27d8
xorl %ebx, %ebx
cmpl $0x3, %ebx
ja 0x27e4
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0xc, %rsi
movl $0x4, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebx
testl %eax, %eax
cmovlel %eax, %ebp
jg 0x27a3
jmp 0x27e6
leaq 0x1db9(%rip), %rdi      # 0x4598
jmp 0x2bc5
movl %ebx, %ebp
cmpl $-0x1, %ebp
je 0x2bbe
movl $0x0, 0x1c(%rsp)
xorl %ebp, %ebp
cmpl $0x3, %ebp
ja 0x282e
movl %ebp, %eax
leaq (%rsp,%rax), %rsi
addq $0x1c, %rsi
movl $0x4, %edx
subl %ebp, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebp
testl %eax, %eax
cmovlel %eax, %ebx
jg 0x27f9
jmp 0x2830
movl %ebp, %ebx
cmpl $-0x1, %ebx
je 0x2bbe
movl 0xc(%rsp), %r14d
leaq (,%r14,4), %r13
movq %r13, %rdi
callq 0x1170
movq %rax, 0x28(%rsp)
testq %rax, %rax
leaq 0x1cb0(%rip), %rbx      # 0x450d
leaq 0x34(%rsp), %rbp
je 0x2c1a
testq %r14, %r14
je 0x287f
movq 0x28(%rsp), %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x1100
leaq 0x19b7(%rip), %rdi      # 0x423d
movl %r14d, %esi
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %edx
testl %edx, %edx
movl 0x14(%rsp), %r12d
je 0x2be3
shll $0x2, %edx
movl %r15d, %edi
movq 0x28(%rsp), %rsi
xorl %ecx, %ecx
callq 0x1804
cmpl $-0x1, %eax
movq 0x20(%rsp), %r14
leaq 0x150(%rsp), %r13
je 0x2c09
movl 0xc(%rsp), %edi
shlq $0x3, %rdi
callq 0x1170
testq %rax, %rax
je 0x2c70
movq %rax, %rbp
movl 0xc(%rsp), %r14d
testl %r14d, %r14d
je 0x2970
xorl %r13d, %r13d
movl $0x21, %edi
callq 0x1170
movq %rax, %r14
movq %rax, (%rbp,%r13,8)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movb $0x0, 0x20(%rax)
xorl %ebx, %ebx
cmpl $0x1f, %ebx
ja 0x2946
movl %ebx, %esi
addq %r14, %rsi
movl $0x20, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebx
testl %eax, %eax
cmovlel %eax, %r12d
jg 0x2915
jmp 0x2949
movl %ebx, %r12d
cmpl $-0x1, %r12d
jne 0x2963
leaq 0x1c57(%rip), %rdi      # 0x45ad
callq 0x1070
movl %r15d, %edi
callq 0x1110
incq %r13
movl 0xc(%rsp), %r14d
cmpq %r14, %r13
jb 0x28f3
movl %r14d, %edi
shlq $0x3, %rdi
callq 0x1170
testq %rax, %rax
je 0x2c45
movq %rax, %r13
xorl %ebx, %ebx
leaq 0x18d5(%rip), %rdi      # 0x4266
movl %r14d, %esi
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %r8d
testq %r8, %r8
je 0x29c7
movb (%rbp), %al
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %r8
jne 0x29af
testb %al, %al
movl $0x0, %ebx
cmovel %r8d, %ebx
cmpl %r8d, %ebx
je 0x29ed
movl 0x10(%rsp), %esi
movl 0x1c(%rsp), %edx
decl %r8d
movq %rbp, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq %r13, %r9
callq 0x14bb
movl 0xc(%rsp), %eax
decl %eax
movl $0xffffffff, (%r13,%rax,8) # imm = 0xFFFFFFFF
movl 0x10(%rsp), %ecx
movl %eax, %edx
imull 0x1c(%rsp), %edx
subl %edx, %ecx
movl %ecx, 0x4(%r13,%rax,8)
leaq 0x1b15(%rip), %rdi      # 0x452a
callq 0x1070
movl 0xc(%rsp), %edx
shll $0x3, %edx
movl %r15d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1890
leaq 0x1b0c(%rip), %rdi      # 0x4541
callq 0x1070
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
je 0x2a6f
xorl %r14d, %r14d
cmpl $-0x1, (%r13,%r14,8)
jne 0x2a63
movl 0x4(%r13,%r14,8), %edx
leaq 0x1832(%rip), %rdi      # 0x428b
movl %r14d, %esi
xorl %eax, %eax
callq 0x10e0
incq %r14
movl 0xc(%rsp), %ebp
cmpq %rbp, %r14
jb 0x2a45
movl 0x10(%rsp), %r14d
movq %r14, %rdi
callq 0x1170
testq %rax, %rax
je 0x2c45
movl $0x0, 0x18(%rsp)
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1100
testq %r14, %r14
je 0x2b47
movl 0x18(%rsp), %edx
testl %ebp, %ebp
setne %al
movl 0x1c(%rsp), %ecx
movl %ebp, %esi
movq %rsi, %rdi
negq %rdi
xorl %r8d, %r8d
testl %ebp, %ebp
je 0x2b0c
movl $0x1, %r10d
movl %eax, %r9d
cmpl $0x0, -0x4(%r13,%r10,8)
je 0x2af3
movl -0x8(%r13,%r10,8), %r11d
cmpl %r11d, %r8d
setae %bl
addl %ecx, %r11d
cmpl %r11d, %r8d
setb %r12b
andb %bl, %r12b
cmpb $0x1, %r12b
je 0x2b11
cmpq %rsi, %r10
setb %r9b
leaq (%rdi,%r10), %r11
incq %r11
incq %r10
cmpq $0x1, %r11
jne 0x2acd
jmp 0x2b17
movl %eax, %r9d
jmp 0x2b17
decl %r11d
movl %r11d, %r8d
testb $0x1, %r9b
jne 0x2b3b
movl %r8d, %r9d
movq 0x48(%rsp), %r10
movb (%r10,%r9), %r9b
movslq %edx, %r10
incl %edx
movl %edx, 0x18(%rsp)
movq 0x28(%rsp), %r11
movb %r9b, (%r11,%r10)
incl %r8d
cmpl %r14d, %r8d
jb 0x2ac0
movl 0x18(%rsp), %esi
xorl %ebp, %ebp
leaq 0x175d(%rip), %rdi      # 0x42b1
xorl %eax, %eax
callq 0x10e0
leaq 0x1a03(%rip), %rdi      # 0x4565
callq 0x1070
movq 0x20(%rsp), %r14
movl 0x14(%rsp), %r12d
leaq 0x150(%rsp), %r13
cmpl $0x3, %ebp
ja 0x2ba9
movl %ebp, %eax
leaq (%rsp,%rax), %rsi
addq $0x18, %rsi
movl $0x4, %edx
subl %ebp, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
addl %ecx, %ebp
testl %eax, %eax
jg 0x2b79
movl 0x18(%rsp), %edx
movl %r15d, %edi
movq 0x28(%rsp), %rsi
xorl %ecx, %ecx
callq 0x1890
jmp 0x2bca
leaq 0x19e8(%rip), %rdi      # 0x45ad
callq 0x1070
movl %r15d, %edi
callq 0x1110
leaq 0x1934(%rip), %rbx      # 0x450d
leaq 0x34(%rsp), %rbp
jmp 0x2540
leaq 0x1989(%rip), %rdi      # 0x4573
callq 0x1070
movl %r15d, %edi
callq 0x1110
movq 0x20(%rsp), %r14
leaq 0x150(%rsp), %r13
jmp 0x2540
leaq 0x199d(%rip), %rdi      # 0x45ad
callq 0x1070
jmp 0x2773
leaq 0x1523(%rip), %rdi      # 0x4144
callq 0x11c0
movl %r15d, %edi
callq 0x1110
movq 0x20(%rsp), %r14
movl 0x14(%rsp), %r12d
leaq 0x150(%rsp), %r13
jmp 0x2540
leaq 0x14f8(%rip), %rdi      # 0x4144
callq 0x11c0
movl %r15d, %edi
callq 0x1110
movq 0x20(%rsp), %r14
movl 0x14(%rsp), %r12d
leaq 0x150(%rsp), %r13
jmp 0x2bd2
leaq 0x14cd(%rip), %rdi      # 0x4144
callq 0x11c0
jmp 0x2bca
leaq 0x155c(%rip), %rdi      # 0x41e4
jmp 0x2ca3
leaq 0x50(%rsp), %rsi
movl %r12d, %edi
callq 0x1250
jmp 0x24e6
leaq 0x1575(%rip), %rdi      # 0x4218
callq 0x11c0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x558, %rsp            # imm = 0x558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq