#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 char uint8_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char const);
int read(int const, char *, int);
int /*<<< orphan*/ write(int const, char *, int);
int16_t get_measurement(const uint8_t cmd, const int file) {
  char config[1] = {cmd};
  write(file, config, 1);
  uint8_t data[2];
  if (read(file, data, 2) != 2) {
    printf("Failed to read the right amount of bytes for command %x", cmd);
    return -1;
  }
  return (data[0] << 8) + data[1];
}


x86 assembly (clang, -O0):
<get_measurement>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x5(%rbp),%al
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0xf(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     118e <get_measurement+0x5e>
lea    0xe87(%rip),%rdi
movsbl -0x5(%rbp),%esi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <get_measurement+0x6e>
movsbl -0xf(%rbp),%eax
shl    $0x8,%eax
movsbl -0xe(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_measurement>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %bl,0x5(%rsp)
lea    0x5(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
jne    1173 <get_measurement+0x43>
movsbl 0x6(%rsp),%ecx
shl    $0x8,%ecx
movsbl 0x7(%rsp),%eax
add    %ecx,%eax
jmp    1187 <get_measurement+0x57>
movsbl %bl,%esi
lea    0xe83(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"Failed to read the right amount of bytes for command %x\00"
define i32 @get_measurement(i8 signext %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca [1 x i8]
%7 = alloca [2 x i8]
store i8 %0, i8* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%9 = load i8, i8* %4
store i8 %9, i8* %8
%10 = load i32, i32* %5
%11 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%12 = call i32 @write(i32 %10, i8* %11, i32 1)
%13 = load i32, i32* %5
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 2)
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8, i8* %4
%19 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8 signext %18)
store i32 -1, i32* %3
br label %29
20: ; preds = %2
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = shl i32 %23, 8
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = add nsw i32 %24, %27
store i32 %28, i32* %3
br label %29
29: ; preds = %20, %17
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [56 x i8] c"Failed to read the right amount of bytes for command %x\00"
define i32 @get_measurement(i8 signext %0, i32 %1) {
%3 = alloca [1 x i8]
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 %0, i8* %5
%6 = call i32 @write(i32 %1, i8* %5, i32 1)
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %1, i8* %7, i32 2)
%9 = icmp eq i32 %8, 2
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8 signext %0)
br label %20
12: ; preds = %2
%13 = load i8, i8* %7
%14 = sext i8 %13 to i32
%15 = shl nsw i32 %14, 8
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = add nsw i32 %15, %18
br label %20
20: ; preds = %12, %10
%21 = phi i32 [ -1, %10 ], [ %19, %12 ]
ret i32 %21
}