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

	int sernuma, sernumb;
	uint8_t i;

	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;
	}

	uint8_t data[8]={0xfe,0xe6,131,0,0xe7,0x1e,0x08,0x0a};
	uint8_t data2[4]={0xfa,0x0f,0xfc,0xc9};

	if (write(sensor->fd,data+5,1)!=1) {                                    // Do HTU31D soft reset
		struct timespec nstime = {0,15e6};
		while (nanosleep (&nstime,&nstime)) ;                                           // Wait 15 microseconds after reset
		if (write(sensor->fd,data+6,1)!=1) {					// Request Status byte
			if (read(sensor->fd,data+6,1)==1) {
				fprintf(stderr, "In4\n");
				if (data[6]!=0) return 3;						// Confirm status is good
				if (write(sensor->fd,data+7,1)!=1) return 3;				// Request Serial Number
				if (read(sensor->fd,data2,4)!=4) return 3;				// Read Serial Number
				if (si7021_crc_check((data2[0]<<16)|(data2[1]<<8)|(data2[2]),data2[2])>0) return 4; // Verify CRC on serial number
				switch (sensor->databits) {                                             // Set proper bit configuration
					case 11 : sensor->databits=0x40; break;
					case 12 : sensor->databits=0x4a; break;
					case 13 : sensor->databits=0x54; break;
					case 14 :
					default : sensor->databits=0x5e; break;
				}
				fprintf(stderr, "HTU31D, Serial Number 0x%x%x%x - ",data[0],data[1],data[2]);
				sensor->sensor_type=HTU31D;
				if (g_debug > 0) fprintf(stderr, "Opened HTU31D on 0x%x\n", i2c_address);         // Debug info
				sensor->address = i2c_address;
				return 0;
			}
		}
	}
	if (write(sensor->fd,data,1)<0) return 2;					// Do soft reset for HTU21D/Si7021
	struct timespec nstime = {0,15e6};
	while (nanosleep (&nstime,&nstime)) ;						// Wait 15 microseconds after reset
	if (write(sensor->fd,data+1,2)<0) return 2;					// Program for 11 bits
	if (write(sensor->fd,data+4,1)<0) return 2;					// Initiate readback configuration
	if (read (sensor->fd,data+3,1)<0) return 3;					// Readback configuration
	if ((data[2]&129)!=(data[3]&129)) return 3;					// Is it valid?

	switch (sensor->databits) {							// Set proper bit configuration
		case 11 : data[2] = 131; break;
		case 12 : data[2] = 3;   break;
		case 13 : data[2] = 130; break;
		case 14 :
		default : data[2] = 2;   break;
	}
	if (write(sensor->fd,data+1,2)<0) return 4;					// Program proper bit configuration
	if (write(sensor->fd,data+4,1)<0) return 4;					// Initiate readback configuration
	if (read(sensor->fd,data+3,1)<0) return 4;					// Read back configuration
	if ((data[2]&129)!=(data[3]&129)) return 4; 					// Is it valid?
	if (write(sensor->fd,data2,2)<0) return 4;					// Initiate Serial Number A readback
	if (read (sensor->fd,data,8)<0) return 4;					// Readback Serial Number A
	for (i=sernuma=0 ; i<8 ; i+=2) {
		if (si7021_crc_check(data[i],data[i+1])==1) return 4;			// Check for valid CRC
		sernuma=(sernuma<<8)|(data[i]);
	}

	if (write(sensor->fd,data2+2,2)<0) return 4;					// Initiate Serial Number B readback
	if (read (sensor->fd,data,6)<0) return 4;					// Readback Serial Number B
	for (i=sernumb=0 ; i<6 ; i+=3) {
		if (si7021_crc_check((data[i]<<8)|(data[i+1]),data[i+2])==1) return 4;	// Check for valid CRC
		sernumb=(sernumb<<16)|(data[i]<<8)|(data[i+1]);				// Concatenate Serial Number B
	}

	switch (data[0]) {								// Display Serial number and set sensor type
		case 0x00 :
		case 0xff : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI7021, Engineering Sample, Serial Number: 0x%x%x\n",sernuma,sernumb); break;
		case 0x0d :
		case 0x14 :
		case 0x15 : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI70%d, Serial Number: 0x%x%x\n",data[0],sernuma,sernumb); break;
		case 0x32 : sensor->sensor_type=HTU21D;
			    if ((sernumb&0xffff)!=0x4854)
				fprintf(stderr, "HTU21D, but serial number is invalid - ");
			    else
				fprintf(stderr, "HTU21D, Serial Number: 0x%x%x%x%x%x - ",data[3],data[4],sernuma,data[0],data[1]);
			    break;
		default   : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI70%d, UNKNOWN SENSOR, Serial Number 0x%x%x\n",data[0],sernuma,sernumb);
	}
	if (sensor->sensor_type==SI7021) {						// If it's a SI7021 get the firmware revision
		data[0]=0x84;
		data[1]=0xb8;
		if (write(sensor->fd,data,2)<0) return 4;
		if (read(sensor->fd,data,1)<0) return 4;
		switch (data[0]) {
			case 0xff : fprintf(stderr, "Firmware revision 1.0 - "); break;
			case 0x20 : fprintf(stderr, "Firmware revision 2.0 - "); break;
			default   : fprintf(stderr, "Firmware revision UNKNOWN 0x%x - ",data[0]);
		}
	}

	if (g_debug > 0) {
		if (sensor->sensor_type==SI7021) fprintf(stderr, "Opened SI7021/HTU21D on 0x%x\n", i2c_address);		// Debug info
		else fprintf(stderr, "Opened HTU21D on 0x%x\n",i2c_address);
	}
	sensor->address = i2c_address;
	return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebx
