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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int write(int, unsigned char *, int);
int smcSetTargetSpeed(int fd, int speed) {
  unsigned char command[3];
  if (speed < 0) {
    command[0] = 0x86;
    speed = -speed;
  } else {
    command[0] = 0x85;
  }
  command[1] = speed & 0x1F;
  command[2] = speed >> 5 & 0x7F;
  if (write(fd, command, sizeof(command)) == -1) {
    perror("error writing");
    return -9999;
  }
  return 0;
}


x86 assembly (clang, -O0):
<smcSetTargetSpeed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1149 <smcSetTargetSpeed+0x29>
movb   $0x86,-0xf(%rbp)
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    114d <smcSetTargetSpeed+0x2d>
movb   $0x85,-0xf(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1f,%eax
mov    %al,-0xe(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x5,%eax
and    $0x7f,%eax
mov    %al,-0xd(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1194 <smcSetTargetSpeed+0x74>
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffd8f1,-0x4(%rbp)
jmp    119b <smcSetTargetSpeed+0x7b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smcSetTargetSpeed>:
push   %rax
test   %esi,%esi
js     112c <smcSetTargetSpeed+0xc>
movb   $0x85,0x5(%rsp)
jmp    1133 <smcSetTargetSpeed+0x13>
movb   $0x86,0x5(%rsp)
neg    %esi
mov    %esi,%eax
and    $0x1f,%al
mov    %al,0x6(%rsp)
shr    $0x5,%esi
and    $0x7f,%sil
mov    %sil,0x7(%rsp)
lea    0x5(%rsp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1161 <smcSetTargetSpeed+0x41>
pop    %rcx
ret
lea    0xe98(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffd8f1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"error writing\00"
define i32 @smcSetTargetSpeed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
store i8 -122, i8* %10
%11 = load i32, i32* %5
%12 = sub nsw i32 0, %11
store i32 %12, i32* %5
br label %15
13: ; preds = %2
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
store i8 -123, i8* %14
br label %15
15: ; preds = %13, %9
%16 = load i32, i32* %5
%17 = and i32 %16, 31
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
store i8 %18, i8* %19
%20 = load i32, i32* %5
%21 = ashr i32 %20, 5
%22 = and i32 %21, 127
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
store i8 %23, i8* %24
%25 = load i32, i32* %4
%26 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%27 = call i32 @write(i32 %25, i8* %26, i32 3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -9999, i32* %3
br label %32
31: ; preds = %15
store i32 0, i32* %3
br label %32
32: ; preds = %31, %29
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"error writing\00"
define i32 @smcSetTargetSpeed(i32 %0, i32 %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
store i8 -122, i8* %4
%7 = sub nsw i32 0, %1
br label %9
8: ; preds = %2
store i8 -123, i8* %4
br label %9
9: ; preds = %8, %6
%10 = phi i32 [ %7, %6 ], [ %1, %8 ]
%11 = trunc i32 %10 to i8
%12 = and i8 %11, 31
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
store i8 %12, i8* %13
%14 = lshr i32 %10, 5
%15 = trunc i32 %14 to i8
%16 = and i8 %15, 127
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 %16, i8* %17
%18 = call i32 @write(i32 %0, i8* %4, i32 3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %9
%21 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %9, %20
%23 = phi i32 [ -9999, %20 ], [ 0, %9 ]
ret i32 %23
}