#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, int *, int);
int write(int, int *, int);
int main(void) {
  int fd = open("/dev/i2c-1", O_RDWR);
  if (fd == -1) {
    perror("open()");
    return -1;
  }
  if (ioctl(fd, I2C_SLAVE, 0x48) < 0) {
    perror("ioctl()");
    return -1;
  }
  uint8_t buf[12] = {};
  buf[0] = 0x48;
  if (write(fd, buf, 1) != 1) {
    perror("write()");
    return -1;
  }
  if (read(fd, buf, 12) != 12) {
    perror("read()");
    return -1;
  }
  close(fd);
  int temp = (buf[0] << 8 | buf[1]) >> 3;
  if (4096 <= temp) {
    temp -= 8192;
  }
  printf("%f℃\n", temp / 16.0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c9 <main+0x49>
lea    0xe5b(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <main+0x154>
mov    -0x8(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    $0x48,%edx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1201 <main+0x81>
lea    0xe2a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <main+0x154>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
movl   $0x48,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     124a <main+0xca>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <main+0x154>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1080 <read@plt>
cmp    $0xc,%eax
je     127c <main+0xfc>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d4 <main+0x154>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x40(%rbp),%eax
shl    $0x8,%eax
or     -0x3c(%rbp),%eax
sar    $0x3,%eax
mov    %eax,-0x44(%rbp)
mov    $0x1000,%eax
cmp    -0x44(%rbp),%eax
jg     12ac <main+0x12c>
mov    -0x44(%rbp),%eax
sub    $0x2000,%eax
mov    %eax,-0x44(%rbp)
cvtsi2sdl -0x44(%rbp),%xmm0
movsd  0xd47(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
lea    0xd69(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     123e <main+0xce>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x48,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1247 <main+0xd7>
xorpd  %xmm0,%xmm0
movapd %xmm0,(%rsp)
movapd %xmm0,0x20(%rsp)
movapd %xmm0,0x10(%rsp)
movl   $0x48,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1250 <main+0xe0>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <read@plt>
cmp    $0xc,%eax
jne    1259 <main+0xe9>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%rsp),%eax
shl    $0x8,%eax
or     0x4(%rsp),%eax
mov    %eax,%ecx
sar    $0x3,%ecx
lea    -0x2000(%rcx),%edx
cmp    $0x8000,%eax
cmovl  %ecx,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
mulsd  0xdd6(%rip),%xmm0
cvttsd2si %xmm0,%esi
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    126a <main+0xfa>
lea    0xdce(%rip),%rdi
jmp    1260 <main+0xf0>
lea    0xdcc(%rip),%rdi
jmp    1260 <main+0xf0>
lea    0xdcb(%rip),%rdi
jmp    1260 <main+0xf0>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@I2C_SLAVE = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [8 x i8] c"write()\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [7 x i8] c"%f\E2\84\83\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [12 x i32]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
11: ; preds = %0
%12 = load i32, i32* %2
%13 = load i32, i32* @I2C_SLAVE
%14 = call i64 @ioctl(i32 %12, i32 %13, i32 72)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
18: ; preds = %11
%19 = bitcast [12 x i32]* %3 to i8*
%20 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 0
store i32 72, i32* %20
%21 = load i32, i32* %2
%22 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 0
%23 = call i32 @write(i32 %21, i32* %22, i32 1)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
27: ; preds = %18
%28 = load i32, i32* %2
%29 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %28, i32* %29, i32 12)
%31 = icmp ne i32 %30, 12
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
34: ; preds = %27
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 0
%38 = load i32, i32* %37
%39 = shl i32 %38, 8
%40 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 1
%41 = load i32, i32* %40
%42 = or i32 %39, %41
%43 = ashr i32 %42, 3
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = icmp sle i32 4096, %44
br i1 %45, label %46, label %49
46: ; preds = %34
%47 = load i32, i32* %4
%48 = sub nsw i32 %47, 8192
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %34
%50 = load i32, i32* %4
%51 = sitofp i32 %50 to double
%52 = fdiv double %51, 1.600000e+01
%53 = fptosi double %52 to i32
%54 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %53)
store i32 0, i32* %1
br label %55
55: ; preds = %49, %32, %25, %16, %9
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@I2C_SLAVE = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [8 x i8] c"write()\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [7 x i8] c"%f\E2\84\83\0A\00"
define i32 @main() {
%1 = alloca [12 x i32]
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %42
7: ; preds = %0
%8 = load i32, i32* @I2C_SLAVE
%9 = tail call i64 @ioctl(i32 %3, i32 %8, i32 72)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %42
13: ; preds = %7
%14 = bitcast [12 x i32]* %1 to i8*
%15 = getelementptr inbounds [12 x i32], [12 x i32]* %1, i64 0, i64 0
store i32 72, i32* %15
%16 = call i32 @write(i32 %3, i32* %15, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %40
20: ; preds = %13
%21 = call i32 @read(i32 %3, i32* %15, i32 12)
%22 = icmp eq i32 %21, 12
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %40
25: ; preds = %20
%26 = tail call i32 @close(i32 %3)
%27 = load i32, i32* %15
%28 = shl i32 %27, 8
%29 = getelementptr inbounds [12 x i32], [12 x i32]* %1, i64 0, i64 1
%30 = load i32, i32* %29
%31 = or i32 %28, %30
%32 = ashr i32 %31, 3
%33 = icmp sgt i32 %31, 32767
%34 = add nsw i32 %32, -8192
%35 = select i1 %33, i32 %34, i32 %32
%36 = sitofp i32 %35 to double
%37 = fmul double %36, 6.250000e-02
%38 = fptosi double %37 to i32
%39 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %25, %23, %18
%41 = phi i32 [ -1, %18 ], [ -1, %23 ], [ 0, %25 ]
br label %42
42: ; preds = %40, %11, %5
%43 = phi i32 [ -1, %5 ], [ -1, %11 ], [ %41, %40 ]
ret i32 %43
}