#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 /*<<< orphan*/ adc_set_chan(int /*<<< orphan*/);
int /*<<< orphan*/ delay_ms(int);
int /*<<< orphan*/ measure_rint();
int /*<<< orphan*/ printf(char *, int, int, int);
int read_bat(int);
int /*<<< orphan*/ relay_closed();
int /*<<< orphan*/ relay_open();
void calibrate(void) {
  int val;
  int stat;
  int i;
  int time;
  measure_rint();
  adc_set_chan(0);
  time = 0;
  stat = 0;
  for (i = 0; i < 5; i++) {
    val = read_bat(10);
    printf("%d %d %d\n", time, val, stat);
    delay_ms(2000);
  }
  relay_closed();
  stat = 1;
  for (;;) {
    val = read_bat(10);
    printf("%d %d %d\n", time, val, stat);
    if (val < 3000)
      break;
    if (val < 2950)
      break;
    delay_ms(2000);
    time += 2;
  }
  relay_open();
  stat = 0;
  for (i = 0; i < 5; i++) {
    val = read_bat(10);
    printf("%d %d %d\n", time, val, stat);
    delay_ms(2000);
    time += 2;
  }
}


x86 assembly (clang, -O0):
<calibrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <measure_rint@plt>
xor    %edi,%edi
call   1030 <adc_set_chan@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    11df <calibrate+0x6f>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    119b <calibrate+0x2b>
mov    $0x0,%al
call   1050 <relay_closed@plt>
movl   $0x1,-0x8(%rbp)
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0xbb8,-0x4(%rbp)
jge    1221 <calibrate+0xb1>
jmp    124b <calibrate+0xdb>
cmpl   $0xb86,-0x4(%rbp)
jge    1233 <calibrate+0xc3>
jmp    124b <calibrate+0xdb>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
jmp    11ed <calibrate+0x7d>
mov    $0x0,%al
call   1070 <relay_open@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    12ad <calibrate+0x13d>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xd79(%rip),%rdi
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1260 <calibrate+0xf0>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <measure_rint@plt>
xor    %edi,%edi
call   1030 <adc_set_chan@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
lea    0xe6b(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
xor    %eax,%eax
call   1050 <relay_closed@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %eax,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
mov    $0x1,%ecx
call   1040 <printf@plt>
cmp    $0xbb8,%ebp
jl     12a2 <calibrate+0x132>
xor    %ebx,%ebx
lea    0xd96(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
add    $0x2,%ebx
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    $0x1,%ecx
call   1040 <printf@plt>
cmp    $0xbb7,%ebp
jg     1270 <calibrate+0x100>
xor    %eax,%eax
call   1070 <relay_open@plt>
mov    $0xa,%edi
call   1090 <read_bat@plt>
lea    0xd46(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
lea    0x2(%rbx),%ebp
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
lea    0x4(%rbx),%ebp
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
lea    0x6(%rbx),%ebp
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
call   1080 <delay_ms@plt>
add    $0x8,%ebx
mov    $0xa,%edi
call   1090 <read_bat@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <printf@plt>
mov    $0x7d0,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <delay_ms@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%d %d %d\0A\00"
define void @calibrate() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @measure_rint()
%6 = call i32 @adc_set_chan(i32 0)
store i32 0, i32* %4
store i32 0, i32* %2
store i32 0, i32* %3
br label %7
7: ; preds = %17, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 5
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = call i32 @read_bat(i32 10)
store i32 %11, i32* %1
%12 = load i32, i32* %4
%13 = load i32, i32* %1
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i32 %14)
%16 = call i32 @delay_ms(i32 2000)
br label %17
17: ; preds = %10
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %7
20: ; preds = %7
%21 = call i32 (...) @relay_closed()
store i32 1, i32* %2
br label %22
22: ; preds = %35, %20
%23 = call i32 @read_bat(i32 10)
store i32 %23, i32* %1
%24 = load i32, i32* %4
%25 = load i32, i32* %1
%26 = load i32, i32* %2
%27 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25, i32 %26)
%28 = load i32, i32* %1
%29 = icmp slt i32 %28, 3000
br i1 %29, label %30, label %31
30: ; preds = %22
br label %39
31: ; preds = %22
%32 = load i32, i32* %1
%33 = icmp slt i32 %32, 2950
br i1 %33, label %34, label %35
34: ; preds = %31
br label %39
35: ; preds = %31
%36 = call i32 @delay_ms(i32 2000)
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 2
store i32 %38, i32* %4
br label %22
39: ; preds = %34, %30
%40 = call i32 (...) @relay_open()
store i32 0, i32* %2
store i32 0, i32* %3
br label %41
41: ; preds = %53, %39
%42 = load i32, i32* %3
%43 = icmp slt i32 %42, 5
br i1 %43, label %44, label %56
44: ; preds = %41
%45 = call i32 @read_bat(i32 10)
store i32 %45, i32* %1
%46 = load i32, i32* %4
%47 = load i32, i32* %1
%48 = load i32, i32* %2
%49 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %47, i32 %48)
%50 = call i32 @delay_ms(i32 2000)
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 2
store i32 %52, i32* %4
br label %53
53: ; preds = %44
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %41
56: ; preds = %41
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"%d %d %d\0A\00"
define void @calibrate() {
%1 = tail call i32 (...) @measure_rint()
%2 = tail call i32 @adc_set_chan(i32 0)
%3 = tail call i32 @read_bat(i32 10)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %3, i32 0)
%5 = tail call i32 @delay_ms(i32 2000)
%6 = tail call i32 @read_bat(i32 10)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %6, i32 0)
%8 = tail call i32 @delay_ms(i32 2000)
%9 = tail call i32 @read_bat(i32 10)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %9, i32 0)
%11 = tail call i32 @delay_ms(i32 2000)
%12 = tail call i32 @read_bat(i32 10)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %12, i32 0)
%14 = tail call i32 @delay_ms(i32 2000)
%15 = tail call i32 @read_bat(i32 10)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %15, i32 0)
%17 = tail call i32 @delay_ms(i32 2000)
%18 = tail call i32 (...) @relay_closed()
%19 = tail call i32 @read_bat(i32 10)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0, i32 %19, i32 1)
%21 = icmp slt i32 %19, 3000
br i1 %21, label %29, label %22
22: ; preds = %0, %22
%23 = phi i32 [ %25, %22 ], [ 0, %0 ]
%24 = tail call i32 @delay_ms(i32 2000)
%25 = add nuw nsw i32 %23, 2
%26 = tail call i32 @read_bat(i32 10)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i32 1)
%28 = icmp slt i32 %26, 3000
br i1 %28, label %29, label %22
29: ; preds = %22, %0
%30 = phi i32 [ 0, %0 ], [ %25, %22 ]
%31 = tail call i32 (...) @relay_open()
%32 = tail call i32 @read_bat(i32 10)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %32, i32 0)
%34 = tail call i32 @delay_ms(i32 2000)
%35 = add nuw nsw i32 %30, 2
%36 = tail call i32 @read_bat(i32 10)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36, i32 0)
%38 = tail call i32 @delay_ms(i32 2000)
%39 = add nuw nsw i32 %30, 4
%40 = tail call i32 @read_bat(i32 10)
%41 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %40, i32 0)
%42 = tail call i32 @delay_ms(i32 2000)
%43 = add nuw nsw i32 %30, 6
%44 = tail call i32 @read_bat(i32 10)
%45 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %44, i32 0)
%46 = tail call i32 @delay_ms(i32 2000)
%47 = add nuw nsw i32 %30, 8
%48 = tail call i32 @read_bat(i32 10)
%49 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %47, i32 %48, i32 0)
%50 = tail call i32 @delay_ms(i32 2000)
ret void
}