// File: /workspace/github_c_makefile_star2_O2/Openvario[P]sensord/ds2482.c
// Function: OWSearch
int OWSearch(t_ds2482 *sensor) {
	// server.log("Function: OneWire Search");

	int deviceAddress4ByteMask = 1;

	sensor->temp_present=0;
	if (sensor->owLastDevice) {
		// server.log("OneWire Search Complete");
		sensor->owLastDevice = 0;
		sensor->owLastDiscrepancy = 0;
		sensor->owDeviceAddress[0] = 0xFFFFFFFF;
		sensor->owDeviceAddress[1] = 0xFFFFFFFF;
	}
	if (!sensor->owLastDevice) { // if the last call was not the last one
		if (OWReset(sensor)==0) { // if there are no parts on 1-wire, return false
			sensor->owLastDiscrepancy = 0;
			return 0;
		}
		int bitNumber = 1;
		int lastZero = 0;
		int deviceAddress4ByteIndex = 0; // Fill last 4 bytes first, data from onewire comes LSB first.

		OWWriteByte(sensor, 0xF0); //Issue the Search ROM command
		do { // loop to do the search
			if (bitNumber < sensor->owLastDiscrepancy) {
				if (sensor->owDeviceAddress[deviceAddress4ByteIndex] & deviceAddress4ByteMask)
					sensor->owTriplet |= 4;  else sensor->owTriplet &= 3;
			} else if (bitNumber == sensor->owLastDiscrepancy) // if equal to last pick 1, if not pick 0
				sensor->owTriplet |= 4; else sensor->owTriplet &= 3;
			if (!OWTriplet(sensor)) return 0;

			// if 0 was picked then record its position in lastZero
			if (sensor->owTriplet==0) lastZero = bitNumber;

			// check for no devices on 1-wire
			if ((sensor->owTriplet&3)==3) break;

			// set or clear the bit in the SerialNum byte serial_byte_number with mask
			if (sensor->owTriplet&4)
				sensor->owDeviceAddress[deviceAddress4ByteIndex] |= deviceAddress4ByteMask;
			else
				sensor->owDeviceAddress[deviceAddress4ByteIndex] &= ~deviceAddress4ByteMask;
			bitNumber++; // increment the byte counter bit number
			deviceAddress4ByteMask <<=  1; // shift the bit mask left

			if (!deviceAddress4ByteMask) { // if the mask is 0 then go to other address block and reset mask to first bit
				deviceAddress4ByteIndex++;
				deviceAddress4ByteMask = 1;
			}
		} while (deviceAddress4ByteIndex <2);

		if (bitNumber == 65) { // if the search was successful then
			sensor->owLastDiscrepancy = lastZero;
			if (sensor->owLastDiscrepancy==0) sensor->owLastDevice = 1; else sensor->owLastDevice = 0;
			// server.log(format("OneWire Device Address = %.8X%.8X", owDeviceAddress[1], owDeviceAddress[0]));
			if (OWCheckCRC(sensor)) {
				if ((sensor->owDeviceAddress[0] & 0xff) == 0x28) {
					sensor->temp_present=1; return 1;
				} else {
					// server.log("OneWire device address CRC check failed");
					return 1;
				}
			}
		}
	}
	// server.log("No One-Wire Devices Found, Resetting Search");
	sensor->owLastDiscrepancy = 0;
	sensor->owLastDevice = 0;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movb $0x0, 0x2c(%rdi)
cmpl $0x0, 0x1c(%rdi)
je 0x68ee
andq $0x0, 0x1c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rbx)
movq %rax, 0x10(%rbx)
movq %rbx, %rdi
callq 0x6546
testl %eax, %eax
je 0x69e7
movq %rbx, %rdi
movl $0xf0, %esi
callq 0x65de
xorl %r14d, %r14d
pushq $0x1
popq %r13
movl %r13d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r12d, %ecx
movslq %r13d, %rdx
cmpl 0x20(%rbx), %r15d
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
jge 0x693f
movl 0x18(%rbx), %eax
testq %rdx, 0x8(%rbx,%rcx,8)
je 0x6944
orl $0x4, %eax
jmp 0x6947
movl 0x18(%rbx), %eax
je 0x693a
andl $0x3, %eax
movl %eax, 0x18(%rbx)
movq %rbx, %rdi
callq 0x681d
testl %eax, %eax
je 0x69f4
movl 0x18(%rbx), %eax
testl %eax, %eax
cmovel %r15d, %r14d
movl %eax, %ecx
notl %ecx
testb $0x3, %cl
je 0x69b7
testb $0x4, %al
jne 0x6983
movl %r13d, %eax
notl %eax
cltq
movq 0x8(%rsp), %rcx
andq %rax, 0x8(%rbx,%rcx,8)
jmp 0x6992
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
orq %rcx, 0x8(%rbx,%rax,8)
addl %r13d, %r13d
cmpl $0x1, %r13d
movl %r13d, %eax
adcl $0x0, %eax
cmpl $0x1, %r13d
adcl $0x0, %r12d
incl %r15d
movl %eax, %r13d
cmpl $0x2, %r12d
jb 0x691a
cmpl $0x41, %r15d
jne 0x69ef
movl %r14d, 0x20(%rbx)
xorl %eax, %eax
testl %r14d, %r14d
sete %al
movl %eax, 0x1c(%rbx)
movq %rbx, %rdi
callq 0x6a05
testl %eax, %eax
je 0x69ef
pushq $0x1
popq %rbp
cmpb $0x28, 0x8(%rbx)
jne 0x69f4
movb $0x1, 0x2c(%rbx)
jmp 0x69f4
andl $0x0, 0x20(%rbx)
xorl %ebp, %ebp
jmp 0x69f4
andq $0x0, 0x1c(%rbx)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq