#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 */
unsigned char OW_CRC(unsigned char *, int);
int OW_ReadBytes(int, unsigned char *, int);
int /*<<< orphan*/ OW_WriteByte(int, int);
scalar_t__ OW_detectKey(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open_port(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setMux();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int fd;
  int i, bytesRead;
  unsigned char buf[64] = {
      0,
  };
  unsigned char inBuf[512] = {
      0,
  };
  unsigned char crc;
  setMux();
  if (argc != 2) {
    printf("Usage: %s <path to serialPort>\n", argv[0]);
    exit(-1);
  }
  fd = open_port(argv[1]);
  if (fd == -1) {
    printf("Failed to open serial port at %s\n", argv[1]);
    exit(-1);
  }
  while (1) {
    if (OW_detectKey(fd)) {
      OW_WriteByte(fd, 0x33);
      if (OW_ReadBytes(fd, inBuf, 8) == 8) {
        if (inBuf[0] == 0x01) {
          if (OW_CRC(inBuf, 7) == inBuf[7]) {
            printf("Valid key detected with ID:");
            printf("%02x%02x%02x%02x%02x%02x\n", inBuf[6], inBuf[5], inBuf[4],
                   inBuf[3], inBuf[2], inBuf[1]);
            sleep(1);
          }
        }
      }
    }
    usleep(1000 * 100);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1060 <memset@plt>
lea    -0x260(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    $0x0,%al
call   1040 <setMux@plt>
cmpl   $0x2,-0x8(%rbp)
je     121c <main+0x6c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <open_port@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1256 <main+0xa6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    125b <main+0xab>
mov    -0x14(%rbp),%edi
call   1030 <OW_detectKey@plt>
cmp    $0x0,%rax
je     132e <main+0x17e>
mov    -0x14(%rbp),%edi
mov    $0x33,%esi
call   1090 <OW_WriteByte@plt>
mov    -0x14(%rbp),%edi
lea    -0x260(%rbp),%rsi
mov    $0x8,%edx
call   1080 <OW_ReadBytes@plt>
cmp    $0x8,%eax
jne    1329 <main+0x179>
movzbl -0x260(%rbp),%eax
cmp    $0x1,%eax
jne    1324 <main+0x174>
lea    -0x260(%rbp),%rdi
mov    $0x7,%esi
call   1070 <OW_CRC@plt>
movzbl %al,%eax
movzbl -0x259(%rbp),%ecx
cmp    %ecx,%eax
jne    131f <main+0x16f>
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movzbl -0x25a(%rbp),%esi
movzbl -0x25b(%rbp),%edx
movzbl -0x25c(%rbp),%ecx
movzbl -0x25d(%rbp),%r8d
ff
movzbl -0x25e(%rbp),%r9d
ff
movzbl -0x25f(%rbp),%eax
lea    0xd53(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1324 <main+0x174>
jmp    1329 <main+0x179>
jmp    132e <main+0x17e>
mov    $0x186a0,%edi
call   10d0 <usleep@plt>
jmp    125b <main+0xab>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x10(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1060 <memset@plt>
xor    %eax,%eax
call   1040 <setMux@plt>
cmp    $0x2,%ebx
jne    12a6 <main+0xf6>
mov    0x8(%r14),%rdi
call   10b0 <open_port@plt>
cmp    $0xffffffff,%eax
je     12b2 <main+0x102>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0xe3e(%rip),%r15
lea    0xe53(%rip),%r12
jmp    121a <main+0x6a>
nopl   (%rax)
mov    $0x186a0,%edi
call   10d0 <usleep@plt>
mov    %ebx,%edi
call   1030 <OW_detectKey@plt>
test   %rax,%rax
je     1210 <main+0x60>
mov    %ebx,%edi
mov    $0x33,%esi
call   1090 <OW_WriteByte@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <OW_ReadBytes@plt>
cmp    $0x8,%eax
jne    1210 <main+0x60>
cmpb   $0x1,0x10(%rsp)
jne    1210 <main+0x60>
mov    %r14,%rdi
mov    $0x7,%esi
call   1070 <OW_CRC@plt>
cmp    0x17(%rsp),%al
jne    1210 <main+0x60>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0x16(%rsp),%esi
movzbl 0x15(%rsp),%edx
movzbl 0x14(%rsp),%ecx
movzbl 0x13(%rsp),%r8d
movzbl 0x12(%rsp),%r9d
movzbl 0x11(%rsp),%eax
mov    %eax,(%rsp)
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1210 <main+0x60>
mov    (%r14),%rsi
lea    0xd50(%rip),%rdi
jmp    12bd <main+0x10d>
mov    0x8(%r14),%rsi
lea    0xd63(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: %s <path to serialPort>\0A\00"
@.str.1 = [34 x i8] c"Failed to open serial port at %s\0A\00"
@.str.2 = [28 x i8] c"Valid key detected with ID:\00"
@.str.3 = [26 x i8] c"%02x%02x%02x%02x%02x%02x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca [512 x i8]
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [64 x i8]* %9 to i8*
%13 = bitcast [512 x i8]* %10 to i8*
%14 = call i32 (...) @setMux()
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @open_port(i8* %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %23
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %23
br label %37
37: ; preds = %36, %86
%38 = load i32, i32* %6
%39 = call i64 @OW_detectKey(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %86
41: ; preds = %37
%42 = load i32, i32* %6
%43 = call i32 @OW_WriteByte(i32 %42, i32 51)
%44 = load i32, i32* %6
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%46 = call i32 @OW_ReadBytes(i32 %44, i8* %45, i32 8)
%47 = icmp eq i32 %46, 8
br i1 %47, label %48, label %85
48: ; preds = %41
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %84
53: ; preds = %48
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%55 = call zeroext i8 @OW_CRC(i8* %54, i32 7)
%56 = zext i8 %55 to i32
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 7
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = icmp eq i32 %56, %59
br i1 %60, label %61, label %83
61: ; preds = %53
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 6
%64 = load i8, i8* %63
%65 = zext i8 %64 to i32
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 5
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 4
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 3
%73 = load i8, i8* %72
%74 = zext i8 %73 to i32
%75 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 2
%76 = load i8, i8* %75
%77 = zext i8 %76 to i32
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 1
%79 = load i8, i8* %78
%80 = zext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %65, i32 %68, i32 %71, i32 %74, i32 %77, i32 %80)
%82 = call i32 @sleep(i32 1)
br label %83
83: ; preds = %61, %53
br label %84
84: ; preds = %83, %48
br label %85
85: ; preds = %84, %41
br label %86
86: ; preds = %85, %37
%87 = call i32 @usleep(i32 100000)
br label %37
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: %s <path to serialPort>\0A\00"
@.str.1 = [34 x i8] c"Failed to open serial port at %s\0A\00"
@.str.2 = [28 x i8] c"Valid key detected with ID:\00"
@.str.3 = [26 x i8] c"%02x%02x%02x%02x%02x%02x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @setMux()
%6 = icmp eq i32 %0, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @open_port(i8* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %24, label %16
16: ; preds = %11
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 7
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 6
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 5
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 4
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 3
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 2
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 1
br label %28
24: ; preds = %11
%25 = load i8*, i8** %12
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = tail call i32 @exit(i32 -1)
unreachable
28: ; preds = %16, %58
%29 = call i64 @OW_detectKey(i32 %14)
%30 = icmp eq i64 %29, 0
br i1 %30, label %58, label %31
31: ; preds = %28
%32 = call i32 @OW_WriteByte(i32 %14, i32 51)
%33 = call i32 @OW_ReadBytes(i32 %14, i8* %4, i32 8)
%34 = icmp eq i32 %33, 8
%35 = load i8, i8* %4
%36 = icmp eq i8 %35, 1
%37 = select i1 %34, i1 %36, i1 false
br i1 %37, label %38, label %58
38: ; preds = %31
%39 = call zeroext i8 @OW_CRC(i8* %4, i32 7)
%40 = load i8, i8* %17
%41 = icmp eq i8 %39, %40
br i1 %41, label %42, label %58
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%44 = load i8, i8* %18
%45 = zext i8 %44 to i32
%46 = load i8, i8* %19
%47 = zext i8 %46 to i32
%48 = load i8, i8* %20
%49 = zext i8 %48 to i32
%50 = load i8, i8* %21
%51 = zext i8 %50 to i32
%52 = load i8, i8* %22
%53 = zext i8 %52 to i32
%54 = load i8, i8* %23
%55 = zext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %47, i32 %49, i32 %51, i32 %53, i32 %55)
%57 = call i32 @sleep(i32 1)
br label %58
58: ; preds = %31, %38, %42, %28
%59 = call i32 @usleep(i32 100000)
br label %28
}