#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*/ i2c_set_address(int, unsigned char);
size_t read(int, unsigned char *, size_t);
int write(int, unsigned char *, int);
int i2c_read_bytes(int device, unsigned char device_addr,
                   unsigned char reg_addr, unsigned char *read_buffer,
                   size_t n) {
  i2c_set_address(device, device_addr);
  if (write(device, &reg_addr, 1) != 1)
    return -1;
  i2c_set_address(device, device_addr);
  if (read(device, read_buffer, n) != n)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %edi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   1040 <i2c_set_address@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1180 <i2c_read_bytes+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <i2c_read_bytes+0x89>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   1040 <i2c_set_address@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
cmp    -0x20(%rbp),%rax
je     11b2 <i2c_read_bytes+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <i2c_read_bytes+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
mov    %dl,0x7(%rsp)
call   1040 <i2c_set_address@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x1,%ecx
jne    118b <i2c_read_bytes+0x5b>
movzbl %bl,%esi
mov    %ebp,%edi
call   1040 <i2c_set_address@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r15,%rcx
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @i2c_read_bytes(i32 %0, i8 zeroext %1, i8 zeroext %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i64
store i32 %0, i32* %7
store i8 %1, i8* %8
store i8 %2, i8* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%12 = load i32, i32* %7
%13 = load i8, i8* %8
%14 = call i32 @i2c_set_address(i32 %12, i8 zeroext %13)
%15 = load i32, i32* %7
%16 = call i32 @write(i32 %15, i8* %9, i32 1)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %31
19: ; preds = %5
%20 = load i32, i32* %7
%21 = load i8, i8* %8
%22 = call i32 @i2c_set_address(i32 %20, i8 zeroext %21)
%23 = load i32, i32* %7
%24 = load i8*, i8** %10
%25 = load i64, i64* %11
%26 = call i64 @read(i32 %23, i8* %24, i64 %25)
%27 = load i64, i64* %11
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %19
store i32 -1, i32* %6
br label %31
30: ; preds = %19
store i32 0, i32* %6
br label %31
31: ; preds = %30, %29, %18
%32 = load i32, i32* %6
ret i32 %32
}

LLVM IR (-O2):
define i32 @i2c_read_bytes(i32 %0, i8 zeroext %1, i8 zeroext %2, i8* nocapture %3, i64 %4) {
%6 = alloca i8
store i8 %2, i8* %6
%7 = tail call i32 @i2c_set_address(i32 %0, i8 zeroext %1)
%8 = call i32 @write(i32 %0, i8* %6, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = tail call i32 @i2c_set_address(i32 %0, i8 zeroext %1)
%12 = tail call i64 @read(i32 %0, i8* %3, i64 %4)
%13 = icmp ne i64 %12, %4
%14 = sext i1 %13 to i32
br label %15
15: ; preds = %10, %5
%16 = phi i32 [ -1, %5 ], [ %14, %10 ]
ret i32 %16
}