#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*/ uint8_t;
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int file;
  int adapter_nr = 1;
  int addr = 0x48;
  char filename[50];
  snprintf(filename, sizeof(filename) - 1, "/dev/i2c-%d", adapter_nr);
  file = open(filename, O_RDWR);
  if (file < 0) {
    perror("Cannot open i2c device");
    return 1;
  }
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    perror("Cannot set slave address");
    close(file);
    return 1;
  }
  uint8_t reg = 0;
  char buf[256];
  ssize_t length;
  if (write(file, &reg, 1) < 0) {
    perror("Cannot write to i2c device");
    goto onexit;
  }
  if ((length = read(file, buf, 4)) < 0) {
    perror("Cannot read from i2c device");
    goto onexit;
  }
  if ((buf[2] & 0x80) == 0) {
    fprintf(stderr, "Not ready.\n");
    goto onexit;
  }
  int value = (buf[0] << 8) | buf[1];
  printf("%lf\n", ((double)value) / 16 / 8);
onexit:
  close(file);
  (void)argc;
  (void)argv;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x48,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x31,%esi
lea    0xe46(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1206 <main+0x76>
lea    0xe27(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x19e>
mov    -0x14(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1244 <main+0xb4>
lea    0xe08(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x19e>
movl   $0x0,-0x54(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x54(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1277 <main+0xe7>
lea    0xddf(%rip),%rdi
call   10b0 <perror@plt>
jmp    131f <main+0x18f>
mov    -0x14(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %eax,-0x164(%rbp)
cmp    $0x0,%eax
jge    12ab <main+0x11b>
lea    0xdc6(%rip),%rdi
call   10b0 <perror@plt>
jmp    131f <main+0x18f>
movsbl -0x15e(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    12da <main+0x14a>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
call   1090 <fprintf@plt>
jmp    131f <main+0x18f>
movsbl -0x160(%rbp),%eax
shl    $0x8,%eax
movsbl -0x15f(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x168(%rbp)
cvtsi2sdl -0x168(%rbp),%xmm0
ff
movsd  0xd05(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xcf1(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x158,%rsp
lea    0xe70(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x31,%esi
mov    $0x1,%ecx
call   1050 <snprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1292 <main+0x102>
mov    %eax,%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x48,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     12a0 <main+0x110>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12ba <main+0x12a>
lea    0x50(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12c8 <main+0x138>
cmpb   $0x0,0x52(%rsp)
js     124e <main+0xbe>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1090 <fprintf@plt>
jmp    127d <main+0xed>
movsbl 0x50(%rsp),%eax
shl    $0x8,%eax
movsbl 0x51(%rsp),%ecx
or     %eax,%ecx
cvtsi2sd %ecx,%xmm0
mulsd  0xd97(%rip),%xmm0
mulsd  0xd97(%rip),%xmm0
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x158,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd83(%rip),%rdi
call   10b0 <perror@plt>
jmp    1286 <main+0xf6>
lea    0xd8c(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
jmp    1286 <main+0xf6>
lea    0xd8b(%rip),%rdi
call   10b0 <perror@plt>
jmp    127d <main+0xed>
lea    0xd98(%rip),%rdi
call   10b0 <perror@plt>
jmp    127d <main+0xed>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Cannot open i2c device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [25 x i8] c"Cannot set slave address\00"
@.str.3 = [27 x i8] c"Cannot write to i2c device\00"
@.str.4 = [28 x i8] c"Cannot read from i2c device\00"
@stderr = global i32 0
@.str.5 = [12 x i8] c"Not ready.\0A\00"
@.str.6 = [5 x i8] c"%lf\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [50 x i8]
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 72, i32* %8
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* %7
%16 = call i32 @snprintf(i8* %14, i32 49, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %75
24: ; preds = %2
%25 = load i32, i32* %6
%26 = load i32, i32* @I2C_SLAVE
%27 = load i32, i32* %8
%28 = call i64 @ioctl(i32 %25, i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 1, i32* %3
br label %75
34: ; preds = %24
store i32 0, i32* %10
%35 = load i32, i32* %6
%36 = call i64 @write(i32 %35, i32* %10, i32 1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %70
40: ; preds = %34
%41 = load i32, i32* %6
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 4)
store i32 %43, i32* %12
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %70
47: ; preds = %40
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 2
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = and i32 %50, 128
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %70
56: ; preds = %47
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = shl i32 %59, 8
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 1
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = or i32 %60, %63
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = sitofp i32 %65 to double
%67 = fdiv double %66, 1.600000e+01
%68 = fdiv double %67, 8.000000e+00
%69 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), double %68)
br label %70
70: ; preds = %56, %53, %45, %38
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %4
%74 = load i8**, i8*** %5
store i32 0, i32* %3
br label %75
75: ; preds = %70, %30, %22
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Cannot open i2c device\00"
@I2C_SLAVE = global i32 0
@.str.2 = [25 x i8] c"Cannot set slave address\00"
@.str.3 = [27 x i8] c"Cannot write to i2c device\00"
@.str.4 = [28 x i8] c"Cannot read from i2c device\00"
@stderr = global i32 0
@.str.5 = [12 x i8] c"Not ready.\0A\00"
@.str.6 = [5 x i8] c"%lf\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 49, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %53
13: ; preds = %2
%14 = load i32, i32* @I2C_SLAVE
%15 = call i64 @ioctl(i32 %9, i32 %14, i32 72)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @close(i32 %9)
br label %53
20: ; preds = %13
%21 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = call i64 @write(i32 %9, i32* %4, i32 1)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %51
27: ; preds = %20
%28 = call i32 @read(i32 %9, i8* %22, i32 4)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %51
32: ; preds = %27
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 2
%34 = load i8, i8* %33
%35 = icmp sgt i8 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %51
39: ; preds = %32
%40 = load i8, i8* %22
%41 = sext i8 %40 to i32
%42 = shl nsw i32 %41, 8
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 1
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = or i32 %42, %45
%47 = sitofp i32 %46 to double
%48 = fmul double %47, 6.250000e-02
%49 = fmul double %48, 1.250000e-01
%50 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), double %49)
br label %51
51: ; preds = %39, %36, %30, %25
%52 = call i32 @close(i32 %9)
br label %53
53: ; preds = %51, %17, %11
%54 = phi i32 [ 1, %11 ], [ 1, %17 ], [ 0, %51 ]
ret i32 %54
}