#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 */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int read_i2c(char *filename, int address, double *temp) {
  unsigned char msb, lsb;
  int fd;
  unsigned char buf[10];
  if ((fd = open(filename, O_RDWR)) < 0) {
    fprintf(stderr, "Failed to open i2c port\n");
    return 1;
  }
  if (ioctl(fd, I2C_SLAVE, address) < 0) {
    fprintf(stderr, "Unable to get bus access to talk to slave\n");
    return 1;
  }
  buf[0] = 0;
  if ((write(fd, buf, 1)) != 1) {
    fprintf(stderr, "Error writing to i2c slave, %s\n", strerror(errno));
    return 1;
  }
  if ((read(fd, buf, 1)) != 1) {
    fprintf(stderr, "Unable to read from slave, %s\n", strerror(errno));
    return 1;
  } else {
    msb = buf[0];
    if (read(fd, buf, 1) != 1) {
      fprintf(stderr, "Unable to read from slave, %s\n", strerror(errno));
      return 1;
    } else {
      int res;
      lsb = buf[0];
      res = (msb << 8) | lsb;
      res >>= 4;
      if (res & (1 << 11))
        res |= 0xF800;
      *temp = res * 0.0625;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_i2c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11b4 <read_i2c+0x54>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <read_i2c+0x1fb>
mov    -0x28(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f5 <read_i2c+0x95>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <read_i2c+0x1fb>
movb   $0x0,-0x32(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     124d <read_i2c+0xed>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <read_i2c+0x1fb>
mov    -0x28(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     12a1 <read_i2c+0x141>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <read_i2c+0x1fb>
mov    -0x32(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     12fb <read_i2c+0x19b>
mov    0x2d08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <read_i2c+0x1fb>
mov    -0x32(%rbp),%al
mov    %al,-0x22(%rbp)
movzbl -0x21(%rbp),%eax
shl    $0x8,%eax
movzbl -0x22(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
and    $0x800,%eax
cmp    $0x0,%eax
je     1336 <read_i2c+0x1d6>
mov    -0x38(%rbp),%eax
or     $0xf800,%eax
mov    %eax,-0x38(%rbp)
cvtsi2sdl -0x38(%rbp),%xmm0
movsd  0xcbd(%rip),%xmm1
mulsd  %xmm1,%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    1354 <read_i2c+0x1f4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_i2c>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     123d <read_i2c+0xdd>
mov    %eax,%ebp
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     124f <read_i2c+0xef>
movb   $0x0,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    126d <read_i2c+0x10d>
lea    0xe(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    128d <read_i2c+0x12d>
movzbl 0xe(%rsp),%r15d
lea    0xe(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    128d <read_i2c+0x12d>
movzbl 0xe(%rsp),%eax
mov    %r15d,%ecx
shl    $0x8,%ecx
or     %eax,%ecx
shr    $0x4,%ecx
mov    %ecx,%eax
or     $0xf800,%eax
test   $0x80,%r15b
cmove  %ecx,%eax
cvtsi2sd %eax,%xmm0
mulsd  0xdcc(%rip),%xmm0
movsd  %xmm0,(%r14)
xor    %ebx,%ebx
jmp    12b7 <read_i2c+0x157>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
jmp    125f <read_i2c+0xff>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebx
jmp    12b7 <read_i2c+0x157>
mov    0x2d5c(%rip),%rax
mov    (%rax),%ebp
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc1(%rip),%rsi
jmp    12ab <read_i2c+0x14b>
mov    0x2d3c(%rip),%rax
mov    (%rax),%ebp
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc1(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Failed to open i2c port\0A\00"
@I2C_SLAVE = global i32 0
@.str.1 = [43 x i8] c"Unable to get bus access to talk to slave\0A\00"
@.str.2 = [32 x i8] c"Error writing to i2c slave, %s\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"Unable to read from slave, %s\0A\00"
define i32 @read_i2c(i8* %0, i32 %1, double* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca double*
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca [10 x i8]
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store double* %2, double** %7
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %86
20: ; preds = %3
%21 = load i32, i32* %10
%22 = load i32, i32* @I2C_SLAVE
%23 = load i32, i32* %6
%24 = call i64 @ioctl(i32 %21, i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %86
29: ; preds = %20
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
store i8 0, i8* %30
%31 = load i32, i32* %10
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 1)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 1, i32* %4
br label %86
40: ; preds = %29
%41 = load i32, i32* %10
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 1)
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %48)
store i32 1, i32* %4
br label %86
50: ; preds = %40
%51 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%52 = load i8, i8* %51
store i8 %52, i8* %8
%53 = load i32, i32* %10
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 1)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %62
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 1, i32* %4
br label %86
62: ; preds = %50
%63 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%64 = load i8, i8* %63
store i8 %64, i8* %9
%65 = load i8, i8* %8
%66 = zext i8 %65 to i32
%67 = shl i32 %66, 8
%68 = load i8, i8* %9
%69 = zext i8 %68 to i32
%70 = or i32 %67, %69
store i32 %70, i32* %12
%71 = load i32, i32* %12
%72 = ashr i32 %71, 4
store i32 %72, i32* %12
%73 = load i32, i32* %12
%74 = and i32 %73, 2048
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %62
%77 = load i32, i32* %12
%78 = or i32 %77, 63488
store i32 %78, i32* %12
br label %79
79: ; preds = %76, %62
%80 = load i32, i32* %12
%81 = sitofp i32 %80 to double
%82 = fmul double %81, 6.250000e-02
%83 = load double*, double** %7
store double %82, double* %83
br label %84
84: ; preds = %79
br label %85
85: ; preds = %84
store i32 0, i32* %4
br label %86
86: ; preds = %85, %57, %45, %35, %26, %17
%87 = load i32, i32* %4
ret i32 %87
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Failed to open i2c port\0A\00"
@I2C_SLAVE = global i32 0
@.str.1 = [43 x i8] c"Unable to get bus access to talk to slave\0A\00"
@.str.2 = [32 x i8] c"Error writing to i2c slave, %s\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"Unable to read from slave, %s\0A\00"
define i32 @read_i2c(i8* nocapture %0, i32 %1, double* nocapture writeonly %2) {
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %57
12: ; preds = %3
%13 = load i32, i32* @I2C_SLAVE
%14 = tail call i64 @ioctl(i32 %7, i32 %13, i32 %1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %57
19: ; preds = %12
store i8 0, i8* %5
%20 = call i32 @write(i32 %7, i8* %5, i32 1)
%21 = icmp eq i32 %20, 1
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %57
27: ; preds = %19
%28 = call i32 @read(i32 %7, i8* %5, i32 1)
%29 = icmp eq i32 %28, 1
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %57
35: ; preds = %27
%36 = load i8, i8* %5
%37 = call i32 @read(i32 %7, i8* %5, i32 1)
%38 = icmp eq i32 %37, 1
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %57
44: ; preds = %35
%45 = load i8, i8* %5
%46 = zext i8 %36 to i32
%47 = shl nuw nsw i32 %46, 8
%48 = zext i8 %45 to i32
%49 = or i32 %47, %48
%50 = lshr i32 %49, 4
%51 = and i32 %46, 128
%52 = icmp eq i32 %51, 0
%53 = or i32 %50, 63488
%54 = select i1 %52, i32 %50, i32 %53
%55 = sitofp i32 %54 to double
%56 = fmul double %55, 6.250000e-02
store double %56, double* %2
br label %57
57: ; preds = %44, %39, %30, %22, %16, %9
%58 = phi i32 [ 1, %9 ], [ 1, %16 ], [ 1, %22 ], [ 1, %30 ], [ 1, %39 ], [ 0, %44 ]
ret i32 %58
}