// File: /workspace/github_c_makefile_star2_O3/WereCatf[P]Gonzales/dhtsensor.c
// Function: dhtRead
structDhtSensor* dhtRead(int port, int pin, int type)
{
	int origMux, origValue, origDebounce;
	structDhtSensor dhtSensor;
	dhtSensor.humidity = -255.0f;
	dhtSensor.temperature = -255.0f;
	int pulseCounts[82] = { 0 };
	struct sched_param sched;

	origMux = readMux(port, pin);
	origValue = readPin(port, pin);
	origDebounce = readDebounce();

	memset(&sched, 0, sizeof(sched));
	sched.sched_priority = sched_get_priority_max(SCHED_FIFO);
	sched_setscheduler(0, SCHED_FIFO, &sched);

	writeMux(port, pin, _GPIO_MUX_OUT);
	writePin(port, pin, _GPIO_LOW);
	sleepMillis(500);
	writePin(port, pin, _GPIO_HIGH);
	sleepMillis(500);
	writePin(port, pin, _GPIO_LOW);
	sleepMillis(20);
	writeMux(port, pin, _GPIO_MUX_IN);
	{
    volatile int i = 0;
    for (i; i < 50; i++)
    {
    }
  }
	uint32_t count = 0;
	do {
		if(count++ >= 32400)
		{
			memset(&sched, 0, sizeof(sched));
		  sched.sched_priority = 0;
		  sched_setscheduler(0, SCHED_OTHER, &sched);
			writeMux(port, pin, origMux);
			writePin(port, pin, origValue);
			writeDebounce(origDebounce);
			return NULL;
		}
	} while(readPin(port, pin));

	int i;
	for(i=0; i<82; i+=2)
	{
		do
		{
			if (pulseCounts[i]++ >= 32400)
			{
				memset(&sched, 0, sizeof(sched));
			  sched.sched_priority = 0;
			  sched_setscheduler(0, SCHED_OTHER, &sched);
				writeMux(port, pin, origMux);
				writePin(port, pin, origValue);
				writeDebounce(origDebounce);
				return NULL;
			}
		} while(!readPin(port, pin));

		do
		{
			if (pulseCounts[i+1]++ >= 32400)
			{
				memset(&sched, 0, sizeof(sched));
			  sched.sched_priority = 0;
			  sched_setscheduler(0, SCHED_OTHER, &sched);
				writeMux(port, pin, origMux);
				writePin(port, pin, origValue);
				writeDebounce(origDebounce);
				return NULL;
			}
		} while(readPin(port, pin));
	}

	memset(&sched, 0, sizeof(sched));
	sched.sched_priority = 0;
	sched_setscheduler(0, SCHED_OTHER, &sched);

	uint32_t threshold = 0;
	for (i=2; i < 82; i+=2) threshold += pulseCounts[i];
	threshold /= 40;

	uint8_t data[5] = {0};
	for (i=3; i < 82; i+=2) {
		int index = (i-3)/16;
		data[index] <<= 1;
		if (pulseCounts[i] >= threshold) data[index] |= 1;
	}

	if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
		if (type == 0) {
			dhtSensor.humidity = (float)data[0];
			dhtSensor.temperature = (float)data[2];
		} else if (type == 1) {
			dhtSensor.humidity = (data[0] * 256 + data[1]) / 10.0f;
			dhtSensor.temperature = ((data[2] & 0x7F) * 256 + data[3]) / 10.0f;
			if (data[2] & 0x80) dhtSensor.temperature *= -1.0f;
		}
		writeMux(port, pin, origMux);
		writePin(port, pin, origValue);
		writeDebounce(origDebounce);
		structDhtSensor* returnSensor = malloc(sizeof(*returnSensor));
		if(returnSensor==NULL) return NULL;
		returnSensor->humidity = dhtSensor.humidity;
		returnSensor->temperature = dhtSensor.temperature;
		return returnSensor;
	}
	writeMux(port, pin, origMux);
	writePin(port, pin, origValue);
	writeDebounce(origDebounce);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movl %edx, 0x10(%rsp)
