// File: /workspace/github_cpp_makefile_star2_O2/SamKG[P]Flush-Reload-Sidechannel/receiver.cpp
// Function: main
int main(int argc, char **argv)
{
	// Put your covert channel setup code here
	int fd = open("sender",O_RDONLY);
	char* mapped = (char*) mmap(NULL, 0x20000, PROT_READ, MAP_PRIVATE, fd, 0);
	// I think using uint64_t prevents prefetching
	int num_ptrs = 0;
	ADDR_PTR probe_ptrs[100];
	int mult = 15;
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_0 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_1 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_2 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_3 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_4 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_5 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_6 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_7 + mult*CACHE_LINE_SIZE);
	probe_ptrs[num_ptrs++] = (uint64_t)(mapped + ADDR_8 + mult*CACHE_LINE_SIZE);
	printf("Please press enter.\n");

	char text_buf[2];
	fgets(text_buf, sizeof(text_buf), stdin);

	printf("Receiver now listening.\n");
	int hitcnt = 0;
	bool listening = true;
	int hits[100];
	while (listening) {
		//spy flushes
		for (int i = 0 ; i < 100 ; i++){
			hits[i] = 0;
		}
		//spy waits TIME_SLOT time
		for (int i = 0 ; i < SLOT_WINDOW_COUNT;  i++){
			for (int i = 0 ; i < num_ptrs ; i++){
				flush_one_block(probe_ptrs[i]);
			}
			CYCLES start_t = get_highres_time();
			while(get_highres_time() - start_t <= TIME_SLOT){
			}
			//spy reloads
			for (int i = 0 ; i < num_ptrs ; i++){
				CYCLES time = probe_one_block(probe_ptrs[i]);
				if ( time < TIME_CUTOFF){
					hits[i]++;
					//printf("HIT %d\t%d\t%d\n",i,hitcnt++,time);
				}
			}
		}
		
		if (hits[0] >= SLOT_HIT_CUTOFF){
			// 0 bit means we have a message!
			int bitstr = 0;
			for (int i = 1 ; i < num_ptrs ; i++){
				if (hits[i] >= SLOT_HIT_CUTOFF){
					//printf("HIT BIT %d\n",i);
					bitstr |= 1<<(i-1);	
				}
			}
			printf("%c",(char)bitstr);
			fflush(stdout);
		}

	}

	printf("Receiver finished.\n");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x4d0, %rsp            # imm = 0x4D0
leaq 0xe62(%rip), %rdi       # 0x2004
xorl %r14d, %r14d
xorl %esi, %esi
xorl %eax, %eax
callq 0x1030
pushq $0x1
popq %rbx
pushq $0x2
popq %rbp
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %ebx, %edx
movl %ebp, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1080
leaq 0x1536(%rax), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x26bd(%rax), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x3844(%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x49cb(%rax), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x5b52(%rax), %rcx
movq %rcx, 0x40(%rsp)
leaq 0x6cd9(%rax), %rcx
movq %rcx, 0x48(%rsp)
leaq 0x7e60(%rax), %rcx
movq %rcx, 0x50(%rsp)
leaq 0x8fe7(%rax), %rcx
movq %rcx, 0x58(%rsp)
addq $0xa16e, %rax           # imm = 0xA16E
movq %rax, 0x60(%rsp)
leaq 0xdcf(%rip), %rdi       # 0x200b
callq 0x1060
movq 0x2d98(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
leaq 0xe(%rsp), %rdi
movl %ebp, %esi
callq 0x1070
leaq 0xdc1(%rip), %rdi       # 0x201f
callq 0x1060
movq 0x2d5e(%rip), %r15      # 0x3fc8
xorl %eax, %eax
cmpq $0x64, %rax
jne 0x12e3
xorl %ebp, %ebp
cmpl $0xa, %ebp
je 0x12f3
xorl %r12d, %r12d
cmpq $0x9, %r12
je 0x1291
movq 0x20(%rsp,%r12,8), %rdi
callq 0x13d2
incq %r12
jmp 0x127c
callq 0x135e
movq %rax, 0x18(%rsp)
callq 0x135e
subq 0x18(%rsp), %rax
cmpq $0x9c5, %rax            # imm = 0x9C5
jb 0x129b
xorl %r12d, %r12d
cmpq $0x9, %r12
je 0x12df
movq 0x20(%rsp,%r12,8), %rdi
callq 0x1382
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq $0xc7, %rax
ja 0x12da
incl 0x340(%rsp,%r12,4)
incq %r12
jmp 0x12b0
incl %ebp
jmp 0x1274
andl $0x0, 0x340(%rsp,%rax,4)
incq %rax
jmp 0x126c
cmpl $0x2, 0x340(%rsp)
movl $0x0, %eax
jl 0x126c
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x8, %rcx
je 0x1327
movl %ebx, %edx
shll %cl, %edx
cmpl $0x2, 0x344(%rsp,%rcx,4)
cmovll %r14d, %edx
orl %edx, %eax
incq %rcx
jmp 0x130a
movsbl %al, %edi
callq 0x1050
movq (%r15), %rdi
callq 0x1040
jmp 0x126a