#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*/ ReceiveI2CAck();
unsigned char ReceiveI2CData();
int /*<<< orphan*/ SendI2CAck();
int /*<<< orphan*/ SendI2CData(char);
int /*<<< orphan*/ SendI2CNack();
int /*<<< orphan*/ SendI2CStart();
int /*<<< orphan*/ SendI2CStop();
int /*<<< orphan*/ gfx_delay_milliseconds(int);
int gfx_i2c_read(unsigned char busnum, unsigned char address, unsigned char reg,
                 unsigned char bytes, unsigned char *p_value) {
  int restart_count = 0;
  if (!p_value)
    return (1);
  while (restart_count++ < 5) {
    SendI2CStart();
    SendI2CData((char)((address << 1) | 0x00));
    if (!ReceiveI2CAck()) {
      SendI2CStop();
      gfx_delay_milliseconds(10);
      continue;
    }
    SendI2CData((unsigned char)(reg & 0xFF));
    SendI2CNack();
    SendI2CStart();
    SendI2CData((char)((address << 1) | 0x01));
    if (!ReceiveI2CAck()) {
      SendI2CStop();
      gfx_delay_milliseconds(10);
      continue;
    }
    *p_value = ReceiveI2CData();
    if (bytes == 2) {
      SendI2CAck();
      *(p_value + 1) = ReceiveI2CData();
    }
    SendI2CNack();
    SendI2CStop();
    return 0;
  }
  return (1);
}


x86 assembly (clang, -O0):
<gfx_i2c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %dil,%sil
mov    %sil,-0x5(%rbp)
mov    %dl,-0x6(%rbp)
mov    %cl,-0x7(%rbp)
mov    %al,-0x8(%rbp)
mov    %r8,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <gfx_i2c_read+0x41>
movl   $0x1,-0x4(%rbp)
jmp    12c4 <gfx_i2c_read+0x144>
jmp    11c6 <gfx_i2c_read+0x46>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x5,%eax
jge    12bd <gfx_i2c_read+0x13d>
mov    $0x0,%al
call   1080 <SendI2CStart@plt>
movzbl -0x6(%rbp),%eax
shl    $0x1,%eax
or     $0x0,%eax
movsbl %al,%edi
call   1090 <SendI2CData@plt>
mov    $0x0,%al
call   1040 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    1219 <gfx_i2c_read+0x99>
mov    $0x0,%al
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
jmp    11c6 <gfx_i2c_read+0x46>
movzbl -0x7(%rbp),%eax
and    $0xff,%eax
movsbl %al,%edi
call   1090 <SendI2CData@plt>
mov    $0x0,%al
call   1030 <SendI2CNack@plt>
mov    $0x0,%al
call   1080 <SendI2CStart@plt>
movzbl -0x6(%rbp),%eax
shl    $0x1,%eax
or     $0x1,%eax
movsbl %al,%edi
call   1090 <SendI2CData@plt>
mov    $0x0,%al
call   1040 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    1270 <gfx_i2c_read+0xf0>
mov    $0x0,%al
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
jmp    11c6 <gfx_i2c_read+0x46>
mov    $0x0,%al
call   1060 <ReceiveI2CData@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movzbl -0x8(%rbp),%eax
cmp    $0x2,%eax
jne    12a3 <gfx_i2c_read+0x123>
mov    $0x0,%al
call   1050 <SendI2CAck@plt>
mov    $0x0,%al
call   1060 <ReceiveI2CData@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    $0x0,%al
call   1030 <SendI2CNack@plt>
mov    $0x0,%al
call   10a0 <SendI2CStop@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c4 <gfx_i2c_read+0x144>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfx_i2c_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r13d
test   %r8,%r8
je     1380 <gfx_i2c_read+0x200>
mov    %r8,%r15
mov    %edx,%ebp
mov    %esi,%ebx
mov    %ecx,0x4(%rsp)
add    %bl,%bl
lea    0x1(%rbx),%r12d
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
movsbl %bl,%r14d
mov    %r14d,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
movsbl %bpl,%ebp
movsbl %r12b,%ebx
test   %eax,%eax
je     11fc <gfx_i2c_read+0x7c>
mov    %ebp,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %ebx,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    134c <gfx_i2c_read+0x1cc>
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %r14d,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1252 <gfx_i2c_read+0xd2>
mov    %ebp,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %ebx,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    134c <gfx_i2c_read+0x1cc>
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %r14d,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
je     12a8 <gfx_i2c_read+0x128>
mov    %ebp,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %ebx,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    134c <gfx_i2c_read+0x1cc>
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %r14d,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
je     12fa <gfx_i2c_read+0x17a>
mov    %ebp,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %ebx,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    134c <gfx_i2c_read+0x1cc>
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %r14d,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1392 <gfx_i2c_read+0x212>
mov    %ebp,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   1080 <SendI2CStart@plt>
mov    %ebx,%edi
call   1090 <SendI2CData@plt>
xor    %eax,%eax
call   1040 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1392 <gfx_i2c_read+0x212>
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <ReceiveI2CData@plt>
mov    %al,(%r15)
cmpb   $0x2,0x4(%rsp)
jne    1372 <gfx_i2c_read+0x1f2>
xor    %eax,%eax
call   1050 <SendI2CAck@plt>
xor    %eax,%eax
call   1060 <ReceiveI2CData@plt>
mov    %al,0x1(%r15)
xor    %eax,%eax
call   1030 <SendI2CNack@plt>
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10a0 <SendI2CStop@plt>
mov    $0xa,%edi
call   1070 <gfx_delay_milliseconds@plt>
jmp    1380 <gfx_i2c_read+0x200>

