#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__ getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(int);
int read_adc(int);
int /*<<< orphan*/ restore_io();
int /*<<< orphan*/ setup_gpio();
int /*<<< orphan*/ setup_io();
int /*<<< orphan*/ setup_spi();
int /*<<< orphan*/ short_wait();
void main(void) {
  int r, v, s, i, chan;
  do {
    printf("Which channel do you want to test? Type 0 or 1.\n");
    chan = (int)getchar();
    (void)getchar();
  } while (chan != '0' && chan != '1');
  chan = chan - '0';
  printf("These are the connections for the analogue to digital test:\n");
  printf("jumper connecting GP11 to SCLK\n");
  printf("jumper connecting GP10 to MOSI\n");
  printf("jumper connecting GP9 to MISO\n");
  printf("jumper connecting GP8 to CSnA\n");
  printf("Potentiometer connections:\n");
  printf("  (call 1 and 3 the ends of the resistor and 2 the wiper)\n");
  printf("  connect 3 to 3V3\n");
  printf("  connect 2 to AD%d\n", chan);
  printf("  connect 1 to GND\n");
  printf("When ready hit enter.\n");
  (void)getchar();
  setup_io();
  setup_gpio();
  setup_spi();
  for (r = 0; r < 100000; r++) {
    v = read_adc(chan);
    s = v >> 4;
    printf("%04d ", v);
    for (i = 0; i < s; i++)
      putchar('#');
    for (i = 0; i < 64 - s; i++)
      putchar(' ');
    putchar(0x0D);
    short_wait();
  }
  printf("\n");
  restore_io();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <getchar@plt>
xor    %eax,%eax
cmpl   $0x30,-0x14(%rbp)
mov    %al,-0x15(%rbp)
je     11d0 <main+0x40>
cmpl   $0x31,-0x14(%rbp)
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1198 <main+0x8>
mov    -0x14(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x14(%rbp)
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xebb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xec9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xef6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xef9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xf00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xf06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    $0x0,%al
call   10a0 <setup_io@plt>
mov    $0x0,%al
call   1030 <setup_gpio@plt>
mov    $0x0,%al
call   1080 <setup_spi@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x186a0,-0x4(%rbp)
jge    1352 <main+0x1c2>
mov    -0x14(%rbp),%edi
call   1090 <read_adc@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%esi
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1301 <main+0x171>
mov    $0x23,%edi
call   1040 <putchar@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12dd <main+0x14d>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x40,%ecx
sub    -0xc(%rbp),%ecx
cmp    %ecx,%eax
jge    1333 <main+0x1a3>
mov    $0x20,%edi
call   1040 <putchar@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1308 <main+0x178>
mov    $0xd,%edi
call   1040 <putchar@plt>
mov    $0x0,%al
call   1060 <short_wait@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    12a4 <main+0x114>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <restore_io@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe69(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %rax,%r12
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %r12d,%eax
and    $0xfffffffe,%eax
cmp    $0x30,%eax
jne    11c0 <main+0x20>
add    $0xffffffd0,%r12d
lea    0xe5c(%rip),%rdi
call   1050 <puts@plt>
lea    0xe8c(%rip),%rdi
call   1050 <puts@plt>
lea    0xe9f(%rip),%rdi
call   1050 <puts@plt>
lea    0xeb2(%rip),%rdi
call   1050 <puts@plt>
lea    0xec4(%rip),%rdi
call   1050 <puts@plt>
lea    0xed6(%rip),%rdi
call   1050 <puts@plt>
lea    0xee5(%rip),%rdi
call   1050 <puts@plt>
lea    0xf13(%rip),%rdi
call   1050 <puts@plt>
lea    0xdb1(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xf09(%rip),%rdi
call   1050 <puts@plt>
lea    0xf10(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
xor    %eax,%eax
call   10b0 <setup_io@plt>
xor    %eax,%eax
call   1030 <setup_gpio@plt>
xor    %eax,%eax
call   1090 <setup_spi@plt>
lea    0xd81(%rip),%r14
xor    %r13d,%r13d
jmp    12be <main+0x11e>
nopl   0x0(%rax)
mov    $0xd,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
call   1070 <short_wait@plt>
add    $0x1,%r13d
cmp    $0x186a0,%r13d
je     1334 <main+0x194>
mov    %r12d,%edi
call   10a0 <read_adc@plt>
mov    %eax,%ebp
mov    %eax,%ebx
sar    $0x4,%ebx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x10,%ebp
jl     1308 <main+0x168>
cmp    $0x2,%ebx
mov    $0x1,%r15d
cmovge %ebx,%r15d
nopl   0x0(%rax,%rax,1)
mov    $0x23,%edi
call   1040 <putchar@plt>
add    $0xffffffff,%r15d
jne    12f0 <main+0x150>
cmp    $0x3ff,%ebp
jg     12a0 <main+0x100>
mov    $0x40,%ebp
sub    %ebx,%ebp
cmp    $0x2,%ebp
mov    $0x1,%eax
cmovl  %eax,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x20,%edi
call   1040 <putchar@plt>
add    $0xffffffff,%ebp
jne    1320 <main+0x180>
jmp    12a0 <main+0x100>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <restore_io@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Which channel do you want to test? Type 0 or 1.\0A\00"
@.str.1 = [61 x i8] c"These are the connections for the analogue to digital test:\0A\00"
@.str.2 = [32 x i8] c"jumper connecting GP11 to SCLK\0A\00"
@.str.3 = [32 x i8] c"jumper connecting GP10 to MOSI\0A\00"
@.str.4 = [31 x i8] c"jumper connecting GP9 to MISO\0A\00"
@.str.5 = [31 x i8] c"jumper connecting GP8 to CSnA\0A\00"
@.str.6 = [28 x i8] c"Potentiometer connections:\0A\00"
@.str.7 = [59 x i8] c" (call 1 and 3 the ends of the resistor and 2 the wiper)\0A\00"
@.str.8 = [20 x i8] c" connect 3 to 3V3\0A\00"
@.str.9 = [21 x i8] c" connect 2 to AD%d\0A\00"
@.str.10 = [20 x i8] c" connect 1 to GND\0A\00"
@.str.11 = [23 x i8] c"When ready hit enter.\0A\00"
@.str.12 = [6 x i8] c"%04d \00"
@.str.13 = [2 x i8] c"\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
br label %6
6: ; preds = %17, %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @getchar()
%9 = trunc i64 %8 to i32
store i32 %9, i32* %5
%10 = call i64 (...) @getchar()
br label %11
11: ; preds = %6
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 48
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 49
br label %17
17: ; preds = %14, %11
%18 = phi i1 [ false, %11 ], [ %16, %14 ]
br i1 %18, label %6, label %19
19: ; preds = %17
%20 = load i32, i32* %5
%21 = sub nsw i32 %20, 48
store i32 %21, i32* %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.7, i64 0, i64 0))
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%30 = load i32, i32* %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0))
%34 = call i64 (...) @getchar()
%35 = call i32 (...) @setup_io()
%36 = call i32 (...) @setup_gpio()
%37 = call i32 (...) @setup_spi()
store i32 0, i32* %1
br label %38
38: ; preds = %71, %19
%39 = load i32, i32* %1
%40 = icmp slt i32 %39, 100000
br i1 %40, label %41, label %74
41: ; preds = %38
%42 = load i32, i32* %5
%43 = call i32 @read_adc(i32 %42)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = ashr i32 %44, 4
store i32 %45, i32* %3
%46 = load i32, i32* %2
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %46)
store i32 0, i32* %4
br label %48
48: ; preds = %54, %41
%49 = load i32, i32* %4
%50 = load i32, i32* %3
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = call i32 @putchar(i32 35)
br label %54
54: ; preds = %52
%55 = load i32, i32* %4
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
br label %48
57: ; preds = %48
store i32 0, i32* %4
br label %58
58: ; preds = %65, %57
%59 = load i32, i32* %4
%60 = load i32, i32* %3
%61 = sub nsw i32 64, %60
%62 = icmp slt i32 %59, %61
br i1 %62, label %63, label %68
63: ; preds = %58
%64 = call i32 @putchar(i32 32)
br label %65
65: ; preds = %63
%66 = load i32, i32* %4
%67 = add nsw i32 %66, 1
store i32 %67, i32* %4
br label %58
68: ; preds = %58
%69 = call i32 @putchar(i32 13)
%70 = call i32 (...) @short_wait()
br label %71
71: ; preds = %68
%72 = load i32, i32* %1
%73 = add nsw i32 %72, 1
store i32 %73, i32* %1
br label %38
74: ; preds = %38
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
%76 = call i32 (...) @restore_io()
ret void
}

