// File: /workspace/github_c_makefile_star2_O1/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, 0xc(%rsp)
movl %esi, %ebp
movl %edi, %ebx
leaq 0x30(%rsp), %rdi
xorl %r14d, %r14d
movl $0x148, %edx            # imm = 0x148
xorl %esi, %esi
callq 0x2090
movl %ebx, %edi
movl %ebp, %esi
callq 0x29a8
movl %eax, 0x8(%rsp)
leaq 0x3843(%rip), %rax      # 0x5af0
movq (%rax), %rax
movslq %ebx, %rbx
leaq 0x3824(%rip), %r12      # 0x5ade
movzbl (%r12,%rbx), %ecx
movl (%rax,%rcx), %eax
movl %ebp, %ecx
shrl %cl, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
callq 0x2a9e
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %r13
movl %r14d, (%r13)
movl $0x1, %r15d
movl $0x1, %edi
callq 0x2100
movl %eax, (%r13)
xorl %edi, %edi
movl $0x1, %esi
movq %r13, %rdx
callq 0x2080
movl %ebx, %edi
movl %ebp, %esi
movl $0x1, %edx
callq 0x29d2
leaq 0x37da(%rip), %rax      # 0x5af0
movq (%rax), %rax
movl $0x1, %r13d
movl %ebp, %ecx
shll %cl, %r13d
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
roll %cl, %r14d
movzbl (%r12,%rbx), %ecx
andl %r14d, (%rax,%rcx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
leaq 0x37a9(%rip), %rax      # 0x5af0
movq (%rax), %rax
movzbl (%r12,%rbx), %ecx
andl %r14d, (%rax,%rcx)
movl %r13d, 0x10(%rsp)
orl %r13d, (%rax,%rcx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
leaq 0x3783(%rip), %rax      # 0x5af0
movq (%rax), %rax
movq %r12, %r13
movzbl (%r12,%rbx), %ecx
andl %r14d, (%rax,%rcx)
movl $0x14, %edi
callq 0x221c
movl %ebx, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x29d2
xorl %eax, %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl (%rsp), %eax
cmpl $0x31, %eax
jg 0x23ac
incl (%rsp)
movl (%rsp), %eax
cmpl $0x32, %eax
jl 0x23a1
leaq 0x373d(%rip), %rax      # 0x5af0
movq (%rax), %rax
movzbl (%r13,%rbx), %edx
movl %ebp, %ecx
shll %cl, %r15d
movl $0x7e91, %ecx           # imm = 0x7E91
decl %ecx
je 0x251d
testl %r15d, (%rax,%rdx)
jne 0x23c6
movl $0x1, %esi
movl %ebp, %ecx
shll %cl, %esi
xorl %ecx, %ecx
movl 0x30(%rsp,%rcx,4), %r8d
cmpl $0x7e91, %r8d           # imm = 0x7E91
movl $0x7e90, %edi           # imm = 0x7E90
cmovgel %r8d, %edi
cmpl %r8d, %edi
je 0x250d
incl %r8d
testl %esi, (%rax,%rdx)
je 0x23f4
movl %r8d, 0x30(%rsp,%rcx,4)
movq %rcx, %rdi
orq $0x1, %rdi
movl 0x34(%rsp,%rcx,4), %r9d
cmpl $0x7e91, %r9d           # imm = 0x7E91
movl $0x7e90, %r8d           # imm = 0x7E90
cmovgel %r9d, %r8d
cmpl %r9d, %r8d
je 0x2515
incl %r9d
testl %esi, (%rax,%rdx)
jne 0x2427
movl %r9d, 0x30(%rsp,%rdi,4)
leaq 0x2(%rcx), %rdi
cmpq $0x50, %rcx
movq %rdi, %rcx
jb 0x23df
leaq 0x1c(%rsp), %rdx
movl $0x0, (%rdx)
xorl %r15d, %r15d
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
xorl %eax, %eax
addl 0x38(%rsp,%r15,4), %eax
addq $0x2, %r15
cmpq $0x50, %r15
jb 0x2463
movl %eax, %ecx
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x25, %rax
movb $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
xorl %ecx, %ecx
movl %ecx, %edx
shrl $0x4, %edx
movb (%rsp,%rdx), %sil
addb %sil, %sil
movb %sil, (%rsp,%rdx)
cmpl %eax, 0x3c(%rsp,%rcx,4)
jb 0x24ad
orb $0x1, %sil
movb %sil, (%rsp,%rdx)
leaq 0x2(%rcx), %rdx
addq $0x3, %rcx
cmpq $0x50, %rcx
movq %rdx, %rcx
jb 0x248f
movzbl (%rsp), %ecx
movzbl 0x1(%rsp), %edx
leal (%rdx,%rcx), %edi
movzbl 0x2(%rsp), %eax
movzbl 0x3(%rsp), %esi
leal (%rsi,%rax), %r8d
addb %dil, %r8b
cmpb %r8b, 0x4(%rsp)
jne 0x2531
movl 0xc(%rsp), %edi
cmpl $0x1, %edi
je 0x257e
testl %edi, %edi
jne 0x25bd
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
jmp 0x25cb
incl %edi
movl %edi, 0x30(%rsp,%rcx,4)
jmp 0x251d
incl %r8d
movl %r8d, 0x30(%rsp,%rdi,4)
leaq 0x1c(%rsp), %rdx
movl $0x0, (%rdx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
movzbl 0x8(%rsp), %edx
movl %ebx, %edi
movl %ebp, %esi
callq 0x29d2
leaq 0x35aa(%rip), %rax      # 0x5af0
movq (%rax), %rax
movzbl (%r13,%rbx), %ecx
andl %r14d, (%rax,%rcx)
testb $0x1, 0x14(%rsp)
je 0x2561
movl 0x10(%rsp), %edx
orl %edx, (%rax,%rcx)
movl 0x18(%rsp), %edi
callq 0x2a90
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
cvtsi2ss %ecx, %xmm1
movl %eax, %ecx
andl $0x7f, %ecx
shll $0x8, %ecx
orl %esi, %ecx
cvtsi2ss %ecx, %xmm2
movss 0xa6b(%rip), %xmm0      # 0x3008
divss %xmm0, %xmm1
divss %xmm0, %xmm2
testb %al, %al
movss %xmm1, 0xc(%rsp)
jns 0x25b6
xorps 0xa5a(%rip), %xmm2      # 0x3010
movaps %xmm2, 0x20(%rsp)
jmp 0x25d0
movss 0xa3f(%rip), %xmm0      # 0x3004
movss %xmm0, 0xc(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x8(%rsp), %eax
movzbl %al, %edx
movl %ebx, %edi
movl %ebp, %esi
callq 0x29d2
leaq 0x3509(%rip), %rax      # 0x5af0
movq (%rax), %rax
movzbl (%r13,%rbx), %ecx
andl %r14d, (%rax,%rcx)
testb $0x1, 0x14(%rsp)
je 0x2602
movl 0x10(%rsp), %edx
orl %edx, (%rax,%rcx)
movl 0x18(%rsp), %edi
callq 0x2a90
movl $0x8, %edi
callq 0x20c0
testq %rax, %rax
je 0x256a
movss 0xc(%rsp), %xmm0
movss %xmm0, (%rax)
movaps 0x20(%rsp), %xmm0
movss %xmm0, 0x4(%rax)
jmp 0x256c