movl %esi, %ebp
movl %edi, %ebx
leaq 0x30(%rsp), %rdi
movl $0x148, %edx            # imm = 0x148
xorl %esi, %esi
callq 0x2090
movl %ebx, %edi
movl %ebp, %esi
callq 0x29b4
movl %eax, 0xc(%rsp)
leaq 0x3846(%rip), %r14      # 0x5af0
movq (%r14), %rax
movslq %ebx, %rbx
leaq 0x3827(%rip), %r15      # 0x5ade
movzbl (%r15,%rbx), %ecx
movl (%rax,%rcx), %eax
movl %ebp, %ecx
shrl %cl, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
callq 0x2aaa
movl %eax, 0x18(%rsp)
movl $0x1, %r13d
movl $0x1, %edi
callq 0x2100
leaq 0x1c(%rsp), %rdx
movl %eax, (%rdx)
xorl %edi, %edi
movl $0x1, %esi
callq 0x2080
movl %ebx, %edi
movl %ebp, %esi
movl $0x1, %edx
callq 0x29de
movq (%r14), %rax
movl %ebp, %ecx
shll %cl, %r13d
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
roll %cl, %r12d
movzbl (%r15,%rbx), %ecx
andl %r12d, (%rax,%rcx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
movq (%r14), %rax
movzbl (%r15,%rbx), %ecx
andl %r12d, (%rax,%rcx)
orl %r13d, (%rax,%rcx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
movq (%r14), %rax
movzbl (%r15,%rbx), %ecx
andl %r12d, (%rax,%rcx)
movl $0x14, %edi
callq 0x221c
movl %ebx, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x29de
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl 0x4(%rsp), %eax
cmpl $0x31, %eax
jg 0x2384
incl 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl $0x32, %eax
jl 0x2377
leaq 0x3765(%rip), %r15      # 0x5af0
movq (%r15), %rax
leaq 0x3749(%rip), %rcx      # 0x5ade
movzbl (%rcx,%rbx), %ecx
movl $0x7e91, %edx           # imm = 0x7E91
decl %edx
je 0x2533
testl %r13d, (%rax,%rcx)
jne 0x239e
xorl %edx, %edx
movl 0x30(%rsp,%rdx,4), %edi
cmpl $0x7e91, %edi           # imm = 0x7E91
movl $0x7e90, %esi           # imm = 0x7E90
cmovgel %edi, %esi
cmpl %edi, %esi
je 0x2525
incl %edi
testl %r13d, (%rax,%rcx)
je 0x23c0
movl %edi, 0x30(%rsp,%rdx,4)
movq %rdx, %rsi
orq $0x1, %rsi
movl 0x34(%rsp,%rdx,4), %r8d
cmpl $0x7e91, %r8d           # imm = 0x7E91
movl $0x7e90, %edi           # imm = 0x7E90
cmovgel %r8d, %edi
cmpl %r8d, %edi
je 0x252d
incl %r8d
testl %r13d, (%rax,%rcx)
jne 0x23f0
movl %r8d, 0x30(%rsp,%rsi,4)
leaq 0x2(%rdx), %rsi
cmpq $0x50, %rdx
movq %rsi, %rdx
jb 0x23ae
leaq 0x1c(%rsp), %rdx
movl $0x0, (%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
pxor %xmm0, %xmm0
movl $0x4, %eax
movd 0x30(%rsp,%rax,8), %xmm1
movd 0x28(%rsp,%rax,8), %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
movd 0x20(%rsp,%rax,8), %xmm1
movd 0x18(%rsp,%rax,8), %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
punpcklqdq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0]
paddd %xmm3, %xmm0
addq $0x4, %rax
cmpq $0x2c, %rax
jne 0x2431
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %ecx
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x25, %rax
movb $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
xorl %ecx, %ecx
movl %ecx, %edx
sarb $0x7, %dl
shrb $0x4, %dl
addb %cl, %dl
sarb $0x4, %dl
movzbl %dl, %edx
movb 0x4(%rsp,%rdx), %sil
addb %sil, %sil
movb %sil, 0x4(%rsp,%rdx)
cmpl %eax, 0x3c(%rsp,%rcx,4)
jb 0x24c1
orb $0x1, %sil
movb %sil, 0x4(%rsp,%rdx)
leaq 0x2(%rcx), %rdx
addq $0x3, %rcx
cmpq $0x50, %rcx
movq %rdx, %rcx
jb 0x2495
movzbl 0x4(%rsp), %ecx
movzbl 0x5(%rsp), %edx
leal (%rdx,%rcx), %edi
movzbl 0x6(%rsp), %eax
movzbl 0x7(%rsp), %esi
leal (%rsi,%rax), %r8d
addb %dil, %r8b
cmpb %r8b, 0x8(%rsp)
jne 0x2547
movl 0x10(%rsp), %edi
cmpl $0x1, %edi
je 0x258f
testl %edi, %edi
jne 0x25d4
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
jmp 0x25e2
incl %esi
movl %esi, 0x30(%rsp,%rdx,4)
jmp 0x2533
incl %edi
movl %edi, 0x30(%rsp,%rsi,4)
leaq 0x1c(%rsp), %rdx
movl $0x0, (%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
movzbl 0xc(%rsp), %edx
movl %ebx, %edi
movl %ebp, %esi
callq 0x29de
movq (%r15), %rax
leaq 0x357f(%rip), %rcx      # 0x5ade
movzbl (%rcx,%rbx), %ecx
andl %r12d, (%rax,%rcx)
testb $0x1, 0x14(%rsp)
je 0x2572
orl %r13d, (%rax,%rcx)
movl 0x18(%rsp), %edi
callq 0x2a9c
xorl %eax, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shll $0x8, %ecx
orl %edx, %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movl %eax, %ecx
andl $0x7f, %ecx
shll $0x8, %ecx
orl %esi, %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
movss 0xa54(%rip), %xmm0      # 0x3008
divss %xmm0, %xmm1
divss %xmm0, %xmm2
testb %al, %al
movss %xmm1, 0x10(%rsp)
jns 0x25cd
xorps 0xa43(%rip), %xmm2      # 0x3010
movaps %xmm2, 0x20(%rsp)
jmp 0x25e7
movss 0xa28(%rip), %xmm0      # 0x3004
movss %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0xc(%rsp), %eax
movzbl %al, %edx
movl %ebx, %edi
movl %ebp, %esi
callq 0x29de
movq (%r15), %rax
leaq 0x34dd(%rip), %rcx      # 0x5ade
movzbl (%rcx,%rbx), %ecx
andl %r12d, (%rax,%rcx)
testb $0x1, 0x14(%rsp)
je 0x2614
orl %r13d, (%rax,%rcx)
movl 0x18(%rsp), %edi
callq 0x2a9c
movl $0x8, %edi
callq 0x20c0
testq %rax, %rax
je 0x257b
movss 0x10(%rsp), %xmm0
movss %xmm0, (%rax)
movdqa 0x20(%rsp), %xmm0
movd %xmm0, 0x4(%rax)
jmp 0x257d