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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, float);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, float);
scalar_t__ read(int, char *, int);
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strstr(char *, char *);
int strtof(char *, int /*<<< orphan*/ *);
int readSensor(const char *sensorName, const char *file, float *temp) {
  char buf[1024];
  char data[4096];
  char *tString = NULL;
  ssize_t len = 0;
  int fd = -1;
  float t = 0.0;
  if (0 > (fd = open(file, O_RDONLY))) {
    perror("Couldn't open w1 device.");
    return -1;
  }
  while ((read(fd, buf, sizeof(buf))) > 0) {
    len += snprintf(data + len, sizeof(data) - len, "%s", buf);
  }
  close(fd);
  tString = strstr(buf, "t=") + 2;
  t = strtof(tString, NULL) / 1000;
  if (t == 85.000) {
    fprintf(stderr, "Sensor: %s - Read invalid temperature %f\n", sensorName,
            t);
    return -1;
  }
  printf("Sensor: %s  - Temp: %.3f\n", sensorName, t);
  *temp = t;
  return 0;
}


x86 assembly (clang, -O0):
<readSensor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x1428(%rbp)
movl   $0x0,-0x142c(%rbp)
movl   $0xffffffff,-0x1430(%rbp)
ff ff ff
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1434(%rbp)
ff
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x1430(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    120d <readSensor+0x7d>
lea    0xe0c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <readSensor+0x1b6>
jmp    1212 <readSensor+0x82>
mov    -0x1430(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1279 <readSensor+0xe9>
lea    -0x1420(%rbp),%rdi
movslq -0x142c(%rbp),%rax
add    %rax,%rdi
movslq -0x142c(%rbp),%rcx
mov    $0x1000,%eax
sub    %rcx,%rax
mov    %eax,%esi
lea    -0x420(%rbp),%rcx
lea    0xdbe(%rip),%rdx
call   1040 <snprintf@plt>
add    -0x142c(%rbp),%eax
mov    %eax,-0x142c(%rbp)
jmp    1212 <readSensor+0x82>
mov    -0x1430(%rbp),%edi
call   1060 <close@plt>
lea    -0x420(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10b0 <strstr@plt>
add    $0x2,%rax
mov    %rax,-0x1428(%rbp)
mov    -0x1428(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <strtof@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x1434(%rbp)
ff
movss  -0x1434(%rbp),%xmm0
ff
cvtss2sd %xmm0,%xmm0
movsd  0xd26(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1317 <readSensor+0x187>
jp     1317 <readSensor+0x187>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
movss  -0x1434(%rbp),%xmm0
ff
lea    0xd21(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1346 <readSensor+0x1b6>
mov    -0x10(%rbp),%rsi
movss  -0x1434(%rbp),%xmm0
ff
lea    0xd27(%rip),%rdi
call   1030 <printf@plt>
movss  -0x1434(%rbp),%xmm0
ff
mov    -0x20(%rbp),%rax
movss  %xmm0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readSensor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1418,%rsp
mov    %rdx,%rbx
mov    %rsi,%rax
mov    %rdi,%r15
mov    0x2e37(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12ce <readSensor+0x13e>
mov    %eax,%r13d
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    122b <readSensor+0x9b>
xor    %ebp,%ebp
lea    0xe2f(%rip),%r12
lea    0x10(%rsp),%rbx
nop
movslq %ebp,%r14
lea    (%rsp,%r14,1),%rdi
add    $0x410,%rdi
mov    $0x1000,%esi
sub    %r14d,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    %eax,%ebp
add    %r14d,%ebp
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     11f0 <readSensor+0x60>
mov    %r13d,%edi
call   1060 <close@plt>
lea    0xde2(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10b0 <strstr@plt>
lea    0x2(%rax),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
call   1050 <strtof@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
cvtsi2ss %ecx,%xmm0
add    $0xfffeb3f8,%eax
cmp    $0x3e7,%eax
ja     1296 <readSensor+0x106>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
mov    %r15,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12ba <readSensor+0x12a>
lea    0xdac(%rip),%rdi
mov    %r15,%rsi
movss  %xmm0,0x4(%rsp)
call   1030 <printf@plt>
mov    0x8(%rsp),%rax
movss  0x4(%rsp),%xmm0
movss  %xmm0,(%rax)
mov    %ebp,%eax
add    $0x1418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    12ba <readSensor+0x12a>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [25 x i8] c"Couldn't open w1 device.\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [3 x i8] c"t=\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"Sensor: %s - Read invalid temperature %f\0A\00"
@.str.4 = [26 x i8] c"Sensor: %s - Temp: %.3f\0A\00"
define i32 @readSensor(i8* %0, i8* %1, float* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca float*
%8 = alloca [1024 x i8]
%9 = alloca [4096 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca float
store i8* %0, i8** %5
store i8* %1, i8** %6
store float* %2, float** %7
store i8* null, i8** %10
store i32 0, i32* %11
store i32 -1, i32* %12
store float 0.000000e+00, float* %13
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %12
%17 = icmp sgt i32 0, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %63
20: ; preds = %3
br label %21
21: ; preds = %26, %20
%22 = load i32, i32* %12
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%24 = call i64 @read(i32 %22, i8* %23, i32 1024)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %21
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = sub i64 4096, %32
%34 = trunc i64 %33 to i32
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%36 = call i32 @snprintf(i8* %30, i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %11
%38 = add nsw i32 %37, %36
store i32 %38, i32* %11
br label %21
39: ; preds = %21
%40 = load i32, i32* %12
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = call i8* @strstr(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%44 = getelementptr inbounds i8, i8* %43, i64 2
store i8* %44, i8** %10
%45 = load i8*, i8** %10
%46 = call i32 @strtof(i8* %45, i32* null)
%47 = sdiv i32 %46, 1000
%48 = sitofp i32 %47 to float
store float %48, float* %13
%49 = load float, float* %13
%50 = fpext float %49 to double
%51 = fcmp oeq double %50, 8.500000e+01
br i1 %51, label %52, label %57
52: ; preds = %39
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %5
%55 = load float, float* %13
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %54, float %55)
store i32 -1, i32* %4
br label %63
57: ; preds = %39
%58 = load i8*, i8** %5
%59 = load float, float* %13
%60 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %58, float %59)
%61 = load float, float* %13
%62 = load float*, float** %7
store float %61, float* %62
store i32 0, i32* %4
br label %63
63: ; preds = %57, %52, %18
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [25 x i8] c"Couldn't open w1 device.\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [3 x i8] c"t=\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"Sensor: %s - Read invalid temperature %f\0A\00"
@.str.4 = [26 x i8] c"Sensor: %s - Temp: %.3f\0A\00"
define i32 @readSensor(i8* %0, i8* nocapture %1, float* nocapture writeonly %2) {
%4 = alloca [1024 x i8]
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = call i64 @read(i32 %9, i8* %6, i32 1024)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %16, label %25
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %39
16: ; preds = %11, %16
%17 = phi i32 [ %22, %16 ], [ 0, %11 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %18
%20 = sub i32 4096, %17
%21 = call i32 @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%22 = add nsw i32 %21, %17
%23 = call i64 @read(i32 %9, i8* %6, i32 1024)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %16, label %25
25: ; preds = %16, %11
%26 = call i32 @close(i32 %9)
%27 = call i8* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds i8, i8* %27, i64 2
%29 = call i32 @strtof(i8* nocapture %28, i32* null)
%30 = sdiv i32 %29, 1000
%31 = sitofp i32 %30 to float
%32 = add i32 %29, -85000
%33 = icmp ult i32 %32, 1000
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %0, float %31)
br label %39
37: ; preds = %25
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %0, float %31)
store float %31, float* %2
br label %39
39: ; preds = %37, %34, %14
%40 = phi i32 [ -1, %14 ], [ -1, %34 ], [ 0, %37 ]
ret i32 %40
}