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


x86 assembly (clang, -O0):
<gfx_i2c_write>:
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)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x5,%eax
jge    1286 <gfx_i2c_write+0x136>
mov    $0x0,%al
call   1050 <SendI2CStart@plt>
movzbl -0x6(%rbp),%eax
shl    $0x1,%eax
or     $0x0,%eax
movzbl %al,%edi
call   1060 <SendI2CData@plt>
mov    $0x0,%al
call   1030 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    11cd <gfx_i2c_write+0x7d>
mov    $0x0,%al
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
jmp    117a <gfx_i2c_write+0x2a>
movzbl -0x7(%rbp),%edi
call   1060 <SendI2CData@plt>
mov    $0x0,%al
call   1030 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    11fc <gfx_i2c_write+0xac>
mov    $0x0,%al
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
jmp    117a <gfx_i2c_write+0x2a>
mov    -0x10(%rbp),%rax
movzbl (%rax),%edi
call   1060 <SendI2CData@plt>
mov    $0x0,%al
call   1030 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    122e <gfx_i2c_write+0xde>
mov    $0x0,%al
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
jmp    117a <gfx_i2c_write+0x2a>
movzbl -0x8(%rbp),%eax
cmp    $0x2,%eax
jne    1273 <gfx_i2c_write+0x123>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1060 <SendI2CData@plt>
mov    $0x0,%al
call   1030 <ReceiveI2CAck@plt>
cmp    $0x0,%eax
jne    126e <gfx_i2c_write+0x11e>
mov    $0x0,%al
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
jmp    117a <gfx_i2c_write+0x2a>
jmp    1273 <gfx_i2c_write+0x123>
mov    $0x0,%al
call   1070 <SendI2CStop@plt>
movl   $0x0,-0x4(%rbp)
jmp    128d <gfx_i2c_write+0x13d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gfx_i2c_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r15
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%ebx
add    %bl,%bl
xor    %eax,%eax
call   1050 <SendI2CStart@plt>
movzbl %bl,%ebx
mov    %ebx,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
movzbl %bpl,%ebp
test   %eax,%eax
je     11cc <gfx_i2c_write+0x7c>
mov    %ebp,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     11cc <gfx_i2c_write+0x7c>
movzbl (%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     11cc <gfx_i2c_write+0x7c>
cmp    $0x2,%r14b
jne    138c <gfx_i2c_write+0x23c>
movzbl 0x1(%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    138c <gfx_i2c_write+0x23c>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1050 <SendI2CStart@plt>
mov    %ebx,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     123f <gfx_i2c_write+0xef>
mov    %ebp,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     123f <gfx_i2c_write+0xef>
movzbl (%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     123f <gfx_i2c_write+0xef>
cmp    $0x2,%r14b
jne    138c <gfx_i2c_write+0x23c>
movzbl 0x1(%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    138c <gfx_i2c_write+0x23c>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1050 <SendI2CStart@plt>
mov    %ebx,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     12b2 <gfx_i2c_write+0x162>
mov    %ebp,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     12b2 <gfx_i2c_write+0x162>
movzbl (%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     12b2 <gfx_i2c_write+0x162>
cmp    $0x2,%r14b
jne    138c <gfx_i2c_write+0x23c>
movzbl 0x1(%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    138c <gfx_i2c_write+0x23c>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1050 <SendI2CStart@plt>
mov    %ebx,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1321 <gfx_i2c_write+0x1d1>
mov    %ebp,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1321 <gfx_i2c_write+0x1d1>
movzbl (%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1321 <gfx_i2c_write+0x1d1>
cmp    $0x2,%r14b
jne    138c <gfx_i2c_write+0x23c>
movzbl 0x1(%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
jne    138c <gfx_i2c_write+0x23c>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
call   1050 <SendI2CStart@plt>
mov    %ebx,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1395 <gfx_i2c_write+0x245>
mov    %ebp,%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1395 <gfx_i2c_write+0x245>
movzbl (%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1395 <gfx_i2c_write+0x245>
cmp    $0x2,%r14b
jne    138c <gfx_i2c_write+0x23c>
movzbl 0x1(%r15),%edi
call   1060 <SendI2CData@plt>
xor    %eax,%eax
call   1030 <ReceiveI2CAck@plt>
test   %eax,%eax
je     1395 <gfx_i2c_write+0x245>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
jmp    13a6 <gfx_i2c_write+0x256>
xor    %eax,%eax
call   1070 <SendI2CStop@plt>
mov    $0xa,%edi
call   1040 <gfx_delay_milliseconds@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @gfx_i2c_write(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8* nocapture %4) {
%6 = shl i8 %1, 1
%7 = icmp eq i8 %3, 2
%8 = getelementptr inbounds i8, i8* %4, i64 1
%9 = tail call i32 (...) @SendI2CStart()
%10 = tail call i32 @SendI2CData(i8 zeroext %6)
%11 = tail call i32 (...) @ReceiveI2CAck()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %104
13: ; preds = %5, %114, %108, %104
%14 = tail call i32 (...) @SendI2CStop()
%15 = tail call i32 @gfx_delay_milliseconds(i32 10)
%16 = tail call i32 (...) @SendI2CStart()
%17 = tail call i32 @SendI2CData(i8 zeroext %6)
%18 = tail call i32 (...) @ReceiveI2CAck()
%19 = icmp eq i32 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %13
%21 = tail call i32 @SendI2CData(i8 zeroext %2)
%22 = tail call i32 (...) @ReceiveI2CAck()
%23 = icmp eq i32 %22, 0
br i1 %23, label %35, label %24
24: ; preds = %20
%25 = load i8, i8* %4
%26 = tail call i32 @SendI2CData(i8 zeroext %25)
%27 = tail call i32 (...) @ReceiveI2CAck()
%28 = icmp eq i32 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %24
br i1 %7, label %30, label %119
30: ; preds = %29
%31 = load i8, i8* %8
%32 = tail call i32 @SendI2CData(i8 zeroext %31)
%33 = tail call i32 (...) @ReceiveI2CAck()
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %119
35: ; preds = %30, %24, %20, %13
%36 = tail call i32 (...) @SendI2CStop()
%37 = tail call i32 @gfx_delay_milliseconds(i32 10)
%38 = tail call i32 (...) @SendI2CStart()
%39 = tail call i32 @SendI2CData(i8 zeroext %6)
%40 = tail call i32 (...) @ReceiveI2CAck()
%41 = icmp eq i32 %40, 0
br i1 %41, label %57, label %42
42: ; preds = %35
%43 = tail call i32 @SendI2CData(i8 zeroext %2)
%44 = tail call i32 (...) @ReceiveI2CAck()
%45 = icmp eq i32 %44, 0
br i1 %45, label %57, label %46
46: ; preds = %42
%47 = load i8, i8* %4
%48 = tail call i32 @SendI2CData(i8 zeroext %47)
%49 = tail call i32 (...) @ReceiveI2CAck()
%50 = icmp eq i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %46
br i1 %7, label %52, label %119
52: ; preds = %51
%53 = load i8, i8* %8
%54 = tail call i32 @SendI2CData(i8 zeroext %53)
%55 = tail call i32 (...) @ReceiveI2CAck()
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %119
57: ; preds = %52, %46, %42, %35
%58 = tail call i32 (...) @SendI2CStop()
%59 = tail call i32 @gfx_delay_milliseconds(i32 10)
%60 = tail call i32 (...) @SendI2CStart()
%61 = tail call i32 @SendI2CData(i8 zeroext %6)
%62 = tail call i32 (...) @ReceiveI2CAck()
%63 = icmp eq i32 %62, 0
br i1 %63, label %79, label %64
64: ; preds = %57
%65 = tail call i32 @SendI2CData(i8 zeroext %2)
%66 = tail call i32 (...) @ReceiveI2CAck()
%67 = icmp eq i32 %66, 0
br i1 %67, label %79, label %68
68: ; preds = %64
%69 = load i8, i8* %4
%70 = tail call i32 @SendI2CData(i8 zeroext %69)
%71 = tail call i32 (...) @ReceiveI2CAck()
%72 = icmp eq i32 %71, 0
br i1 %72, label %79, label %73
73: ; preds = %68
br i1 %7, label %74, label %119
74: ; preds = %73
%75 = load i8, i8* %8
%76 = tail call i32 @SendI2CData(i8 zeroext %75)
%77 = tail call i32 (...) @ReceiveI2CAck()
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %119
79: ; preds = %74, %68, %64, %57
%80 = tail call i32 (...) @SendI2CStop()
%81 = tail call i32 @gfx_delay_milliseconds(i32 10)
%82 = tail call i32 (...) @SendI2CStart()
%83 = tail call i32 @SendI2CData(i8 zeroext %6)
%84 = tail call i32 (...) @ReceiveI2CAck()
%85 = icmp eq i32 %84, 0
br i1 %85, label %101, label %86
86: ; preds = %79
%87 = tail call i32 @SendI2CData(i8 zeroext %2)
%88 = tail call i32 (...) @ReceiveI2CAck()
%89 = icmp eq i32 %88, 0
br i1 %89, label %101, label %90
90: ; preds = %86
%91 = load i8, i8* %4
%92 = tail call i32 @SendI2CData(i8 zeroext %91)
%93 = tail call i32 (...) @ReceiveI2CAck()
%94 = icmp eq i32 %93, 0
br i1 %94, label %101, label %95
95: ; preds = %90
br i1 %7, label %96, label %119
96: ; preds = %95
%97 = load i8, i8* %8
%98 = tail call i32 @SendI2CData(i8 zeroext %97)
%99 = tail call i32 (...) @ReceiveI2CAck()
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %119
101: ; preds = %96, %90, %86, %79
%102 = tail call i32 (...) @SendI2CStop()
%103 = tail call i32 @gfx_delay_milliseconds(i32 10)
br label %121
104: ; preds = %5
%105 = tail call i32 @SendI2CData(i8 zeroext %2)
%106 = tail call i32 (...) @ReceiveI2CAck()
%107 = icmp eq i32 %106, 0
br i1 %107, label %13, label %108
108: ; preds = %104
%109 = load i8, i8* %4
%110 = tail call i32 @SendI2CData(i8 zeroext %109)
%111 = tail call i32 (...) @ReceiveI2CAck()
%112 = icmp eq i32 %111, 0
br i1 %112, label %13, label %113
113: ; preds = %108
br i1 %7, label %114, label %119
114: ; preds = %113
%115 = load i8, i8* %8
%116 = tail call i32 @SendI2CData(i8 zeroext %115)
%117 = tail call i32 (...) @ReceiveI2CAck()
%118 = icmp eq i32 %117, 0
br i1 %118, label %13, label %119
119: ; preds = %96, %95, %74, %73, %52, %51, %30, %29, %114, %113
%120 = tail call i32 (...) @SendI2CStop()
br label %121
121: ; preds = %101, %119
ret i32 0
}