#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;
  unsigned char len;
  unsigned char *buf;
  int /*<<< orphan*/ flags;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
scalar_t__ eeprom_write(int, unsigned int, unsigned int, int /*<<< orphan*/ *,
                        int /*<<< orphan*/);
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_read(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;
  if (len > 8) {
    fprintf(stderr, "I can only write MAX_BYTES bytes at a time!\n");
    return -1;
  }
  if (eeprom_write(fd, addr, offset, NULL, 0) < 0)
    return -1;
  msg_rdwr.msgs = &i2cmsg;
  msg_rdwr.nmsgs = 1;
  i2cmsg.addr = addr;
  i2cmsg.flags = I2C_M_RD;
  i2cmsg.len = len;
  i2cmsg.buf = buf;
  if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
    perror("ioctl()");
    fprintf(stderr, "ioctl returned %d\n", i);
    return -1;
  }
  fprintf(stderr, "Read %d bytes from eeprom at 0x%02x, offset %08x\n", len,
          addr, offset);
  return 0;
}


x86 assembly (clang, -O0):
<eeprom_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    118b <eeprom_read+0x4b>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <eeprom_read+0x121>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1060 <eeprom_write@plt>
cmp    $0x0,%rax
jge    11b6 <eeprom_read+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <eeprom_read+0x121>
lea    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    1238 <eeprom_read+0xf8>
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x4c(%rbp),%edx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <eeprom_read+0x121>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
movzbl -0x19(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eeprom_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebp
cmp    $0x9,%bpl
jb     1179 <eeprom_read+0x39>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0xe99(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    11ff <eeprom_read+0xbf>
mov    %rcx,%r13
mov    %edx,%r14d
mov    %esi,%r12d
mov    %edi,%ebx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <eeprom_write@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     11ff <eeprom_read+0xbf>
mov    %rsp,%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x18(%rsp)
mov    %r12d,(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %bpl,0x4(%rsp)
mov    %r13,0x8(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     1211 <eeprom_read+0xd1>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
xor    %r15d,%r15d
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0xe13(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11ff <eeprom_read+0xbf>

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i8, i8*, i32 }
@stderr = global i32 0
@.str = [45 x i8] c"I can only write MAX_BYTES bytes at a time!\0A\00"
@I2C_M_RD = global i32 0
@I2C_RDWR = global i32 0
@.str.1 = [8 x i8] c"ioctl()\00"
@.str.2 = [19 x i8] c"ioctl returned %d\0A\00"
@.str.3 = [50 x i8] c"Read %d bytes from eeprom at 0x%02x, offset %08x\0A\00"
define i32 @eeprom_read(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
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
%15 = load i8, i8* %11
%16 = zext i8 %15 to i32
%17 = icmp sgt i32 %16, 8
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %55
21: ; preds = %5
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = call i64 @eeprom_write(i32 %22, i32 %23, i32 %24, i32* null, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %6
br label %55
28: ; preds = %21
%29 = 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** %29
%30 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 0
store i32 1, i32* %30
%31 = load i32, i32* %8
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @I2C_M_RD
%34 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 3
store i32 %33, i32* %34
%35 = load i8, i8* %11
%36 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 1
store i8 %35, i8* %36
%37 = load i8*, i8** %10
%38 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 2
store i8* %37, i8** %38
%39 = load i32, i32* %7
%40 = load i32, i32* @I2C_RDWR
%41 = call i32 @ioctl(i32 %39, i32 %40, %struct.i2c_rdwr_ioctl_data* %12)
store i32 %41, i32* %14
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %28
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @stderr
%46 = load i32, i32* %14
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 -1, i32* %6
br label %55
48: ; preds = %28
%49 = load i32, i32* @stderr
%50 = load i8, i8* %11
%51 = zext i8 %50 to i32
%52 = load i32, i32* %8
%53 = load i32, i32* %9
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %52, i32 %53)
store i32 0, i32* %6
br label %55
55: ; preds = %48, %43, %27, %18
%56 = load i32, i32* %6
ret i32 %56
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i8, i8*, i32 }
@stderr = global i32 0
@.str = [45 x i8] c"I can only write MAX_BYTES bytes at a time!\0A\00"
@I2C_M_RD = global i32 0
@I2C_RDWR = global i32 0
@.str.1 = [8 x i8] c"ioctl()\00"
@.str.2 = [19 x i8] c"ioctl returned %d\0A\00"
@.str.3 = [50 x i8] c"Read %d bytes from eeprom at 0x%02x, offset %08x\0A\00"
define i32 @eeprom_read(i32 %0, i32 %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca %struct.i2c_rdwr_ioctl_data
%7 = alloca %struct.i2c_msg
%8 = bitcast %struct.i2c_rdwr_ioctl_data* %6 to i8*
%9 = bitcast %struct.i2c_msg* %7 to i8*
%10 = zext i8 %4 to i32
%11 = icmp ugt i8 %4, 8
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %36
15: ; preds = %5
%16 = tail call i64 @eeprom_write(i32 %0, i32 %1, i32 %2, i32* null, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %36, label %18
18: ; preds = %15
%19 = 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** %19
%20 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 1, i32* %20
%21 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 0
store i32 %1, i32* %21
%22 = load i32, i32* @I2C_M_RD
%23 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 3
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 1
store i8 %4, i8* %24
%25 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 2
store i8* %3, i8** %25
%26 = load i32, i32* @I2C_RDWR
%27 = call i32 @ioctl(i32 %0, i32 %26, %struct.i2c_rdwr_ioctl_data* %6)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %36
33: ; preds = %18
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 %1, i32 %2)
br label %36
36: ; preds = %15, %33, %29, %12
%37 = phi i32 [ -1, %12 ], [ -1, %29 ], [ 0, %33 ], [ -1, %15 ]
ret i32 %37
}