// File: /workspace/github_c_makefile_star2_O2/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
andw $0x0, 0x8(%rsp)
movl $0x8994, 0x4(%rsp)      # imm = 0x8994
leaq 0x16e5(%rip), %rdi      # 0x83d8
pushq $0x2
popq %rbp
movl %ebp, %esi
xorl %eax, %eax
callq 0x2240
movl %eax, (%r14)
testl %eax, %eax
js 0x6e9c
movzbl %bl, %edx
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x6ebb
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
jne 0x6ee8
movups 0x2e74(%rip), %xmm0     # 0x9bb8
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x6d4d
movl (%r14), %edi
leaq 0x6(%rsp), %r15
pushq $0x1
popq %rdx
movq %r15, %rsi
callq 0x20b0
pushq $0x2
popq %rbp
cmpq $0x1, %rax
jne 0x6ee8
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x2180
cmpq $0x6, %rax
jne 0x6e08
leaq 0x4(%rsp), %rsi
movl $0x8236a230, (%rsi)     # imm = 0x8236A230
movl (%r14), %edi
pushq $0x2
popq %rdx
callq 0x20b0
cmpq $0x2, %rax
jne 0x6ee8
leaq 0x10(%rsp), %r12
movq $0xe4e1c0, 0x8(%r12)    # imm = 0xE4E1C0
movq %r12, %rdi
movq %r12, %rsi
callq 0x2120
testl %eax, %eax
jne 0x6dc0
movl (%r14), %edi
pushq $0x2
popq %rdx
movq %r15, %rsi
callq 0x20b0
cmpq $0x2, %rax
jne 0x6ee8
movl (%r14), %edi
leaq 0x4(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x2180
cmpq $0x6, %rax
jne 0x6ef9
pushq $0x3
jmp 0x6ee7
movb $0x6, 0x30(%r14)
movzbl 0x4(%rsp), %r15d
movl %r15d, %edi
shll $0x8, %edi
movzbl 0x5(%rsp), %r12d
orl %r12d, %edi
movzbl 0x6(%rsp), %esi
callq 0x6c90
pushq $0x4
popq %rbp
testl %eax, %eax
jne 0x6ee8
movzbl 0x7(%rsp), %edi
movl %edi, 0xc(%rsp)
shll $0x8, %edi
movzbl 0x8(%rsp), %r13d
orl %r13d, %edi
movzbl 0x9(%rsp), %esi
callq 0x6c90
testl %eax, %eax
jne 0x6ee8
movq 0x6193(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2b35(%rip), %rsi      # 0x99a4
movl %r15d, %edx
movl %r12d, %ecx
movl 0xc(%rsp), %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x21c0
leaq 0x65e9(%rip), %rax      # 0xd474
cmpl $0x0, (%rax)
jg 0x6f5d
movb %bl, 0x4(%r14)
xorl %ebp, %ebp
jmp 0x6ee8
movq 0x6155(%rip), %rax      # 0xcff8
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22e0
leaq 0x152a(%rip), %rsi      # 0x83e3
jmp 0x6ed8
movq 0x6136(%rip), %rax      # 0xcff8
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22e0
leaq 0x1168(%rip), %rsi      # 0x8040
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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 0x6c90
pushq $0x5
popq %rbp
testl %eax, %eax
jne 0x6ee8
movzbl 0x7(%rsp), %edi
movl %edi, 0xc(%rsp)
shll $0x8, %edi
movzbl 0x8(%rsp), %r13d
orl %r13d, %edi
movzbl 0x9(%rsp), %esi
callq 0x6c90
testl %eax, %eax
jne 0x6ee8
movq 0x60aa(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2a70(%rip), %rsi      # 0x99c8
jmp 0x6e6f
cmpb $0x6, 0x30(%r14)
movq 0x608f(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x2a79(%rip), %rax      # 0x99ec
leaq 0x2a80(%rip), %rdi      # 0x99fa
cmoveq %rax, %rdi
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x6e94