#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*/ OS_mdelay(int);
int receive_i2c_ack(int *);
int send_i2c_data(unsigned char);
int send_i2c_start();
int send_i2c_stop();
int I2C_write(unsigned int address, unsigned int reg, unsigned long value,
              unsigned int bytes) {
  int err;
  int restart_count = 0;
  int ack;
  unsigned int b;
  int nack;
  while (restart_count++ < 5) {
    err = send_i2c_start();
    if (err)
      return err;
    err = send_i2c_data((char)((address << 1) | 0x00));
    if (err)
      return err;
    err = receive_i2c_ack(&ack);
    if (err)
      return err;
    if (!ack) {
      {}
      err = send_i2c_stop();
      if (err)
        return err;
      OS_mdelay(10);
      continue;
    }
    err = send_i2c_data((unsigned char)reg);
    if (err)
      return err;
    err = receive_i2c_ack(&ack);
    if (err)
      return err;
    if (!ack) {
      {}
      err = send_i2c_stop();
      if (err)
        return err;
      OS_mdelay(10);
      continue;
    }
    nack = 0;
    for (b = 0; b < bytes; b++) {
      err = send_i2c_data((unsigned char)(value >> (b * 8)));
      if (err)
        return err;
      err = receive_i2c_ack(&ack);
      if (err)
        return err;
      if (!ack) {
        {}
        err = send_i2c_stop();
        if (err)
          return err;
        OS_mdelay(10);
        nack = 1;
        break;
      }
    }
    if (nack)
      continue;
    err = send_i2c_stop();
    if (err)
      return err;
    return 0;
  }
  {}
  return 0x1122;
}


x86 assembly (clang, -O0):
<I2C_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cmp    $0x5,%eax
jge    1393 <I2C_write+0x243>
mov    $0x0,%al
call   1070 <send_i2c_start@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     119f <I2C_write+0x4f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
or     $0x0,%eax
movzbl %al,%edi
call   1050 <send_i2c_data@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11c8 <I2C_write+0x78>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
lea    -0x28(%rbp),%rdi
call   1030 <receive_i2c_ack@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e9 <I2C_write+0x99>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
cmpl   $0x0,-0x28(%rbp)
jne    1221 <I2C_write+0xd1>
mov    $0x0,%al
call   1040 <send_i2c_stop@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1212 <I2C_write+0xc2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
mov    $0xa,%edi
call   1060 <OS_mdelay@plt>
jmp    116c <I2C_write+0x1c>
mov    -0xc(%rbp),%eax
movzbl %al,%edi
call   1050 <send_i2c_data@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1244 <I2C_write+0xf4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
lea    -0x28(%rbp),%rdi
call   1030 <receive_i2c_ack@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1265 <I2C_write+0x115>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
cmpl   $0x0,-0x28(%rbp)
jne    129d <I2C_write+0x14d>
mov    $0x0,%al
call   1040 <send_i2c_stop@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     128e <I2C_write+0x13e>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
mov    $0xa,%edi
call   1060 <OS_mdelay@plt>
jmp    116c <I2C_write+0x1c>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1359 <I2C_write+0x209>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
movzbl %al,%edi
call   1050 <send_i2c_data@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12e6 <I2C_write+0x196>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
lea    -0x28(%rbp),%rdi
call   1030 <receive_i2c_ack@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1307 <I2C_write+0x1b7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
cmpl   $0x0,-0x28(%rbp)
jne    1346 <I2C_write+0x1f6>
mov    $0x0,%al
call   1040 <send_i2c_stop@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1330 <I2C_write+0x1e0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
mov    $0xa,%edi
call   1060 <OS_mdelay@plt>
movl   $0x1,-0x30(%rbp)
jmp    1359 <I2C_write+0x209>
jmp    134b <I2C_write+0x1fb>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ab <I2C_write+0x15b>
cmpl   $0x0,-0x30(%rbp)
je     1368 <I2C_write+0x218>
jmp    116c <I2C_write+0x1c>
mov    $0x0,%al
call   1040 <send_i2c_stop@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1387 <I2C_write+0x237>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
movl   $0x0,-0x4(%rbp)
jmp    139a <I2C_write+0x24a>
movl   $0x1122,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<I2C_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%r12d
add    %dil,%dil
mov    %ecx,%ebp
shl    $0x3,%rbp
mov    $0x1,%r14d
movzbl %dil,%eax
mov    %eax,0x14(%rsp)
lea    0xc(%rsp),%r13
xor    %eax,%eax
call   1070 <send_i2c_start@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
mov    0x14(%rsp),%edi
call   1050 <send_i2c_data@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
mov    %r13,%rdi
call   1030 <receive_i2c_ack@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
cmpl   $0x0,0xc(%rsp)
je     1240 <I2C_write+0xf0>
movzbl %r12b,%edi
call   1050 <send_i2c_data@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
mov    %r13,%rdi
call   1030 <receive_i2c_ack@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
cmpl   $0x0,0xc(%rsp)
je     1240 <I2C_write+0xf0>
mov    %r12d,0x10(%rsp)
test   %r15d,%r15d
je     127a <I2C_write+0x12a>
mov    %r15d,%r12d
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r15d,%ecx
and    $0xf8,%cl
mov    %rbx,%rax
shr    %cl,%rax
movzbl %al,%edi
call   1050 <send_i2c_data@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
mov    %r13,%rdi
call   1030 <receive_i2c_ack@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
cmpl   $0x0,0xc(%rsp)
je     1265 <I2C_write+0x115>
add    $0x8,%r15
cmp    %r15,%rbp
jne    1200 <I2C_write+0xb0>
jmp    127a <I2C_write+0x12a>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <send_i2c_stop@plt>
test   %eax,%eax
jne    1281 <I2C_write+0x131>
mov    $0xa,%edi
call   1060 <OS_mdelay@plt>
add    $0x1,%r14d
cmp    $0x6,%r14d
jne    1183 <I2C_write+0x33>
jmp    1290 <I2C_write+0x140>
xor    %eax,%eax
call   1040 <send_i2c_stop@plt>
test   %eax,%eax
mov    %r12d,%r15d
mov    0x10(%rsp),%r12d
je     124b <I2C_write+0xfb>
jmp    1281 <I2C_write+0x131>
xor    %eax,%eax
call   1040 <send_i2c_stop@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1122,%eax
jmp    1281 <I2C_write+0x131>

