#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int /*<<< orphan*/ perror(char *);
unsigned int getspeed(char *fan) {
  unsigned int fanspeed = 0;
  FILE *fd = fopen(fan, "r");
  if (fd == NULL) {
    perror("");
    return -1;
  }
  fscanf(fd, "%4d", &fanspeed);
  fclose(fd);
  return fanspeed;
}


x86 assembly (clang, -O0):
<getspeed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118a <getspeed+0x4a>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <getspeed+0x6d>
mov    -0x20(%rbp),%rdi
lea    0xe6d(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getspeed>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1187 <getspeed+0x47>
mov    %rax,%rbx
lea    0xe9a(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1181 <getspeed+0x41>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [4 x i8] c"%4d\00"
define i32 @getspeed(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %4
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %18
12: ; preds = %1
%13 = load i32*, i32** %5
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%15 = load i32*, i32** %5
%16 = call i32 @fclose(i32* %15)
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %18
18: ; preds = %12, %10
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [4 x i8] c"%4d\00"
define i32 @getspeed(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %12
8: ; preds = %1
%9 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%10 = call i32 @fclose(i32* %4)
%11 = load i32, i32* %2
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ -1, %6 ], [ %11, %8 ]
ret i32 %13
}