#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*/ O_RDWR;
int /*<<< orphan*/ _i2c_busno_in_range(int);
int _i2c_set_fd(int, int);
int /*<<< orphan*/ log_message(char *, char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
bool i2c_open_bus(int busno) {
  char buff[16];
  int fd;
  if (!_i2c_busno_in_range(busno)) {
    log_message(
        "[ERROR][%d-%d-%d %d:%d:%d] ",
        "i2c_open_bus: bus number (%d) is out of range, maximum is %d\r\n",
        busno, 5);
    return false;
  }
  sprintf(buff, "/dev/i2c-%d", busno);
  fd = open(buff, O_RDWR);
  if (fd < 0) {
    return false;
  }
  return _i2c_set_fd(busno, fd);
}


x86 assembly (clang, -O0):
<i2c_open_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <_i2c_busno_in_range@plt>
cmp    $0x0,%eax
jne    1193 <i2c_open_bus+0x43>
mov    -0x8(%rbp),%edx
lea    0xe8a(%rip),%rdi
lea    0xe9f(%rip),%rsi
mov    $0x5,%ecx
call   1030 <log_message@plt>
movl   $0x0,-0x4(%rbp)
jmp    11df <i2c_open_bus+0x8f>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xeba(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d1 <i2c_open_bus+0x81>
movl   $0x0,-0x4(%rbp)
jmp    11df <i2c_open_bus+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1070 <_i2c_set_fd@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_open_bus>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
call   1050 <_i2c_busno_in_range@plt>
test   %eax,%eax
je     119b <i2c_open_bus+0x4b>
lea    0xef2(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   1060 <sprintf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     11b5 <i2c_open_bus+0x65>
mov    %ebx,%edi
mov    %eax,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1070 <_i2c_set_fd@plt>
lea    0xe5e(%rip),%rdi
lea    0xe73(%rip),%rsi
mov    %ebx,%edx
mov    $0x5,%ecx
call   1030 <log_message@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.1 = [63 x i8] c"i2c_open_bus: bus number (%d) is out of range, maximum is %d\0D\0A\00"
@.str.2 = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
define i32 @i2c_open_bus(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @_i2c_busno_in_range(i32 %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 5)
store i32 0, i32* %2
br label %26
12: ; preds = %1
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %12
store i32 0, i32* %2
br label %26
22: ; preds = %12
%23 = load i32, i32* %3
%24 = load i32, i32* %5
%25 = call i32 @_i2c_set_fd(i32 %23, i32 %24)
store i32 %25, i32* %2
br label %26
26: ; preds = %22, %21, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.1 = [63 x i8] c"i2c_open_bus: bus number (%d) is out of range, maximum is %d\0D\0A\00"
@.str.2 = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
define i32 @i2c_open_bus(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @_i2c_busno_in_range(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 5)
br label %15
8: ; preds = %1
%9 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %3, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = tail call i32 @_i2c_set_fd(i32 %0, i32 %11)
br label %15
15: ; preds = %8, %13, %6
%16 = phi i32 [ %14, %13 ], [ 0, %6 ], [ 0, %8 ]
ret i32 %16
}