LLVM IR (-O0):
define i32 @I2C_write(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i32 0, i32* %11
br label %15
15: ; preds = %118, %75, %50, %4
%16 = load i32, i32* %11
%17 = add nsw i32 %16, 1
store i32 %17, i32* %11
%18 = icmp slt i32 %16, 5
br i1 %18, label %19, label %126
19: ; preds = %15
%20 = call i32 (...) @send_i2c_start()
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %10
store i32 %24, i32* %5
br label %127
25: ; preds = %19
%26 = load i32, i32* %6
%27 = shl i32 %26, 1
%28 = or i32 %27, 0
%29 = trunc i32 %28 to i8
%30 = call i32 @send_i2c_data(i8 zeroext %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = load i32, i32* %10
store i32 %34, i32* %5
br label %127
35: ; preds = %25
%36 = call i32 @receive_i2c_ack(i32* %12)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %127
41: ; preds = %35
%42 = load i32, i32* %12
%43 = icmp ne i32 %42, 0
br i1 %43, label %52, label %44
44: ; preds = %41
%45 = call i32 (...) @send_i2c_stop()
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %10
store i32 %49, i32* %5
br label %127
50: ; preds = %44
%51 = call i32 @OS_mdelay(i32 10)
br label %15
52: ; preds = %41
%53 = load i32, i32* %7
%54 = trunc i32 %53 to i8
%55 = call i32 @send_i2c_data(i8 zeroext %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = load i32, i32* %10
store i32 %59, i32* %5
br label %127
60: ; preds = %52
%61 = call i32 @receive_i2c_ack(i32* %12)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = load i32, i32* %10
store i32 %65, i32* %5
br label %127
66: ; preds = %60
%67 = load i32, i32* %12
%68 = icmp ne i32 %67, 0
br i1 %68, label %77, label %69
69: ; preds = %66
%70 = call i32 (...) @send_i2c_stop()
store i32 %70, i32* %10
%71 = load i32, i32* %10
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = load i32, i32* %10
store i32 %74, i32* %5
br label %127
75: ; preds = %69
%76 = call i32 @OS_mdelay(i32 10)
br label %15
77: ; preds = %66
store i32 0, i32* %14
store i32 0, i32* %13
br label %78
78: ; preds = %112, %77
%79 = load i32, i32* %13
%80 = load i32, i32* %9
%81 = icmp ult i32 %79, %80
br i1 %81, label %82, label %115
82: ; preds = %78
%83 = load i64, i64* %8
%84 = load i32, i32* %13
%85 = mul i32 %84, 8
%86 = zext i32 %85 to i64
%87 = lshr i64 %83, %86
%88 = trunc i64 %87 to i8
%89 = call i32 @send_i2c_data(i8 zeroext %88)
store i32 %89, i32* %10
%90 = load i32, i32* %10
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %82
%93 = load i32, i32* %10
store i32 %93, i32* %5
br label %127
94: ; preds = %82
%95 = call i32 @receive_i2c_ack(i32* %12)
store i32 %95, i32* %10
%96 = load i32, i32* %10
%97 = icmp ne i32 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %94
%99 = load i32, i32* %10
store i32 %99, i32* %5
br label %127
100: ; preds = %94
%101 = load i32, i32* %12
%102 = icmp ne i32 %101, 0
br i1 %102, label %111, label %103
103: ; preds = %100
%104 = call i32 (...) @send_i2c_stop()
store i32 %104, i32* %10
%105 = load i32, i32* %10
%106 = icmp ne i32 %105, 0
br i1 %106, label %107, label %109
107: ; preds = %103
%108 = load i32, i32* %10
store i32 %108, i32* %5
br label %127
109: ; preds = %103
%110 = call i32 @OS_mdelay(i32 10)
store i32 1, i32* %14
br label %115
111: ; preds = %100
br label %112
112: ; preds = %111
%113 = load i32, i32* %13
%114 = add i32 %113, 1
store i32 %114, i32* %13
br label %78
115: ; preds = %109, %78
%116 = load i32, i32* %14
%117 = icmp ne i32 %116, 0
br i1 %117, label %118, label %119
118: ; preds = %115
br label %15
119: ; preds = %115
%120 = call i32 (...) @send_i2c_stop()
store i32 %120, i32* %10
%121 = load i32, i32* %10
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %125
123: ; preds = %119
%124 = load i32, i32* %10
store i32 %124, i32* %5
br label %127
125: ; preds = %119
store i32 0, i32* %5
br label %127
126: ; preds = %15
store i32 4386, i32* %5
br label %127
127: ; preds = %126, %125, %123, %107, %98, %92, %73, %64, %58, %48, %39, %33, %23
%128 = load i32, i32* %5
ret i32 %128
}

LLVM IR (-O2):
define i32 @I2C_write(i32 %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = trunc i32 %0 to i8
%8 = shl i8 %7, 1
%9 = trunc i32 %1 to i8
%10 = icmp eq i32 %3, 0
%11 = zext i32 %3 to i64
br label %12
12: ; preds = %4, %28
%13 = phi i32 [ 1, %4 ], [ %30, %28 ]
%14 = call i32 (...) @send_i2c_start()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %67
16: ; preds = %12
%17 = call i32 @send_i2c_data(i8 zeroext %8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %67
19: ; preds = %16
%20 = call i32 @receive_i2c_ack(i32* %5)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %67
22: ; preds = %19
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = call i32 (...) @send_i2c_stop()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %67
28: ; preds = %25, %62, %42
%29 = call i32 @OS_mdelay(i32 10)
%30 = add nuw nsw i32 %13, 1
%31 = icmp eq i32 %30, 6
br i1 %31, label %67, label %12
32: ; preds = %22
%33 = call i32 @send_i2c_data(i8 zeroext %9)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %67
35: ; preds = %32
%36 = call i32 @receive_i2c_ack(i32* %5)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %67
38: ; preds = %35
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %38
br i1 %10, label %65, label %47
42: ; preds = %38
%43 = call i32 (...) @send_i2c_stop()
%44 = icmp eq i32 %43, 0
br i1 %44, label %28, label %67
45: ; preds = %58
%46 = icmp eq i64 %61, %11
br i1 %46, label %65, label %47
47: ; preds = %41, %45
%48 = phi i64 [ %61, %45 ], [ 0, %41 ]
%49 = shl i64 %48, 3
%50 = and i64 %49, 4294967288
%51 = lshr i64 %2, %50
%52 = trunc i64 %51 to i8
%53 = call i32 @send_i2c_data(i8 zeroext %52)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %47
%56 = call i32 @receive_i2c_ack(i32* %5)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %67
58: ; preds = %55
%59 = load i32, i32* %5
%60 = icmp eq i32 %59, 0
%61 = add nuw nsw i64 %48, 1
br i1 %60, label %62, label %45
62: ; preds = %58
%63 = call i32 (...) @send_i2c_stop()
%64 = icmp eq i32 %63, 0
br i1 %64, label %28, label %67
65: ; preds = %41, %45
%66 = call i32 (...) @send_i2c_stop()
br label %67
67: ; preds = %28, %62, %42, %35, %32, %25, %19, %16, %12, %55, %47, %65
%68 = phi i32 [ %66, %65 ], [ %56, %55 ], [ %53, %47 ], [ 4386, %28 ], [ %63, %62 ], [ %43, %42 ], [ %36, %35 ], [ %33, %32 ], [ %26, %25 ], [ %20, %19 ], [ %17, %16 ], [ %14, %12 ]
ret i32 %68
}