// File: /workspace/github_c_makefile_star2_O1/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 0x2683
movq %rsi, %r14
movl %edi, %ebx
movq 0x8(%rsi), %r15
leaq 0x9cd(%rip), %rsi       # 0x3020
movq %r15, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2683
leaq 0x9bd(%rip), %rsi       # 0x3023
movq %r15, %rdi
callq 0x20b0
testl %eax, %eax
je 0x2683
cmpl $0x3, %ebx
jg 0x26cc
leaq 0xb01(%rip), %rdi       # 0x317f
jmp 0x2822
leaq 0xb0f(%rip), %rdi       # 0x3199
callq 0x2050
leaq 0xb78(%rip), %rdi       # 0x320e
callq 0x2050
leaq 0xb90(%rip), %rdi       # 0x3232
callq 0x2050
leaq 0xbae(%rip), %rdi       # 0x325c
callq 0x2050
leaq 0xbd5(%rip), %rdi       # 0x328f
callq 0x2050
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
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 0x270f
leaq 0xa4b(%rip), %rdi       # 0x3155
jmp 0x2822
movq 0x18(%r14), %rdi
callq 0x20e0
movzbl %al, %ebx
cmpl $0x20, %ebx
jb 0x272c
leaq 0xa0c(%rip), %rdi       # 0x3133
jmp 0x2822
xorl %eax, %eax
callq 0x2933
cmpl $0x2, %eax
je 0x2749
cmpl $0x1, %eax
jne 0x275f
leaq 0x9d7(%rip), %rdi       # 0x311b
jmp 0x2822
leaq 0x9b4(%rip), %rdi       # 0x3104
callq 0x2050
movl $0x2, %eax
jmp 0x26c1
movzbl %bpl, %ebp
movq 0x8(%r14), %r14
leaq 0x8bc(%rip), %rsi       # 0x302a
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27c6
leaq 0x8af(%rip), %rsi       # 0x3030
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27c6
leaq 0x8d1(%rip), %rsi       # 0x3065
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27fc
leaq 0x8c4(%rip), %rsi       # 0x306b
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x27fc
leaq 0x8f3(%rip), %rdi       # 0x30ad
movq %r14, %rsi
xorl %eax, %eax
callq 0x2070
jmp 0x2827
movb $-0x3, %r15b
movl %ebp, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x226c
testq %rax, %rax
jne 0x27e0
incb %r15b
jne 0x27c9
jmp 0x281b
movq %rax, %r14
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x843(%rip), %rdi       # 0x3036
movb $0x2, %al
callq 0x2070
jmp 0x285c
xorl %r15d, %r15d
movl %ebp, %edi
movl %ebx, %esi
movl $0x1, %edx
callq 0x226c
testq %rax, %rax
jne 0x2831
incl %r15d
cmpb $0x3, %r15b
jne 0x27ff
leaq 0x8c7(%rip), %rdi       # 0x30e9
callq 0x2050
movl $0x1, %eax
jmp 0x26c1
movq %rax, %r14
cvtss2sd 0x4(%rax), %xmm0
cvtss2sd (%rax), %xmm1
leaq 0x82d(%rip), %rdi       # 0x3071
movb $0x2, %al
callq 0x2070
leaq 0x84e(%rip), %rdi       # 0x30a0
movl %r15d, %esi
xorl %eax, %eax
callq 0x2070
movq %r14, %rdi
callq 0x2030
jmp 0x26bf
nopl (%rax)