#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 read(int, unsigned char *, int);
int write(int, unsigned char **, int);
int smcGetVariable(int fd, unsigned char variableId) {
  unsigned char command[] = {0xA1, variableId};
  if (write(fd, &command, sizeof(command)) == -1) {
    perror("error writing");
    return -9999;
  }
  unsigned char response[2];
  if (read(fd, response, 2) != 2) {
    perror("error reading");
    return -9999;
  }
  return response[0] + 256 * response[1];
}


x86 assembly (clang, -O0):
<smcGetVariable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movb   $0xa1,-0xb(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0xa(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xb(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    117d <smcGetVariable+0x4d>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffd8f1,-0x4(%rbp)
jmp    11bf <smcGetVariable+0x8f>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
je     11af <smcGetVariable+0x7f>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffd8f1,-0x4(%rbp)
jmp    11bf <smcGetVariable+0x8f>
movzbl -0xd(%rbp),%eax
movzbl -0xc(%rbp),%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smcGetVariable>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0xa1,0xc(%rsp)
mov    %sil,0xd(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1176 <smcGetVariable+0x46>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jne    117f <smcGetVariable+0x4f>
movzwl 0xe(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe83(%rip),%rdi
jmp    1186 <smcGetVariable+0x56>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffd8f1,%eax
jmp    1170 <smcGetVariable+0x40>

LLVM IR (-O0):
@.str = [14 x i8] c"error writing\00"
@.str.1 = [14 x i8] c"error reading\00"
define i32 @smcGetVariable(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca [2 x i8]
%7 = alloca [2 x i8]
store i32 %0, i32* %4
store i8 %1, i8* %5
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
store i8 -95, i8* %8
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = load i8, i8* %5
store i8 %10, i8* %9
%11 = load i32, i32* %4
%12 = bitcast [2 x i8]* %6 to i8**
%13 = call i32 @write(i32 %11, i8** %12, i32 2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -9999, i32* %3
br label %33
17: ; preds = %2
%18 = load i32, i32* %4
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 2)
%21 = icmp ne i32 %20, 2
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -9999, i32* %3
br label %33
24: ; preds = %17
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = mul nsw i32 256, %30
%32 = add nsw i32 %27, %31
store i32 %32, i32* %3
br label %33
33: ; preds = %24, %22, %15
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [14 x i8] c"error writing\00"
@.str.1 = [14 x i8] c"error reading\00"
define i32 @smcGetVariable(i32 %0, i8 zeroext %1) {
%3 = alloca [2 x i8]
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 -95, i8* %5
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 %1, i8* %6
%7 = bitcast [2 x i8]* %3 to i8**
%8 = call i32 @write(i32 %0, i8** %7, i32 2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %28
12: ; preds = %2
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%14 = call i32 @read(i32 %0, i8* %13, i32 2)
%15 = icmp eq i32 %14, 2
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %26
18: ; preds = %12
%19 = load i8, i8* %13
%20 = zext i8 %19 to i32
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = shl nuw nsw i32 %23, 8
%25 = or i32 %24, %20
br label %26
26: ; preds = %18, %16
%27 = phi i32 [ -9999, %16 ], [ %25, %18 ]
br label %28
28: ; preds = %26, %10
%29 = phi i32 [ -9999, %10 ], [ %27, %26 ]
ret i32 %29
}