LLVM IR (-O0):
define i32 @gfx_i2c_read(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i32
store i8 %0, i8* %7
store i8 %1, i8* %8
store i8 %2, i8* %9
store i8 %3, i8* %10
store i8* %4, i8** %11
store i32 0, i32* %12
%13 = load i8*, i8** %11
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %5
store i32 1, i32* %6
br label %68
16: ; preds = %5
br label %17
17: ; preds = %50, %31, %16
%18 = load i32, i32* %12
%19 = add nsw i32 %18, 1
store i32 %19, i32* %12
%20 = icmp slt i32 %18, 5
br i1 %20, label %21, label %67
21: ; preds = %17
%22 = call i32 (...) @SendI2CStart()
%23 = load i8, i8* %8
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 1
%26 = or i32 %25, 0
%27 = trunc i32 %26 to i8
%28 = call i32 @SendI2CData(i8 signext %27)
%29 = call i32 (...) @ReceiveI2CAck()
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %21
%32 = call i32 (...) @SendI2CStop()
%33 = call i32 @gfx_delay_milliseconds(i32 10)
br label %17
34: ; preds = %21
%35 = load i8, i8* %9
%36 = zext i8 %35 to i32
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = call i32 @SendI2CData(i8 signext %38)
%40 = call i32 (...) @SendI2CNack()
%41 = call i32 (...) @SendI2CStart()
%42 = load i8, i8* %8
%43 = zext i8 %42 to i32
%44 = shl i32 %43, 1
%45 = or i32 %44, 1
%46 = trunc i32 %45 to i8
%47 = call i32 @SendI2CData(i8 signext %46)
%48 = call i32 (...) @ReceiveI2CAck()
%49 = icmp ne i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %34
%51 = call i32 (...) @SendI2CStop()
%52 = call i32 @gfx_delay_milliseconds(i32 10)
br label %17
53: ; preds = %34
%54 = call zeroext i8 (...) @ReceiveI2CData()
%55 = load i8*, i8** %11
store i8 %54, i8* %55
%56 = load i8, i8* %10
%57 = zext i8 %56 to i32
%58 = icmp eq i32 %57, 2
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = call i32 (...) @SendI2CAck()
%61 = call zeroext i8 (...) @ReceiveI2CData()
%62 = load i8*, i8** %11
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8 %61, i8* %63
br label %64
64: ; preds = %59, %53
%65 = call i32 (...) @SendI2CNack()
%66 = call i32 (...) @SendI2CStop()
store i32 0, i32* %6
br label %68
67: ; preds = %17
store i32 1, i32* %6
br label %68
68: ; preds = %67, %64, %15
%69 = load i32, i32* %6
ret i32 %69
}

