#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 /*<<< orphan*/ strerror(int /*<<< orphan*/);
int write(int, int /*<<< orphan*/ *, int);
bool i2c_write_bytes(int busno, uint8_t *byte, 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 = write(fd, byte, len);
  if (r == -1) {
    log_message("[ERROR][%d-%d-%d %d:%d:%d] ",
                "I2C write got an error [%d]: %s\r\n", errno, strerror(errno));
    return false;
  }
  return r == len;
}


x86 assembly (clang, -O0):
<i2c_write_bytes>:
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_write_bytes+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    1205 <i2c_write_bytes+0xc5>
mov    0x2e4e(%rip),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f4 <i2c_write_bytes+0xb4>
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>
movl   $0x0,-0x4(%rbp)
jmp    1205 <i2c_write_bytes+0xc5>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write_bytes>:
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    117f <i2c_write_bytes+0x3f>
mov    0x2e84(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     119a <i2c_write_bytes+0x5a>
xor    %ebx,%ebx
cmp    %r14d,%eax
sete   %bl
jmp    11bf <i2c_write_bytes+0x7f>
lea    0xe7a(%rip),%rdi
lea    0xe8f(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <log_message@plt>
jmp    11bf <i2c_write_bytes+0x7f>
mov    0x0(%rbp),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe55(%rip),%rdi
lea    0xebc(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
mov    %eax,%ecx
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 = [34 x i8] c"I2C write got an error [%d]: %s\0D\0A\00"
define i32 @i2c_write_bytes(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 %34
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 @write(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 ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %27)
store i32 0, i32* %4
br label %34
29: ; preds = %17
%30 = load i32, i32* %9
%31 = load i32, i32* %7
%32 = icmp eq i32 %30, %31
%33 = zext i1 %32 to i32
store i32 %33, i32* %4
br label %34
34: ; preds = %29, %24, %14
%35 = load i32, i32* %4
ret i32 %35
}

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 = [34 x i8] c"I2C write got an error [%d]: %s\0D\0A\00"
define i32 @i2c_write_bytes(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 %18
8: ; preds = %3
store i32 0, i32* @errno
%9 = tail call i32 @write(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 ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
br label %18
15: ; preds = %8
%16 = icmp eq i32 %9, %2
%17 = zext i1 %16 to i32
br label %18
18: ; preds = %11, %15, %6
%19 = phi i32 [ 0, %6 ], [ 0, %11 ], [ %17, %15 ]
ret i32 %19
}