#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 */
int EXIT_FAILURE;
int atoi(char *);
int /*<<< orphan*/ change_speed(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 4) {
    fprintf(stderr, "Too few arguments.\n");
    return EXIT_FAILURE;
  } else if (argc > 4) {
    fprintf(stderr, "Too many arguments.\n");
    return EXIT_FAILURE;
  }
  int sleep_no_fan = atoi(argv[1]);
  int sleep_auto_fan = atoi(argv[2]);
  int safe_temp = atoi(argv[3]);
  for (;;) {
    FILE *temp_input = fopen("/proc/acpi/ibm/thermal", "r");
    if (temp_input == NULL) {
      fprintf(stderr, "You are not running kernel with Thinkpad patch!\n");
      return EXIT_FAILURE;
    }
    int temp = safe_temp + 1;
    fscanf(temp_input, "temperatures: %d", &temp);
    fclose(temp_input);
    bool is_auto = true;
    if (temp > safe_temp) {
      change_speed(10);
      is_auto = true;
    } else if (temp <= safe_temp) {
      change_speed(0);
      is_auto = false;
    }
    if (is_auto) {
      sleep(sleep_auto_fan);
    } else {
      sleep(sleep_no_fan);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11b6 <main+0x46>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <main+0x185>
cmpl   $0x4,-0x8(%rbp)
jle    11e6 <main+0x76>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <main+0x185>
jmp    11eb <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe07(%rip),%rdi
lea    0xe17(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1263 <main+0xf3>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f5 <main+0x185>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdfc(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    12b2 <main+0x142>
mov    $0xa,%edi
call   1040 <change_speed@plt>
movl   $0x1,-0x30(%rbp)
jmp    12d1 <main+0x161>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     12cc <main+0x15c>
xor    %edi,%edi
call   1040 <change_speed@plt>
movl   $0x0,-0x30(%rbp)
jmp    12d1 <main+0x161>
cmpl   $0x0,-0x30(%rbp)
je     12e8 <main+0x178>
mov    -0x18(%rbp),%edi
call   1090 <sleep@plt>
jmp    12f0 <main+0x180>
mov    -0x14(%rbp),%edi
call   1090 <sleep@plt>
jmp    121b <main+0xab>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jg     1195 <main+0x25>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
jmp    1272 <main+0x102>
cmp    $0x4,%edi
jne    1262 <main+0xf2>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r13d
mov    0x18(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
lea    0xe5e(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1250 <main+0xe0>
mov    %rax,%r12
lea    0x1(%rbx),%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,0x4(%rsp)
mov    %r12,%rdi
lea    0xe74(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1050 <fscanf@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
cmp    %ebx,0x4(%rsp)
mov    $0x0,%edi
mov    $0xa,%eax
cmovg  %eax,%edi
mov    %r14d,%ebp
cmovg  %r13d,%ebp
call   1040 <change_speed@plt>
mov    %ebp,%edi
call   1090 <sleep@plt>
lea    0xded(%rip),%rdi
lea    0xdfd(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f0 <main+0x80>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
jmp    1272 <main+0x102>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Too few arguments.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Too many arguments.\0A\00"
@.str.2 = [23 x i8] c"/proc/acpi/ibm/thermal\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [49 x i8] c"You are not running kernel with Thinkpad patch!\0A\00"
@.str.5 = [17 x i8] c"temperatures: %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %76
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 4
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %3
br label %76
25: ; preds = %18
br label %26
26: ; preds = %25
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %6
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %7
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %8
br label %39
39: ; preds = %75, %26
%40 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
store i32 %46, i32* %3
br label %76
47: ; preds = %39
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
%50 = load i32*, i32** %9
%51 = call i32 @fscanf(i32* %50, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32* %10)
%52 = load i32*, i32** %9
%53 = call i32 @fclose(i32* %52)
store i32 1, i32* %11
%54 = load i32, i32* %10
%55 = load i32, i32* %8
%56 = icmp sgt i32 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 @change_speed(i32 10)
store i32 1, i32* %11
br label %66
59: ; preds = %47
%60 = load i32, i32* %10
%61 = load i32, i32* %8
%62 = icmp sle i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @change_speed(i32 0)
store i32 0, i32* %11
br label %65
65: ; preds = %63, %59
br label %66
66: ; preds = %65, %57
%67 = load i32, i32* %11
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32, i32* %7
%71 = call i32 @sleep(i32 %70)
br label %75
72: ; preds = %66
%73 = load i32, i32* %6
%74 = call i32 @sleep(i32 %73)
br label %75
75: ; preds = %72, %69
br label %39
76: ; preds = %43, %21, %14
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Too few arguments.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Too many arguments.\0A\00"
@.str.2 = [23 x i8] c"/proc/acpi/ibm/thermal\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [49 x i8] c"You are not running kernel with Thinkpad patch!\0A\00"
@.str.5 = [17 x i8] c"temperatures: %d\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %43
8: ; preds = %2
%9 = icmp eq i32 %0, 4
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %43
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %13
%26 = bitcast i32* %3 to i8*
%27 = add nsw i32 %22, 1
br label %31
28: ; preds = %31, %13
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
br label %43
31: ; preds = %25, %31
%32 = phi i32* [ %23, %25 ], [ %41, %31 ]
store i32 %27, i32* %3
%33 = call i32 @fscanf(i32* %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%34 = call i32 @fclose(i32* %32)
%35 = load i32, i32* %3
%36 = icmp sgt i32 %35, %22
%37 = select i1 %36, i32 10, i32 0
%38 = select i1 %36, i32 %19, i32 %16
%39 = call i32 @change_speed(i32 %37)
%40 = call i32 @sleep(i32 %38)
%41 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %28, label %31
43: ; preds = %28, %10, %5
%44 = load i32, i32* @EXIT_FAILURE
ret i32 %44
}