#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 _i2c_get_fd(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ log_message(char *, char *, int, ...);
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int i2c_read(int busno, uint8_t *buffer, int len) {
  int fd = _i2c_get_fd(busno);
  if (fd <= 0) {
    log_message("[ERROR][%d-%d-%d %d:%d:%d] ",
                "i2c_write_bytes: the bus number (%d) is out of range or the "
                "bus is not yet open\r\n",
                busno);
    return false;
  }
  errno = 0;
  int r = read(fd, buffer, len);
  if (r == -1) {
    log_message("[ERROR][%d-%d-%d %d:%d:%d] ",
                "I2C read got an error [%d]: %s\r\n", errno, strerror(errno));
  }
  return r;
}


x86 assembly (clang, -O0):
<i2c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <_i2c_get_fd@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     118b <i2c_read+0x4b>
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rdi
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1050 <log_message@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ee <i2c_read+0xae>
mov    0x2e4e(%rip),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e8 <i2c_read+0xa8>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xe26(%rip),%rdi
lea    0xe8d(%rip),%rsi
mov    $0x0,%al
call   1050 <log_message@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <_i2c_get_fd@plt>
test   %eax,%eax
jle    11a1 <i2c_read+0x61>
mov    0x2e84(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11ba <i2c_read+0x7a>
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe78(%rip),%rdi
lea    0xedf(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <log_message@plt>
mov    $0xffffffff,%ebx
jmp    11ba <i2c_read+0x7a>
lea    0xe58(%rip),%rdi
lea    0xe6d(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <log_message@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.1 = [82 x i8] c"i2c_write_bytes: the bus number (%d) is out of range or the bus is not yet open\0D\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"I2C read got an error [%d]: %s\0D\0A\00"
define i32 @i2c_read(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i32 @_i2c_get_fd(i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i32, i32* %5
%16 = call i32 (i8*, i8*, i32, ...) @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 0, i32* %4
br label %31
17: ; preds = %3
store i32 0, i32* @errno
%18 = load i32, i32* %8
%19 = load i32*, i32** %6
%20 = load i32, i32* %7
%21 = call i32 @read(i32 %18, i32* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, i8*, i32, ...) @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %27)
br label %29
29: ; preds = %24, %17
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %14
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.1 = [82 x i8] c"i2c_write_bytes: the bus number (%d) is out of range or the bus is not yet open\0D\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"I2C read got an error [%d]: %s\0D\0A\00"
define i32 @i2c_read(i32 %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 @_i2c_get_fd(i32 %0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, i8*, i32, ...) @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %15
8: ; preds = %3
store i32 0, i32* @errno
%9 = tail call i32 @read(i32 %4, i32* %1, i32 %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 (i8*, i8*, i32, ...) @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
br label %15
15: ; preds = %8, %11, %6
%16 = phi i32 [ 0, %6 ], [ -1, %11 ], [ %9, %8 ]
ret i32 %16
}