// File: /workspace/github_c_makefile_star2_O2/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, 0x20(%rsp)
movl %esi, %r15d
movl %edi, %ebp
leaq 0x30(%rsp), %rdi
movl $0x148, %edx            # imm = 0x148
xorl %esi, %esi
callq 0x2090
movl %ebp, %edi
movl %r15d, %esi
callq 0x294c
movl %eax, 0x8(%rsp)
leaq 0x384a(%rip), %rcx      # 0x5af0
movq (%rcx), %rax
movq %rcx, %r14
movl %ebp, 0x18(%rsp)
movslq %ebp, %r13
leaq 0x3824(%rip), %rbx      # 0x5ade
movzbl (%rbx,%r13), %ecx
movl (%rax,%rcx), %eax
movl %r15d, %ecx
shrl %cl, %eax
movl %eax, 0x10(%rsp)
xorl %eax, %eax
callq 0x2a2e
movl %eax, 0x14(%rsp)
pushq $0x1
popq %r12
movl %r12d, %edi
callq 0x2100
leaq 0x1c(%rsp), %rdx
movl %eax, (%rdx)
xorl %edi, %edi
movl %r12d, %esi
callq 0x2080
movl %r13d, %edi
movl %r15d, %esi
movl %r12d, %edx
callq 0x2972
movq (%r14), %rax
movl %r12d, %r14d
movl %r15d, %ecx
shll %cl, %r14d
pushq $-0x2
popq %rbp
roll %cl, %ebp
movzbl (%rbx,%r13), %ecx
andl %ebp, (%rax,%rcx)
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
movl %r13d, %edi
movl %r15d, %esi
movl %r12d, %edx
callq 0x2a91
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x221c
leaq 0x37ad(%rip), %rax      # 0x5af0
movq %rax, %r12
movq (%rax), %rax
movzbl (%rbx,%r13), %ecx
andl %ebp, (%rax,%rcx)
pushq $0x14
popq %rdi
callq 0x221c
movl %r13d, %edi
movl %r15d, 0xc(%rsp)
movl %r15d, %esi
xorl %edx, %edx
callq 0x2972
movl $0x0, (%rsp)
movl (%rsp), %eax
movl (%rsp), %eax
cmpl $0x31, %eax
jg 0x2382
incl (%rsp)
jmp 0x2375
movl 0x10(%rsp), %ebx
andl $0x1, %ebx
movq (%r12), %rax
leaq 0x374a(%rip), %rcx      # 0x5ade
movzbl (%rcx,%r13), %ecx
movl $0x7e91, %edx           # imm = 0x7E91
decl %edx
je 0x23ff
testl %r14d, (%rax,%rcx)
jne 0x239e
xorl %edx, %edx
cmpq $0x51, %rdx
ja 0x2455
movl 0x30(%rsp,%rdx,4), %esi
cmpl $0x7e90, %esi           # imm = 0x7E90
jge 0x23f1
incl %esi
testl %r14d, (%rax,%rcx)
je 0x23b8
movl %esi, 0x30(%rsp,%rdx,4)
movq %rdx, %rsi
orq $0x1, %rsi
movl 0x34(%rsp,%rdx,4), %edi
cmpl $0x7e90, %edi           # imm = 0x7E90
jge 0x23f9
incl %edi
testl %r14d, (%rax,%rcx)
jne 0x23d7
movl %edi, 0x30(%rsp,%rsi,4)
addq $0x2, %rdx
jmp 0x23aa
incl %esi
movl %esi, 0x30(%rsp,%rdx,4)
jmp 0x23ff
incl %edi
movl %edi, 0x30(%rsp,%rsi,4)
leaq 0x1c(%rsp), %rdx
andl $0x0, (%rdx)
xorl %r13d, %r13d
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
movzbl 0x8(%rsp), %edx
movl 0x18(%rsp), %ebp
movl %ebp, %edi
movl 0xc(%rsp), %r14d
movl %r14d, %esi
callq 0x2972
movl %ebp, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x2a91
movl 0x14(%rsp), %edi
callq 0x2a20
movq %r13, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c(%rsp), %rdx
andl $0x0, (%rdx)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x2080
pushq $0x2
popq %rax
cmpq $0x51, %rax
ja 0x247d
addl 0x30(%rsp,%rax,4), %r14d
addq $0x2, %rax
jmp 0x246c
pushq $0x28
popq %rcx
movl %r14d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
andl $0x0, (%rsp)
movb $0x0, 0x4(%rsp)
pushq $0x3
popq %rdx
movb $0x10, %sil
cmpq $0x51, %rdx
ja 0x24c8
leal -0x3(%rdx), %eax
cbtw
idivb %sil
movzbl %al, %eax
movb (%rsp,%rax), %dil
addb %dil, %dil
movb %dil, (%rsp,%rax)
cmpl %ecx, 0x30(%rsp,%rdx,4)
jb 0x24c2
orb $0x1, %dil
movb %dil, (%rsp,%rax)
addq $0x2, %rdx
jmp 0x2498
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 0x250d
movl 0x20(%rsp), %edi
cmpl $0x1, %edi
je 0x253f
testl %edi, %edi
jne 0x257e
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x10(%rsp)
cvtsi2ss %eax, %xmm0
jmp 0x258c
movzbl 0x8(%rsp), %edx
movl 0x18(%rsp), %ebp
movl %ebp, %edi
movl 0xc(%rsp), %r14d
movl %r14d, %esi
callq 0x2972
movl %ebp, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x2a91
movl 0x14(%rsp), %edi
callq 0x2a20
jmp 0x25e9
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 0xaaa(%rip), %xmm0      # 0x3008
divss %xmm0, %xmm1
movss %xmm1, 0x10(%rsp)
divss %xmm0, %xmm2
testb %al, %al
jns 0x2577
xorps 0xa99(%rip), %xmm2      # 0x3010
movaps %xmm2, 0x20(%rsp)
jmp 0x2591
movss 0xa7e(%rip), %xmm0      # 0x3004
movss %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x8(%rsp), %eax
movzbl %al, %edx
movl 0x18(%rsp), %ebp
movl %ebp, %edi
movl 0xc(%rsp), %r14d
movl %r14d, %esi
callq 0x2972
movl %ebp, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x2a91
movl 0x14(%rsp), %edi
callq 0x2a20
pushq $0x8
popq %rdi
callq 0x20c0
testq %rax, %rax
je 0x25e9
movq %rax, %r13
movss 0x10(%rsp), %xmm0
movss %xmm0, (%rax)
movaps 0x20(%rsp), %xmm0
movss %xmm0, 0x4(%rax)
jmp 0x2440
xorl %r13d, %r13d
jmp 0x2440