// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdi, %rbx
movb $0x0, 0x2c(%rdi)
cmpl $0x0, 0x1c(%rdi)
je 0x77cb
movq $0x0, 0x1c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rbx)
movq %rax, 0x10(%rbx)
cmpl $0x0, 0x1c(%rbx)
je 0x77ea
movq $0x0, 0x1c(%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x7404
testl %eax, %eax
je 0x78c7
movq %rbx, %rdi
movl $0xf0, %esi
callq 0x7498
xorl %ebp, %ebp
movl $0x1, %r12d
movl $0x1, %r14d
xorl %r15d, %r15d
movl %r15d, %r13d
cmpl 0x20(%rbx), %r14d
jge 0x7831
movslq %r12d, %rax
testq %rax, 0x8(%rbx,%r13,8)
je 0x7833
orb $0x4, 0x18(%rbx)
jmp 0x7837
je 0x782b
andl $0x3, 0x18(%rbx)
movq %rbx, %rdi
callq 0x76fc
testl %eax, %eax
je 0x78d5
movl 0x18(%rbx), %eax
testl %eax, %eax
cmovel %r14d, %ebp
movl %eax, %ecx
notl %ecx
testb $0x3, %cl
je 0x7894
testb $0x4, %al
jne 0x786b
movl %r12d, %eax
notl %eax
cltq
andq %rax, 0x8(%rbx,%r13,8)
jmp 0x7873
movslq %r12d, %rax
orq %rax, 0x8(%rbx,%r13,8)
addl %r12d, %r12d
cmpl $0x1, %r12d
movl %r12d, %eax
adcl $0x0, %eax
cmpl $0x1, %r12d
adcl $0x0, %r15d
incl %r14d
movl %eax, %r12d
cmpl $0x2, %r15d
jb 0x7818
movb $0x1, %r15b
cmpl $0x41, %r14d
jne 0x78d3
movl %ebp, 0x20(%rbx)
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movl %eax, 0x1c(%rbx)
movq %rbx, %rdi
callq 0x78e8
testl %eax, %eax
je 0x78d3
movl $0x1, %eax
cmpb $0x28, 0x8(%rbx)
jne 0x78d7
movb $0x1, 0x2c(%rbx)
jmp 0x78d7
movl $0x0, 0x20(%rbx)
jmp 0x77d9
jmp 0x78da
xorl %eax, %eax
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0x77d1
jmp 0x77db