// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movb $0x0, 0x2f(%rdi)
movzbl 0x30(%rdi), %eax
cmpl $0x1, %eax
jne 0x8508
movl (%rbx), %edi
leaq 0xa(%rsp), %rsi
movl $0x3, %edx
callq 0x2180
movl $0x4, %ebp
cmpq $0x3, %rax
jne 0x87a2
movzwl 0xa(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movzbl 0xc(%rsp), %ecx
movl %eax, %edx
shll $0x8, %edx
movl $0x988000, %esi         # imm = 0x988000
movl $0x800000, %edi         # imm = 0x800000
movl $0xff8000, %r8d         # imm = 0xFF8000
testl %edx, %edi
je 0x8495
movl %esi, %r9d
andl %r8d, %r9d
xorl %r9d, %edx
shrl %edi
shrl %r8d
shrl %esi
cmpl $0x80, %edi
jne 0x8488
xorl %r14d, %r14d
cmpl %ecx, %edx
setne %cl
jne 0x84f7
imull $0x7d, %eax, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x24a3(%rip), %xmm0     # 0xa960
addsd 0x24bb(%rip), %xmm0     # 0xa980
cmpl $0x0, 0x50(%rbx)
je 0x84e8
movss 0x24cd(%rip), %xmm1     # 0xa9a0
subss 0x24(%rbx), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0x24ac(%rip), %xmm1     # 0xa990
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
movb 0x2d(%rbx), %al
movb %al, 0x2f(%rbx)
movb %cl, %r14b
cmpb $0x1, 0x30(%rbx)
jne 0x8531
movl %r14d, %ebp
jmp 0x87a2
movb $0x0, 0x2e(%rbx)
leal -0x6(%rax), %ecx
cmpl $0x2, %ecx
jb 0x8610
cmpl $0x2, %eax
je 0x85ed
xorl %ebp, %ebp
cmpl $0x3, %eax
je 0x8442
jmp 0x87a2
leaq 0xa(%rsp), %rsi
movb $-0x20, (%rsi)
movl (%rbx), %edi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
jne 0x87a2
movl (%rbx), %edi
leaq 0xa(%rsp), %rsi
movl $0x3, %edx
callq 0x2180
cmpq $0x3, %rax
jne 0x87a2
movzwl 0xa(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movzbl 0xc(%rsp), %ecx
movl %eax, %edx
shll $0x8, %edx
movl $0x988000, %esi         # imm = 0x988000
movl $0x800000, %edi         # imm = 0x800000
movl $0xff8000, %r8d         # imm = 0xFF8000
testl %edx, %edi
je 0x859d
movl %esi, %r9d
andl %r8d, %r9d
xorl %r9d, %edx
shrl %edi
shrl %r8d
shrl %esi
cmpl $0x80, %edi
jne 0x8590
movl $0x2, %ebp
cmpl %ecx, %edx
jne 0x85e5
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x2394(%rip), %xmm0     # 0xa958
mulsd 0x2394(%rip), %xmm0     # 0xa960
addsd 0x2394(%rip), %xmm0     # 0xa968
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
movb 0x2c(%rbx), %al
movb %al, 0x2e(%rbx)
xorl %ebp, %ebp
orl %r14d, %ebp
jmp 0x87a2
leaq 0xa(%rsp), %rsi
movb $0x0, (%rsi)
movl (%rbx), %edi
movl $0x1, %edx
callq 0x20b0
movl $0x4, %ebp
cmpq $0x1, %rax
jne 0x87a2
movl (%rbx), %edi
leaq 0xa(%rsp), %rsi
movl $0x6, %edx
callq 0x2180
movl $0x4, %ebp
cmpq $0x6, %rax
jne 0x87a2
movzwl 0xa(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edi
movzbl 0xc(%rsp), %eax
movl %edi, %ecx
shll $0x8, %ecx
movl $0x988000, %edx         # imm = 0x988000
movl $0x800000, %esi         # imm = 0x800000
movl $0xff8000, %r8d         # imm = 0xFF8000
testl %ecx, %esi
je 0x8663
movl %edx, %r9d
andl %r8d, %r9d
xorl %r9d, %ecx
shrl %esi
shrl %r8d
shrl %edx
cmpl $0x80, %esi
jne 0x8656
movzwl 0xd(%rsp), %edx
rolw $0x8, %dx
movzwl %dx, %r9d
movzbl 0xf(%rsp), %edx
movl %r9d, %esi
shll $0x8, %esi
movl $0x988000, %r8d         # imm = 0x988000
movl $0x800000, %r10d        # imm = 0x800000
movl $0xff8000, %r11d        # imm = 0xFF8000
testl %esi, %r10d
je 0x86a9
movl %r8d, %ebp
andl %r11d, %ebp
xorl %ebp, %esi
shrl %r10d
shrl %r11d
shrl %r8d
cmpl $0x80, %r10d
jne 0x869c
xorl %r8d, %r8d
cmpl %edx, %esi
setne %r10b
movzbl 0x30(%rbx), %r11d
cmpl $0x7, %r11d
je 0x873f
cmpl $0x6, %r11d
je 0x86ff
cmpl $0x2, %r11d
jne 0x877e
imull $0xa5, %edi, %edi
cvtsi2sd %edi, %xmm0
movsd 0x227f(%rip), %xmm1     # 0xa970
divsd %xmm1, %xmm0
addsd 0x228b(%rip), %xmm0     # 0xa988
jmp 0x875d
imull $0xaf, %edi, %edi
cvtsi2sd %edi, %xmm0
movsd 0x225f(%rip), %xmm1     # 0xa970
divsd %xmm1, %xmm0
addsd 0x225b(%rip), %xmm0     # 0xa978
cvtsd2ss %xmm0, %xmm2
imull $0x7d, %r9d, %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movss %xmm2, 0x24(%rbx)
divsd %xmm1, %xmm0
addsd 0x2243(%rip), %xmm0     # 0xa980
jmp 0x8775
imull $0xaf, %edi, %edi
cvtsi2sd %edi, %xmm0
movsd 0x221f(%rip), %xmm1     # 0xa970
divsd %xmm1, %xmm0
addsd 0x221b(%rip), %xmm0     # 0xa978
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
imull $0x64, %r9d, %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
cmpl %eax, %ecx
jne 0x878a
movb 0x2c(%rbx), %dil
movb %dil, 0x2e(%rbx)
movb %r10b, %r8b
cmpl %edx, %esi
jne 0x8797
movb 0x2d(%rbx), %dl
movb %dl, 0x2f(%rbx)
xorl %edx, %edx
cmpl %eax, %ecx
setne %dl
leal (%r8,%rdx,2), %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq