#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 write(int, unsigned char *, int);
int writeDO(int module, int output, int value) {
  char *fileName = "/dev/i2c-1";
  unsigned char buf[10];
  unsigned char address;
  int fd;
  if ((module < 1) || (module > 6))
    return -1;
  if ((output < 1) || (output > 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 + (output - 1) * 2);
  buf[0] = address;
  buf[1] = (unsigned char)(value);
  if ((write(fd, buf, 2)) != 2) {
    printf("Error writing to Pi-Cubes I/O module.\n");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<writeDO>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xe98(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1180 <writeDO+0x30>
cmpl   $0x6,-0x8(%rbp)
jle    118c <writeDO+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <writeDO+0x12e>
cmpl   $0x1,-0xc(%rbp)
jl     11a0 <writeDO+0x50>
cmpl   $0x4,-0xc(%rbp)
jle    11ac <writeDO+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <writeDO+0x12e>
mov    -0x18(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11e2 <writeDO+0x92>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <writeDO+0x12e>
mov    -0x28(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    $0x1c,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    121a <writeDO+0xca>
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <writeDO+0x12e>
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,-0x23(%rbp)
mov    -0x23(%rbp),%al
mov    %al,-0x22(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x21(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
cmp    $0x2,%eax
je     126f <writeDO+0x11f>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <writeDO+0x12e>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeDO>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    -0x7(%rbp),%eax
mov    $0xffffffff,%r15d
cmp    $0xfffffffa,%eax
jb     120c <writeDO+0xbc>
mov    %esi,%ebx
lea    -0x5(%rbx),%eax
cmp    $0xfffffffc,%eax
jb     120c <writeDO+0xbc>
mov    %edx,%r14d
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ee <writeDO+0x9e>
mov    %eax,%r12d
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    $0x1c,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f7 <writeDO+0xa7>
shl    $0x3,%ebp
lea    0x0(%rbp,%rbx,2),%eax
add    $0xf6,%al
mov    %al,0x6(%rsp)
mov    %r14b,0x7(%rsp)
lea    0x6(%rsp),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
jne    1200 <writeDO+0xb0>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
jmp    120c <writeDO+0xbc>
lea    0xe75(%rip),%rdi
jmp    1207 <writeDO+0xb7>
lea    0xe33(%rip),%rdi
jmp    1207 <writeDO+0xb7>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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"
define i32 @writeDO(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [10 x i8]
%10 = alloca i8
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %8
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 6
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 -1, i32* %4
br label %62
18: ; preds = %14
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 1
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %6
%23 = icmp sgt i32 %22, 4
br i1 %23, label %24, label %25
24: ; preds = %21, %18
store i32 -1, i32* %4
br label %62
25: ; preds = %21
%26 = load i8*, i8** %8
%27 = load i32, i32* @O_RDWR
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %11
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
32: ; preds = %25
%33 = load i32, i32* %11
%34 = load i32, i32* @I2C_SLAVE
%35 = call i64 @ioctl(i32 %33, i32 %34, i32 28)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
39: ; preds = %32
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 1
%42 = mul nsw i32 %41, 8
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 1
%45 = mul nsw i32 %44, 2
%46 = add nsw i32 %42, %45
%47 = trunc i32 %46 to i8
store i8 %47, i8* %10
%48 = load i8, i8* %10
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
store i8 %48, i8* %49
%50 = load i32, i32* %7
%51 = trunc i32 %50 to i8
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 1
store i8 %51, i8* %52
%53 = load i32, i32* %11
%54 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%55 = call i32 @write(i32 %53, i8* %54, i32 2)
%56 = icmp ne i32 %55, 2
br i1 %56, label %57, label %59
57: ; preds = %39
%58 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
59: ; preds = %39
%60 = load i32, i32* %11
%61 = call i32 @close(i32 %60)
store i32 0, i32* %4
br label %62
62: ; preds = %59, %57, %37, %30, %24, %17
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-1\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@str = [38 x i8] c"Error writing to Pi-Cubes I/O module.\00"
@str.4 = [57 x i8] c"Unable to get bus access to talk to Pi-Cubes I/O module.\00"
@str.5 = [27 x i8] c"Failed to open i2c-1 port.\00"
define i32 @writeDO(i32 %0, i32 %1, i32 %2) {
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%6 = add i32 %0, -7
%7 = icmp ult i32 %6, -6
%8 = add i32 %1, -5
%9 = icmp ult i32 %8, -4
%10 = or i1 %7, %9
br i1 %10, label %37, label %11
11: ; preds = %3
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %37
17: ; preds = %11
%18 = load i32, i32* @I2C_SLAVE
%19 = tail call i64 @ioctl(i32 %13, i32 %18, i32 28)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.4, i64 0, i64 0))
br label %37
23: ; preds = %17
%24 = shl nuw nsw i32 %0, 3
%25 = shl nuw nsw i32 %1, 1
%26 = add nuw nsw i32 %25, %24
%27 = trunc i32 %26 to i8
%28 = add nsw i8 %27, -10
store i8 %28, i8* %5
%29 = trunc i32 %2 to i8
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 1
store i8 %29, i8* %30
%31 = call i32 @write(i32 %13, i8* %5, i32 2)
%32 = icmp eq i32 %31, 2
br i1 %32, label %35, label %33
33: ; preds = %23
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %37
35: ; preds = %23
%36 = tail call i32 @close(i32 %13)
br label %37
37: ; preds = %3, %35, %33, %21, %15
%38 = phi i32 [ -1, %15 ], [ -1, %21 ], [ -1, %33 ], [ 0, %35 ], [ -1, %3 ]
ret i32 %38
}