// File: /workspace/github_c_makefile_star2_O3/WereCatf[P]Gonzales/dhtsensor.c
// Function: main
int main(int argc, char* argv[])
{
	uint8_t port, pin, retries;
	structDhtSensor *dhtSensor=NULL;

	if(argc <= 1 || strcmp(argv[1], "-h")==0 || strcmp(argv[1], "--help")==0){
		printf("This is a simple application for reading temperature and\n"
			"humidity off a DHT11- or DHT22-sensor. Call it as follows:\n\n");
		printf("dhtsensor [sensortype] [port] [pin]\n");
		printf("  'sensortype' can be 'dht11' or 'dht22'.\n");
		printf("  'port' can be a number 0-6, or a letter 'a'-'g'.\n");
		printf("  'pin' can be a number 0-31.\n");
		return 0;
	}

	if(argc < 4){
		printf("Too few parameters given!\n");
		return 1;
	}

	port = atoi(argv[2]);
	if(*argv[2] >= 'a' && *argv[2] <= 'g') port = *argv[2] - 'a';
	if(*argv[2] >= 'A' && *argv[2] <= 'G') port = *argv[2] - 'A';
	if(*argv[2] >= '0' && *argv[2] <= '6') port = *argv[2] - '0';

	pin = atoi(argv[3]);
	if(port > 6){
		printf("Port must be in the range of 0-6, or a-g!\n");
		return 1;
	}
	if(pin > 31){
		printf("Pin must be in the range of 0-31!\n");
		return 1;
	}

	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;
	}

	retries=3;
	if(strcmp(argv[1], "dht11")==0 || strcmp(argv[1], "DHT11")==0){
		while(dhtSensor==NULL){
			dhtSensor=dhtRead(port, pin, 0);
			if(dhtSensor){
				printf("DHT11 temperature: %.01fC, humidity: %.01f%%.\n",
					dhtSensor->temperature, dhtSensor->humidity);
				break;
			}
			retries--;
			if(!retries){
				printf("Error reading from sensor!\n");
				return 1;
			}
		}
	} else if(strcmp(argv[1], "dht22")==0 || strcmp(argv[1], "DHT22")==0){
		while(dhtSensor==NULL){
			dhtSensor=dhtRead(port, pin, 1);
			if(dhtSensor){
				printf("DHT22 temperature: %.01fC, humidity: %.01f%%.\n",
					dhtSensor->temperature, dhtSensor->humidity);
				printf("Retries: %d\n", 3 - retries);
				break;
			}
			retries--;
			if(!retries){
				printf("Error reading from sensor!\n");
				return 1;
			}
		}
	} else {
		printf("Unknown argument '%s' given, should be 'dht11' or 'dht22'!\n",
			argv[1]);
		return 1;
	}
	free(dhtSensor);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
cmpl $0x2, %edi
jl 0x2691
movq %rsi, %r14
movl %edi, %ebx
movq 0x8(%rsi), %rdi
cmpb $0x2d, (%rdi)
jne 0x2670
cmpb $0x68, 0x1(%rdi)
jne 0x2670
cmpb $0x0, 0x2(%rdi)
je 0x2691
leaq 0x9a9(%rip), %rsi       # 0x3020
callq 0x20b0
testl %eax, %eax
je 0x2691
cmpl $0x3, %ebx
ja 0x26d4
leaq 0xaf0(%rip), %rdi       # 0x317c
jmp 0x282a
leaq 0xafe(%rip), %rdi       # 0x3196
callq 0x2050
leaq 0xb67(%rip), %rdi       # 0x320b
callq 0x2050
leaq 0xb7f(%rip), %rdi       # 0x322f
callq 0x2050
leaq 0xb9d(%rip), %rdi       # 0x3259
callq 0x2050
leaq 0xbc4(%rip), %rdi       # 0x328c
callq 0x2050
xorl %eax, %eax
jmp 0x2834
movq 0x10(%r14), %rbx
movq %rbx, %rdi
callq 0x20e0
movb (%rbx), %cl
leal -0x61(%rcx), %edx
cmpb $0x7, %dl
movzbl %dl, %edx
cmovael %eax, %edx
leal -0x41(%rcx), %eax
cmpb $0x7, %al
movzbl %al, %eax
cmovael %edx, %eax
addb $-0x30, %cl
cmpb $0x7, %cl
movzbl %cl, %ebp
cmovael %eax, %ebp
cmpb $0x7, %bpl
jb 0x2717
leaq 0xa40(%rip), %rdi       # 0x3152
jmp 0x282a
movq 0x18(%r14), %rdi
callq 0x20e0
movzbl %al, %ebx
cmpl $0x20, %ebx
jb 0x2734
leaq 0xa01(%rip), %rdi       # 0x3130
jmp 0x282a
xorl %eax, %eax
callq 0x293f
cmpl $0x2, %eax
je 0x2751
cmpl $0x1, %eax
jne 0x2767
leaq 0x9cc(%rip), %rdi       # 0x3118
jmp 0x282a
leaq 0x9a9(%rip), %rdi       # 0x3101
callq 0x2050
movl $0x2, %eax
jmp 0x2834
movzbl %bpl, %ebp
movq 0x8(%r14), %r14
leaq 0x8b1(%rip), %rsi       # 0x3027
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27ce
leaq 0x8a4(%rip), %rsi       # 0x302d
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27ce
leaq 0x8c6(%rip), %rsi       # 0x3062
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2804
leaq 0x8b9(%rip), %rsi       # 0x3068
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2804
leaq 0x8e8(%rip), %rdi       # 0x30aa
movq %r14, %rsi
xorl %eax, %eax
callq 0x2070
jmp 0x282f
movb $-0x3, %r15b
movl %ebp, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x226c
testq %rax, %rax
jne 0x27e8
incb %r15b
jne 0x27d1
jmp 0x2823
movq %rax, %r14
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x838(%rip), %rdi       # 0x3033
movb $0x2, %al
callq 0x2070
jmp 0x286a
xorl %r15d, %r15d
movl %ebp, %edi
movl %ebx, %esi
movl $0x1, %edx
callq 0x226c
testq %rax, %rax
jne 0x283f
incl %r15d
cmpb $0x3, %r15b
jne 0x2807
leaq 0x8bc(%rip), %rdi       # 0x30e6
callq 0x2050
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x81c(%rip), %rdi       # 0x306e
movb $0x2, %al
callq 0x2070
leaq 0x83d(%rip), %rdi       # 0x309d
movl %r15d, %esi
xorl %eax, %eax
callq 0x2070
movq %r14, %rdi
callq 0x2030
jmp 0x26cd
nop