// File: /workspace/github_c_makefile_star2_O2/WereCatf[P]Gonzales/bench.c
// Function: main
int main(int argc, char* argv[])
{
	struct timespec timeStart, timeEnd;
	uint32_t counter;
	double accum;

	int error=initGpio();
	if(error==1){
		printf("Error opening /dev/mem!\n");
		return 1;
	} else if(error==2){
		printf("Error mmapping memory!\n");
		return 2;
	}

	int origMux, origValue;
	origMux = readMux(3, 25);
	origValue = readPin(3, 25);
	writeMux(3, 25, _GPIO_MUX_OUT);

#ifdef _WITH_LIBSOC
	gpio *testPin;
	int libsocOrigMux;
	testPin = libsoc_gpio_request(121, LS_SHARED);
	if(testPin == NULL)
	{
			printf("Error setting up libsoc!\n");
			return 1;
	}
	libsocOrigMux = libsoc_gpio_get_direction(testPin);
	libsoc_gpio_set_direction(testPin, OUTPUT);
	printf("Test 10 million pin-togglings using libsoc on PD25...\n");
	clock_gettime(CLOCK_MONOTONIC, &timeStart);
	for(counter=0; counter<5000000; counter++){
		libsoc_gpio_set_level(testPin, LOW);
		libsoc_gpio_set_level(testPin, HIGH);
	}
	clock_gettime(CLOCK_MONOTONIC, &timeEnd);
	accum = ( timeEnd.tv_sec - timeStart.tv_sec )
		+ ( timeEnd.tv_nsec - timeStart.tv_nsec )
		/ 1E9;
	printf( "That took %.02lf seconds at ~%.02lfMbps\n", accum,  1E7 / accum / 1E6);
#endif

	printf("Test 10 million pin-togglings using writePin on PD25...\n");
	clock_gettime(CLOCK_MONOTONIC, &timeStart);
	for(counter=0; counter<5000000; counter++){
		writePin(3, 25, _GPIO_LOW);
		writePin(3, 25, _GPIO_HIGH);
	}
	clock_gettime(CLOCK_MONOTONIC, &timeEnd);
	accum = ( timeEnd.tv_sec - timeStart.tv_sec )
		+ ( timeEnd.tv_nsec - timeStart.tv_nsec )
		/ 1E9;
	printf( "That took %.02lf seconds at ~%.02lfMbps\n", accum,  1E7 / accum / 1E6);

	printf("Test 10 million pin-togglings using clearPort and setPort on PD25...\n");
	uint32_t mask = 1 << 25;
	clock_gettime(CLOCK_MONOTONIC, &timeStart);
	for(counter=0; counter<5000000; counter++){
		clearPort(3, mask); //Toggle pin LOW
		setPort(3, mask); //Toggle pin HIGH
	}
	clock_gettime(CLOCK_MONOTONIC, &timeEnd);
	accum = ( timeEnd.tv_sec - timeStart.tv_sec )
		+ ( timeEnd.tv_nsec - timeStart.tv_nsec )
		/ 1E9;
	printf( "That took %.02lf seconds at ~%.02lfMbps\n", accum,  1E7 / accum / 1E6);

#ifdef _WITH_LIBSOC
	libsoc_gpio_set_direction(testPin, libsocOrigMux);
	libsoc_gpio_free(testPin);
#endif

	writeMux(3, 25, origMux);
	writePin(3, 25, origValue);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %eax, %eax
callq 0x244c
cmpl $0x2, %eax
je 0x21cb
cmpl $0x1, %eax
jne 0x21df
leaq 0xf1b(%rip), %rdi       # 0x30dd
callq 0x2030
pushq $0x1
jmp 0x21d9
leaq 0xef4(%rip), %rdi       # 0x30c6
callq 0x2030
pushq $0x2
popq %rax
jmp 0x2388
pushq $0x3
popq %r14
pushq $0x19
popq %r15
movl %r14d, %edi
movl %r15d, %esi
callq 0x24b8
movl %eax, 0x4(%rsp)
leaq 0x38b3(%rip), %rbp      # 0x5ab0
movq (%rbp), %rax
leaq 0x3896(%rip), %r13      # 0x5a9e
movzbl 0x3(%r13), %ecx
movl (%rax,%rcx), %ebx
pushq $0x1
popq %r12
movl %r14d, %edi
movl %r15d, %esi
movl %r12d, %edx
callq 0x24de
leaq 0xe20(%rip), %rdi       # 0x3049
callq 0x2030
leaq 0x8(%rsp), %rsi
movl %r12d, %edi
callq 0x2040
movq (%rbp), %rax
movl $0x4c4b40, %ecx         # imm = 0x4C4B40
movl $0xfdffffff, %edx       # imm = 0xFDFFFFFF
subl $0x1, %ecx
jb 0x2267
movzbl 0x3(%r13), %esi
andl %edx, (%rax,%rsi)
movzbl 0x3(%r13), %esi
andl %edx, (%rax,%rsi)
orl $0x2000000, (%rax,%rsi) # imm = 0x2000000
jmp 0x2249
pushq $0x1
popq %r14
leaq 0x18(%rsp), %r15
movl %r14d, %edi
movq %r15, %rsi
callq 0x2040
movq (%r15), %rax
movq 0x8(%r15), %rcx
leaq 0x8(%rsp), %r15
subq (%r15), %rax
cvtsi2sd %rax, %xmm1
subq 0x8(%r15), %rcx
cvtsi2sd %rcx, %xmm0
divsd 0xd68(%rip), %xmm0      # 0x3008
addsd %xmm1, %xmm0
movsd 0xd64(%rip), %xmm1      # 0x3010
divsd %xmm0, %xmm1
divsd 0xd60(%rip), %xmm1      # 0x3018
leaq 0xd61(%rip), %rdi       # 0x3020
movb $0x2, %al
callq 0x2060
leaq 0xdb4(%rip), %rdi       # 0x3081
callq 0x2030
movl %r14d, %edi
movq %r15, %rsi
callq 0x2040
movq (%rbp), %rax
movl $0x4c4b40, %ecx         # imm = 0x4C4B40
subl $0x1, %ecx
jb 0x2305
movzbl 0x3(%r13), %edx
andl $0xfdffffff, (%rax,%rdx) # imm = 0xFDFFFFFF
movzbl 0x3(%r13), %edx
orl $0x2000000, (%rax,%rdx) # imm = 0x2000000
jmp 0x22e6
shrl $0x19, %ebx
pushq $0x1
popq %rdi
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x2040
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq 0x8(%rsp), %rax
cvtsi2sd %rax, %xmm1
subq 0x10(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
divsd 0xccd(%rip), %xmm0      # 0x3008
addsd %xmm1, %xmm0
movsd 0xcc9(%rip), %xmm1      # 0x3010
divsd %xmm0, %xmm1
divsd 0xcc5(%rip), %xmm1      # 0x3018
leaq 0xcc6(%rip), %rdi       # 0x3020
movb $0x2, %al
callq 0x2060
movzbl 0x4(%rsp), %edx
pushq $0x3
popq %rbp
pushq $0x19
popq %r14
movl %ebp, %edi
movl %r14d, %esi
callq 0x24de
andl $0x1, %ebx
movl %ebp, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x25fd
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop