#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 analog(int);
int /*<<< orphan*/ beep();
int /*<<< orphan*/ left_button();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ right_button();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tone(float, double);
void wait_for_light(int light_port_) {
  int l_on_, l_off_, l_mid_, t;
  printf("Cal. with sensoron port #%d\n", light_port_);
  sleep(2.);
  printf("Cal: press Left when light on\n");
  while (!left_button())
    ;
  l_on_ = analog(light_port_);
  beep();
  printf("Cal: light on   value is=%d\n", l_on_);
  sleep(1.);
  beep();
  printf("Cal: press Right when light off\n");
  while (!right_button())
    ;
  l_off_ = analog(light_port_);
  beep();
  printf("Cal: light off  value is=%d\n", l_off_);
  sleep(1.);
  beep();
  if ((l_off_ - l_on_) >= 15) {
    l_mid_ = (l_on_ + l_off_) / 2;
    printf("Good CalibrationDiff=%d Waiting\n", (l_off_ - l_on_));
    while (analog(light_port_) > l_mid_)
      tone(440., 0.1);
  } else {
    if (l_off_ < 128) {
      printf("Bad Calibration Add Shielding!!\n");
      while (1)
        for (t = 100; t < 10000; t = t + 200)
          tone((float)t, 0.05);
    } else {
      printf("Bad Calibration Aim sensor!!\n");
      while (1)
        for (t = 10000; t > 200; t = t - 200)
          tone((float)t, 0.05);
    }
  }
}


x86 assembly (clang, -O0):
<wait_for_light>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <left_button@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c0 <wait_for_light+0x50>
jmp    11c5 <wait_for_light+0x55>
jmp    11a4 <wait_for_light+0x34>
mov    -0x4(%rbp),%edi
call   1080 <analog@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <beep@plt>
mov    -0x8(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1050 <beep@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <right_button@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1223 <wait_for_light+0xb3>
jmp    1228 <wait_for_light+0xb8>
jmp    1207 <wait_for_light+0x97>
mov    -0x4(%rbp),%edi
call   1080 <analog@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <beep@plt>
mov    -0xc(%rbp),%esi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1050 <beep@plt>
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0xf,%eax
jl     12c0 <wait_for_light+0x150>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
sub    -0x8(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1080 <analog@plt>
cmp    -0x10(%rbp),%eax
jle    12bb <wait_for_light+0x14b>
movss  0xd67(%rip),%xmm0
movsd  0xd57(%rip),%xmm1
call   1070 <tone@plt>
jmp    1290 <wait_for_light+0x120>
jmp    135f <wait_for_light+0x1ef>
cmpl   $0x80,-0xc(%rbp)
jge    1316 <wait_for_light+0x1a6>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x64,-0x14(%rbp)
cmpl   $0x2710,-0x14(%rbp)
jge    1311 <wait_for_light+0x1a1>
cvtsi2ssl -0x14(%rbp),%xmm0
movsd  0xd04(%rip),%xmm1
call   1070 <tone@plt>
mov    -0x14(%rbp),%eax
add    $0xc8,%eax
mov    %eax,-0x14(%rbp)
jmp    12e2 <wait_for_light+0x172>
jmp    12db <wait_for_light+0x16b>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2710,-0x14(%rbp)
cmpl   $0xc8,-0x14(%rbp)
jle    135a <wait_for_light+0x1ea>
cvtsi2ssl -0x14(%rbp),%xmm0
movsd  0xcbb(%rip),%xmm1
call   1070 <tone@plt>
mov    -0x14(%rbp),%eax
sub    $0xc8,%eax
mov    %eax,-0x14(%rbp)
jmp    132b <wait_for_light+0x1bb>
jmp    1324 <wait_for_light+0x1b4>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_light>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0xe83(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
lea    0xee0(%rip),%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <left_button@plt>
test   %eax,%eax
je     11c0 <wait_for_light+0x40>
mov    %r15d,%edi
call   1090 <analog@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1060 <beep@plt>
lea    0xe4d(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1060 <beep@plt>
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <right_button@plt>
test   %eax,%eax
je     1210 <wait_for_light+0x90>
mov    %r15d,%edi
call   1090 <analog@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <beep@plt>
lea    0xe1b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1060 <beep@plt>
mov    %ebx,%esi
sub    %r14d,%esi
cmp    $0xf,%esi
jl     12a3 <wait_for_light+0x123>
add    %r14d,%ebx
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
sar    %ebp
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1090 <analog@plt>
cmp    %ebp,%eax
jle    12f4 <wait_for_light+0x174>
movss  0xd7c(%rip),%xmm0
movsd  0xd6c(%rip),%xmm1
call   1080 <tone@plt>
jmp    1280 <wait_for_light+0x100>
cmp    $0x7f,%ebx
jg     12ff <wait_for_light+0x17f>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xffffff9c,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xc8,%ebx
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movsd  0xd1b(%rip),%xmm1
call   1080 <tone@plt>
cmp    $0x2648,%ebx
jb     12d0 <wait_for_light+0x150>
jmp    12c0 <wait_for_light+0x140>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
nopl   0x0(%rax,%rax,1)
mov    $0x27d8,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffff38,%ebx
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movsd  0xccb(%rip),%xmm1
call   1080 <tone@plt>
cmp    $0x190,%ebx
ja     1320 <wait_for_light+0x1a0>
jmp    1310 <wait_for_light+0x190>

LLVM IR (-O0):
@.str = [29 x i8] c"Cal. with sensoron port #%d\0A\00"
@.str.1 = [31 x i8] c"Cal: press Left when light on\0A\00"
@.str.2 = [29 x i8] c"Cal: light on value is=%d\0A\00"
@.str.3 = [33 x i8] c"Cal: press Right when light off\0A\00"
@.str.4 = [29 x i8] c"Cal: light off value is=%d\0A\00"
@.str.5 = [33 x i8] c"Good CalibrationDiff=%d Waiting\0A\00"
@.str.6 = [33 x i8] c"Bad Calibration Add Shielding!!\0A\00"
@.str.7 = [30 x i8] c"Bad Calibration Aim sensor!!\0A\00"
define void @wait_for_light(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @sleep(i32 2)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %15, %1
%12 = call i32 (...) @left_button()
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %16
15: ; preds = %11
br label %11
16: ; preds = %11
%17 = load i32, i32* %2
%18 = call i32 @analog(i32 %17)
store i32 %18, i32* %3
%19 = call i32 (...) @beep()
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = call i32 @sleep(i32 1)
%23 = call i32 (...) @beep()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %29, %16
%26 = call i32 (...) @right_button()
%27 = icmp ne i32 %26, 0
%28 = xor i1 %27, true
br i1 %28, label %29, label %30
29: ; preds = %25
br label %25
30: ; preds = %25
%31 = load i32, i32* %2
%32 = call i32 @analog(i32 %31)
store i32 %32, i32* %4
%33 = call i32 (...) @beep()
%34 = load i32, i32* %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @sleep(i32 1)
%37 = call i32 (...) @beep()
%38 = load i32, i32* %4
%39 = load i32, i32* %3
%40 = sub nsw i32 %38, %39
%41 = icmp sge i32 %40, 15
br i1 %41, label %42, label %59
42: ; preds = %30
%43 = load i32, i32* %3
%44 = load i32, i32* %4
%45 = add nsw i32 %43, %44
%46 = sdiv i32 %45, 2
store i32 %46, i32* %5
%47 = load i32, i32* %4
%48 = load i32, i32* %3
%49 = sub nsw i32 %47, %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %56, %42
%52 = load i32, i32* %2
%53 = call i32 @analog(i32 %52)
%54 = load i32, i32* %5
%55 = icmp sgt i32 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @tone(float 4.400000e+02, double 1.000000e-01)
br label %51
58: ; preds = %51
br label %90
59: ; preds = %30
%60 = load i32, i32* %4
%61 = icmp slt i32 %60, 128
br i1 %61, label %62, label %76
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %75
store i32 100, i32* %6
br label %65
65: ; preds = %72, %64
%66 = load i32, i32* %6
%67 = icmp slt i32 %66, 10000
br i1 %67, label %68, label %75
68: ; preds = %65
%69 = load i32, i32* %6
%70 = sitofp i32 %69 to float
%71 = call i32 @tone(float %70, double 5.000000e-02)
br label %72
72: ; preds = %68
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 200
store i32 %74, i32* %6
br label %65
75: ; preds = %65
br label %64
76: ; preds = %59
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %78
78: ; preds = %76, %89
store i32 10000, i32* %6
br label %79
79: ; preds = %86, %78
%80 = load i32, i32* %6
%81 = icmp sgt i32 %80, 200
br i1 %81, label %82, label %89
82: ; preds = %79
%83 = load i32, i32* %6
%84 = sitofp i32 %83 to float
%85 = call i32 @tone(float %84, double 5.000000e-02)
br label %86
86: ; preds = %82
%87 = load i32, i32* %6
%88 = sub nsw i32 %87, 200
store i32 %88, i32* %6
br label %79
89: ; preds = %79
br label %78
90: ; preds = %58
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"Cal. with sensoron port #%d\0A\00"
@.str.2 = [29 x i8] c"Cal: light on value is=%d\0A\00"
@.str.4 = [29 x i8] c"Cal: light off value is=%d\0A\00"
@.str.5 = [33 x i8] c"Good CalibrationDiff=%d Waiting\0A\00"
@str = [30 x i8] c"Cal: press Left when light on\00"
@str.8 = [32 x i8] c"Cal: press Right when light off\00"
@str.9 = [29 x i8] c"Bad Calibration Aim sensor!!\00"
@str.10 = [32 x i8] c"Bad Calibration Add Shielding!!\00"
define void @wait_for_light(i32 %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32 @sleep(i32 2)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %5
5: ; preds = %5, %1
%6 = tail call i32 (...) @left_button()
%7 = icmp eq i32 %6, 0
br i1 %7, label %5, label %8
8: ; preds = %5
%9 = tail call i32 @analog(i32 %0)
%10 = tail call i32 (...) @beep()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 (...) @beep()
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
br label %15
15: ; preds = %15, %8
%16 = tail call i32 (...) @right_button()
%17 = icmp eq i32 %16, 0
br i1 %17, label %15, label %18
18: ; preds = %15
%19 = tail call i32 @analog(i32 %0)
%20 = tail call i32 (...) @beep()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 (...) @beep()
%24 = sub nsw i32 %19, %9
%25 = icmp sgt i32 %24, 14
br i1 %25, label %26, label %36
26: ; preds = %18
%27 = add nsw i32 %19, %9
%28 = sdiv i32 %27, 2
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%30 = tail call i32 @analog(i32 %0)
%31 = icmp sgt i32 %30, %28
br i1 %31, label %32, label %56
32: ; preds = %26, %32
%33 = tail call i32 @tone(float 4.400000e+02, double 1.000000e-01)
%34 = tail call i32 @analog(i32 %0)
%35 = icmp sgt i32 %34, %28
br i1 %35, label %32, label %56
36: ; preds = %18
%37 = icmp slt i32 %19, 128
br i1 %37, label %38, label %47
38: ; preds = %36
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
br label %40
40: ; preds = %41, %38
br label %41
41: ; preds = %40, %41
%42 = phi i32 [ 100, %40 ], [ %45, %41 ]
%43 = sitofp i32 %42 to float
%44 = tail call i32 @tone(float %43, double 5.000000e-02)
%45 = add nuw nsw i32 %42, 200
%46 = icmp ult i32 %42, 9800
br i1 %46, label %41, label %40
47: ; preds = %36
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, i64 0, i64 0))
br label %49
49: ; preds = %50, %47
br label %50
50: ; preds = %49, %50
%51 = phi i32 [ 10000, %49 ], [ %54, %50 ]
%52 = sitofp i32 %51 to float
%53 = tail call i32 @tone(float %52, double 5.000000e-02)
%54 = add nsw i32 %51, -200
%55 = icmp ugt i32 %51, 400
br i1 %55, label %50, label %49
56: ; preds = %32, %26
ret void
}