#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int readADC(int port) {
  FILE *puerto;
  if (port == 0) {
    puerto = fopen("/sys/devices/ocp.2/helper.14/AIN0", "r");
  }
  if (port == 1) {
    puerto = fopen("/sys/devices/ocp.2/helper.14/AIN1", "r");
  }
  if (port == 2) {
    puerto = fopen("/sys/devices/ocp.2/helper.14/AIN2", "r");
  }
  if (port == 3) {
    puerto = fopen("/sys/devices/ocp.2/helper.14/AIN3", "r");
  }
  char valor[5];
  int totalADC, potencia, digitos;
  fread(&valor, 5, 5, puerto);
  int j;
  digitos = 3;
  for (j = 0; j < 4; j++) {
    if (valor[j] == 10) {
      digitos = j - 1;
    }
  }
  totalADC = 0;
  potencia = 1;
  for (j = digitos; j >= 0; j--) {
    totalADC += (valor[j] - 48) * potencia;
    potencia *= 10;
  }
  fclose(puerto);
  return totalADC;
}


x86 assembly (clang, -O0):
<readADC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    115c <readADC+0x2c>
lea    0xeb4(%rip),%rdi
lea    0xecf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    117d <readADC+0x4d>
lea    0xeb7(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
jne    119e <readADC+0x6e>
lea    0xeb8(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x3,-0x4(%rbp)
jne    11bf <readADC+0x8f>
lea    0xeb9(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x15(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x5,%edx
mov    %edx,%esi
call   1030 <fread@plt>
movl   $0x3,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    1219 <readADC+0xe9>
movslq -0x28(%rbp),%rax
movsbl -0x15(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    1206 <readADC+0xd6>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    120b <readADC+0xdb>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e1 <readADC+0xb1>
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1262 <readADC+0x132>
movslq -0x28(%rbp),%rax
movsbl -0x15(%rbp,%rax,1),%eax
sub    $0x30,%eax
imul   -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
imul   $0xa,-0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <readADC+0xfd>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readADC>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
ja     115e <readADC+0x2e>
movslq %edi,%rax
lea    0xf44(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
lea    0xecc(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
lea    0xb(%rsp),%r15
mov    %r15,%rdi
mov    $0x5,%esi
mov    $0x5,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %eax,%eax
cmpb   $0xa,0xb(%rsp)
setne  %al
lea    -0x1(,%rax,4),%eax
xor    %ebx,%ebx
cmpb   $0xa,0xc(%rsp)
cmove  %ebx,%eax
cmpb   $0xa,0xd(%rsp)
mov    $0x1,%edx
cmovne %eax,%edx
cmpb   $0xa,0xe(%rsp)
mov    $0x2,%ecx
cmovne %edx,%ecx
test   %ecx,%ecx
js     1206 <readADC+0xd6>
mov    %ecx,%eax
movsbl 0xb(%rsp,%rax,1),%ebx
add    $0xffffffd0,%ebx
test   %ecx,%ecx
je     1206 <readADC+0xd6>
mov    %rax,%rcx
movsbl 0xa(%rsp,%rax,1),%edx
lea    (%rdx,%rdx,4),%edx
lea    (%rbx,%rdx,2),%ebx
add    $0xfffffe20,%ebx
add    $0xffffffffffffffff,%rcx
je     1206 <readADC+0xd6>
lea    -0x2(%rax),%rcx
movsbl 0xb(%rsp,%rcx,1),%edx
imul   $0x64,%edx,%edx
add    %edx,%ebx
add    $0xffffed40,%ebx
test   %rcx,%rcx
je     1206 <readADC+0xd6>
movsbl -0x3(%rax,%r15,1),%eax
imul   $0x3e8,%eax,%eax
add    %eax,%ebx
add    $0xffff4480,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN1\00"
@.str.3 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN2\00"
@.str.4 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN3\00"
define i32 @readADC(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [5 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
br label %13
13: ; preds = %11, %1
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %3
br label %18
18: ; preds = %16, %13
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %3
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, 3
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32* @fopen(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %3
br label %28
28: ; preds = %26, %23
%29 = bitcast [5 x i8]* %4 to i8**
%30 = load i32*, i32** %3
%31 = call i32 @fread(i8** %29, i32 5, i32 5, i32* %30)
store i32 3, i32* %7
store i32 0, i32* %8
br label %32
32: ; preds = %46, %28
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 4
br i1 %34, label %35, label %49
35: ; preds = %32
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32, i32* %8
%44 = sub nsw i32 %43, 1
store i32 %44, i32* %7
br label %45
45: ; preds = %42, %35
br label %46
46: ; preds = %45
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %32
49: ; preds = %32
store i32 0, i32* %5
store i32 1, i32* %6
%50 = load i32, i32* %7
store i32 %50, i32* %8
br label %51
51: ; preds = %67, %49
%52 = load i32, i32* %8
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %70
54: ; preds = %51
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = sub nsw i32 %59, 48
%61 = load i32, i32* %6
%62 = mul nsw i32 %60, %61
%63 = load i32, i32* %5
%64 = add nsw i32 %63, %62
store i32 %64, i32* %5
%65 = load i32, i32* %6
%66 = mul nsw i32 %65, 10
store i32 %66, i32* %6
br label %67
67: ; preds = %54
%68 = load i32, i32* %8
%69 = add nsw i32 %68, -1
store i32 %69, i32* %8
br label %51
70: ; preds = %51
%71 = load i32*, i32** %3
%72 = call i32 @fclose(i32* %71)
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
@.str = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN0\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN1\00"
@.str.3 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN2\00"
@.str.4 = [34 x i8] c"/sys/devices/ocp.2/helper.14/AIN3\00"
@reltable.readADC = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([34 x i8]* @.str to i64), i64 ptrtoint ([4 x i32]* @reltable.readADC to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([34 x i8]* @.str.2 to i64), i64 ptrtoint ([4 x i32]* @reltable.readADC to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([34 x i8]* @.str.3 to i64), i64 ptrtoint ([4 x i32]* @reltable.readADC to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([34 x i8]* @.str.4 to i64), i64 ptrtoint ([4 x i32]* @reltable.readADC to i64)) to i32)]
define i32 @readADC(i32 %0) {
%2 = alloca [5 x i8]
%3 = icmp ult i32 %0, 4
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = sext i32 %0 to i64
%6 = shl i64 %5, 2
%7 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.readADC to i8*), i64 %6)
%8 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %1, %4
%10 = phi i32* [ undef, %1 ], [ %8, %4 ]
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%12 = bitcast [5 x i8]* %2 to i8**
%13 = call i32 @fread(i8** %12, i32 5, i32 5, i32* %10)
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 10
%16 = select i1 %15, i32 -1, i32 3
%17 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
%20 = select i1 %19, i32 0, i32 %16
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 2
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
%24 = select i1 %23, i32 1, i32 %20
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 3
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
%28 = select i1 %27, i32 2, i32 %24
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %63
30: ; preds = %9
%31 = zext i32 %28 to i64
%32 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = add nsw i32 %34, -48
%36 = add nsw i64 %31, -1
%37 = icmp eq i32 %28, 0
br i1 %37, label %63, label %38
38: ; preds = %30
%39 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 %36
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = mul nsw i32 %41, 10
%43 = add nsw i32 %42, -480
%44 = add nsw i32 %43, %35
%45 = add nsw i64 %31, -2
%46 = icmp eq i64 %36, 0
br i1 %46, label %63, label %47
47: ; preds = %38
%48 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 %45
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = mul nsw i32 %50, 100
%52 = add nsw i32 %51, -4800
%53 = add nsw i32 %52, %44
%54 = icmp eq i64 %45, 0
br i1 %54, label %63, label %55
55: ; preds = %47
%56 = add nsw i64 %31, -3
%57 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = mul nsw i32 %59, 1000
%61 = add nsw i32 %60, -48000
%62 = add nsw i32 %61, %53
br label %63
63: ; preds = %30, %38, %47, %55, %9
%64 = phi i32 [ 0, %9 ], [ %35, %30 ], [ %44, %38 ], [ %53, %47 ], [ %62, %55 ]
%65 = tail call i32 @fclose(i32* %10)
ret i32 %64
}