#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 */
struct i2c_rdwr_ioctl_data {
  int nmsgs;
  struct i2c_msg *msgs;
};
struct i2c_msg {
  unsigned int addr;
  int len;
  char *buf;
  scalar_t__ flags;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int eeprom_write(int fd, unsigned int addr, unsigned int offset,
                 unsigned char *buf, unsigned char len) {
  struct i2c_rdwr_ioctl_data msg_rdwr;
  struct i2c_msg i2cmsg;
  int i;
  char _buf[8 + 1];
  if (len > 8) {
    fprintf(stderr, "I can only write MAX_BYTES bytes at a time!\n");
    return -1;
  }
  if (len + offset > 256) {
    fprintf(stderr, "Sorry, len(%d)+offset(%d) > 256 (page boundary)\n", len,
            offset);
    return -1;
  }
  _buf[0] = offset;
  for (i = 0; i < len; i++)
    _buf[1 + i] = buf[i];
  msg_rdwr.msgs = &i2cmsg;
  msg_rdwr.nmsgs = 1;
  i2cmsg.addr = addr;
  i2cmsg.flags = 0;
  i2cmsg.len = 1 + len;
  i2cmsg.buf = _buf;
  if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
    perror("ioctl()");
    fprintf(stderr, "ioctl returned %d\n", i);
    return -1;
  }
  if (len > 0)
    fprintf(stderr, "Wrote %d bytes to eeprom at 0x%02x, offset %08x\n", len,
            addr, offset);
  else
    fprintf(stderr, "Positioned pointer in eeprom at 0x%02x to offset %08x\n",
            addr, offset);
  return 0;
}


