#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, int *);
int getFanRPM() {
  FILE *fp;
  char line[128];
  int rpm = 0;
  if (!(fp = fopen("/proc/acpi/ibm/fan", "r"))) {
    perror("fopen");
    return 0;
  }
  while (!feof(fp)) {
    fgets(line, sizeof(line), fp);
    if (sscanf(line, "speed: %d\n", &rpm) == 1) {
      fclose(fp);
      return rpm;
    }
  }
  fclose(fp);
  return rpm;
}


x86 assembly (clang, -O0):
<getFanRPM>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x94(%rbp)
lea    0xe84(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ae <getFanRPM+0x4e>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1237 <getFanRPM+0xd7>
jmp    11b3 <getFanRPM+0x53>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d1 <getFanRPM+0x71>
jmp    1225 <getFanRPM+0xc5>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
lea    -0x90(%rbp),%rdi
lea    0xe27(%rip),%rsi
lea    -0x94(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1220 <getFanRPM+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <getFanRPM+0xd7>
jmp    11b3 <getFanRPM+0x53>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFanRPM>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x98,%rsp
movl   $0x0,0xc(%rsp)
lea    0xe83(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fa <getFanRPM+0x9a>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe7e(%rip),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    11df <getFanRPM+0x7f>
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11b0 <getFanRPM+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
jmp    11eb <getFanRPM+0x8b>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/acpi/ibm/fan\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [11 x i8] c"speed: %d\0A\00"
define i32 @getFanRPM() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %31
9: ; preds = %0
br label %10
10: ; preds = %26, %9
%11 = load i32*, i32** %2
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %27
15: ; preds = %10
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %2
%18 = call i32 @fgets(i8* %16, i32 128, i32* %17)
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %4
store i32 %25, i32* %1
br label %31
26: ; preds = %15
br label %10
27: ; preds = %10
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %4
store i32 %30, i32* %1
br label %31
31: ; preds = %27, %22, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/acpi/ibm/fan\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [11 x i8] c"speed: %d\0A\00"
define i32 @getFanRPM() {
%1 = alloca [128 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %22
9: ; preds = %0, %12
%10 = call i32 @feof(i32* %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = call i32 @fgets(i8* %3, i32 128, i32* %5)
%14 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %9
16: ; preds = %12
%17 = call i32 @fclose(i32* %5)
%18 = load i32, i32* %2
br label %22
19: ; preds = %9
%20 = call i32 @fclose(i32* %5)
%21 = load i32, i32* %2
br label %22
22: ; preds = %19, %16, %7
%23 = phi i32 [ %18, %16 ], [ %21, %19 ], [ 0, %7 ]
ret i32 %23
}