#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 */
struct dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_LNK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *strstr(char *, char *);
int strtof(char *, int /*<<< orphan*/ *);
float w1_get_temperature(void) {
  DIR *dir;
  struct dirent *dirent;
  char dev[16];
  char devPath[128];
  char buf[256];
  char tmpData[6];
  char path[] = "/sys/bus/w1/devices";
  float tempC = 0.0;
  dir = opendir(path);
  if (dir != NULL) {
    while (dirent = readdir(dir)) {
      if (dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL) {
        strcpy(dev, dirent->d_name);
        printf("Device: %s\n", dev);
      }
    }
    (void)closedir(dir);
  } else {
    perror("Couldn't open the w1 devices directory");
    return tempC;
  }
  sprintf(devPath, "%s/%s/w1_slave", path, dev);
  int fd = open(devPath, O_RDONLY);
  if (fd == -1) {
    perror("Couldn't open the w1 device.");
    return tempC;
  }
  if (read(fd, buf, 256) > 0) {
    strncpy(tmpData, strstr(buf, "t=") + 2, 5);
    tempC = strtof(tmpData, NULL) / 1000;
  }
  close(fd);
  return tempC;
}


x86 assembly (clang, -O0):
<w1_get_temperature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e0,%rsp
mov    0xe1e(%rip),%rax
mov    %rax,-0x1d0(%rbp)
mov    0xe18(%rip),%rax
mov    %rax,-0x1c8(%rbp)
mov    0xe13(%rip),%eax
mov    %eax,-0x1c0(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1d4(%rbp)
ff
lea    -0x1d0(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ae <w1_get_temperature+0xde>
jmp    122e <w1_get_temperature+0x5e>
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12a0 <w1_get_temperature+0xd0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <w1_get_temperature+0xcb>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xda9(%rip),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     129b <w1_get_temperature+0xcb>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x30(%rbp),%rsi
lea    0xd82(%rip),%rdi
call   1060 <printf@plt>
jmp    122e <w1_get_temperature+0x5e>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
jmp    12cc <w1_get_temperature+0xfc>
lea    0xd6f(%rip),%rdi
call   10d0 <perror@plt>
movss  -0x1d4(%rbp),%xmm0
ff
movss  %xmm0,-0x4(%rbp)
jmp    13c6 <w1_get_temperature+0x1f6>
lea    -0xb0(%rbp),%rdi
lea    -0x1d0(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    0xd66(%rip),%rsi
call   10e0 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x1d8(%rbp)
cmpl   $0xffffffff,-0x1d8(%rbp)
jne    1330 <w1_get_temperature+0x160>
lea    0xd41(%rip),%rdi
call   10d0 <perror@plt>
movss  -0x1d4(%rbp),%xmm0
ff
movss  %xmm0,-0x4(%rbp)
jmp    13c6 <w1_get_temperature+0x1f6>
mov    -0x1d8(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
cmp    $0x0,%rax
jle    13ae <w1_get_temperature+0x1de>
lea    -0x1b6(%rbp),%rax
mov    %rax,-0x1e0(%rbp)
lea    -0x1b0(%rbp),%rdi
lea    0xd0a(%rip),%rsi
call   10f0 <strstr@plt>
mov    -0x1e0(%rbp),%rdi
mov    %rax,%rsi
add    $0x8,%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
lea    -0x1b6(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <strtof@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x1d4(%rbp)
ff
mov    -0x1d8(%rbp),%edi
call   1080 <close@plt>
movss  -0x1d4(%rbp),%xmm0
ff
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x1e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<w1_get_temperature>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1c8,%rsp
movaps 0xe18(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x736563,0x20(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1355 <w1_get_temperature+0x185>
mov    %rax,%r13
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1282 <w1_get_temperature+0xb2>
mov    0x2db9(%rip),%rbp
lea    0xdee(%rip),%r14
lea    0x30(%rsp),%r15
lea    0xde6(%rip),%r12
jmp    124d <w1_get_temperature+0x7d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     1282 <w1_get_temperature+0xb2>
mov    (%rax),%rcx
cmp    0x0(%rbp),%rcx
jne    1240 <w1_get_temperature+0x70>
mov    0x8(%rax),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10f0 <strstr@plt>
test   %rax,%rax
je     1240 <w1_get_temperature+0x70>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
jmp    1240 <w1_get_temperature+0x70>
mov    %r13,%rdi
call   1090 <closedir@plt>
lea    0xdba(%rip),%rsi
lea    0x140(%rsp),%rbx
lea    0x10(%rsp),%rdx
lea    0x30(%rsp),%rcx
mov    %rbx,%rdi
call   10e0 <sprintf@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     135e <w1_get_temperature+0x18e>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %rax,%rax
jle    132d <w1_get_temperature+0x15d>
lea    0xd93(%rip),%rsi
lea    0x40(%rsp),%rdi
call   10f0 <strstr@plt>
lea    0x8(%rax),%rsi
lea    0x2a(%rsp),%r14
mov    %r14,%rdi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <strtof@plt>
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
jmp    1330 <w1_get_temperature+0x160>
xorps  %xmm0,%xmm0
movss  %xmm0,0xc(%rsp)
mov    %ebx,%edi
call   1080 <close@plt>
movss  0xc(%rsp),%xmm0
add    $0x1c8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc8(%rip),%rdi
jmp    1365 <w1_get_temperature+0x195>
lea    0xcf5(%rip),%rdi
call   10d0 <perror@plt>
xorps  %xmm0,%xmm0
jmp    1343 <w1_get_temperature+0x173>

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@__const.w1_get_temperature.path = [20 x i8] c"/sys/bus/w1/devices\00"
@DT_LNK = global i64 0
@.str = [4 x i8] c"28-\00"
@.str.1 = [12 x i8] c"Device: %s\0A\00"
@.str.2 = [39 x i8] c"Couldn't open the w1 devices directory\00"
@.str.3 = [15 x i8] c"%s/%s/w1_slave\00"
@O_RDONLY = global i32 0
@.str.4 = [29 x i8] c"Couldn't open the w1 device.\00"
@.str.5 = [3 x i8] c"t=\00"
define float @w1_get_temperature() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca [16 x i8]
%5 = alloca [128 x i8]
%6 = alloca [256 x i8]
%7 = alloca [6 x i8]
%8 = alloca [20 x i8]
%9 = alloca float
%10 = alloca i32
%11 = bitcast [20 x i8]* %8 to i8*
store float 0.000000e+00, float* %9
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%13 = call i32* @opendir(i8* %12)
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %45
16: ; preds = %0
br label %17
17: ; preds = %41, %16
%18 = load i32*, i32** %2
%19 = call %struct.dirent* @readdir(i32* %18)
store %struct.dirent* %19, %struct.dirent** %3
%20 = icmp ne %struct.dirent* %19, null
br i1 %20, label %21, label %42
21: ; preds = %17
%22 = load %struct.dirent*, %struct.dirent** %3
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @DT_LNK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %41
27: ; preds = %21
%28 = load %struct.dirent*, %struct.dirent** %3
%29 = getelementptr inbounds %struct.dirent, %struct.dirent* %28, i32 0, i32 1
%30 = load i8*, i8** %29
%31 = call i32* @strstr(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%35 = load %struct.dirent*, %struct.dirent** %3
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 1
%37 = load i8*, i8** %36
%38 = call i32 @strcpy(i8* %34, i8* %37)
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %33, %27, %21
br label %17
42: ; preds = %17
%43 = load i32*, i32** %2
%44 = call i32 @closedir(i32* %43)
br label %48
45: ; preds = %0
%46 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%47 = load float, float* %9
store float %47, float* %1
br label %80
48: ; preds = %42
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%52 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %51)
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* %53, i32 %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%60 = load float, float* %9
store float %60, float* %1
br label %80
61: ; preds = %48
%62 = load i32, i32* %10
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%64 = call i64 @read(i32 %62, i8* %63, i32 256)
%65 = icmp sgt i64 %64, 0
br i1 %65, label %66, label %76
66: ; preds = %61
%67 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%69 = call i32* @strstr(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%70 = getelementptr inbounds i32, i32* %69, i64 2
%71 = call i32 @strncpy(i8* %67, i32* %70, i32 5)
%72 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%73 = call i32 @strtof(i8* %72, i32* null)
%74 = sdiv i32 %73, 1000
%75 = sitofp i32 %74 to float
store float %75, float* %9
br label %76
76: ; preds = %66, %61
%77 = load i32, i32* %10
%78 = call i32 @close(i32 %77)
%79 = load float, float* %9
store float %79, float* %1
br label %80
80: ; preds = %76, %58, %45
%81 = load float, float* %1
ret float %81
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@__const.w1_get_temperature.path = [20 x i8] c"/sys/bus/w1/devices\00"
@DT_LNK = global i64 0
@.str = [4 x i8] c"28-\00"
@.str.1 = [12 x i8] c"Device: %s\0A\00"
@.str.2 = [39 x i8] c"Couldn't open the w1 devices directory\00"
@.str.3 = [15 x i8] c"%s/%s/w1_slave\00"
@O_RDONLY = global i32 0
@.str.4 = [29 x i8] c"Couldn't open the w1 device.\00"
@.str.5 = [3 x i8] c"t=\00"
define float @w1_get_temperature() {
%1 = alloca [16 x i8]
%2 = alloca [128 x i8]
%3 = alloca [256 x i8]
%4 = alloca [6 x i8]
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%11 = call i32* @opendir(i8* %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %39, label %13
13: ; preds = %0
%14 = call %struct.dirent* @readdir(i32* %11)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %33, label %16
16: ; preds = %13, %30
%17 = phi %struct.dirent* [ %31, %30 ], [ %14, %13 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @DT_LNK
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = call i32* @strstr(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = call i32 @strcpy(i8* %6, i8* %24)
%29 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %30
30: ; preds = %27, %22, %16
%31 = call %struct.dirent* @readdir(i32* %11)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %33, label %16
33: ; preds = %30, %13
%34 = call i32 @closedir(i32* %11)
%35 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %6)
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %7, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %41, label %43
39: ; preds = %0
%40 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %56
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %56
43: ; preds = %33
%44 = call i64 @read(i32 %37, i8* %8, i32 256)
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = call i32* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%48 = getelementptr inbounds i32, i32* %47, i64 2
%49 = call i32 @strncpy(i8* %9, i32* %48, i32 5)
%50 = call i32 @strtof(i8* nocapture %9, i32* null)
%51 = sdiv i32 %50, 1000
%52 = sitofp i32 %51 to float
br label %53
53: ; preds = %46, %43
%54 = phi float [ %52, %46 ], [ 0.000000e+00, %43 ]
%55 = call i32 @close(i32 %37)
br label %56
56: ; preds = %41, %53, %39
%57 = phi float [ 0.000000e+00, %39 ], [ 0.000000e+00, %41 ], [ %54, %53 ]
ret float %57
}