x86 assembly (clang, -O0):
<eeprom_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %r8b,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x8,%eax
jle    117b <eeprom_write+0x4b>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <eeprom_write+0x1a2>
movzbl -0x19(%rbp),%eax
add    -0x10(%rbp),%eax
cmp    $0x100,%eax
jbe    11b7 <eeprom_write+0x87>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
movzbl -0x19(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <eeprom_write+0x1a2>
mov    -0x10(%rbp),%eax
mov    %al,-0x55(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
movzbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jge    11f8 <eeprom_write+0xc8>
mov    -0x18(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
cltq
mov    %cl,-0x55(%rbp,%rax,1)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11c4 <eeprom_write+0x94>
lea    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x38(%rbp)
movzbl -0x19(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
lea    -0x55(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    127a <eeprom_write+0x14a>
lea    0xe0f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x4c(%rbp),%edx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <eeprom_write+0x1a2>
movzbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jle    12ae <eeprom_write+0x17e>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
movzbl -0x19(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12cb <eeprom_write+0x19b>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eeprom_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%ebx
cmp    $0x9,%bl
jb     1173 <eeprom_write+0x33>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    124e <eeprom_write+0x10e>
mov    %edx,%r14d
lea    (%rbx,%r14,1),%eax
cmp    $0x101,%eax
jb     11a7 <eeprom_write+0x67>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe9c(%rip),%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    124e <eeprom_write+0x10e>
mov    %esi,%r15d
mov    %edi,%ebp
mov    %r14b,0x7(%rsp)
test   %bl,%bl
je     11c5 <eeprom_write+0x85>
lea    0x8(%rsp),%rdi
movzbl %bl,%edx
mov    %rcx,%rsi
call   1050 <memcpy@plt>
lea    0x10(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x1,0x28(%rsp)
mov    %r15d,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebx,%eax
add    $0x1,%eax
mov    %eax,0x14(%rsp)
lea    0x7(%rsp),%rax
mov    %rax,0x18(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     125b <eeprom_write+0x11b>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
test   %bl,%bl
je     1238 <eeprom_write+0xf8>
lea    0xe54(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edx
mov    %r15d,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    124e <eeprom_write+0x10e>
lea    0xe6b(%rip),%rsi
xor    %ebp,%ebp
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xdfa(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    124e <eeprom_write+0x10e>

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i64 }
@stderr = global i32 0
@.str = [45 x i8] c"I can only write MAX_BYTES bytes at a time!\0A\00"
@.str.1 = [49 x i8] c"Sorry, len(%d)+offset(%d) > 256 (page boundary)\0A\00"
@I2C_RDWR = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [19 x i8] c"ioctl returned %d\0A\00"
@.str.4 = [49 x i8] c"Wrote %d bytes to eeprom at 0x%02x, offset %08x\0A\00"
@.str.5 = [55 x i8] c"Positioned pointer in eeprom at 0x%02x to offset %08x\0A\00"
define i32 @eeprom_write(i32 %0, i32 %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
%12 = alloca %struct.i2c_rdwr_ioctl_data
%13 = alloca %struct.i2c_msg
%14 = alloca i32
%15 = alloca [9 x i8]
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8 %4, i8* %11
%16 = load i8, i8* %11
%17 = zext i8 %16 to i32
%18 = icmp sgt i32 %17, 8
br i1 %18, label %19, label %22
19: ; preds = %5
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %94
22: ; preds = %5
%23 = load i8, i8* %11
%24 = zext i8 %23 to i32
%25 = load i32, i32* %9
%26 = add i32 %24, %25
%27 = icmp ugt i32 %26, 256
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8, i8* %11
%31 = zext i8 %30 to i32
%32 = load i32, i32* %9
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %32)
store i32 -1, i32* %6
br label %94
34: ; preds = %22
%35 = load i32, i32* %9
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %15, i64 0, i64 0
store i8 %36, i8* %37
store i32 0, i32* %14
br label %38
38: ; preds = %53, %34
%39 = load i32, i32* %14
%40 = load i8, i8* %11
%41 = zext i8 %40 to i32
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %56
43: ; preds = %38
%44 = load i8*, i8** %10
%45 = load i32, i32* %14
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = load i32, i32* %14
%50 = add nsw i32 1, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [9 x i8], [9 x i8]* %15, i64 0, i64 %51
store i8 %48, i8* %52
br label %53
53: ; preds = %43
%54 = load i32, i32* %14
%55 = add nsw i32 %54, 1
store i32 %55, i32* %14
br label %38
56: ; preds = %38
%57 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
store %struct.i2c_msg* %13, %struct.i2c_msg** %57
%58 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 0
store i32 1, i32* %58
%59 = load i32, i32* %8
%60 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 0
store i32 %59, i32* %60
%61 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 3
store i64 0, i64* %61
%62 = load i8, i8* %11
%63 = zext i8 %62 to i32
%64 = add nsw i32 1, %63
%65 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 1
store i32 %64, i32* %65
%66 = getelementptr inbounds [9 x i8], [9 x i8]* %15, i64 0, i64 0
%67 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 2
store i8* %66, i8** %67
%68 = load i32, i32* %7
%69 = load i32, i32* @I2C_RDWR
%70 = call i32 @ioctl(i32 %68, i32 %69, %struct.i2c_rdwr_ioctl_data* %12)
store i32 %70, i32* %14
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %56
%73 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%74 = load i32, i32* @stderr
%75 = load i32, i32* %14
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %75)
store i32 -1, i32* %6
br label %94
77: ; preds = %56
%78 = load i8, i8* %11
%79 = zext i8 %78 to i32
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %88
81: ; preds = %77
%82 = load i32, i32* @stderr
%83 = load i8, i8* %11
%84 = zext i8 %83 to i32
%85 = load i32, i32* %8
%86 = load i32, i32* %9
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %84, i32 %85, i32 %86)
br label %93
88: ; preds = %77
%89 = load i32, i32* @stderr
%90 = load i32, i32* %8
%91 = load i32, i32* %9
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %90, i32 %91)
br label %93
93: ; preds = %88, %81
store i32 0, i32* %6
br label %94
94: ; preds = %93, %72, %28, %19
%95 = load i32, i32* %6
ret i32 %95
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i64 }
@stderr = global i32 0
@.str = [45 x i8] c"I can only write MAX_BYTES bytes at a time!\0A\00"
@.str.1 = [49 x i8] c"Sorry, len(%d)+offset(%d) > 256 (page boundary)\0A\00"
@I2C_RDWR = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [19 x i8] c"ioctl returned %d\0A\00"
@.str.4 = [49 x i8] c"Wrote %d bytes to eeprom at 0x%02x, offset %08x\0A\00"
@.str.5 = [55 x i8] c"Positioned pointer in eeprom at 0x%02x to offset %08x\0A\00"
define i32 @eeprom_write(i32 %0, i32 %1, i32 %2, i8* nocapture %3, i8 zeroext %4) {
%6 = alloca %struct.i2c_rdwr_ioctl_data
%7 = alloca %struct.i2c_msg
%8 = alloca [9 x i8]
%9 = bitcast %struct.i2c_rdwr_ioctl_data* %6 to i8*
%10 = bitcast %struct.i2c_msg* %7 to i8*
%11 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%12 = zext i8 %4 to i32
%13 = icmp ugt i8 %4, 8
br i1 %13, label %14, label %17
14: ; preds = %5
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %50
17: ; preds = %5
%18 = add i32 %12, %2
%19 = icmp ugt i32 %18, 256
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %2)
br label %50
23: ; preds = %17
%24 = trunc i32 %2 to i8
store i8 %24, i8* %11
%25 = icmp eq i8 %4, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 1
%28 = zext i8 %4 to i64
br label %29
29: ; preds = %26, %23
%30 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 1
store %struct.i2c_msg* %7, %struct.i2c_msg** %30
%31 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 1, i32* %31
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 0
store i32 %1, i32* %32
%33 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 3
store i64 0, i64* %33
%34 = add nuw nsw i32 %12, 1
%35 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 2
store i8* %11, i8** %36
%37 = load i32, i32* @I2C_RDWR
%38 = call i32 @ioctl(i32 %0, i32 %37, %struct.i2c_rdwr_ioctl_data* %6)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %29
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %50
44: ; preds = %29
%45 = load i32, i32* @stderr
br i1 %25, label %48, label %46
46: ; preds = %44
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %1, i32 %2)
br label %50
48: ; preds = %44
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %1, i32 %2)
br label %50
50: ; preds = %46, %48, %40, %20, %14
%51 = phi i32 [ -1, %14 ], [ -1, %20 ], [ -1, %40 ], [ 0, %48 ], [ 0, %46 ]
ret i32 %51
}