#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;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ i2c_smbus_read_byte(int);
int /*<<< orphan*/ i2c_smbus_write_byte_data(int, int, int);
scalar_t__ ioctl(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int ri2c(uint8_t addr, uint16_t reg, uint8_t *buf) {
  int fd;
  if ((fd = open("/dev/i2c-0", O_RDWR)) < 0) {
    printf("open error! %d\n", fd);
    return -1;
  }
  if (ioctl(fd, 0x0703, addr) < 0) {
    printf("address error!\n");
    return -1;
  }
  i2c_smbus_write_byte_data(fd, reg & 0xff, (reg >> 8) & 0xff);
  *buf = i2c_smbus_read_byte(fd);
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<ri2c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b0 <ri2c+0x50>
mov    -0x1c(%rbp),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <ri2c+0xc0>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    $0x703,%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e4 <ri2c+0x84>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <ri2c+0xc0>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
and    $0xff,%esi
mov    -0xc(%rbp),%edx
sar    $0x8,%edx
and    $0xff,%edx
call   1070 <i2c_smbus_write_byte_data@plt>
mov    -0x1c(%rbp),%edi
call   1060 <i2c_smbus_read_byte@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ri2c>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11d5 <ri2c+0x65>
mov    %ebp,%edi
mov    $0x703,%esi
mov    %ebx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11e7 <ri2c+0x77>
mov    %r15d,%eax
movzbl %al,%esi
movzbl %ah,%edx
mov    %ebp,%edi
call   1080 <i2c_smbus_write_byte_data@plt>
mov    %ebp,%edi
call   1070 <i2c_smbus_read_byte@plt>
mov    %eax,(%r14)
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    11f8 <ri2c+0x88>
lea    0xe2f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f3 <ri2c+0x83>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open error! %d\0A\00"
@.str.2 = [16 x i8] c"address error!\0A\00"
define i32 @ri2c(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 -1, i32* %4
br label %35
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i32, i32* %5
%18 = call i64 @ioctl(i32 %16, i32 1795, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
22: ; preds = %15
%23 = load i32, i32* %8
%24 = load i32, i32* %6
%25 = and i32 %24, 255
%26 = load i32, i32* %6
%27 = ashr i32 %26, 8
%28 = and i32 %27, 255
%29 = call i32 @i2c_smbus_write_byte_data(i32 %23, i32 %25, i32 %28)
%30 = load i32, i32* %8
%31 = call i32 @i2c_smbus_read_byte(i32 %30)
%32 = load i32*, i32** %7
store i32 %31, i32* %32
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
store i32 1, i32* %4
br label %35
35: ; preds = %22, %20, %12
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open error! %d\0A\00"
@str = [15 x i8] c"address error!\00"
define i32 @ri2c(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %21
9: ; preds = %3
%10 = tail call i64 @ioctl(i32 %5, i32 1795, i32 %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %21
14: ; preds = %9
%15 = and i32 %1, 255
%16 = lshr i32 %1, 8
%17 = and i32 %16, 255
%18 = tail call i32 @i2c_smbus_write_byte_data(i32 %5, i32 %15, i32 %17)
%19 = tail call i32 @i2c_smbus_read_byte(i32 %5)
store i32 %19, i32* %2
%20 = tail call i32 @close(i32 %5)
br label %21
21: ; preds = %14, %12, %7
%22 = phi i32 [ -1, %7 ], [ -1, %12 ], [ 1, %14 ]
ret i32 %22
}