#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 */
typedef unsigned int blk_off;

/* Variables and functions */
scalar_t__ i2c_write(int, unsigned char, unsigned int, unsigned char *,
                     unsigned int);
int /*<<< orphan*/ usleep(int);
int eeprom_write(int fd, unsigned dev_addr, unsigned offset,
                 unsigned char *buffer, unsigned cnt) {
  unsigned end = offset + cnt;
  unsigned blk_off;
  int rcode = 0;
  while (offset < end) {
    unsigned alen, len, maxlen;
    unsigned char addr[2];
    blk_off = offset & 0xFF;
    addr[0] = offset >> 8;
    addr[1] = blk_off;
    alen = 2;
    addr[0] |= dev_addr;
    maxlen = (1 << 4) - ((blk_off) & ((1 << 4) - 1));
    len = end - offset;
    if (i2c_write(fd, addr[0], offset, buffer, len) != 0)
      rcode = 1;
    buffer += len;
    offset += len;
    usleep(11 * 1000);
  }
  return rcode;
}


x86 assembly (clang, -O0):
<eeprom_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    11ed <eeprom_write+0xcd>
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x8,%eax
mov    %al,-0x36(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,-0x35(%rbp)
movl   $0x2,-0x2c(%rbp)
mov    -0x8(%rbp),%ecx
movzbl -0x36(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x36(%rbp)
mov    -0x24(%rbp),%ecx
and    $0xf,%ecx
mov    $0x10,%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x36(%rbp),%al
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%r8d
movzbl %al,%esi
call   1030 <i2c_write@plt>
cmp    $0x0,%rax
je     11c5 <eeprom_write+0xa5>
movl   $0x1,-0x28(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x2af8,%edi
call   1040 <usleep@plt>
jmp    1149 <eeprom_write+0x29>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eeprom_write>:
push   %rbx
lea    (%r8,%rdx,1),%eax
xor    %ebx,%ebx
cmp    %edx,%eax
jbe    114c <eeprom_write+0x2c>
mov    %edx,%eax
shr    $0x8,%eax
or     %esi,%eax
movzbl %al,%esi
call   1030 <i2c_write@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    $0x2af8,%edi
call   1040 <usleep@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @eeprom_write(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [2 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%18 = load i32, i32* %8
%19 = load i32, i32* %10
%20 = add i32 %18, %19
store i32 %20, i32* %11
store i32 0, i32* %13
br label %21
21: ; preds = %56, %5
%22 = load i32, i32* %8
%23 = load i32, i32* %11
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %65
25: ; preds = %21
%26 = load i32, i32* %8
%27 = and i32 %26, 255
store i32 %27, i32* %12
%28 = load i32, i32* %8
%29 = lshr i32 %28, 8
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %17, i64 0, i64 0
store i8 %30, i8* %31
%32 = load i32, i32* %12
%33 = trunc i32 %32 to i8
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %17, i64 0, i64 1
store i8 %33, i8* %34
store i32 2, i32* %14
%35 = load i32, i32* %7
%36 = getelementptr inbounds [2 x i8], [2 x i8]* %17, i64 0, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = or i32 %38, %35
%40 = trunc i32 %39 to i8
store i8 %40, i8* %36
%41 = load i32, i32* %12
%42 = and i32 %41, 15
%43 = sub i32 16, %42
store i32 %43, i32* %16
%44 = load i32, i32* %11
%45 = load i32, i32* %8
%46 = sub i32 %44, %45
store i32 %46, i32* %15
%47 = load i32, i32* %6
%48 = getelementptr inbounds [2 x i8], [2 x i8]* %17, i64 0, i64 0
%49 = load i8, i8* %48
%50 = load i32, i32* %8
%51 = load i8*, i8** %9
%52 = load i32, i32* %15
%53 = call i64 @i2c_write(i32 %47, i8 zeroext %49, i32 %50, i8* %51, i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %25
store i32 1, i32* %13
br label %56
56: ; preds = %55, %25
%57 = load i32, i32* %15
%58 = load i8*, i8** %9
%59 = zext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %9
%61 = load i32, i32* %15
%62 = load i32, i32* %8
%63 = add i32 %62, %61
store i32 %63, i32* %8
%64 = call i32 @usleep(i32 11000)
br label %21
65: ; preds = %21
%66 = load i32, i32* %13
ret i32 %66
}

LLVM IR (-O2):
define i32 @eeprom_write(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = add i32 %4, %2
%7 = icmp ugt i32 %6, %2
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = lshr i32 %2, 8
%10 = or i32 %9, %1
%11 = trunc i32 %10 to i8
%12 = tail call i64 @i2c_write(i32 %0, i8 zeroext %11, i32 %2, i8* %3, i32 %4)
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @usleep(i32 11000)
br label %16
16: ; preds = %8, %5
%17 = phi i32 [ 0, %5 ], [ %14, %8 ]
ret i32 %17
}