#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char **, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int readADC(unsigned int pin) {
  int fd;
  char buf[64];
  char val[4];
  snprintf(buf, sizeof(buf), "/sys/devices/ocp.2/helper.14/AIN%d", pin);
  fd = open(buf, O_RDONLY);
  if (fd < 0) {
    perror("ADC - problem opening ADC");
  }
  read(fd, &val, 4);
  close(fd);
  return atoi(val);
}


x86 assembly (clang, -O0):
<readADC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x40,%esi
lea    0xe82(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ae <readADC+0x4e>
lea    0xe7a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x54(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
lea    -0x54(%rbp),%rdi
call   1080 <atoi@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readADC>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ecx
lea    0xe90(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c4 <readADC+0x64>
lea    0xc(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14,%rdi
call   1080 <atoi@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
jmp    1199 <readADC+0x39>

LLVM IR (-O0):
@.str = [35 x i8] c"/sys/devices/ocp.2/helper.14/AIN%d\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"ADC - problem opening ADC\00"
define i32 @readADC(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca [4 x i8]
store i32 %0, i32* %2
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = load i32, i32* %3
%18 = bitcast [4 x i8]* %5 to i8**
%19 = call i32 @read(i32 %17, i8** %18, i32 4)
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%23 = call i32 @atoi(i8* %22)
ret i32 %23
}

LLVM IR (-O2):
@.str = [35 x i8] c"/sys/devices/ocp.2/helper.14/AIN%d\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"ADC - problem opening ADC\00"
define i32 @readADC(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = bitcast [4 x i8]* %3 to i8**
%14 = call i32 @read(i32 %8, i8** %13, i32 4)
%15 = call i32 @close(i32 %8)
%16 = call i32 @atoi(i8* %5)
ret i32 %16
}