#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int open_i2c_dev(int *file_i2c, char *filename, uint8_t i2c_addr) {
  if ((*file_i2c = open(filename, O_RDWR)) < 0) {
    printf("Failed to open the i2c bus\n");
    return -1;
  }
  if (ioctl(*file_i2c, I2C_SLAVE, i2c_addr) < 0) {
    printf("Failed to acquire bus access and/or talk to slave.\n");
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<open_i2c_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    117c <open_i2c_dev+0x4c>
lea    0xe95(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <open_i2c_dev+0x8c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11b5 <open_i2c_dev+0x85>
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11bc <open_i2c_dev+0x8c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_i2c_dev>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%rbx
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <open@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     1179 <open_i2c_dev+0x49>
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    %ebp,%edx
call   1040 <ioctl@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    118a <open_i2c_dev+0x5a>
mov    $0xfffffffe,%ebx
lea    0xe89(%rip),%rdi
jmp    1185 <open_i2c_dev+0x55>
mov    $0xffffffff,%ebx
lea    0xeae(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [28 x i8] c"Failed to open the i2c bus\0A\00"
@I2C_SLAVE = global i32 0
@.str.1 = [52 x i8] c"Failed to acquire bus access and/or talk to slave.\0A\00"
define i32 @open_i2c_dev(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i8*, i8** %6
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
%11 = load i32*, i32** %5
store i32 %10, i32* %11
%12 = icmp slt i32 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %25
15: ; preds = %3
%16 = load i32*, i32** %5
%17 = load i32, i32* %16
%18 = load i32, i32* @I2C_SLAVE
%19 = load i32, i32* %7
%20 = call i64 @ioctl(i32 %17, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %25
24: ; preds = %15
store i32 0, i32* %4
br label %25
25: ; preds = %24, %22, %13
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@str = [51 x i8] c"Failed to acquire bus access and/or talk to slave.\00"
@str.2 = [27 x i8] c"Failed to open the i2c bus\00"
define i32 @open_i2c_dev(i32* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* %1, i32 %4)
store i32 %5, i32* %0
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i32, i32* @I2C_SLAVE
%9 = tail call i64 @ioctl(i32 %5, i32 %8, i32 %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7, %3
%12 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.2, i64 0, i64 0), %3 ], [ getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0), %7 ]
%13 = phi i32 [ -1, %3 ], [ -2, %7 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %12)
br label %15
15: ; preds = %11, %7
%16 = phi i32 [ 0, %7 ], [ %13, %11 ]
ret i32 %16
}