movq %rdi, %r14
leaq 0x1430(%rip), %rdi      # 0x83d8
pushq $0x2
popq %r15
movl %r15d, %esi
xorl %eax, %eax
callq 0x2240
movl %eax, (%r14)
testl %eax, %eax
js 0x71ee
movzbl %bl, %ebp
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x720d
movabsq $0xa081ee70083e6fe, %rax # imm = 0xA081EE70083E6FE
leaq 0x15(%rsp), %rsi
movq %rax, -0x5(%rsi)
movl $0xc9fc0ffa, 0x24(%rsp) # imm = 0xC9FC0FFA
movl (%r14), %edi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
je 0x705d
pushq $0x3
popq %r13
movups 0x2ba5(%rip), %xmm0     # 0x9bb8
leaq 0x30(%rsp), %r12
movaps %xmm0, (%r12)
movq %r12, %rdi
movq %r12, %rsi
callq 0x2120
testl %eax, %eax
jne 0x701d
movl (%r14), %edi
leaq 0x16(%rsp), %r12
pushq $0x1
popq %rdx
movq %r12, %rsi
callq 0x20b0
cmpq $0x1, %rax
je 0x705d
movl (%r14), %edi
pushq $0x1
popq %rdx
movq %r12, %rsi
callq 0x2180
cmpq $0x1, %rax
je 0x724d
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x20b0
testq %rax, %rax
js 0x723b
movups 0x2b3b(%rip), %xmm0     # 0x9bb8
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7086
movl (%r14), %edi
leaq 0x11(%rsp), %r12
pushq $0x2
popq %rdx
movq %r12, %rsi
callq 0x20b0
pushq $0x2
popq %r15
testq %rax, %rax
js 0x723b
movl (%r14), %edi
leaq 0x14(%rsp), %r13
pushq $0x1
popq %rdx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x723b
movl (%r14), %edi
leaq 0x13(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2180
pushq $0x3
popq %r15
testq %rax, %rax
js 0x723b
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x723b
movl 0x3c(%r14), %eax
leal -0x58(,%rax,8), %ecx
movl $0x820383, %edx         # imm = 0x820383
shrl %cl, %edx
addl $-0xb, %eax
cmpl $0x3, %eax
pushq $0x2
popq %rax
cmovbl %edx, %eax
movb %al, 0x12(%rsp)
movl (%r14), %edi
pushq $0x2
popq %rdx
movq %r12, %rsi
callq 0x20b0
pushq $0x4
popq %r15
testq %rax, %rax
js 0x723b
movl (%r14), %edi
pushq $0x1
popq %rdx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x723b
movl (%r14), %edi
pushq $0x1
popq %rdx
leaq 0x13(%rsp), %rsi
callq 0x2180
testq %rax, %rax
js 0x723b
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x723b
movl (%r14), %edi
leaq 0x24(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x20b0
testq %rax, %rax
js 0x723b
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x2180
testq %rax, %rax
js 0x723b
xorl %r12d, %r12d
movl $0x0, 0x20(%rsp)
cmpq $0x7, %r12
ja 0x72ce
movzbl 0x10(%rsp,%r12), %r13d
movzbl 0x11(%rsp,%r12), %esi
movl %r13d, %edi
callq 0x6c90
testl %eax, %eax
jne 0x723b
movl 0x20(%rsp), %eax
shll $0x8, %eax
orl %r13d, %eax
movl %eax, 0x20(%rsp)
addq $0x2, %r12
jmp 0x71b8
movq 0x5e03(%rip), %rax      # 0xcff8
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22e0
leaq 0x11d8(%rip), %rsi      # 0x83e3
jmp 0x722a
movq 0x5de4(%rip), %rax      # 0xcff8
movq (%rax), %rbx
callq 0x2040
movl (%rax), %edi
callq 0x22e0
leaq 0xe16(%rip), %rsi       # 0x8040
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5da4(%rip), %r15      # 0xcff8
movq (%r15), %rcx
leaq 0x27aa(%rip), %rdi      # 0x9a08
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
cmpb $0x0, 0x16(%rsp)
jne 0x72c6
movl (%r14), %edi
leaq 0x17(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
jne 0x72c6
movl (%r14), %edi
leaq 0x24(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2180
cmpq $0x4, %rax
jne 0x72c6
movzbl 0x24(%rsp), %eax
shll $0x10, %eax
movzbl 0x25(%rsp), %edi
shll $0x8, %edi
orl %eax, %edi
movzbl 0x26(%rsp), %esi
orl %esi, %edi
callq 0x6c90
pushq $0x4
popq %r13
testl %eax, %eax
je 0x735c
movl %r13d, %r15d
jmp 0x723b
movl (%r14), %edi
leaq 0x26(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x20b0
testq %rax, %rax
js 0x723b
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
pushq $0x6
popq %rdx
callq 0x2180
testq %rax, %rax
js 0x723b
xorl %ecx, %ecx
movl $0x0, 0x1c(%rsp)
cmpq $0x5, %rcx
ja 0x73b4
movzbl 0x10(%rsp,%rcx), %r12d
shll $0x8, %r12d
movzbl 0x11(%rsp,%rcx), %r13d
leal (%r12,%r13), %edi
movq %rcx, 0x28(%rsp)
movzbl 0x12(%rsp,%rcx), %esi
callq 0x6c90
testl %eax, %eax
jne 0x723b
movl 0x1c(%rsp), %eax
shll $0x10, %eax
orl %eax, %r12d
orl %r13d, %r12d
movq 0x28(%rsp), %rcx
addq $0x3, %rcx
movl %r12d, 0x1c(%rsp)
jmp 0x730a
movl 0x3c(%r14), %eax
imull $0xa, %eax, %ecx
addl $-0xb, %eax
addl $-0x2e, %ecx
cmpl $0x3, %eax
pushq $0x5e
popq %rax
cmovbl %ecx, %eax
movl %eax, 0x3c(%r14)
movq (%r15), %rdi
movzbl 0x10(%rsp), %edx
movzbl 0x11(%rsp), %ecx
movzbl 0x12(%rsp), %r8d
leaq 0x267d(%rip), %rsi      # 0x9a0d
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x21c0
movb $0x2, 0x30(%r14)
leaq 0x60ce(%rip), %rax      # 0xd474
cmpl $0x0, (%rax)
jg 0x741d
movb %bl, 0x4(%r14)
jmp 0x72c6
movzbl 0x10(%rsp), %edx
leal -0x14(%rdx), %eax
cmpl $0x2, %eax
jb 0x7405
cmpl $0xff, %edx
je 0x73db
cmpl $0xd, %edx
je 0x7405
cmpl $0x32, %edx
je 0x7435
testl %edx, %edx
jne 0x7481
movb $0x3, 0x30(%r14)
movq 0x5c11(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2655(%rip), %rsi      # 0x9a46
movl 0x20(%rsp), %edx
movl 0x1c(%rsp), %ecx
xorl %eax, %eax
callq 0x21c0
jmp 0x74a7
movb $0x3, 0x30(%r14)
movq 0x5be7(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x265e(%rip), %rsi      # 0x9a79
jmp 0x7497
movq (%r15), %rdi
leaq 0x2608(%rip), %rsi      # 0x9a2f
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x73ab
movb $0x1, 0x30(%r14)
movq 0x5bb7(%rip), %rax      # 0xcff8
movq (%rax), %rcx
cmpw $0x4854, 0x1c(%rsp)     # imm = 0x4854
jne 0x750e
movzbl 0x13(%rsp), %edx
movzbl 0x14(%rsp), %eax
movzbl 0x11(%rsp), %esi
movl %esi, (%rsp)
leaq 0x2656(%rip), %rsi      # 0x9ac0
pushq $0x32
popq %r9
movq %rcx, %rdi
movl %eax, %ecx
movl 0x20(%rsp), %r8d
xorl %eax, %eax
callq 0x21c0
jmp 0x74a7
movb $0x3, 0x30(%r14)
movq 0x5b6b(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x2650(%rip), %rsi      # 0x9ae7
movl 0x20(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorl %eax, %eax
callq 0x21c0
cmpb $0x3, 0x30(%r14)
jne 0x754a
leaq 0x10(%rsp), %rsi
movw $0xb884, (%rsi)         # imm = 0xB884
movl (%r14), %edi
pushq $0x2
popq %rdx
callq 0x20b0
testq %rax, %rax
js 0x723b
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2180
testq %rax, %rax
js 0x723b
movzbl 0x10(%rsp), %edx
movq 0x5b03(%rip), %rax      # 0xcff8
cmpl $0x20, %edx
je 0x7522
cmpl $0xff, %edx
jne 0x7539
movq (%rax), %rcx
leaq 0x2609(%rip), %rdi      # 0x9b15
jmp 0x752c
leaq 0x2583(%rip), %rdi      # 0x9a98
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x74a7
movq (%rax), %rcx
leaq 0x2602(%rip), %rdi      # 0x9b2e
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x754a
movq (%rax), %rdi
leaq 0x2604(%rip), %rsi      # 0x9b47
xorl %eax, %eax
callq 0x21c0
leaq 0x5f23(%rip), %rax      # 0xd474
cmpl $0x0, (%rax)
jg 0x7562
movb %bl, 0x4(%r14)
xorl %r15d, %r15d
jmp 0x723b
cmpb $0x3, 0x30(%r14)
movq 0x5a8a(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x25f1(%rip), %rax      # 0x9b69
leaq 0x2608(%rip), %rsi      # 0x9b87
cmoveq %rax, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x7556