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


x86 assembly (clang, -O0):
<maestroGetPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movb   $0x90,-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 <maestroGetPosition+0x4d>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <maestroGetPosition+0x8f>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
je     11af <maestroGetPosition+0x7f>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <maestroGetPosition+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):
<maestroGetPosition>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0x90,0xc(%rsp)
mov    %sil,0xd(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1176 <maestroGetPosition+0x46>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <read@plt>
cmp    $0x2,%eax
jne    117f <maestroGetPosition+0x4f>
movzwl 0xe(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe83(%rip),%rdi
jmp    1186 <maestroGetPosition+0x56>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1170 <maestroGetPosition+0x40>

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