// File: /workspace/github_c_makefile_star2_O1/Openvario[P]sensord/humidity.c
// Function: sht4x_open
int sht4x_open (t_ds2482 *sensor, unsigned char i2c_address) {

	uint8_t data[6]={0x94,0x89,0,0,0,0};

	sensor->fd = open(I2C_DEVICE, O_RDWR);
	if (sensor->fd < 0) {
		fprintf(stderr, "Error opening file: %s\n", strerror(errno));
		return 1;
	}
	if (ioctl(sensor->fd, I2C_SLAVE, i2c_address) < 0) {
		fprintf(stderr, "ioctl error: %s\n", strerror(errno));
		return 1;
	}

	if (write(sensor->fd,data,1)!=1) return 2;                                    // Do SHT4x soft reset
	struct timespec nstime = {0,15e6};
	while (nanosleep (&nstime,&nstime)) ;                                           // Wait 15 microseconds after reset
	if (write(sensor->fd,data+2,1)!=1) return 2;                                    // Request Serial Number
	if (read(sensor->fd,data,6)!=6) {
		sensor->sensor_type=SHT4X;
		if (si7021_crc_check((data[0]<<8)|(data[1]),data[2])!=0) return 4;
		if (si7021_crc_check((data[3]<<8)|(data[4]),data[5])!=0) return 4;
		fprintf(stderr, "SHT4x, Serial Number: 0x%x%x%x%x - ",data[0],data[1],data[3],data[4]);
	} else {
		data[0]=0x30;
		data[1]=0xa2;
		data[2]=0x36;
		data[3]=0x82;
		if (write(sensor->fd,data,2)!=2) return 2;                                    // Do SHT4x soft reset
		nstime.tv_nsec=15e6;
		while (nanosleep (&nstime,&nstime)) ;                                           // Wait 15 microseconds after reset
		if (write(sensor->fd,data+2,2)!=2) return 2;                                    // Request Serial Number
		if (read(sensor->fd,data,6)!=6) {
			sensor->sensor_type=SHT85;
			if (si7021_crc_check((data[0]<<8)|(data[1]),data[2])!=0) return 5;
			if (si7021_crc_check((data[3]<<8)|(data[4]),data[5])!=0) return 5;
			fprintf(stderr, "SHT85, Serial Number: 0x%x%x%x%x - ",data[0],data[1],data[3],data[4]);
		} else return 3;
	}

	if (g_debug > 0) {
		if (sensor->sensor_type==SHT4X)
			fprintf(stderr, "Opened SHT4x\n");
		else
			fprintf(stderr, "Opened SHT85\n");
	}
	sensor->address = i2c_address;
	return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, %r14
movw $0x0, 0x8(%rsp)
movl $0x8994, 0x4(%rsp)      # imm = 0x8994
leaq 0x181a(%rip), %rdi      # 0x93d8
movl $0x2, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, (%r14)
testl %eax, %eax
js 0x7e40
movzbl %bl, %edx
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x7e47
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
movl $0x2, %ebp
cmpq $0x1, %rax
jne 0x7e51
movups 0x2f22(%rip), %xmm0     # 0xab38
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7c1f
movl (%r14), %edi
leaq 0x6(%rsp), %r15
movl $0x1, %edx
movq %r15, %rsi
callq 0x20b0
movl $0x2, %ebp
cmpq $0x1, %rax
jne 0x7e51
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
movl $0x6, %edx
callq 0x2180
cmpq $0x6, %rax
jne 0x7d64
leaq 0x4(%rsp), %rsi
movl $0x8236a230, (%rsi)     # imm = 0x8236A230
movl (%r14), %edi
movl $0x2, %edx
callq 0x20b0
cmpq $0x2, %rax
jne 0x7e51
leaq 0x10(%rsp), %r12
movq $0xe4e1c0, 0x8(%r12)    # imm = 0xE4E1C0
movq %r12, %rdi
movq %r12, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7c9e
movl (%r14), %edi
movl $0x2, %edx
movq %r15, %rsi
callq 0x20b0
cmpq $0x2, %rax
jne 0x7e51
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
movl $0x6, %edx
callq 0x2180
movl $0x3, %ebp
cmpq $0x6, %rax
je 0x7e51
movb $0x7, 0x30(%r14)
movzbl 0x4(%rsp), %r15d
movl %r15d, %edi
shll $0x8, %edi
movzbl 0x5(%rsp), %r12d
orl %r12d, %edi
movzbl 0x6(%rsp), %esi
callq 0x7b5c
movl $0x5, %ebp
testl %eax, %eax
jne 0x7e51
movzbl 0x7(%rsp), %edi
movl %edi, 0xc(%rsp)
shll $0x8, %edi
movzbl 0x8(%rsp), %r13d
orl %r13d, %edi
movzbl 0x9(%rsp), %esi
callq 0x7b5c
testl %eax, %eax
jne 0x7e51
movq 0x62b1(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2bf7(%rip), %rsi      # 0xa948
movl %r15d, %edx
movl %r12d, %ecx
movl 0xc(%rsp), %r8d
movl %r13d, %r9d
jmp 0x7e25
movb $0x6, 0x30(%r14)
movzbl 0x4(%rsp), %edx
movzbl 0x5(%rsp), %ecx
movzbl 0x6(%rsp), %eax
movl %edx, %edi
shll $0x10, %edi
movl %ecx, %esi
shll $0x8, %esi
orl %edi, %esi
movl $0x988000, %edi         # imm = 0x988000
movl $0x800000, %r8d         # imm = 0x800000
movl $0xff8000, %r9d         # imm = 0xFF8000
testl %esi, %r8d
je 0x7da3
movl %edi, %r10d
andl %r9d, %r10d
xorl %r10d, %esi
shrl %r8d
shrl %r9d
shrl %edi
cmpl $0x80, %r8d
jne 0x7d95
movl $0x4, %ebp
cmpl %eax, %esi
jne 0x7e51
movzbl 0x7(%rsp), %r8d
movzbl 0x8(%rsp), %r9d
movzbl 0x9(%rsp), %eax
movl %r8d, %edi
shll $0x10, %edi
movl %r9d, %esi
shll $0x8, %esi
orl %edi, %esi
movl $0x988000, %edi         # imm = 0x988000
movl $0x800000, %r10d        # imm = 0x800000
movl $0xff8000, %r11d        # imm = 0xFF8000
testl %esi, %r10d
je 0x7dff
movl %edi, %r15d
andl %r11d, %r15d
xorl %r15d, %esi
shrl %r10d
shrl %r11d
shrl %edi
cmpl $0x80, %r10d
jne 0x7df1
cmpl %eax, %esi
jne 0x7e51
movq 0x61dd(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2aff(%rip), %rsi      # 0xa924
xorl %eax, %eax
callq 0x21c0
leaq 0x6641(%rip), %rax      # 0xe474
cmpl $0x0, (%rax)
jg 0x7e62
movb %bl, 0x4(%r14)
xorl %ebp, %ebp
jmp 0x7e51
callq 0x3964
jmp 0x7e4c
callq 0x3938
movl $0x1, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x3907
jmp 0x7e38