#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*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int);
int /*<<< orphan*/ perror(char *);
void setspeed(unsigned int fanspeed) {
  FILE *left = fopen("/sys/bus/platform/devices/applesmc.768/fan1_min", "w");
  FILE *right = fopen("/sys/bus/platform/devices/applesmc.768/fan2_min", "w");
  if (left == NULL || right == NULL) {
    perror("");
    return;
  }
  if (fanspeed > 6000)
    fanspeed = 6000;
  else if (fanspeed < 2000)
    fanspeed = 2000;
  fprintf(left, "%d", fanspeed);
  fprintf(right, "%d", fanspeed);
  fclose(left);
  fclose(right);
}


x86 assembly (clang, -O0):
<setspeed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeae(%rip),%rdi
lea    0xed7(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xec9(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118f <setspeed+0x4f>
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <setspeed+0x60>
lea    0xece(%rip),%rdi
call   1060 <perror@plt>
jmp    120a <setspeed+0xca>
cmpl   $0x1770,-0x4(%rbp)
jbe    11b9 <setspeed+0x79>
movl   $0x1770,-0x4(%rbp)
jmp    11d2 <setspeed+0x92>
cmpl   $0x7d0,-0x4(%rbp)
jae    11cd <setspeed+0x8d>
movl   $0x7d0,-0x4(%rbp)
jmp    11d2 <setspeed+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe82(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setspeed>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xeb0(%rip),%rdi
lea    0xed9(%rip),%rbx
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
lea    0xec9(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fopen@plt>
test   %r14,%r14
je     11d4 <setspeed+0x94>
mov    %rax,%rbx
test   %rax,%rax
je     11d4 <setspeed+0x94>
cmp    $0x7d1,%ebp
mov    $0x7d0,%eax
cmovae %ebp,%eax
cmp    $0x1770,%eax
mov    $0x1770,%ebp
cmovb  %eax,%ebp
lea    0xec2(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe89(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <perror@plt>

LLVM IR (-O0):
@.str = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan1_min\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan2_min\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"%d\00"
define void @setspeed(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
store i32 %0, i32* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 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** %3
%6 = call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9, %1
%13 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %34
14: ; preds = %9
%15 = load i32, i32* %2
%16 = icmp ugt i32 %15, 6000
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 6000, i32* %2
br label %23
18: ; preds = %14
%19 = load i32, i32* %2
%20 = icmp ult i32 %19, 2000
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 2000, i32* %2
br label %22
22: ; preds = %21, %18
br label %23
23: ; preds = %22, %17
%24 = load i32*, i32** %3
%25 = load i32, i32* %2
%26 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %4
%28 = load i32, i32* %2
%29 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %23, %12
ret void
}

LLVM IR (-O2):
@.str = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan1_min\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [48 x i8] c"/sys/bus/platform/devices/applesmc.768/fan2_min\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [3 x i8] c"%d\00"
define void @setspeed(i32 %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %2, null
%5 = icmp eq i32* %3, null
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %18
9: ; preds = %1
%10 = icmp ugt i32 %0, 2000
%11 = select i1 %10, i32 %0, i32 2000
%12 = icmp ult i32 %11, 6000
%13 = select i1 %12, i32 %11, i32 6000
%14 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%16 = tail call i32 @fclose(i32* %2)
%17 = tail call i32 @fclose(i32* %3)
br label %18
18: ; preds = %9, %7
ret void
}