#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 int /*<<< orphan*/ uint8_t;
typedef scalar_t__ uint16_t;

/* Variables and functions */
scalar_t__ get_trans_len(scalar_t__, scalar_t__);
int write_eeprom_page(int, scalar_t__, int /*<<< orphan*/ *, scalar_t__);
int write_eeprom(int fd, uint16_t offset, uint8_t *buf, uint16_t count) {
  uint16_t len = count;
  int rc = 0;
  while (len > 0) {
    uint16_t this_trans_len = get_trans_len(offset, len);
    rc = write_eeprom_page(fd, offset, buf, this_trans_len);
    if (rc < 0)
      return rc;
    buf += this_trans_len;
    offset += this_trans_len;
    len -= this_trans_len;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_eeprom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    11be <write_eeprom+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <get_trans_len@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1030 <write_eeprom_page@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118e <write_eeprom+0x6e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <write_eeprom+0xa5>
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1146 <write_eeprom+0x26>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_eeprom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rcx,%rcx
jle    1175 <write_eeprom+0x55>
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %edi,%r12d
nop
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <get_trans_len@plt>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
mov    %rax,%rcx
call   1030 <write_eeprom_page@plt>
test   %eax,%eax
js     1172 <write_eeprom+0x52>
lea    0x0(%r13,%rbp,4),%r13
add    %rbp,%rbx
sub    %rbp,%r15
jg     1140 <write_eeprom+0x20>
jmp    1175 <write_eeprom+0x55>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_eeprom(i32 %0, i64 %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i64 %3, i64* %9
%13 = load i64, i64* %9
store i64 %13, i64* %10
store i32 0, i32* %11
br label %14
14: ; preds = %30, %4
%15 = load i64, i64* %10
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %14
%18 = load i64, i64* %7
%19 = load i64, i64* %10
%20 = call i64 @get_trans_len(i64 %18, i64 %19)
store i64 %20, i64* %12
%21 = load i32, i32* %6
%22 = load i64, i64* %7
%23 = load i32*, i32** %8
%24 = load i64, i64* %12
%25 = call i32 @write_eeprom_page(i32 %21, i64 %22, i32* %23, i64 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = load i32, i32* %11
store i32 %29, i32* %5
br label %41
30: ; preds = %17
%31 = load i64, i64* %12
%32 = load i32*, i32** %8
%33 = getelementptr inbounds i32, i32* %32, i64 %31
store i32* %33, i32** %8
%34 = load i64, i64* %12
%35 = load i64, i64* %7
%36 = add nsw i64 %35, %34
store i64 %36, i64* %7
%37 = load i64, i64* %12
%38 = load i64, i64* %10
%39 = sub nsw i64 %38, %37
store i64 %39, i64* %10
br label %14
40: ; preds = %14
store i32 0, i32* %5
br label %41
41: ; preds = %40, %28
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
define i32 @write_eeprom(i32 %0, i64 %1, i32* %2, i64 %3) {
%5 = icmp sgt i64 %3, 0
br i1 %5, label %6, label %18
6: ; preds = %4, %13
%7 = phi i64 [ %16, %13 ], [ %3, %4 ]
%8 = phi i64 [ %15, %13 ], [ %1, %4 ]
%9 = phi i32* [ %14, %13 ], [ %2, %4 ]
%10 = tail call i64 @get_trans_len(i64 %8, i64 %7)
%11 = tail call i32 @write_eeprom_page(i32 %0, i64 %8, i32* %9, i64 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %6
%14 = getelementptr inbounds i32, i32* %9, i64 %10
%15 = add nsw i64 %10, %8
%16 = sub nsw i64 %7, %10
%17 = icmp sgt i64 %16, 0
br i1 %17, label %6, label %18
18: ; preds = %13, %6, %4
%19 = phi i32 [ 0, %4 ], [ %11, %6 ], [ 0, %13 ]
ret i32 %19
}