#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 uint8_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, float);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int ADS_address = 0x49;
  int I2CFile;
  uint8_t writeBuf[3];
  uint8_t readBuf[2];
  int16_t val;
  I2CFile = open("/dev/i2c-1", O_RDWR);
  ioctl(I2CFile, I2C_SLAVE, ADS_address);
  writeBuf[0] = 1;
  writeBuf[1] = 0xc3;
  writeBuf[2] = 0x03;
  readBuf[0] = 0;
  readBuf[1] = 0;
  write(I2CFile, writeBuf, 3);
  while ((readBuf[0] & 0x80) == 0) {
    read(I2CFile, readBuf, 2);
  }
  writeBuf[0] = 0;
  write(I2CFile, writeBuf, 1);
  read(I2CFile, readBuf, 2);
  val = readBuf[0] << 8 | readBuf[1];
  printf("Voltage Reading %f (V) \n", (float)val * 4.096 / 32767.0);
  close(I2CFile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x49,-0x8(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1050 <ioctl@plt>
movl   $0x1,-0x18(%rbp)
movl   $0xc3,-0x14(%rbp)
movl   $0x3,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11fd <main+0x9d>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1070 <read@plt>
jmp    11d6 <main+0x76>
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1070 <read@plt>
mov    -0x20(%rbp),%eax
shl    $0x8,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cvtsi2ssl -0x24(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdc5(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xdb1(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
lea    0xdbd(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    $0x49,%edx
call   1050 <ioctl@plt>
movabs $0xc300000001,%rax
mov    %rax,0xc(%rsp)
movl   $0x3,0x14(%rsp)
movq   $0x0,0x4(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1030 <write@plt>
lea    0x4(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1070 <read@plt>
testb  $0x80,0x4(%rsp)
je     11d0 <main+0x70>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1070 <read@plt>
mov    0x4(%rsp),%eax
shl    $0x8,%eax
or     0x8(%rsp),%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xdd5(%rip),%xmm0
divsd  0xdd5(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
lea    0xddd(%rip),%rdi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.1 = [25 x i8] c"Voltage Reading %f (V) \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i32]
%5 = alloca [2 x i32]
%6 = alloca i32
store i32 0, i32* %1
store i32 73, i32* %2
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = load i32, i32* @I2C_SLAVE
%11 = load i32, i32* %2
%12 = call i32 @ioctl(i32 %9, i32 %10, i32 %11)
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
store i32 1, i32* %13
%14 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 1
store i32 195, i32* %14
%15 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 2
store i32 3, i32* %15
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
store i32 0, i32* %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
store i32 0, i32* %17
%18 = load i32, i32* %3
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
%20 = call i32 @write(i32 %18, i32* %19, i32 3)
br label %21
21: ; preds = %26, %0
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%23 = load i32, i32* %22
%24 = and i32 %23, 128
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* %3
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%29 = call i32 @read(i32 %27, i32* %28, i32 2)
br label %21
30: ; preds = %21
%31 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
store i32 0, i32* %31
%32 = load i32, i32* %3
%33 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
%34 = call i32 @write(i32 %32, i32* %33, i32 1)
%35 = load i32, i32* %3
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%37 = call i32 @read(i32 %35, i32* %36, i32 2)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%39 = load i32, i32* %38
%40 = shl i32 %39, 8
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%42 = load i32, i32* %41
%43 = or i32 %40, %42
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = sitofp i32 %44 to float
%46 = fpext float %45 to double
%47 = fmul double %46, 4.096000e+00
%48 = fdiv double %47, 3.276700e+04
%49 = fptrunc double %48 to float
%50 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), float %49)
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.1 = [25 x i8] c"Voltage Reading %f (V) \0A\00"
define i32 @main() {
%1 = alloca [3 x i32]
%2 = alloca [2 x i32]
%3 = bitcast [3 x i32]* %1 to i8*
%4 = bitcast [2 x i32]* %2 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @I2C_SLAVE
%8 = tail call i32 @ioctl(i32 %6, i32 %7, i32 73)
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 0
store i32 1, i32* %9
%10 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 1
store i32 195, i32* %10
%11 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 2
store i32 3, i32* %11
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
store i32 0, i32* %12
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
store i32 0, i32* %13
%14 = call i32 @write(i32 %6, i32* %9, i32 3)
br label %15
15: ; preds = %0, %15
%16 = call i32 @read(i32 %6, i32* %12, i32 2)
%17 = load i32, i32* %12
%18 = and i32 %17, 128
%19 = icmp eq i32 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %15
store i32 0, i32* %9
%21 = call i32 @write(i32 %6, i32* %9, i32 1)
%22 = call i32 @read(i32 %6, i32* %12, i32 2)
%23 = load i32, i32* %12
%24 = shl i32 %23, 8
%25 = load i32, i32* %13
%26 = or i32 %24, %25
%27 = sitofp i32 %26 to float
%28 = fpext float %27 to double
%29 = fmul double %28, 4.096000e+00
%30 = fdiv double %29, 3.276700e+04
%31 = fptrunc double %30 to float
%32 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), float %31)
%33 = tail call i32 @close(i32 %6)
ret i32 0
}