// File: /workspace/github_c_makefile_star2_O2/Openvario[P]sensord/humidity.c
// Function: si7021_read_humidity
int si7021_read_humidity (t_ds2482 *sensor) {
	uint8_t data[6],x,y;
	double temp;

	sensor->humidity_valid=0;
	if (sensor->sensor_type!=HTU21D) sensor->temp_valid=0;
	switch (sensor->sensor_type) {
		case SI7021 : case HTU21D :
			if (read(sensor->fd, data, 3) != 3) return 4;
			if ((x=si7021_crc_check((data[0]<<8)|data[1],data[2]))==0) {
				temp = (double) ((data[0]<<8)|data[1]) * 125/65536.0 - 6;
				if (sensor->compensate) temp += (25-sensor->temperature) * -0.15;
				sensor->humidity = temp;
				sensor->humidity_valid = sensor->humidity_present;
			}
			if (sensor->sensor_type==HTU21D) return x;
			data[0]=0xe0;
			if (write(sensor->fd,data, 1) != 1) return 4;
			if (read(sensor->fd, data, 3) != 3) return 4;
			if ((y=si7021_crc_check((data[0]<<8)|data[1],data[2]))==0) {
				sensor->temperature = (double) ((data[0]<<8)|data[1]) * 175.72/65536.0 - 46.85;
				sensor->temp_valid = sensor->temp_present;
			}
			return ((y<<1)|x);
			break;
		case HTU31D :
			data[0]=0x0;
			if (write(sensor->fd,data, 1) != 1) return 4;
		case SHT4X : case SHT85 :
			if (read(sensor->fd, data, 6) != 6) return 4;
			x=si7021_crc_check((data[0]<<8)|data[1],data[2]);
			y=si7021_crc_check((data[3]<<8)|data[4],data[5]);
			switch (sensor->sensor_type) {
				case HTU31D :
					sensor->temperature = (double) ((data[0]<<8)|data[1]) * 165/65535.0 - 40;
					sensor->humidity = (double) ((data[3]<<8)|data[4]) * 100/65535.0;
					break;
				case SHT4X :
					sensor->temperature = (double) ((data[0]<<8)|data[1]) * 175/65535.0 - 45;
					sensor->humidity = (double) ((data[3]<<8)|data[4]) * 125/65535.0 - 6;
					break;
				case SHT85 :
					sensor->temperature = (double) ((data[0]<<8)|data[1]) * 175/65535.0 - 45;
 					sensor->humidity = (double) ((data[3]<<8)|data[4]) * 100/65535.0;
					break;
			}
			if (x==0) sensor->temp_valid = sensor->temp_present;
			if (y==0) sensor->humidity_valid = sensor->humidity_present;
			return ((x<<1)|y);
			break;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movb $0x0, 0x2f(%rdi)
movzbl 0x30(%rdi), %ecx
pushq $0x4
popq %r15
cmpl $0x1, %ecx
jne 0x7821
movl (%rbx), %edi
leaq 0x2(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x2180
cmpq $0x3, %rax
jne 0x78ea
movzwl 0x2(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
movzbl 0x4(%rsp), %esi
movl %ebp, %edi
callq 0x6c90
movl %eax, %r14d
testl %eax, %eax
jne 0x7789
imull $0x7d, %ebp, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x2211(%rip), %xmm0     # 0x9960
addsd 0x2229(%rip), %xmm0     # 0x9980
cmpl $0x0, 0x50(%rbx)
je 0x777a
movss 0x223b(%rip), %xmm1     # 0x99a0
subss 0x24(%rbx), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0x221a(%rip), %xmm1     # 0x9990
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
movb 0x2d(%rbx), %al
movb %al, 0x2f(%rbx)
cmpb $0x1, 0x30(%rbx)
movl %r14d, %eax
je 0x7984
leaq 0x2(%rsp), %rsi
movb $-0x20, (%rsi)
movl (%rbx), %edi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
jne 0x78ea
movl (%rbx), %edi
leaq 0x2(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x2180
movq %rax, %rcx
movl %r15d, %eax
cmpq $0x3, %rcx
jne 0x7984
movzwl 0x2(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
movzbl 0x4(%rsp), %esi
movl %ebp, %edi
callq 0x6c90
testl %eax, %eax
jne 0x7818
cvtsi2sd %ebp, %xmm0
mulsd 0x215f(%rip), %xmm0     # 0x9958
mulsd 0x215f(%rip), %xmm0     # 0x9960
addsd 0x215f(%rip), %xmm0     # 0x9968
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
movb 0x2c(%rbx), %cl
movb %cl, 0x2e(%rbx)
leal (%r14,%rax,2), %eax
jmp 0x7984
movb $0x0, 0x2e(%rbx)
leal -0x6(%rcx), %eax
cmpl $0x2, %eax
jb 0x785e
cmpl $0x2, %ecx
je 0x7842
xorl %eax, %eax
cmpl $0x3, %ecx
je 0x7708
jmp 0x7984
leaq 0x2(%rsp), %rsi
movb $0x0, (%rsi)
movl (%rbx), %edi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
jne 0x78ea
movl (%rbx), %edi
leaq 0x2(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x2180
movq %rax, %rcx
movl %r15d, %eax
cmpq $0x6, %rcx
jne 0x7984
movzwl 0x2(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %r15d
movzbl 0x4(%rsp), %esi
movl %r15d, %edi
callq 0x6c90
movl %eax, %r14d
movzwl 0x5(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
movzbl 0x7(%rsp), %esi
movl %ebp, %edi
callq 0x6c90
movzbl 0x30(%rbx), %ecx
cmpl $0x7, %ecx
je 0x792f
cmpl $0x6, %ecx
je 0x78f2
cmpl $0x2, %ecx
jne 0x796b
imull $0xa5, %r15d, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x2094(%rip), %xmm1     # 0x9970
divsd %xmm1, %xmm0
addsd 0x20a0(%rip), %xmm0     # 0x9988
jmp 0x794e
movl %r15d, %eax
jmp 0x7984
imull $0xaf, %r15d, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x206b(%rip), %xmm1     # 0x9970
divsd %xmm1, %xmm0
addsd 0x2067(%rip), %xmm0     # 0x9978
cvtsd2ss %xmm0, %xmm2
imull $0x7d, %ebp, %ecx
cvtsi2sd %ecx, %xmm0
movss %xmm2, 0x24(%rbx)
divsd %xmm1, %xmm0
addsd 0x2053(%rip), %xmm0     # 0x9980
jmp 0x7962
imull $0xaf, %r15d, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x202e(%rip), %xmm1     # 0x9970
divsd %xmm1, %xmm0
addsd 0x202a(%rip), %xmm0     # 0x9978
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
imull $0x64, %ebp, %ecx
cvtsi2sd %ecx, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
testl %r14d, %r14d
jne 0x7976
movb 0x2c(%rbx), %cl
movb %cl, 0x2e(%rbx)
testl %eax, %eax
jne 0x7980
movb 0x2d(%rbx), %cl
movb %cl, 0x2f(%rbx)
leal (%rax,%r14,2), %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq