// File: /workspace/github_c_makefile_star2_O1/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 0x1552(%rip), %rdi      # 0x93d8
movl $0x2, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, (%r14)
testl %eax, %eax
js 0x81dd
movzbl %bl, %ebp
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x81e4
movabsq $0xa081ee70083e6fe, %rax # imm = 0xA081EE70083E6FE
leaq 0x15(%rsp), %rsi
movq %rax, -0x5(%rsi)
movl $0xc9fc0ffa, 0x20(%rsp) # imm = 0xC9FC0FFA
movl (%r14), %edi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
je 0x7f48
movups 0x2c4b(%rip), %xmm0     # 0xab38
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7ef6
movl (%r14), %edi
leaq 0x16(%rsp), %r15
movl $0x1, %edx
movq %r15, %rsi
callq 0x20b0
movb $0x1, %r12b
cmpq $0x1, %rax
jne 0x7f25
jmp 0x7f3f
movl (%r14), %edi
movl $0x1, %edx
movq %r15, %rsi
callq 0x2180
cmpq $0x1, %rax
je 0x8201
testb %r12b, %r12b
je 0x81ef
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
testq %rax, %rax
js 0x81d5
movups 0x2bce(%rip), %xmm0     # 0xab38
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7f73
movl (%r14), %edi
leaq 0x11(%rsp), %r12
movl $0x2, %edx
movq %r12, %rsi
callq 0x20b0
movl $0x2, %r15d
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
leaq 0x14(%rsp), %r13
movl $0x1, %edx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
leaq 0x13(%rsp), %rsi
movl $0x1, %edx
callq 0x2180
movl $0x3, %r15d
testq %rax, %rax
js 0x81ef
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x81ef
movl 0x3c(%r14), %eax
leal -0x58(,%rax,8), %ecx
movl $0x820383, %edx         # imm = 0x820383
shrl %cl, %edx
addl $-0xb, %eax
cmpl $0x3, %eax
movl $0x2, %eax
cmovbl %edx, %eax
movb %al, 0x12(%rsp)
movl (%r14), %edi
movl $0x2, %edx
movq %r12, %rsi
callq 0x20b0
movl $0x4, %r15d
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
movl $0x1, %edx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
movl $0x1, %edx
leaq 0x13(%rsp), %rsi
callq 0x2180
testq %rax, %rax
js 0x81ef
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x81ef
movl (%r14), %edi
leaq 0x20(%rsp), %rsi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x8, %edx
callq 0x2180
testq %rax, %rax
js 0x81ef
xorl %eax, %eax
xorl %r12d, %r12d
movzbl 0x10(%rsp,%rax), %ecx
movzbl 0x11(%rsp,%rax), %edx
movl %ecx, %esi
shll $0x8, %esi
movl $0x988000, %edi         # imm = 0x988000
movl $0x800000, %r8d         # imm = 0x800000
movl $0xff8000, %r9d         # imm = 0xFF8000
testl %esi, %r8d
je 0x80e5
movl %edi, %r10d
andl %r9d, %r10d
xorl %r10d, %esi
shrl %r8d
shrl %r9d
shrl %edi
cmpl $0x80, %r8d
jne 0x80d7
cmpl %edx, %esi
jne 0x81ef
shll $0x8, %r12d
orl %ecx, %r12d
leaq 0x2(%rax), %rcx
cmpq $0x6, %rax
movq %rcx, %rax
jb 0x80b7
movl (%r14), %edi
leaq 0x22(%rsp), %rsi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x6, %edx
callq 0x2180
testq %rax, %rax
js 0x81ef
movl %r12d, 0x24(%rsp)
movl $0x0, 0x1c(%rsp)
movq $-0x3, %r13
movzbl 0x13(%rsp,%r13), %r12d
shll $0x8, %r12d
movzbl 0x14(%rsp,%r13), %eax
movq %rax, 0x28(%rsp)
leal (%r12,%rax), %edi
movzbl 0x15(%rsp,%r13), %esi
callq 0x7b5c
testl %eax, %eax
jne 0x81ef
movl 0x1c(%rsp), %eax
shll $0x10, %eax
orl %r12d, %eax
orl 0x28(%rsp), %eax
movl %eax, 0x1c(%rsp)
addq $0x3, %r13
jb 0x815c
movzbl 0x10(%rsp), %r9d
cmpl $0x13, %r9d
jle 0x82a4
leal -0x14(%r9), %eax
cmpl $0x2, %eax
jb 0x82af
cmpl $0x32, %r9d
je 0x8376
cmpl $0xff, %r9d
je 0x82c7
jmp 0x82ee
movl $0x2, %r15d
jmp 0x81ef
callq 0x3ac8
jmp 0x81e9
callq 0x3a9c
movl $0x1, %r15d
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5df0(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x2776(%rip), %rdi      # 0xa988
movl $0x4, %esi
movl $0x1, %edx
callq 0x22a0
movl $0x3, %r15d
cmpb $0x0, 0x16(%rsp)
jne 0x8289
movl (%r14), %edi
leaq 0x17(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
jne 0x8289
movl (%r14), %edi
leaq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x2180
cmpq $0x4, %rax
jne 0x8289
movzbl 0x20(%rsp), %eax
shll $0x10, %eax
movzbl 0x21(%rsp), %edi
shll $0x8, %edi
orl %eax, %edi
movzbl 0x22(%rsp), %esi
orl %esi, %edi
callq 0x7b5c
xorl %r15d, %r15d
testl %eax, %eax
je 0x8291
movl $0x4, %r15d
xorl %r12d, %r12d
jmp 0x7f3f
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movl %ebp, %edx
movl %ebp, %ecx
callq 0x3990
jmp 0x8289
testl %r9d, %r9d
je 0x82c7
cmpl $0xd, %r9d
jne 0x82ee
movb $0x3, 0x30(%r14)
movq 0x5d3d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2734(%rip), %rsi      # 0xa9f9
jmp 0x8304
movb $0x3, 0x30(%r14)
movq 0x5d25(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x26e9(%rip), %rsi      # 0xa9c6
movl 0x24(%rsp), %edx
movl 0x1c(%rsp), %ecx
xorl %eax, %eax
callq 0x21c0
jmp 0x8317
movb $0x3, 0x30(%r14)
movq 0x5cfe(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2763(%rip), %rsi      # 0xaa67
movl %r9d, %edx
movl 0x24(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorl %eax, %eax
callq 0x21c0
cmpb $0x3, 0x30(%r14)
jne 0x83e4
leaq 0x10(%rsp), %rsi
movw $0xb884, (%rsi)         # imm = 0xB884
movl (%r14), %edi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x81ef
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x2180
testq %rax, %rax
js 0x81ef
movzbl 0x10(%rsp), %edx
cmpl $0x20, %edx
je 0x83c5
cmpl $0xff, %edx
jne 0x83cc
callq 0x3a4c
jmp 0x83e4
movb $0x1, 0x30(%r14)
movzwl 0x1c(%rsp), %eax
movq 0x5c71(%rip), %rcx      # 0xdff8
movq (%rcx), %rdi
cmpl $0x4854, %eax           # imm = 0x4854
jne 0x83bb
movzbl 0x13(%rsp), %edx
movzbl 0x14(%rsp), %ecx
movzbl 0x11(%rsp), %eax
movl %eax, (%rsp)
leaq 0x2696(%rip), %rsi      # 0xaa40
movl 0x24(%rsp), %r8d
xorl %eax, %eax
callq 0x21c0
jmp 0x8317
callq 0x3a0f
jmp 0x8317
callq 0x3a2a
jmp 0x83e4
movq 0x5c25(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x26ea(%rip), %rsi      # 0xaac7
xorl %eax, %eax
callq 0x21c0
leaq 0x6089(%rip), %rax      # 0xe474
cmpl $0x0, (%rax)
jg 0x83fc
movb %bl, 0x4(%r14)
xorl %r15d, %r15d
jmp 0x81ef
leaq 0x30(%r14), %rdi
movl %ebp, %esi
callq 0x3a6e
jmp 0x83f0