LLVM IR (-O2):
define i32 @gfx_i2c_read(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8* writeonly %4) {
%6 = icmp eq i8* %4, null
br i1 %6, label %93, label %7
7: ; preds = %5
%8 = shl i8 %1, 1
%9 = or i8 %8, 1
%10 = tail call i32 (...) @SendI2CStart()
%11 = tail call i32 @SendI2CData(i8 signext %8)
%12 = tail call i32 (...) @ReceiveI2CAck()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %76
14: ; preds = %7, %76
%15 = tail call i32 (...) @SendI2CStop()
%16 = tail call i32 @gfx_delay_milliseconds(i32 10)
%17 = tail call i32 (...) @SendI2CStart()
%18 = tail call i32 @SendI2CData(i8 signext %8)
%19 = tail call i32 (...) @ReceiveI2CAck()
%20 = icmp eq i32 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %14
%22 = tail call i32 @SendI2CData(i8 signext %2)
%23 = tail call i32 (...) @SendI2CNack()
%24 = tail call i32 (...) @SendI2CStart()
%25 = tail call i32 @SendI2CData(i8 signext %9)
%26 = tail call i32 (...) @ReceiveI2CAck()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %83
28: ; preds = %14, %21
%29 = tail call i32 (...) @SendI2CStop()
%30 = tail call i32 @gfx_delay_milliseconds(i32 10)
%31 = tail call i32 (...) @SendI2CStart()
%32 = tail call i32 @SendI2CData(i8 signext %8)
%33 = tail call i32 (...) @ReceiveI2CAck()
%34 = icmp eq i32 %33, 0
br i1 %34, label %42, label %35
35: ; preds = %28
%36 = tail call i32 @SendI2CData(i8 signext %2)
%37 = tail call i32 (...) @SendI2CNack()
%38 = tail call i32 (...) @SendI2CStart()
%39 = tail call i32 @SendI2CData(i8 signext %9)
%40 = tail call i32 (...) @ReceiveI2CAck()
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %83
42: ; preds = %28, %35
%43 = tail call i32 (...) @SendI2CStop()
%44 = tail call i32 @gfx_delay_milliseconds(i32 10)
%45 = tail call i32 (...) @SendI2CStart()
%46 = tail call i32 @SendI2CData(i8 signext %8)
%47 = tail call i32 (...) @ReceiveI2CAck()
%48 = icmp eq i32 %47, 0
br i1 %48, label %56, label %49
49: ; preds = %42
%50 = tail call i32 @SendI2CData(i8 signext %2)
%51 = tail call i32 (...) @SendI2CNack()
%52 = tail call i32 (...) @SendI2CStart()
%53 = tail call i32 @SendI2CData(i8 signext %9)
%54 = tail call i32 (...) @ReceiveI2CAck()
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %83
56: ; preds = %42, %49
%57 = tail call i32 (...) @SendI2CStop()
%58 = tail call i32 @gfx_delay_milliseconds(i32 10)
%59 = tail call i32 (...) @SendI2CStart()
%60 = tail call i32 @SendI2CData(i8 signext %8)
%61 = tail call i32 (...) @ReceiveI2CAck()
%62 = icmp eq i32 %61, 0
br i1 %62, label %73, label %63
63: ; preds = %56
%64 = tail call i32 @SendI2CData(i8 signext %2)
%65 = tail call i32 (...) @SendI2CNack()
%66 = tail call i32 (...) @SendI2CStart()
%67 = tail call i32 @SendI2CData(i8 signext %9)
%68 = tail call i32 (...) @ReceiveI2CAck()
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %83
70: ; preds = %63
%71 = tail call i32 (...) @SendI2CStop()
%72 = tail call i32 @gfx_delay_milliseconds(i32 10)
br label %93
73: ; preds = %56
%74 = tail call i32 (...) @SendI2CStop()
%75 = tail call i32 @gfx_delay_milliseconds(i32 10)
br label %93
76: ; preds = %7
%77 = tail call i32 @SendI2CData(i8 signext %2)
%78 = tail call i32 (...) @SendI2CNack()
%79 = tail call i32 (...) @SendI2CStart()
%80 = tail call i32 @SendI2CData(i8 signext %9)
%81 = tail call i32 (...) @ReceiveI2CAck()
%82 = icmp eq i32 %81, 0
br i1 %82, label %14, label %83
83: ; preds = %63, %49, %35, %21, %76
%84 = tail call zeroext i8 (...) @ReceiveI2CData()
store i8 %84, i8* %4
%85 = icmp eq i8 %3, 2
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = tail call i32 (...) @SendI2CAck()
%88 = tail call zeroext i8 (...) @ReceiveI2CData()
%89 = getelementptr inbounds i8, i8* %4, i64 1
store i8 %88, i8* %89
br label %90
90: ; preds = %86, %83
%91 = tail call i32 (...) @SendI2CNack()
%92 = tail call i32 (...) @SendI2CStop()
br label %93
93: ; preds = %70, %73, %5, %90
%94 = phi i32 [ 0, %90 ], [ 1, %5 ], [ 1, %73 ], [ 1, %70 ]
ret i32 %94
}