#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 */
int getch();
int /*<<< orphan*/ printw(char *);
scalar_t__ readHcsr04();
int /*<<< orphan*/ refresh();
int /*<<< orphan*/ timeout(int);
int /*<<< orphan*/ usleep(int);
void run(int usSet, int usDelta) {
  int key;
  int usNow, i;
  long usTmp;
  printw("Ceck distance from setpoint\n\r");
  timeout(100);
  printw("\n\rPress Enter for exit\n\r");
  do {
    usTmp = 0;
    for (i = 0; i < 10; i++) {
      usTmp += readHcsr04();
      usleep(1000);
    }
    usNow = usTmp / 10;
    if (usNow > (usSet + usDelta))
      printw(" You are too far                \r");
    else if (usNow < (usSet - usDelta))
      printw("You are too close         \r");
    else
      printw("You are in range                \r");
    refresh();
    key = getch();
  } while (key != 10);
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe8b(%rip),%rdi
call   1050 <printw@plt>
mov    $0x64,%edi
call   1030 <timeout@plt>
lea    0xe93(%rip),%rdi
call   1050 <printw@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    11d0 <run+0x70>
mov    $0x0,%al
call   1040 <readHcsr04@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x3e8,%edi
call   1080 <usleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119f <run+0x3f>
mov    -0x20(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    -0x8(%rbp),%ecx
cmp    %ecx,%eax
jle    1203 <run+0xa3>
lea    0xe3e(%rip),%rdi
call   1050 <printw@plt>
jmp    1236 <run+0xd6>
mov    -0x10(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    -0x8(%rbp),%ecx
cmp    %ecx,%eax
jge    1225 <run+0xc5>
lea    0xe3e(%rip),%rdi
call   1050 <printw@plt>
jmp    1231 <run+0xd1>
lea    0xe49(%rip),%rdi
call   1050 <printw@plt>
jmp    1236 <run+0xd6>
mov    $0x0,%al
call   1060 <refresh@plt>
mov    $0x0,%al
call   1070 <getch@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jne    1190 <run+0x30>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe88(%rip),%rdi
call   1050 <printw@plt>
mov    $0x64,%edi
call   1030 <timeout@plt>
lea    0xe90(%rip),%rdi
call   1050 <printw@plt>
lea    (%r14,%r15,1),%ebp
sub    %r14d,%r15d
lea    0xed4(%rip),%r12
lea    0xe8f(%rip),%r13
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%r14
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%r14
add    %rbx,%r14
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%r14
add    %rbx,%r14
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%r14
add    %rbx,%r14
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%r14
add    %rbx,%r14
mov    $0x3e8,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1040 <readHcsr04@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    $0x3e8,%edi
call   1080 <usleep@plt>
mov    %rbx,%rax
movabs $0x6666666666666667,%rcx
66 66 66
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
shr    $0x2,%rdx
add    %eax,%edx
cmp    %edx,%r15d
mov    %r12,%rdi
lea    0xd9c(%rip),%rax
cmovg  %rax,%rdi
cmp    %edx,%ebp
cmovl  %r13,%rdi
call   1050 <printw@plt>
xor    %eax,%eax
call   1060 <refresh@plt>
xor    %eax,%eax
call   1070 <getch@plt>
cmp    $0xa,%eax
jne    11b0 <run+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Ceck distance from setpoint\0A\0D\00"
@.str.1 = [25 x i8] c"\0A\0DPress Enter for exit\0A\0D\00"
@.str.2 = [34 x i8] c" You are too far \0D\00"
@.str.3 = [28 x i8] c"You are too close \0D\00"
@.str.4 = [34 x i8] c"You are in range \0D\00"
define void @run(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = call i32 @printw(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @timeout(i32 100)
%11 = call i32 @printw(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %49, %2
store i64 0, i64* %8
store i32 0, i32* %7
br label %13
13: ; preds = %21, %12
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 10
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = call i64 (...) @readHcsr04()
%18 = load i64, i64* %8
%19 = add nsw i64 %18, %17
store i64 %19, i64* %8
%20 = call i32 @usleep(i32 1000)
br label %21
21: ; preds = %16
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %13
24: ; preds = %13
%25 = load i64, i64* %8
%26 = sdiv i64 %25, 10
%27 = trunc i64 %26 to i32
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = add nsw i32 %29, %30
%32 = icmp sgt i32 %28, %31
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @printw(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %46
35: ; preds = %24
%36 = load i32, i32* %6
%37 = load i32, i32* %3
%38 = load i32, i32* %4
%39 = sub nsw i32 %37, %38
%40 = icmp slt i32 %36, %39
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @printw(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %45
43: ; preds = %35
%44 = call i32 @printw(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %41
br label %46
46: ; preds = %45, %33
%47 = call i32 (...) @refresh()
%48 = call i32 (...) @getch()
store i32 %48, i32* %5
br label %49
49: ; preds = %46
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 10
br i1 %51, label %12, label %52
52: ; preds = %49
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"Ceck distance from setpoint\0A\0D\00"
@.str.1 = [25 x i8] c"\0A\0DPress Enter for exit\0A\0D\00"
@.str.2 = [34 x i8] c" You are too far \0D\00"
@.str.3 = [28 x i8] c"You are too close \0D\00"
@.str.4 = [34 x i8] c"You are in range \0D\00"
define void @run(i32 %0, i32 %1) {
%3 = tail call i32 @printw(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @timeout(i32 100)
%5 = tail call i32 @printw(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%6 = add nsw i32 %1, %0
%7 = sub nsw i32 %0, %1
br label %8
8: ; preds = %8, %2
%9 = tail call i64 (...) @readHcsr04()
%10 = tail call i32 @usleep(i32 1000)
%11 = tail call i64 (...) @readHcsr04()
%12 = add nsw i64 %11, %9
%13 = tail call i32 @usleep(i32 1000)
%14 = tail call i64 (...) @readHcsr04()
%15 = add nsw i64 %14, %12
%16 = tail call i32 @usleep(i32 1000)
%17 = tail call i64 (...) @readHcsr04()
%18 = add nsw i64 %17, %15
%19 = tail call i32 @usleep(i32 1000)
%20 = tail call i64 (...) @readHcsr04()
%21 = add nsw i64 %20, %18
%22 = tail call i32 @usleep(i32 1000)
%23 = tail call i64 (...) @readHcsr04()
%24 = add nsw i64 %23, %21
%25 = tail call i32 @usleep(i32 1000)
%26 = tail call i64 (...) @readHcsr04()
%27 = add nsw i64 %26, %24
%28 = tail call i32 @usleep(i32 1000)
%29 = tail call i64 (...) @readHcsr04()
%30 = add nsw i64 %29, %27
%31 = tail call i32 @usleep(i32 1000)
%32 = tail call i64 (...) @readHcsr04()
%33 = add nsw i64 %32, %30
%34 = tail call i32 @usleep(i32 1000)
%35 = tail call i64 (...) @readHcsr04()
%36 = add nsw i64 %35, %33
%37 = tail call i32 @usleep(i32 1000)
%38 = sdiv i64 %36, 10
%39 = trunc i64 %38 to i32
%40 = icmp slt i32 %6, %39
%41 = icmp sgt i32 %7, %39
%42 = select i1 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0)
%43 = select i1 %40, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %42
%44 = tail call i32 @printw(i8* %43)
%45 = tail call i32 (...) @refresh()
%46 = tail call i32 (...) @getch()
%47 = icmp eq i32 %46, 10
br i1 %47, label %48, label %8
48: ; preds = %8
ret void
}