#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ IDGetBit(unsigned long long *, int);
int /*<<< orphan*/ IDSetBit(unsigned long long *, int, int);
int /*<<< orphan*/ WriteBit(int);
int /*<<< orphan*/ doReset();
char readBit();
int /*<<< orphan*/ writeByte(int);
int searchSensor(unsigned long long *ID, int *LastBitChange) {
  int BitIndex;
  char Bit, NoBit;
  if (*LastBitChange < 0) {
    return 0;
  }
  if (*LastBitChange < 64) {
    IDSetBit(ID, *LastBitChange, 1);
    for (BitIndex = *LastBitChange + 1; BitIndex < 64; BitIndex++) {
      IDSetBit(ID, BitIndex, 0);
    }
  }
  *LastBitChange = -1;
  if (!doReset()) {
    return -1;
  }
  writeByte(0XF0);
  for (BitIndex = 0; BitIndex < 64; BitIndex++) {
    NoBit = readBit();
    Bit = readBit();
    if (Bit && NoBit)
      return -2;
    if (!Bit && !NoBit) {
      if (IDGetBit(ID, BitIndex)) {
        WriteBit(1);
      } else {
        *LastBitChange = BitIndex;
        WriteBit(0);
      }
    } else if (!Bit) {
      WriteBit(1);
      IDSetBit(ID, BitIndex, 1);
    } else {
      WriteBit(0);
      IDSetBit(ID, BitIndex, 0);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<searchSensor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1189 <searchSensor+0x29>
movl   $0x0,-0x4(%rbp)
jmp    1308 <searchSensor+0x1a8>
mov    -0x18(%rbp),%rax
cmpl   $0x40,(%rax)
jge    11e1 <searchSensor+0x81>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1050 <IDSetBit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x40,-0x1c(%rbp)
jge    11dc <searchSensor+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1050 <IDSetBit@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b6 <searchSensor+0x56>
jmp    11e1 <searchSensor+0x81>
mov    -0x18(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    $0x0,%al
call   1060 <doReset@plt>
cmp    $0x0,%eax
jne    1207 <searchSensor+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1308 <searchSensor+0x1a8>
mov    $0xf0,%edi
call   1080 <writeByte@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x40,-0x1c(%rbp)
jge    1301 <searchSensor+0x1a1>
mov    $0x0,%al
call   1040 <readBit@plt>
mov    %al,-0x1e(%rbp)
mov    $0x0,%al
call   1040 <readBit@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
je     125c <searchSensor+0xfc>
movsbl -0x1e(%rbp),%eax
cmp    $0x0,%eax
je     125c <searchSensor+0xfc>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1308 <searchSensor+0x1a8>
cmpb   $0x0,-0x1d(%rbp)
jne    12aa <searchSensor+0x14a>
cmpb   $0x0,-0x1e(%rbp)
jne    12aa <searchSensor+0x14a>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <IDGetBit@plt>
cmp    $0x0,%rax
je     1295 <searchSensor+0x135>
mov    $0x1,%edi
call   1030 <WriteBit@plt>
jmp    12a5 <searchSensor+0x145>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
xor    %edi,%edi
call   1030 <WriteBit@plt>
jmp    12ee <searchSensor+0x18e>
cmpb   $0x0,-0x1d(%rbp)
jne    12d4 <searchSensor+0x174>
mov    $0x1,%edi
call   1030 <WriteBit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <IDSetBit@plt>
jmp    12e9 <searchSensor+0x189>
xor    %edi,%edi
call   1030 <WriteBit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1050 <IDSetBit@plt>
jmp    12ee <searchSensor+0x18e>
jmp    12f3 <searchSensor+0x193>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1218 <searchSensor+0xb8>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<searchSensor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    (%rsi),%esi
test   %esi,%esi
js     1257 <searchSensor+0xf7>
mov    %rdi,%r15
cmp    $0x3f,%esi
ja     11b4 <searchSensor+0x54>
mov    %r15,%rdi
mov    $0x1,%edx
call   1050 <IDSetBit@plt>
mov    (%r14),%ebp
cmp    $0x3e,%ebp
jg     11b4 <searchSensor+0x54>
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <IDSetBit@plt>
add    $0x1,%ebp
cmp    $0x40,%ebp
jne    11a0 <searchSensor+0x40>
movl   $0xffffffff,(%r14)
xor    %eax,%eax
call   1060 <doReset@plt>
test   %eax,%eax
je     125b <searchSensor+0xfb>
mov    $0xf0,%edi
call   1080 <writeByte@plt>
xor    %ebp,%ebp
jmp    11f9 <searchSensor+0x99>
mov    $0x1,%edi
call   1030 <WriteBit@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1050 <IDSetBit@plt>
add    $0x1,%ebp
cmp    $0x40,%ebp
je     1269 <searchSensor+0x109>
xor    %eax,%eax
call   1040 <readBit@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <readBit@plt>
test   %al,%al
je     1211 <searchSensor+0xb1>
test   %bl,%bl
jne    1262 <searchSensor+0x102>
or     %al,%bl
je     1230 <searchSensor+0xd0>
test   %al,%al
je     11d8 <searchSensor+0x78>
xor    %edi,%edi
call   1030 <WriteBit@plt>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
jmp    11ec <searchSensor+0x8c>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <IDGetBit@plt>
test   %rax,%rax
je     124b <searchSensor+0xeb>
mov    $0x1,%edi
call   1030 <WriteBit@plt>
jmp    11f1 <searchSensor+0x91>
mov    %ebp,(%r14)
xor    %edi,%edi
call   1030 <WriteBit@plt>
jmp    11f1 <searchSensor+0x91>
xor    %eax,%eax
jmp    126e <searchSensor+0x10e>
mov    $0xffffffff,%eax
jmp    126e <searchSensor+0x10e>
mov    $0xfffffffe,%eax
jmp    126e <searchSensor+0x10e>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @searchSensor(i64* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
store i64* %0, i64** %4
store i32* %1, i32** %5
%9 = load i32*, i32** %5
%10 = load i32, i32* %9
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %94
13: ; preds = %2
%14 = load i32*, i32** %5
%15 = load i32, i32* %14
%16 = icmp slt i32 %15, 64
br i1 %16, label %17, label %36
17: ; preds = %13
%18 = load i64*, i64** %4
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = call i32 @IDSetBit(i64* %18, i32 %20, i32 1)
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %25
25: ; preds = %32, %17
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 64
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i64*, i64** %4
%30 = load i32, i32* %6
%31 = call i32 @IDSetBit(i64* %29, i32 %30, i32 0)
br label %32
32: ; preds = %28
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %25
35: ; preds = %25
br label %36
36: ; preds = %35, %13
%37 = load i32*, i32** %5
store i32 -1, i32* %37
%38 = call i32 (...) @doReset()
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %36
store i32 -1, i32* %3
br label %94
41: ; preds = %36
%42 = call i32 @writeByte(i32 240)
store i32 0, i32* %6
br label %43
43: ; preds = %90, %41
%44 = load i32, i32* %6
%45 = icmp slt i32 %44, 64
br i1 %45, label %46, label %93
46: ; preds = %43
%47 = call signext i8 (...) @readBit()
store i8 %47, i8* %8
%48 = call signext i8 (...) @readBit()
store i8 %48, i8* %7
%49 = load i8, i8* %7
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %46
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -2, i32* %3
br label %94
57: ; preds = %52, %46
%58 = load i8, i8* %7
%59 = icmp ne i8 %58, 0
br i1 %59, label %75, label %60
60: ; preds = %57
%61 = load i8, i8* %8
%62 = icmp ne i8 %61, 0
br i1 %62, label %75, label %63
63: ; preds = %60
%64 = load i64*, i64** %4
%65 = load i32, i32* %6
%66 = call i64 @IDGetBit(i64* %64, i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @WriteBit(i32 1)
br label %74
70: ; preds = %63
%71 = load i32, i32* %6
%72 = load i32*, i32** %5
store i32 %71, i32* %72
%73 = call i32 @WriteBit(i32 0)
br label %74
74: ; preds = %70, %68
br label %89
75: ; preds = %60, %57
%76 = load i8, i8* %7
%77 = icmp ne i8 %76, 0
br i1 %77, label %83, label %78
78: ; preds = %75
%79 = call i32 @WriteBit(i32 1)
%80 = load i64*, i64** %4
%81 = load i32, i32* %6
%82 = call i32 @IDSetBit(i64* %80, i32 %81, i32 1)
br label %88
83: ; preds = %75
%84 = call i32 @WriteBit(i32 0)
%85 = load i64*, i64** %4
%86 = load i32, i32* %6
%87 = call i32 @IDSetBit(i64* %85, i32 %86, i32 0)
br label %88
88: ; preds = %83, %78
br label %89
89: ; preds = %88, %74
br label %90
90: ; preds = %89
%91 = load i32, i32* %6
%92 = add nsw i32 %91, 1
store i32 %92, i32* %6
br label %43
93: ; preds = %43
store i32 1, i32* %3
br label %94
94: ; preds = %93, %56, %40, %12
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
define i32 @searchSensor(i64* %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 0
br i1 %4, label %47, label %5
5: ; preds = %2
%6 = icmp ult i32 %3, 64
br i1 %6, label %7, label %16
7: ; preds = %5
%8 = tail call i32 @IDSetBit(i64* %0, i32 %3, i32 1)
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 63
br i1 %10, label %11, label %16
11: ; preds = %7, %11
%12 = phi i32 [ %13, %11 ], [ %9, %7 ]
%13 = add nsw i32 %12, 1
%14 = tail call i32 @IDSetBit(i64* %0, i32 %13, i32 0)
%15 = icmp eq i32 %13, 63
br i1 %15, label %16, label %11
16: ; preds = %11, %7, %5
store i32 -1, i32* %1
%17 = tail call i32 (...) @doReset()
%18 = icmp eq i32 %17, 0
br i1 %18, label %47, label %19
19: ; preds = %16
%20 = tail call i32 @writeByte(i32 240)
br label %21
21: ; preds = %19, %44
%22 = phi i32 [ 0, %19 ], [ %45, %44 ]
%23 = tail call signext i8 (...) @readBit()
%24 = tail call signext i8 (...) @readBit()
%25 = icmp ne i8 %24, 0
%26 = icmp ne i8 %23, 0
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %47, label %28
28: ; preds = %21
%29 = select i1 %25, i1 true, i1 %26
br i1 %29, label %37, label %30
30: ; preds = %28
%31 = tail call i64 @IDGetBit(i64* %0, i32 %22)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @WriteBit(i32 1)
br label %44
35: ; preds = %30
store i32 %22, i32* %1
%36 = tail call i32 @WriteBit(i32 0)
br label %44
37: ; preds = %28
br i1 %25, label %41, label %38
38: ; preds = %37
%39 = tail call i32 @WriteBit(i32 1)
%40 = tail call i32 @IDSetBit(i64* %0, i32 %22, i32 1)
br label %44
41: ; preds = %37
%42 = tail call i32 @WriteBit(i32 0)
%43 = tail call i32 @IDSetBit(i64* %0, i32 %22, i32 0)
br label %44
44: ; preds = %35, %33, %41, %38
%45 = add nuw nsw i32 %22, 1
%46 = icmp eq i32 %45, 64
br i1 %46, label %47, label %21
47: ; preds = %44, %21, %16, %2
%48 = phi i32 [ 0, %2 ], [ -1, %16 ], [ 1, %44 ], [ -2, %21 ]
ret i32 %48
}