#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*/ result;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, float);
int /*<<< orphan*/ read(int, unsigned char **, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int fd;
  unsigned char result[2];
  unsigned char integer_value = 0;
  float decimal_value = 0;
  float temperature = 0;
  fd = open("/dev/ds18b20", 0);
  if (fd < 0) {
    perror("open device failed\n");
    exit(1);
  }
  while (1) {
    read(fd, &result, sizeof(result));
    integer_value = ((result[0] & 0xf0) >> 4) | ((result[1] & 0x07) << 4);
    decimal_value =
        0.5 * ((result[0] & 0x0f) >> 3) + 0.25 * ((result[0] & 0x07) >> 2);
    temperature = (float)integer_value + decimal_value;
    printf("%6.2f\n", temperature);
    sleep(4);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0xb(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
lea    0xe86(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b4 <main+0x54>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11b9 <main+0x59>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
movzbl -0xa(%rbp),%eax
shr    $0x4,%eax
mov    %eax,-0x34(%rbp)
movzbl -0x9(%rbp),%ecx
and    $0x7,%ecx
mov    %ecx,-0x30(%rbp)
shl    $0x4,%ecx
or     %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    %al,-0xb(%rbp)
movzbl -0xa(%rbp),%eax
mov    %eax,%ecx
and    $0x8,%ecx
mov    %ecx,-0x28(%rbp)
shr    $0x3,%ecx
cvtsi2sd %ecx,%xmm0
and    $0x4,%eax
mov    %eax,-0x24(%rbp)
shr    $0x2,%eax
cvtsi2sd %eax,%xmm1
movsd  0xdef(%rip),%xmm2
mulsd  %xmm2,%xmm1
movsd  0xdeb(%rip),%xmm2
mulsd  %xmm2,%xmm0
movsd  %xmm0,-0x20(%rbp)
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x10(%rbp)
movzbl -0xb(%rbp),%eax
cvtsi2ss %eax,%xmm0
addss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
lea    0xddb(%rip),%rdi
call   1030 <printf@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
jmp    11b9 <main+0x59>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
xor    %esi,%esi
call   1050 <open@plt>
test   %eax,%eax
js     120f <main+0xaf>
mov    %eax,%ebx
lea    0xe(%rsp),%r14
lea    0xea4(%rip),%r15
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1040 <read@plt>
movzbl 0xe(%rsp),%eax
movzbl 0xf(%rsp),%ecx
shl    $0x4,%cl
mov    %eax,%edx
shr    $0x4,%dl
or     %cl,%dl
and    $0x7f,%dl
mov    %eax,%ecx
shr    $0x3,%ecx
and    $0x1,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
shr    $0x2,%eax
and    $0x1,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mulsd  0xe26(%rip),%xmm1
mulsd  0xe26(%rip),%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsd2ss %xmm0,%xmm1
movzbl %dl,%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
addss  %xmm1,%xmm0
mov    %r15,%rdi
call   1030 <printf@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
jmp    1190 <main+0x30>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ds18b20\00"
@.str.1 = [20 x i8] c"open device failed\0A\00"
@.str.2 = [7 x i8] c"%6.2f\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i8
%5 = alloca float
%6 = alloca float
store i32 0, i32* %1
store i8 0, i8* %4
store float 0.000000e+00, float* %5
store float 0.000000e+00, float* %6
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
br label %14
14: ; preds = %13, %14
%15 = load i32, i32* %2
%16 = bitcast [2 x i8]* %3 to i8**
%17 = call i32 @read(i32 %15, i8** %16, i32 2)
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = and i32 %20, 240
%22 = ashr i32 %21, 4
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = and i32 %25, 7
%27 = shl i32 %26, 4
%28 = or i32 %22, %27
%29 = trunc i32 %28 to i8
store i8 %29, i8* %4
%30 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = and i32 %32, 15
%34 = ashr i32 %33, 3
%35 = sitofp i32 %34 to double
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = and i32 %38, 7
%40 = ashr i32 %39, 2
%41 = sitofp i32 %40 to double
%42 = fmul double 2.500000e-01, %41
%43 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %35, double %42)
%44 = fptrunc double %43 to float
store float %44, float* %5
%45 = load i8, i8* %4
%46 = uitofp i8 %45 to float
%47 = load float, float* %5
%48 = fadd float %46, %47
store float %48, float* %6
%49 = load float, float* %6
%50 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), float %49)
%51 = call i32 @sleep(i32 4)
br label %14
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ds18b20\00"
@.str.1 = [20 x i8] c"open device failed\0A\00"
@.str.2 = [7 x i8] c"%6.2f\0A\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = bitcast [2 x i8]* %1 to i8**
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
br label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %5, %11
%12 = call i32 @read(i32 %3, i8** %6, i32 2)
%13 = load i8, i8* %2
%14 = zext i8 %13 to i32
%15 = lshr i8 %13, 4
%16 = load i8, i8* %7
%17 = shl i8 %16, 4
%18 = and i8 %17, 112
%19 = or i8 %18, %15
%20 = lshr i32 %14, 3
%21 = and i32 %20, 1
%22 = sitofp i32 %21 to double
%23 = lshr i32 %14, 2
%24 = and i32 %23, 1
%25 = sitofp i32 %24 to double
%26 = fmul double %25, 2.500000e-01
%27 = tail call double @llvm.fmuladd.f64(double %22, double 5.000000e-01, double %26)
%28 = fptrunc double %27 to float
%29 = uitofp i8 %19 to float
%30 = fadd float %29, %28
%31 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), float %30)
%32 = tail call i32 @sleep(i32 4)
br label %11
}