#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);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int i2c_read_byte(int device, unsigned char device_addr,
                  unsigned char reg_addr) {
  unsigned char buf[1];
  i2c_set_address(device, device_addr);
  if (write(device, &reg_addr, 1) != 1)
    return -1;
  i2c_set_address(device, device_addr);
  if (read(device, buf, 1) != 1)
    return -1;
  return buf[0];
}


x86 assembly (clang, -O0):
<i2c_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %edi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%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     1178 <i2c_read_byte+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <i2c_read_byte+0x81>
mov    -0x8(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   1040 <i2c_set_address@plt>
mov    -0x8(%rbp),%edi
lea    -0xb(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11aa <i2c_read_byte+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <i2c_read_byte+0x81>
movzbl -0xb(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_byte>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    %dl,0xf(%rsp)
call   1040 <i2c_set_address@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0xffffffff,%r14d
cmp    $0x1,%eax
jne    1188 <i2c_read_byte+0x58>
movzbl %bpl,%esi
mov    %ebx,%edi
call   1040 <i2c_set_address@plt>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    1188 <i2c_read_byte+0x58>
movzbl 0xe(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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