#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 uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ resp;
typedef int int32_t;
typedef int /*<<< orphan*/ UP3D_CMD_70_X;

/* Variables and functions */
int UP3DCOMM_Read(int *, int);
int UP3DCOMM_Write(int *, int);
int le32toh(int /*<<< orphan*/);
int32_t UP3D_GetAxisPosition(int axis) {
  if ((axis < 1) || (axis > 4))
    return -1;
  uint8_t UP3D_CMD_70_X[] = {0x70, '0' + axis - 1};
  uint8_t resp[2048];
  if ((UP3DCOMM_Write(UP3D_CMD_70_X, sizeof(UP3D_CMD_70_X)) !=
       sizeof(UP3D_CMD_70_X)) ||
      (5 != UP3DCOMM_Read(resp, sizeof(resp))) || (6 != resp[4]))
    return 0;
  return le32toh(*((uint32_t *)resp));
}


x86 assembly (clang, -O0):
<UP3D_GetAxisPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2010,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1152 <UP3D_GetAxisPosition+0x22>
cmpl   $0x4,-0x8(%rbp)
jle    115e <UP3D_GetAxisPosition+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <UP3D_GetAxisPosition+0xa6>
movl   $0x70,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x30,%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1030 <UP3DCOMM_Write@plt>
cltq
cmp    $0x8,%rax
jne    11bc <UP3D_GetAxisPosition+0x8c>
lea    -0x2010(%rbp),%rdi
mov    $0x2000,%esi
call   1050 <UP3DCOMM_Read@plt>
mov    %eax,%ecx
mov    $0x5,%eax
cmp    %ecx,%eax
jne    11bc <UP3D_GetAxisPosition+0x8c>
mov    $0x6,%eax
cmp    -0x2000(%rbp),%eax
je     11c8 <UP3D_GetAxisPosition+0x98>
movl   $0x0,-0x4(%rbp)
jmp    11d6 <UP3D_GetAxisPosition+0xa6>
mov    -0x2010(%rbp),%edi
call   1040 <le32toh@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UP3D_GetAxisPosition>:
push   %rbx
sub    $0x2010,%rsp
lea    -0x5(%rdi),%eax
mov    $0xffffffff,%ebx
cmp    $0xfffffffc,%eax
jb     1190 <UP3D_GetAxisPosition+0x60>
movl   $0x70,0x8(%rsp)
add    $0x2f,%edi
mov    %edi,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
call   1030 <UP3DCOMM_Write@plt>
xor    %ebx,%ebx
cmp    $0x8,%eax
jne    1190 <UP3D_GetAxisPosition+0x60>
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
call   1050 <UP3DCOMM_Read@plt>
cmp    $0x5,%eax
jne    1190 <UP3D_GetAxisPosition+0x60>
cmpl   $0x6,0x20(%rsp)
jne    1190 <UP3D_GetAxisPosition+0x60>
mov    0x10(%rsp),%edi
call   1040 <le32toh@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x2010,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @UP3D_GetAxisPosition(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca [2048 x i32]
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 1
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* %3
%10 = icmp sgt i32 %9, 4
br i1 %10, label %11, label %12
11: ; preds = %8, %1
store i32 -1, i32* %2
br label %35
12: ; preds = %8
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
store i32 112, i32* %13
%14 = getelementptr inbounds i32, i32* %13, i64 1
%15 = load i32, i32* %3
%16 = add nsw i32 48, %15
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%19 = call i32 @UP3DCOMM_Write(i32* %18, i32 8)
%20 = sext i32 %19 to i64
%21 = icmp ne i64 %20, 8
br i1 %21, label %30, label %22
22: ; preds = %12
%23 = getelementptr inbounds [2048 x i32], [2048 x i32]* %5, i64 0, i64 0
%24 = call i32 @UP3DCOMM_Read(i32* %23, i32 8192)
%25 = icmp ne i32 5, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = getelementptr inbounds [2048 x i32], [2048 x i32]* %5, i64 0, i64 4
%28 = load i32, i32* %27
%29 = icmp ne i32 6, %28
br i1 %29, label %30, label %31
30: ; preds = %26, %22, %12
store i32 0, i32* %2
br label %35
31: ; preds = %26
%32 = getelementptr inbounds [2048 x i32], [2048 x i32]* %5, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @le32toh(i32 %33)
store i32 %34, i32* %2
br label %35
35: ; preds = %31, %30, %11
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
define i32 @UP3D_GetAxisPosition(i32 %0) {
%2 = alloca [2 x i32]
%3 = alloca [2048 x i32]
%4 = add i32 %0, -5
%5 = icmp ult i32 %4, -4
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = bitcast [2 x i32]* %2 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
store i32 112, i32* %8
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%10 = add nuw nsw i32 %0, 47
store i32 %10, i32* %9
%11 = bitcast [2048 x i32]* %3 to i8*
%12 = call i32 @UP3DCOMM_Write(i32* %8, i32 8)
%13 = icmp eq i32 %12, 8
br i1 %13, label %14, label %25
14: ; preds = %6
%15 = getelementptr inbounds [2048 x i32], [2048 x i32]* %3, i64 0, i64 0
%16 = call i32 @UP3DCOMM_Read(i32* %15, i32 8192)
%17 = icmp ne i32 %16, 5
%18 = getelementptr inbounds [2048 x i32], [2048 x i32]* %3, i64 0, i64 4
%19 = load i32, i32* %18
%20 = icmp ne i32 %19, 6
%21 = select i1 %17, i1 true, i1 %20
br i1 %21, label %25, label %22
22: ; preds = %14
%23 = load i32, i32* %15
%24 = call i32 @le32toh(i32 %23)
br label %25
25: ; preds = %6, %14, %22
%26 = phi i32 [ %24, %22 ], [ 0, %14 ], [ 0, %6 ]
br label %27
27: ; preds = %1, %25
%28 = phi i32 [ %26, %25 ], [ -1, %1 ]
ret i32 %28
}