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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getspeed(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setspeed(int);
int /*<<< orphan*/ usleep(int);
int main() {
  int temp, speed;
  unsigned char counter = 0;
  FILE *tempfile = NULL;
  while (1) {
    tempfile = fopen("/sys/bus/platform/devices/applesmc.768/temp8_input", "r");
    if (errno)
      perror("");
    else {
      fscanf(tempfile, "%5d", &temp);
      fclose(tempfile);
      speed = getspeed("/sys/bus/platform/devices/applesmc.768/fan1_min");
      if (temp >= 48000 && speed < 6000 && counter % 4 == 0) {
        speed += 1000;
        setspeed(speed);
        setspeed(speed);
      } else if (temp <= 42000 && speed > 2000) {
        speed -= 1000;
        setspeed(speed);
        setspeed(speed);
      }
    }
    counter++;
    usleep(5 * 1000000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0xd(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe6e(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e37(%rip),%rax
cmpq   $0x0,(%rax)
je     11c4 <main+0x54>
lea    0xe7e(%rip),%rdi
call   1080 <perror@plt>
jmp    127e <main+0x10e>
mov    -0x18(%rbp),%rdi
lea    0xe66(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe51(%rip),%rdi
call   1060 <getspeed@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xbb80,-0x8(%rbp)
jl     123f <main+0xcf>
cmpl   $0x1770,-0xc(%rbp)
jge    123f <main+0xcf>
movzbl -0xd(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    123f <main+0xcf>
mov    -0xc(%rbp),%eax
add    $0x3e8,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <setspeed@plt>
mov    -0xc(%rbp),%edi
call   1030 <setspeed@plt>
jmp    1279 <main+0x109>
cmpl   $0xa410,-0x8(%rbp)
jg     1274 <main+0x104>
cmpl   $0x7d0,-0xc(%rbp)
jle    1274 <main+0x104>
mov    -0xc(%rbp),%eax
sub    $0x3e8,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <setspeed@plt>
mov    -0xc(%rbp),%edi
call   1030 <setspeed@plt>
jmp    1279 <main+0x109>
jmp    127e <main+0x10e>
mov    -0xd(%rbp),%al
add    $0x1,%al
mov    %al,-0xd(%rbp)
mov    $0x4c4b40,%edi
call   1090 <usleep@plt>
jmp    118b <main+0x1b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r13d,%r13d
lea    0xe7b(%rip),%r14
lea    0xea7(%rip),%rbx
mov    0x2e4d(%rip),%r12
jmp    11ba <main+0x4a>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xe91(%rip),%rdi
call   1080 <perror@plt>
add    $0x1,%r13b
mov    $0x4c4b40,%edi
call   1090 <usleep@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <fopen@plt>
cmpq   $0x0,(%r12)
jne    11a0 <main+0x30>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0xe5c(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1050 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xe47(%rip),%rdi
call   1060 <getspeed@plt>
mov    %eax,%r15d
mov    0x4(%rsp),%eax
cmp    $0xbb80,%eax
jl     1220 <main+0xb0>
cmp    $0x176f,%r15d
jg     1220 <main+0xb0>
mov    %r13d,%ecx
and    $0x3,%cl
jne    1220 <main+0xb0>
add    $0x3e8,%r15d
jmp    123b <main+0xcb>
nop
cmp    $0xa410,%eax
jg     11ac <main+0x3c>
cmp    $0x7d1,%r15d
jl     11ac <main+0x3c>
add    $0xfffffc18,%r15d
mov    %r15d,%edi
call   1030 <setspeed@plt>
mov    %r15d,%edi
call   1030 <setspeed@plt>
jmp    11ac <main+0x3c>

LLVM IR (-O0):
@.str = [51 x i8] c"/sys/bus/platform/devices/applesmc.768/temp8_input\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c"%5d\00"
@.str.4 = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan1_min\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32*
store i32 0, i32* %1
store i8 0, i8* %4
store i32* null, i32** %5
br label %6
6: ; preds = %0, %50
%7 = call i32* @fopen(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i64, i64* @errno
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %50
12: ; preds = %6
%13 = load i32*, i32** %5
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%15 = load i32*, i32** %5
%16 = call i32 @fclose(i32* %15)
%17 = call i32 @getspeed(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
store i32 %17, i32* %3
%18 = load i32, i32* %2
%19 = icmp sge i32 %18, 48000
br i1 %19, label %20, label %35
20: ; preds = %12
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 6000
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = load i8, i8* %4
%25 = zext i8 %24 to i32
%26 = srem i32 %25, 4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1000
store i32 %30, i32* %3
%31 = load i32, i32* %3
%32 = call i32 @setspeed(i32 %31)
%33 = load i32, i32* %3
%34 = call i32 @setspeed(i32 %33)
br label %49
35: ; preds = %23, %20, %12
%36 = load i32, i32* %2
%37 = icmp sle i32 %36, 42000
br i1 %37, label %38, label %48
38: ; preds = %35
%39 = load i32, i32* %3
%40 = icmp sgt i32 %39, 2000
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* %3
%43 = sub nsw i32 %42, 1000
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = call i32 @setspeed(i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @setspeed(i32 %46)
br label %48
48: ; preds = %41, %38, %35
br label %49
49: ; preds = %48, %28
br label %50
50: ; preds = %49, %10
%51 = load i8, i8* %4
%52 = add i8 %51, 1
store i8 %52, i8* %4
%53 = call i32 @usleep(i32 5000000)
br label %6
}

LLVM IR (-O2):
@.str = [51 x i8] c"/sys/bus/platform/devices/applesmc.768/temp8_input\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c"%5d\00"
@.str.4 = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan1_min\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
br label %3
3: ; preds = %33, %0
%4 = phi i8 [ 0, %0 ], [ %34, %33 ]
%5 = call i32* @fopen(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = load i64, i64* @errno
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %33
10: ; preds = %3
%11 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%12 = call i32 @fclose(i32* %5)
%13 = call i32 @getspeed(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 47999
%16 = icmp slt i32 %13, 6000
%17 = select i1 %15, i1 %16, i1 false
%18 = and i8 %4, 3
%19 = icmp eq i8 %18, 0
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %21, label %25
21: ; preds = %10
%22 = add nsw i32 %13, 1000
%23 = call i32 @setspeed(i32 %22)
%24 = call i32 @setspeed(i32 %22)
br label %33
25: ; preds = %10
%26 = icmp slt i32 %14, 42001
%27 = icmp sgt i32 %13, 2000
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = add nsw i32 %13, -1000
%31 = call i32 @setspeed(i32 %30)
%32 = call i32 @setspeed(i32 %30)
br label %33
33: ; preds = %21, %29, %25, %8
%34 = add i8 %4, 1
%35 = call i32 @usleep(i32 5000000)
br label %3
}