#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*/ FailMessage(char *);
scalar_t__ I2CWrapperSlaveAddress(int, int);
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int I2CWrapperOpen(int BUS, int SlaveAddress) {
  int handle;
  char I2C_dev[256];
  if (BUS < 0)
    return -1;
  if (BUS > 1)
    return -1;
  sprintf(I2C_dev, "/dev/i2c-%d", BUS);
  handle = open(I2C_dev, O_RDWR);
  if (handle < 0) {
    FailMessage("Failed to open the i2c bus\n");
    return -1;
  }
  if (I2CWrapperSlaveAddress(handle, SlaveAddress) < 0) {
    close(handle);
    return -2;
  }
  return handle;
}


x86 assembly (clang, -O0):
<I2CWrapperOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1177 <I2CWrapperOpen+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <I2CWrapperOpen+0xbc>
cmpl   $0x1,-0x8(%rbp)
jle    118d <I2CWrapperOpen+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <I2CWrapperOpen+0xbc>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe62(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11dd <I2CWrapperOpen+0x8d>
lea    0xe40(%rip),%rdi
call   1030 <FailMessage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <I2CWrapperOpen+0xbc>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <I2CWrapperSlaveAddress@plt>
cmp    $0x0,%rax
jge    1206 <I2CWrapperOpen+0xb6>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120c <I2CWrapperOpen+0xbc>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<I2CWrapperOpen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    $0xffffffff,%ebp
cmp    $0x1,%edi
ja     11c0 <I2CWrapperOpen+0x70>
mov    %esi,%r14d
mov    %edi,%edx
lea    0xe8f(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a6 <I2CWrapperOpen+0x56>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1040 <I2CWrapperSlaveAddress@plt>
test   %rax,%rax
js     11b4 <I2CWrapperOpen+0x64>
mov    %ebx,%ebp
jmp    11c0 <I2CWrapperOpen+0x70>
lea    0xe5f(%rip),%rdi
call   1030 <FailMessage@plt>
jmp    11c0 <I2CWrapperOpen+0x70>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Failed to open the i2c bus\0A\00"
define i32 @I2CWrapperOpen(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %36
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %3
br label %36
15: ; preds = %11
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @FailMessage(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
26: ; preds = %15
%27 = load i32, i32* %6
%28 = load i32, i32* %5
%29 = call i64 @I2CWrapperSlaveAddress(i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 -2, i32* %3
br label %36
34: ; preds = %26
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %31, %24, %14, %10
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Failed to open the i2c bus\0A\00"
define i32 @I2CWrapperOpen(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp ugt i32 %0, 1
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @FailMessage(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %18
13: ; preds = %6
%14 = tail call i64 @I2CWrapperSlaveAddress(i32 %9, i32 %1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @close(i32 %9)
br label %18
18: ; preds = %13, %2, %16, %11
%19 = phi i32 [ -1, %11 ], [ -2, %16 ], [ -1, %2 ], [ %9, %13 ]
ret i32 %19
}