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

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int readUI(int module, int input, int typeui, signed long *value) {
  char *fileName = "/dev/i2c-1";
  unsigned char buf[10];
  unsigned char address;
  int fd;
  if ((module < 1) || (module > 6))
    return -1;
  if ((input < 1) || (input > 4))
    return -1;
  if ((fd = open(fileName, O_RDWR)) < 0) {
    printf("Failed to open i2c-1 port.\n");
    return -1;
  }
  if (ioctl(fd, I2C_SLAVE, 0x1C) < 0) {
    printf("Unable to get bus access to talk to Pi-Cubes I/O module.\n");
    return -1;
  }
  address = (unsigned char)((module - 1) * 8 + (input - 1) * 2);
  buf[0] = address;
  buf[1] = (unsigned char)(typeui);
  if ((write(fd, buf, 2)) != 2) {
    printf("Error writing to Pi-Cubes I/O module.\n");
    return -1;
  }
  address = (unsigned char)(48 + (module - 1) * 16 + (input - 1) * 4);
  buf[0] = address;
  if ((write(fd, buf, 1)) != 1) {
    printf("Error writing to Pi-Cubes I/O module.\n");
    return -1;
  }
  if ((read(fd, buf, 4)) != 4) {
    printf("Error reading from Pi-Cubes I/O module.\n");
    return -1;
  }
  *value = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<readUI>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    0xe84(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1194 <readUI+0x34>
cmpl   $0x6,-0x8(%rbp)
jle    11a0 <readUI+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
cmpl   $0x1,-0xc(%rbp)
jl     11b4 <readUI+0x54>
cmpl   $0x4,-0xc(%rbp)
jle    11c0 <readUI+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
mov    -0x20(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    11f6 <readUI+0x96>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
mov    -0x30(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    $0x1c,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    122e <readUI+0xce>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
shl    $0x3,%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x1,%ecx
add    %ecx,%eax
mov    %al,-0x2b(%rbp)
mov    -0x2b(%rbp),%al
mov    %al,-0x2a(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x29(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     1283 <readUI+0x123>
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
shl    $0x4,%eax
add    $0x30,%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
shl    $0x2,%ecx
add    %ecx,%eax
mov    %al,-0x2b(%rbp)
mov    -0x2b(%rbp),%al
mov    %al,-0x2a(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     12d5 <readUI+0x175>
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
mov    -0x30(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x4,%eax
je     1307 <readUI+0x1a7>
lea    0xd92(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133f <readUI+0x1df>
movzbl -0x2a(%rbp),%eax
movzbl -0x29(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x28(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x27(%rbp),%ecx
shl    $0x18,%ecx
or     %ecx,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readUI>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    -0x7(%rbp),%eax
mov    $0xffffffff,%r15d
cmp    $0xfffffffa,%eax
jb     1276 <readUI+0x116>
mov    %esi,%ebx
lea    -0x5(%rbx),%eax
cmp    $0xfffffffc,%eax
jb     1276 <readUI+0x116>
mov    %rcx,%r14
mov    %edx,%r13d
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1258 <readUI+0xf8>
mov    %eax,%r12d
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    $0x1c,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     126a <readUI+0x10a>
lea    -0x8(,%rbp,8),%eax
lea    (%rax,%rbx,2),%eax
add    $0xfffffffe,%eax
mov    %al,0xe(%rsp)
mov    %r13b,0xf(%rsp)
lea    0xe(%rsp),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
jne    1261 <readUI+0x101>
add    $0xffffffff,%ebp
add    $0xffffffff,%ebx
shl    $0x4,%ebp
lea    0x0(%rbp,%rbx,4),%eax
add    $0x30,%al
mov    %al,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1261 <readUI+0x101>
lea    0xe(%rsp),%rsi
mov    %r12d,%edi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x4,%eax
jne    1288 <readUI+0x128>
movslq 0xe(%rsp),%rax
mov    %rax,(%r14)
mov    %r12d,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
jmp    1276 <readUI+0x116>
lea    0xe33(%rip),%rdi
jmp    1271 <readUI+0x111>
lea    0xdcb(%rip),%rdi
jmp    1271 <readUI+0x111>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7c(%rip),%rdi
jmp    1271 <readUI+0x111>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Failed to open i2c-1 port.\0A\00"
@I2C_SLAVE = global i32 0
@.str.2 = [58 x i8] c"Unable to get bus access to talk to Pi-Cubes I/O module.\0A\00"
@.str.3 = [39 x i8] c"Error writing to Pi-Cubes I/O module.\0A\00"
@.str.4 = [41 x i8] c"Error reading from Pi-Cubes I/O module.\0A\00"
define i32 @readUI(i32 %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca [10 x i8]
%12 = alloca i8
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i64* %3, i64** %9
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %10
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 6
br i1 %18, label %19, label %20
19: ; preds = %16, %4
store i32 -1, i32* %5
br label %109
20: ; preds = %16
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* %7
%25 = icmp sgt i32 %24, 4
br i1 %25, label %26, label %27
26: ; preds = %23, %20
store i32 -1, i32* %5
br label %109
27: ; preds = %23
%28 = load i8*, i8** %10
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %13
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
34: ; preds = %27
%35 = load i32, i32* %13
%36 = load i32, i32* @I2C_SLAVE
%37 = call i64 @ioctl(i32 %35, i32 %36, i32 28)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
41: ; preds = %34
%42 = load i32, i32* %6
%43 = sub nsw i32 %42, 1
%44 = mul nsw i32 %43, 8
%45 = load i32, i32* %7
%46 = sub nsw i32 %45, 1
%47 = mul nsw i32 %46, 2
%48 = add nsw i32 %44, %47
%49 = trunc i32 %48 to i8
store i8 %49, i8* %12
%50 = load i8, i8* %12
%51 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
store i8 %50, i8* %51
%52 = load i32, i32* %8
%53 = trunc i32 %52 to i8
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 1
store i8 %53, i8* %54
%55 = load i32, i32* %13
%56 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%57 = call i32 @write(i32 %55, i8* %56, i32 2)
%58 = icmp ne i32 %57, 2
br i1 %58, label %59, label %61
59: ; preds = %41
%60 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
61: ; preds = %41
%62 = load i32, i32* %6
%63 = sub nsw i32 %62, 1
%64 = mul nsw i32 %63, 16
%65 = add nsw i32 48, %64
%66 = load i32, i32* %7
%67 = sub nsw i32 %66, 1
%68 = mul nsw i32 %67, 4
%69 = add nsw i32 %65, %68
%70 = trunc i32 %69 to i8
store i8 %70, i8* %12
%71 = load i8, i8* %12
%72 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
store i8 %71, i8* %72
%73 = load i32, i32* %13
%74 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%75 = call i32 @write(i32 %73, i8* %74, i32 1)
%76 = icmp ne i32 %75, 1
br i1 %76, label %77, label %79
77: ; preds = %61
%78 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
79: ; preds = %61
%80 = load i32, i32* %13
%81 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%82 = call i32 @read(i32 %80, i8* %81, i32 4)
%83 = icmp ne i32 %82, 4
br i1 %83, label %84, label %86
84: ; preds = %79
%85 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %109
86: ; preds = %79
%87 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 0
%88 = load i8, i8* %87
%89 = zext i8 %88 to i32
%90 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 1
%91 = load i8, i8* %90
%92 = zext i8 %91 to i32
%93 = shl i32 %92, 8
%94 = or i32 %89, %93
%95 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 2
%96 = load i8, i8* %95
%97 = zext i8 %96 to i32
%98 = shl i32 %97, 16
%99 = or i32 %94, %98
%100 = getelementptr inbounds [10 x i8], [10 x i8]* %11, i64 0, i64 3
%101 = load i8, i8* %100
%102 = zext i8 %101 to i32
%103 = shl i32 %102, 24
%104 = or i32 %99, %103
%105 = sext i32 %104 to i64
%106 = load i64*, i64** %9
store i64 %105, i64* %106
%107 = load i32, i32* %13
%108 = call i32 @close(i32 %107)
store i32 0, i32* %5
br label %109
109: ; preds = %86, %84, %77, %59, %39, %32, %26, %19
%110 = load i32, i32* %5
ret i32 %110
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@str = [40 x i8] c"Error reading from Pi-Cubes I/O module.\00"
@str.6 = [38 x i8] c"Error writing to Pi-Cubes I/O module.\00"
@str.7 = [57 x i8] c"Unable to get bus access to talk to Pi-Cubes I/O module.\00"
@str.8 = [27 x i8] c"Failed to open i2c-1 port.\00"
define i32 @readUI(i32 %0, i32 %1, i32 %2, i64* nocapture writeonly %3) {
%5 = alloca [10 x i8]
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%7 = add i32 %0, -7
%8 = icmp ult i32 %7, -6
%9 = add i32 %1, -5
%10 = icmp ult i32 %9, -4
%11 = or i1 %8, %10
br i1 %11, label %71, label %12
12: ; preds = %4
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %71
18: ; preds = %12
%19 = load i32, i32* @I2C_SLAVE
%20 = tail call i64 @ioctl(i32 %14, i32 %19, i32 28)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.7, i64 0, i64 0))
br label %71
24: ; preds = %18
%25 = add nsw i32 %0, -1
%26 = shl nuw nsw i32 %25, 3
%27 = add nsw i32 %1, -1
%28 = shl nuw nsw i32 %27, 1
%29 = add nuw nsw i32 %28, %26
%30 = trunc i32 %29 to i8
store i8 %30, i8* %6
%31 = trunc i32 %2 to i8
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
store i8 %31, i8* %32
%33 = call i32 @write(i32 %14, i8* %6, i32 2)
%34 = icmp eq i32 %33, 2
br i1 %34, label %37, label %35
35: ; preds = %24
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %71
37: ; preds = %24
%38 = shl nuw nsw i32 %25, 4
%39 = add nuw nsw i32 %38, 48
%40 = shl nuw nsw i32 %27, 2
%41 = add nuw nsw i32 %39, %40
%42 = trunc i32 %41 to i8
store i8 %42, i8* %6
%43 = call i32 @write(i32 %14, i8* %6, i32 1)
%44 = icmp eq i32 %43, 1
br i1 %44, label %47, label %45
45: ; preds = %37
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %71
47: ; preds = %37
%48 = call i32 @read(i32 %14, i8* %6, i32 4)
%49 = icmp eq i32 %48, 4
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %71
52: ; preds = %47
%53 = load i8, i8* %6
%54 = zext i8 %53 to i32
%55 = load i8, i8* %32
%56 = zext i8 %55 to i32
%57 = shl nuw nsw i32 %56, 8
%58 = or i32 %57, %54
%59 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 2
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = shl nuw nsw i32 %61, 16
%63 = or i32 %58, %62
%64 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 3
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = shl nuw i32 %66, 24
%68 = or i32 %63, %67
%69 = sext i32 %68 to i64
store i64 %69, i64* %3
%70 = tail call i32 @close(i32 %14)
br label %71
71: ; preds = %4, %52, %50, %45, %35, %22, %16
%72 = phi i32 [ -1, %16 ], [ -1, %22 ], [ -1, %35 ], [ -1, %45 ], [ -1, %50 ], [ 0, %52 ], [ -1, %4 ]
ret i32 %72
}