// File: SamKG[P]Flush-Reload-Sidechannel/sender.cpp
// Function: main
int main(int argc, char **argv)
{
	// Put your covert channel setup code here
	int fd = open("sender",O_RDONLY);
	mapped = (char*) mmap(NULL, 0x20000, PROT_READ, MAP_PRIVATE, fd, 0);
	int mult = 15;
	sendbit_arr[0] = ADDR_0 + mult*CACHE_LINE_SIZE;
	sendbit_arr[1] = ADDR_1 + mult*CACHE_LINE_SIZE;
	sendbit_arr[2] = ADDR_2 + mult*CACHE_LINE_SIZE;
	sendbit_arr[3] = ADDR_3 + mult*CACHE_LINE_SIZE;
	sendbit_arr[4] = ADDR_4 + mult*CACHE_LINE_SIZE;
	sendbit_arr[5] = ADDR_5 + mult*CACHE_LINE_SIZE;
	sendbit_arr[6] = ADDR_6 + mult*CACHE_LINE_SIZE;
	sendbit_arr[7] = ADDR_7 + mult*CACHE_LINE_SIZE;
	sendbit_arr[8] = ADDR_8 + mult*CACHE_LINE_SIZE;
	printf("Please type a message.\n");
	
	bool sending = true;
	while (sending) {
		char text_buf[128];
		fgets(text_buf, sizeof(text_buf), stdin);
		clock_t begin = clock();
		for (int i = 0 ; i < 128 ; i++){
			if (text_buf[i] == '\0'){
				break;
			}
			int c = text_buf[i];	
			CYCLES start_t = get_highres_time();
			while(get_highres_time() - start_t < SLOT_HIT_CUTOFF*TIME_SLOT*SENDER_PULSE){//pulse for 1000 cycles
				send_bit(c,0);
				send_bit(c,1);
				send_bit(c,2);
				send_bit(c,3);
				send_bit(c,4);
				send_bit(c,5);
				send_bit(c,6);
				send_bit(c,7);
				send_bit(c,8);
			}
			start_t = get_highres_time();
        		while(get_highres_time() - start_t <= TIME_SLOT*100){
			}
		}		
		clock_t end = clock();
		printf("TOTAL TIME SPENT: %f\n",((double)end - begin)/CLOCKS_PER_SEC);
		// Put your covert channel code here
	}

	printf("Sender finished.\n");

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x1060(%rip), %rdi      # 0xc010
xorl %esi, %esi
movb $0x0, %al
callq 0x1050
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x1, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x307d(%rip)      # 0xe060
movl $0xf, -0x18(%rbp)
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x1176, %eax           # imm = 0x1176
cltq
movq %rax, 0x3072(%rip)      # 0xe070
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x22fd, %eax           # imm = 0x22FD
cltq
movq %rax, 0x3066(%rip)      # 0xe078
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x3484, %eax           # imm = 0x3484
cltq
movq %rax, 0x305a(%rip)      # 0xe080
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x460b, %eax           # imm = 0x460B
cltq
movq %rax, 0x304e(%rip)      # 0xe088
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x5792, %eax           # imm = 0x5792
cltq
movq %rax, 0x3042(%rip)      # 0xe090
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x6919, %eax           # imm = 0x6919
cltq
movq %rax, 0x3036(%rip)      # 0xe098
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x7aa0, %eax           # imm = 0x7AA0
cltq
movq %rax, 0x302a(%rip)      # 0xe0a0
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x8c27, %eax           # imm = 0x8C27
cltq
movq %rax, 0x301e(%rip)      # 0xe0a8
movl -0x18(%rbp), %eax
shll $0x6, %eax
addl $0x9dae, %eax           # imm = 0x9DAE
cltq
movq %rax, 0x3012(%rip)      # 0xe0b0
leaq 0xf72(%rip), %rdi       # 0xc017
movb $0x0, %al
callq 0x1030
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
je 0xb279
leaq -0xa0(%rbp), %rdi
movq 0x2f18(%rip), %rax      # 0xdfe0
movq (%rax), %rdx
movl $0x80, %esi
callq 0x1060
callq 0x1040
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
cmpl $0x80, -0xac(%rbp)
jge 0xb238
movslq -0xac(%rbp), %rax
movsbl -0xa0(%rbp,%rax), %eax
cmpl $0x0, %eax
jne 0xb114
jmp 0xb238
movslq -0xac(%rbp), %rax
movsbl -0xa0(%rbp,%rax), %eax
movl %eax, -0xb0(%rbp)
callq 0xb320
movq %rax, -0xb8(%rbp)
callq 0xb320
movq -0xb8(%rbp), %rcx
subq %rcx, %rax
cmpq $0x4e20, %rax           # imm = 0x4E20
jae 0xb1fd
movl -0xb0(%rbp), %eax
xorl %esi, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x1, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x2, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x3, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x4, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x5, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x6, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x7, %esi
movsbl %al, %edi
callq 0xb2a0
movl -0xb0(%rbp), %eax
movl $0x8, %esi
movsbl %al, %edi
callq 0xb2a0
jmp 0xb135
callq 0xb320
movq %rax, -0xb8(%rbp)
callq 0xb320
movq -0xb8(%rbp), %rcx
subq %rcx, %rax
cmpq $0x3d090, %rax          # imm = 0x3D090
ja 0xb222
jmp 0xb209
jmp 0xb224
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xb0eb
callq 0x1040
movq %rax, -0xc0(%rbp)
cvtsi2sdq -0xc0(%rbp), %xmm0
cvtsi2sdq -0xa8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd 0xda6(%rip), %xmm1      # 0xc008
divsd %xmm1, %xmm0
leaq 0xdc2(%rip), %rdi       # 0xc02f
movb $0x1, %al
callq 0x1030
jmp 0xb0b0
leaq 0xdc5(%rip), %rdi       # 0xc045
movb $0x0, %al
callq 0x1030
xorl %eax, %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)