#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 */
unsigned char I2CRead();
int /*<<< orphan*/ I2CStart();
int /*<<< orphan*/ I2CStop();
int /*<<< orphan*/ I2CWriteAck(unsigned char);
unsigned char I2CReadByte(unsigned char device_addr, unsigned char reg_addr,
                          unsigned char *data) {
  unsigned char error = 0;
  I2CStart();
  error += !I2CWriteAck((device_addr << 1) | 0);
  error += !I2CWriteAck(reg_addr);
  I2CStart();
  error += !I2CWriteAck((device_addr << 1) | 1);
  if (error) {
    I2CStop();
    return error;
  }
  *data = I2CRead();
  I2CStop();
  return error;
}


x86 assembly (clang, -O0):
<I2CReadByte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x2(%rbp)
mov    %al,-0x3(%rbp)
mov    %rdx,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    $0x0,%al
call   1050 <I2CStart@plt>
movzbl -0x2(%rbp),%eax
shl    $0x1,%eax
or     $0x0,%eax
movzbl %al,%edi
call   1060 <I2CWriteAck@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%ecx
movzbl -0x11(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x11(%rbp)
movzbl -0x3(%rbp),%edi
call   1060 <I2CWriteAck@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%ecx
movzbl -0x11(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x11(%rbp)
mov    $0x0,%al
call   1050 <I2CStart@plt>
movzbl -0x2(%rbp),%eax
shl    $0x1,%eax
or     $0x1,%eax
movzbl %al,%edi
call   1060 <I2CWriteAck@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%ecx
movzbl -0x11(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x11(%rbp)
cmpb   $0x0,-0x11(%rbp)
je     11f5 <I2CReadByte+0xb5>
mov    $0x0,%al
call   1040 <I2CStop@plt>
mov    -0x11(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    1211 <I2CReadByte+0xd1>
mov    $0x0,%al
call   1030 <I2CRead@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    $0x0,%al
call   1040 <I2CStop@plt>
mov    -0x11(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<I2CReadByte>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <I2CStart@plt>
add    %bpl,%bpl
movzbl %bpl,%ebp
mov    %ebp,%edi
call   1060 <I2CWriteAck@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1060 <I2CWriteAck@plt>
test   %eax,%eax
sete   %bl
cmp    $0x1,%r12d
adc    $0x0,%bl
xor    %eax,%eax
call   1050 <I2CStart@plt>
or     $0x1,%bpl
movzbl %bpl,%edi
call   1060 <I2CWriteAck@plt>
test   %eax,%eax
sete   %bpl
add    %bl,%bpl
jne    11a5 <I2CReadByte+0x65>
xor    %eax,%eax
call   1030 <I2CRead@plt>
mov    %al,(%r14)
xor    %eax,%eax
call   1040 <I2CStop@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @I2CReadByte(i8 zeroext %0, i8 zeroext %1, i8* %2) {
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8
store i8 %0, i8* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
store i8 0, i8* %8
%9 = call i32 (...) @I2CStart()
%10 = load i8, i8* %5
%11 = zext i8 %10 to i32
%12 = shl i32 %11, 1
%13 = or i32 %12, 0
%14 = trunc i32 %13 to i8
%15 = call i32 @I2CWriteAck(i8 zeroext %14)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
%18 = zext i1 %17 to i32
%19 = load i8, i8* %8
%20 = zext i8 %19 to i32
%21 = add nsw i32 %20, %18
%22 = trunc i32 %21 to i8
store i8 %22, i8* %8
%23 = load i8, i8* %6
%24 = call i32 @I2CWriteAck(i8 zeroext %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
%27 = zext i1 %26 to i32
%28 = load i8, i8* %8
%29 = zext i8 %28 to i32
%30 = add nsw i32 %29, %27
%31 = trunc i32 %30 to i8
store i8 %31, i8* %8
%32 = call i32 (...) @I2CStart()
%33 = load i8, i8* %5
%34 = zext i8 %33 to i32
%35 = shl i32 %34, 1
%36 = or i32 %35, 1
%37 = trunc i32 %36 to i8
%38 = call i32 @I2CWriteAck(i8 zeroext %37)
%39 = icmp ne i32 %38, 0
%40 = xor i1 %39, true
%41 = zext i1 %40 to i32
%42 = load i8, i8* %8
%43 = zext i8 %42 to i32
%44 = add nsw i32 %43, %41
%45 = trunc i32 %44 to i8
store i8 %45, i8* %8
%46 = load i8, i8* %8
%47 = icmp ne i8 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %3
%49 = call i32 (...) @I2CStop()
%50 = load i8, i8* %8
store i8 %50, i8* %4
br label %56
51: ; preds = %3
%52 = call zeroext i8 (...) @I2CRead()
%53 = load i8*, i8** %7
store i8 %52, i8* %53
%54 = call i32 (...) @I2CStop()
%55 = load i8, i8* %8
store i8 %55, i8* %4
br label %56
56: ; preds = %51, %48
%57 = load i8, i8* %4
ret i8 %57
}

LLVM IR (-O2):
define zeroext i8 @I2CReadByte(i8 zeroext %0, i8 zeroext %1, i8* nocapture writeonly %2) {
%4 = tail call i32 (...) @I2CStart()
%5 = shl i8 %0, 1
%6 = tail call i32 @I2CWriteAck(i8 zeroext %5)
%7 = icmp eq i32 %6, 0
%8 = tail call i32 @I2CWriteAck(i8 zeroext %1)
%9 = icmp eq i32 %8, 0
%10 = zext i1 %9 to i8
%11 = zext i1 %7 to i8
%12 = add nuw nsw i8 %10, %11
%13 = tail call i32 (...) @I2CStart()
%14 = or i8 %5, 1
%15 = tail call i32 @I2CWriteAck(i8 zeroext %14)
%16 = icmp eq i32 %15, 0
%17 = zext i1 %16 to i8
%18 = add nuw nsw i8 %12, %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = tail call zeroext i8 (...) @I2CRead()
store i8 %21, i8* %2
br label %22
22: ; preds = %3, %20
%23 = tail call i32 (...) @I2CStop()
ret i8 %18
}