#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int ioctl(int, int, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_eeprom(int, unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ write_eeprom(int, unsigned char *, int /*<<< orphan*/, int);
int main(void) {
  int fd, n, res;
  unsigned char buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
  fd = open("/dev/i2c-0", O_RDWR);
  if (fd < 0) {
    printf("####i2c test device open failed ####\n");
    return (-1);
  }
  res = ioctl(fd, 0x0704, 0);
  res = ioctl(fd, 0x0703, 0x50);
  write_eeprom(fd, buf, 0, sizeof(buf));
  memset(buf, 0, sizeof(buf));
  read_eeprom(fd, buf, 0, sizeof(buf));
  for (n = 0; n < sizeof(buf); n++) {
    printf("0x%x\n", buf[n]);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c6 <main+0x56>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <main+0xf5>
mov    -0x8(%rbp),%edi
mov    $0x704,%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x703,%esi
mov    $0x50,%edx
call   1060 <ioctl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
mov    $0x8,%ecx
call   1090 <write_eeprom@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
mov    $0x8,%ecx
call   1040 <read_eeprom@plt>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x8,%rax
jae    125d <main+0xed>
movslq -0xc(%rbp),%rax
movzbl -0x18(%rbp,%rax,1),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    122a <main+0xba>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x807060504030201,%rax
06 07 08
mov    %rax,0x8(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1296 <main+0x116>
mov    %eax,%ebp
xor    %r14d,%r14d
mov    %eax,%edi
mov    $0x704,%esi
xor    %edx,%edx
call   1070 <ioctl@plt>
mov    %ebp,%edi
mov    $0x703,%esi
mov    $0x50,%edx
call   1070 <ioctl@plt>
lea    0x8(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
mov    $0x8,%ecx
call   10a0 <write_eeprom@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
mov    $0x8,%ecx
call   1050 <read_eeprom@plt>
movzbl 0x8(%rsp),%esi
lea    0xdf1(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0x9(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xa(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xb(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xc(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xd(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xe(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0xf(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    12a8 <main+0x128>
lea    0xd74(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [8 x i8] c"\01\02\03\04\05\06\07\08"
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [38 x i8] c"####i2c test device open failed ####\0A\00"
@.str.2 = [6 x i8] c"0x%x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [8 x i8]
store i32 0, i32* %1
%6 = bitcast [8 x i8]* %5 to i8*
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %43
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @ioctl(i32 %14, i32 1796, i32 0)
store i32 %15, i32* %4
%16 = load i32, i32* %2
%17 = call i32 @ioctl(i32 %16, i32 1795, i32 80)
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%20 = call i32 @write_eeprom(i32 %18, i8* %19, i32 0, i32 8)
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 8)
%23 = load i32, i32* %2
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%25 = call i32 @read_eeprom(i32 %23, i8* %24, i32 0, i32 8)
store i32 0, i32* %3
br label %26
26: ; preds = %37, %13
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %28, 8
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %30
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %26
40: ; preds = %26
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %40, %11
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.2 = [6 x i8] c"0x%x\0A\00"
@str = [37 x i8] c"####i2c test device open failed ####\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to [8 x i8]*
%3 = bitcast i64* %1 to i8*
store i64 578437695752307201, i64* %1
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %48
9: ; preds = %0
%10 = tail call i32 @ioctl(i32 %5, i32 1796, i32 0)
%11 = tail call i32 @ioctl(i32 %5, i32 1795, i32 80)
%12 = call i32 @write_eeprom(i32 %5, i8* %3, i32 0, i32 8)
%13 = call i32 @memset(i8* %3, i32 0, i32 8)
%14 = call i32 @read_eeprom(i32 %5, i8* %3, i32 0, i32 8)
%15 = bitcast i64* %1 to i8*
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 1
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 4
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 5
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 6
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 7
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = call i32 @close(i32 %5)
br label %48
48: ; preds = %9, %7
%49 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %49
}