LLVM IR (-O2):
@.str.9 = [21 x i8] c" connect 2 to AD%d\0A\00"
@.str.12 = [6 x i8] c"%04d \00"
@str = [48 x i8] c"Which channel do you want to test? Type 0 or 1.\00"
@str.14 = [60 x i8] c"These are the connections for the analogue to digital test:\00"
@str.15 = [31 x i8] c"jumper connecting GP11 to SCLK\00"
@str.16 = [31 x i8] c"jumper connecting GP10 to MOSI\00"
@str.17 = [30 x i8] c"jumper connecting GP9 to MISO\00"
@str.18 = [30 x i8] c"jumper connecting GP8 to CSnA\00"
@str.19 = [27 x i8] c"Potentiometer connections:\00"
@str.20 = [58 x i8] c" (call 1 and 3 the ends of the resistor and 2 the wiper)\00"
@str.21 = [19 x i8] c" connect 3 to 3V3\00"
@str.22 = [19 x i8] c" connect 1 to GND\00"
@str.23 = [22 x i8] c"When ready hit enter.\00"
define void @main() {
br label %1
1: ; preds = %1, %0
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 (...) @getchar()
%4 = tail call i64 (...) @getchar()
%5 = and i64 %3, 4294967294
%6 = icmp eq i64 %5, 48
br i1 %6, label %7, label %1
7: ; preds = %1
%8 = trunc i64 %3 to i32
%9 = add nsw i32 %8, -48
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.14, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.15, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.16, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.17, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.18, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.19, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str.20, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.21, i64 0, i64 0))
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.9, i64 0, i64 0), i32 %9)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.22, i64 0, i64 0))
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.23, i64 0, i64 0))
%21 = tail call i64 (...) @getchar()
%22 = tail call i32 (...) @setup_io()
%23 = tail call i32 (...) @setup_gpio()
%24 = tail call i32 (...) @setup_spi()
br label %25
25: ; preds = %7, %48
%26 = phi i32 [ 0, %7 ], [ %51, %48 ]
%27 = tail call i32 @read_adc(i32 %9)
%28 = ashr i32 %27, 4
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i32 %27)
%30 = icmp sgt i32 %27, 15
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i32 @llvm.smax.i32(i32 %28, i32 1)
br label %38
33: ; preds = %38
%34 = icmp slt i32 %27, 1024
br i1 %34, label %35, label %48
35: ; preds = %25, %33
%36 = sub nsw i32 64, %28
%37 = call i32 @llvm.smax.i32(i32 %36, i32 1)
br label %43
38: ; preds = %31, %38
%39 = phi i32 [ %41, %38 ], [ 0, %31 ]
%40 = tail call i32 @putchar(i32 35)
%41 = add nuw nsw i32 %39, 1
%42 = icmp eq i32 %41, %32
br i1 %42, label %33, label %38
43: ; preds = %35, %43
%44 = phi i32 [ %46, %43 ], [ 0, %35 ]
%45 = tail call i32 @putchar(i32 32)
%46 = add nuw nsw i32 %44, 1
%47 = icmp eq i32 %46, %37
br i1 %47, label %48, label %43
48: ; preds = %43, %33
%49 = tail call i32 @putchar(i32 13)
%50 = tail call i32 (...) @short_wait()
%51 = add nuw nsw i32 %26, 1
%52 = icmp eq i32 %51, 100000
br i1 %52, label %53, label %25
53: ; preds = %48
%54 = tail call i32 @putchar(i32 10)
%55 = tail call i32 (...) @restore_io()
ret void
}