// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movss 0x1d58(%rip), %xmm0     # 0x4004
movss %xmm0, -0x28(%rbp)
movss 0x1d4b(%rip), %xmm0     # 0x4004
movss %xmm0, -0x24(%rbp)
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x148, %edx            # imm = 0x148
callq 0x2080
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x2ea0
movl %eax, -0x18(%rbp)
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x31a0
movl %eax, -0x1c(%rbp)
movb $0x0, %al
callq 0x3100
movl %eax, -0x20(%rbp)
leaq -0x174(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x2080
movl $0x1, %edi
callq 0x20f0
movl %eax, -0x174(%rbp)
xorl %edi, %edi
movl $0x1, %esi
leaq -0x174(%rbp), %rdx
callq 0x2070
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl $0x1, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
xorl %edx, %edx
callq 0x31e0
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x2210
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl $0x1, %edx
callq 0x31e0
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x2210
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
xorl %edx, %edx
callq 0x31e0
movl $0x14, %edi
callq 0x2210
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
xorl %edx, %edx
callq 0x2ef0
movl $0x0, -0x178(%rbp)
movl -0x178(%rbp), %eax
movl -0x178(%rbp), %eax
cmpl $0x32, %eax
jge 0x23c0
jmp 0x23af
movl -0x178(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x178(%rbp)
jmp 0x23a2
movl $0x0, -0x17c(%rbp)
movl -0x17c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x17c(%rbp)
cmpl $0x7e90, %eax           # imm = 0x7E90
jb 0x2446
leaq -0x174(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x2080
movl $0x0, -0x174(%rbp)
xorl %esi, %esi
leaq -0x174(%rbp), %rdx
movl %esi, %edi
callq 0x2070
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %eax
movzbl %al, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %eax
movzbl %al, %edx
callq 0x31e0
movl -0x20(%rbp), %edi
callq 0x30d0
movq $0x0, -0x8(%rbp)
jmp 0x2881
jmp 0x2448
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x31a0
cmpl $0x0, %eax
jne 0x23ca
movl $0x0, -0x180(%rbp)
cmpl $0x52, -0x180(%rbp)
jge 0x25cf
jmp 0x2475
movslq -0x180(%rbp), %rcx
movl -0x170(%rbp,%rcx,4), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x170(%rbp,%rcx,4)
cmpl $0x7e90, %eax           # imm = 0x7E90
jl 0x24fa
leaq -0x174(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x2080
movl $0x0, -0x174(%rbp)
xorl %esi, %esi
leaq -0x174(%rbp), %rdx
movl %esi, %edi
callq 0x2070
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %eax
movzbl %al, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %eax
movzbl %al, %edx
callq 0x31e0
movl -0x20(%rbp), %edi
callq 0x30d0
movq $0x0, -0x8(%rbp)
jmp 0x2881
jmp 0x24fc
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x31a0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2475
jmp 0x2519
movl -0x180(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
movl -0x170(%rbp,%rcx,4), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x170(%rbp,%rcx,4)
cmpl $0x7e90, %eax           # imm = 0x7E90
jl 0x25a3
leaq -0x174(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x2080
movl $0x0, -0x174(%rbp)
xorl %esi, %esi
leaq -0x174(%rbp), %rdx
movl %esi, %edi
callq 0x2070
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %eax
movzbl %al, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %eax
movzbl %al, %edx
callq 0x31e0
movl -0x20(%rbp), %edi
callq 0x30d0
movq $0x0, -0x8(%rbp)
jmp 0x2881
jmp 0x25a5
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x31a0
cmpl $0x0, %eax
jne 0x2519
jmp 0x25bb
movl -0x180(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x180(%rbp)
jmp 0x2466
leaq -0x174(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x2080
movl $0x0, -0x174(%rbp)
xorl %esi, %esi
leaq -0x174(%rbp), %rdx
movl %esi, %edi
callq 0x2070
movl $0x0, -0x184(%rbp)
movl $0x2, -0x180(%rbp)
cmpl $0x52, -0x180(%rbp)
jge 0x2644
movslq -0x180(%rbp), %rax
movl -0x170(%rbp,%rax,4), %eax
addl -0x184(%rbp), %eax
movl %eax, -0x184(%rbp)
movl -0x180(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x180(%rbp)
jmp 0x2610
movl -0x184(%rbp), %eax
movl $0x28, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x184(%rbp)
leaq -0x189(%rbp), %rdi
xorl %esi, %esi
movl $0x5, %edx
callq 0x2080
movl $0x3, -0x180(%rbp)
cmpl $0x52, -0x180(%rbp)
jge 0x26f0
movl -0x180(%rbp), %eax
subl $0x3, %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, -0x190(%rbp)
movslq -0x190(%rbp), %rax
movzbl -0x189(%rbp,%rax), %ecx
shll %ecx
movb %cl, -0x189(%rbp,%rax)
movslq -0x180(%rbp), %rax
movl -0x170(%rbp,%rax,4), %eax
cmpl -0x184(%rbp), %eax
jb 0x26dd
movslq -0x190(%rbp), %rax
movzbl -0x189(%rbp,%rax), %ecx
orl $0x1, %ecx
movb %cl, -0x189(%rbp,%rax)
jmp 0x26df
movl -0x180(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x180(%rbp)
jmp 0x2676
movzbl -0x185(%rbp), %eax
movzbl -0x189(%rbp), %ecx
movzbl -0x188(%rbp), %edx
addl %edx, %ecx
movzbl -0x187(%rbp), %edx
addl %edx, %ecx
movzbl -0x186(%rbp), %edx
addl %edx, %ecx
andl $0xff, %ecx
cmpl %ecx, %eax
jne 0x284f
cmpl $0x0, -0x14(%rbp)
jne 0x2752
movzbl -0x189(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x28(%rbp)
movzbl -0x187(%rbp), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x24(%rbp)
jmp 0x27d2
cmpl $0x1, -0x14(%rbp)
jne 0x27d0
movzbl -0x189(%rbp), %eax
shll $0x8, %eax
movzbl -0x188(%rbp), %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movss 0x1891(%rip), %xmm1     # 0x4008
divss %xmm1, %xmm0
movss %xmm0, -0x28(%rbp)
movzbl -0x187(%rbp), %eax
andl $0x7f, %eax
shll $0x8, %eax
movzbl -0x186(%rbp), %ecx
addl %ecx, %eax
cvtsi2ss %eax, %xmm0
movss 0x1866(%rip), %xmm1     # 0x4008
divss %xmm1, %xmm0
movss %xmm0, -0x24(%rbp)
movzbl -0x187(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x27ce
movss 0x1848(%rip), %xmm0     # 0x400c
mulss -0x24(%rbp), %xmm0
movss %xmm0, -0x24(%rbp)
jmp 0x27d0
jmp 0x27d2
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %eax
movzbl %al, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %eax
movzbl %al, %edx
callq 0x31e0
movl -0x20(%rbp), %edi
callq 0x30d0
movl $0x8, %edi
callq 0x20b0
movq %rax, -0x198(%rbp)
cmpq $0x0, -0x198(%rbp)
jne 0x2821
movq $0x0, -0x8(%rbp)
jmp 0x2881
movss -0x28(%rbp), %xmm0
movq -0x198(%rbp), %rax
movss %xmm0, (%rax)
movss -0x24(%rbp), %xmm0
movq -0x198(%rbp), %rax
movss %xmm0, 0x4(%rax)
movq -0x198(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2881
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x18(%rbp), %eax
movzbl %al, %edx
callq 0x2ef0
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
movl -0x1c(%rbp), %eax
movzbl %al, %edx
callq 0x31e0
movl -0x20(%rbp), %edi
callq 0x30d0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nop