// File: /workspace/github_cpp_makefile_star2_O3/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 $0x548, %rsp            # imm = 0x548
movl %esi, %ebx
movq %rdi, %r12
movl $0x10, 0x3c(%rsp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1240
cmpl $-0x1, %eax
je 0x2ac3
movl %eax, %r13d
leaq 0x50(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %bx
movw %bx, 0x2(%rsi)
movl %eax, %edi
movl $0x10, %edx
callq 0x11b0
cmpl $-0x1, %eax
je 0x2acc
movl 0x54(%rsp), %edi
callq 0x1090
movzwl 0x52(%rsp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x1dd2(%rip), %rdi      # 0x41fc
movq %rax, %rsi
xorl %eax, %eax
callq 0x10e0
movl %r13d, %edi
movl $0x3, %esi
callq 0x1190
cmpl $-0x1, %eax
je 0x2ade
leaq 0x140(%rsp), %rbp
leaq 0x20b4(%rip), %r14      # 0x450d
leaq 0x20(%rsp), %rbx
movq %r12, 0x30(%rsp)
movl %r13d, 0x18(%rsp)
movl $0x400, %edx            # imm = 0x400
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1100
movq %r14, %rdi
callq 0x1070
movl %r13d, %edi
movq %rbx, %rsi
leaq 0x3c(%rsp), %rdx
callq 0x11e0
cmpl $-0x1, %eax
je 0x2468
movl %eax, %r15d
xorl %edi, %edi
callq 0x1170
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x10b0
movl $0x400, %esi            # imm = 0x400
movq %rbp, %rdi
leaq 0x1d69(%rip), %rdx      # 0x4225
movq %rax, %rcx
xorl %eax, %eax
callq 0x10f0
movl 0x24(%rsp), %edi
callq 0x1090
leaq 0x1d57(%rip), %rdi      # 0x422d
movq %rax, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x0, 0x10(%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x190f
testq %rax, %rax
je 0x265d
xorps %xmm0, %xmm0
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)
movaps %xmm0, 0xc0(%rsp)
xorl %ebx, %ebx
movb %bl, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movb %bl, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movl 0x10(%rsp), %esi
movq %rax, 0x40(%rsp)
movq %rax, %rdi
leaq 0x60(%rsp), %rdx
callq 0x2b50
movl $0x0, 0xc(%rsp)
movq %r12, %rdi
callq 0x10c0
cmpq $0x7f, %rax
movl $0x7f, %ecx
cmovaeq %rcx, %rax
leaq 0xc0(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1160
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0x10, %rsi
movl $0x4, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
jle 0x25da
addl %eax, %ebx
cmpl $0x4, %ebx
jl 0x25b4
xorl %ebx, %ebx
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0xc0, %rsi
movl $0x80, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
jle 0x2608
addl %eax, %ebx
cmpl $0x80, %ebx
jl 0x25dc
xorl %ebx, %ebx
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0x60, %rsi
movl $0x20, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
jle 0x2630
addl %eax, %ebx
cmpl $0x20, %ebx
jl 0x260a
xorl %ebx, %ebx
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0x90, %rsi
movl $0x20, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
jle 0x267b
addl %eax, %ebx
cmpl $0x20, %ebx
jb 0x2632
jmp 0x2684
leaq 0x1a5e(%rip), %rdi      # 0x40c2
movq %r12, %rsi
xorl %eax, %eax
callq 0x10e0
movl %r15d, %edi
callq 0x1110
jmp 0x2468
cmpl $-0x1, %eax
je 0x280e
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1140
testl %eax, %eax
je 0x26c4
xorl %ebx, %ebx
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
jle 0x26d0
addl %eax, %ebx
cmpl $0x4, %ebx
jb 0x269c
jmp 0x26d9
leaq 0x1ecd(%rip), %rdi      # 0x4598
jmp 0x2815
cmpl $-0x1, %eax
je 0x280e
movl $0x0, 0x14(%rsp)
xorl %ebx, %ebx
movl %ebx, %eax
leaq (%rsp,%rax), %rsi
addq $0x14, %rsi
movl $0x4, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
jle 0x270b
addl %eax, %ebx
cmpl $0x4, %ebx
jb 0x26e3
jmp 0x2714
cmpl $-0x1, %eax
je 0x280e
movl 0xc(%rsp), %ebx
leaq (,%rbx,4), %r14
movq %r14, %rdi
callq 0x1180
testq %rax, %rax
je 0x2a76
movq %rax, %rbp
testl %ebx, %ebx
je 0x2748
movq %rbp, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x1100
leaq 0x1aee(%rip), %rdi      # 0x423d
movl %ebx, %esi
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %edx
testl %edx, %edx
leaq 0x1da8(%rip), %r14      # 0x450d
je 0x282c
shll $0x2, %edx
movl %r15d, %edi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x1804
cmpl $-0x1, %eax
je 0x2835
movl 0xc(%rsp), %ebx
leaq (,%rbx,8), %rdi
callq 0x1180
testq %rax, %rax
je 0x2a76
movq %rax, %r14
testl %ebx, %ebx
je 0x285b
xorl %r12d, %r12d
movl $0x1, %edi
movl $0x21, %esi
callq 0x1120
movq %rax, %r13
movq %rax, (%r14,%r12,8)
xorl %ebx, %ebx
movl %ebx, %esi
addq %r13, %rsi
movl $0x20, %edx
subl %ebx, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x1040
testl %eax, %eax
jle 0x27e7
addl %eax, %ebx
cmpl $0x20, %ebx
jb 0x27c4
jmp 0x2800
cmpl $-0x1, %eax
jne 0x2800
leaq 0x1dba(%rip), %rdi      # 0x45ad
callq 0x1070
movl %r15d, %edi
callq 0x1110
incq %r12
movl 0xc(%rsp), %ebx
cmpq %rbx, %r12
jb 0x27ac
jmp 0x285d
leaq 0x1d98(%rip), %rdi      # 0x45ad
callq 0x1070
movl %r15d, %edi
callq 0x1110
leaq 0x20(%rsp), %rbx
jmp 0x2468
leaq 0x1d40(%rip), %rdi      # 0x4573
jmp 0x283c
leaq 0x1d71(%rip), %rdi      # 0x45ad
callq 0x1070
movl %r15d, %edi
callq 0x1110
leaq 0x140(%rsp), %rbp
leaq 0x20(%rsp), %rbx
jmp 0x2468
xorl %ebx, %ebx
movl %ebx, %edi
shlq $0x3, %rdi
callq 0x1180
testq %rax, %rax
je 0x2a8c
movq %rax, %r13
xorl %r12d, %r12d
leaq 0x19e8(%rip), %rdi      # 0x4266
movl %ebx, %esi
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %eax
testq %rax, %rax
movq 0x40(%rsp), %rdi
je 0x28b8
movb (%r14), %cl
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rsi, (%r13,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x289f
testb %cl, %cl
movl $0x0, %r12d
cmovel %eax, %r12d
cmpl %eax, %r12d
je 0x28dc
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
leal -0x1(%rax), %r8d
movq %r14, (%rsp)
movq %rbp, %rcx
movq %r13, %r9
callq 0x14b2
movl 0xc(%rsp), %eax
leaq 0x19a8(%rip), %r12      # 0x428b
decl %eax
movl $0xffffffff, (%r13,%rax,8) # imm = 0xFFFFFFFF
movl 0x10(%rsp), %ecx
movl %eax, %edx
imull 0x14(%rsp), %edx
subl %edx, %ecx
movl %ecx, 0x4(%r13,%rax,8)
leaq 0x1c23(%rip), %rdi      # 0x452a
callq 0x1070
movl 0xc(%rsp), %edx
shll $0x3, %edx
xorl %ebx, %ebx
movl %r15d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1888
leaq 0x1c18(%rip), %rdi      # 0x4541
callq 0x1070
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x2963
movl %eax, %ebx
xorl %r14d, %r14d
cmpl $-0x1, (%r13,%r14,8)
jne 0x2959
movl 0x4(%r13,%r14,8), %edx
movq %r12, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x10e0
movl 0xc(%rsp), %ebx
incq %r14
movl %ebx, %eax
cmpq %rax, %r14
jb 0x293b
movl 0x10(%rsp), %ebp
movl $0x1, %edi
movq %rbp, %rsi
callq 0x1120
testq %rax, %rax
je 0x2a8c
movq %rax, %r14
movl $0x0, 0x1c(%rsp)
xorl %r12d, %r12d
movl $0x0, %esi
testl %ebp, %ebp
movq 0x40(%rsp), %r11
je 0x29f6
movl 0x14(%rsp), %eax
movl %ebx, %ecx
xorl %edx, %edx
xorl %esi, %esi
testl %ebx, %ebx
je 0x29d4
xorl %edi, %edi
cmpl $0x0, 0x4(%r13,%rdi,8)
je 0x29cc
movl (%r13,%rdi,8), %r8d
cmpl %r8d, %edx
setae %r9b
addl %eax, %r8d
cmpl %r8d, %edx
setb %r10b
testb %r10b, %r9b
jne 0x29ee
incq %rdi
cmpq %rdi, %rcx
jne 0x29a9
movl %edx, %edi
movb (%r11,%rdi), %dil
movslq %esi, %r8
incl %esi
movl %esi, 0x1c(%rsp)
movb %dil, (%r14,%r8)
incl %edx
movl %edx, %r8d
jmp 0x29f1
movl %r8d, %edx
cmpl %ebp, %r8d
jb 0x29a3
leaq 0x18b4(%rip), %rdi      # 0x42b1
xorl %eax, %eax
callq 0x10e0
leaq 0x1b5a(%rip), %rdi      # 0x4565
callq 0x1070
movl 0x18(%rsp), %r13d
leaq 0x140(%rsp), %rbp
leaq 0x20(%rsp), %rbx
movl %r12d, %eax
leaq (%rsp,%rax), %rsi
addq $0x1c, %rsi
movl $0x4, %edx
subl %r12d, %edx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x10d0
testl %eax, %eax
jle 0x2a4c
addl %eax, %r12d
cmpl $0x4, %r12d
jl 0x2a22
movl 0x1c(%rsp), %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1888
movl %r15d, %edi
callq 0x1110
movq 0x30(%rsp), %r12
leaq 0x1a9c(%rip), %r14      # 0x450d
jmp 0x2468
leaq 0x16c7(%rip), %rdi      # 0x4144
callq 0x11d0
movl %r15d, %edi
callq 0x1110
jmp 0x2aaa
leaq 0x16b1(%rip), %rdi      # 0x4144
callq 0x11d0
movl %r15d, %edi
callq 0x1110
movq 0x30(%rsp), %r12
movl 0x18(%rsp), %r13d
leaq 0x140(%rsp), %rbp
leaq 0x1a54(%rip), %r14      # 0x450d
leaq 0x20(%rsp), %rbx
jmp 0x2468
leaq 0x171a(%rip), %rdi      # 0x41e4
jmp 0x2ae5
leaq 0x50(%rsp), %rsi
movl %r13d, %edi
callq 0x1260
jmp 0x240e
leaq 0x1733(%rip), %rdi      # 0x4218
callq 0x11d0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq