int i2c_read_array(unsigned char addr, char *buf, int len) {
  int ret;
  int i;
  ret = i2c_select_device(0x50, 0);
  if (ret) {
    __s5p_printf("i2c devices no response.\n");
    goto i2c_select_device_fail;
  }
  __s5p_printf("%p - addr is 0x%p\n", __func__, addr);
  ret = i2c_send_addr(addr);
  if (ret) {
    __s5p_printf("i2c send address fail.\n");
    goto i2c_fail;
  }
  ret = i2c_restart(0x50, 1);
  if (ret) {
    __s5p_printf("i2c restart fail.\n");
    goto i2c_fail;
  }
  for (i = 0; i < len - 1; i++) {
    ret = i2c_receive_data((1 << 0), buf + i);
    if (ret) {
      __s5p_printf("i2c receive data fail, 0x%p bytes success\n", i);
      goto i2c_fail;
    }
  }
  i2c_receive_data((0 << 0), buf + i);
  ret = len;
i2c_fail:
  i2c_stop();
  i2c_trx_resume();
i2c_select_device_fail:
  return ret;
}


x86 assembly (clang, -O0):
<i2c_read_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x50,%edi
xor    %esi,%esi
mov    $0x0,%al
call   1090 <i2c_select_device@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11b3 <i2c_read_array+0x43>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <__s5p_printf@plt>
jmp    12b5 <i2c_read_array+0x145>
movzbl -0x1(%rbp),%edx
lea    0xe5c(%rip),%rdi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1030 <__s5p_printf@plt>
movzbl -0x1(%rbp),%edi
mov    $0x0,%al
call   1050 <i2c_send_addr@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11f7 <i2c_read_array+0x87>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <__s5p_printf@plt>
jmp    12a7 <i2c_read_array+0x137>
mov    $0x50,%edi
mov    $0x1,%esi
mov    $0x0,%al
call   1040 <i2c_restart@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1228 <i2c_read_array+0xb8>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <__s5p_printf@plt>
jmp    12a7 <i2c_read_array+0x137>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    128d <i2c_read_array+0x11d>
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edi
mov    $0x0,%al
call   1060 <i2c_receive_data@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     127a <i2c_read_array+0x10a>
mov    -0x1c(%rbp),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1030 <__s5p_printf@plt>
jmp    12a7 <i2c_read_array+0x137>
jmp    127f <i2c_read_array+0x10f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    122f <i2c_read_array+0xbf>
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
xor    %edi,%edi
mov    $0x0,%al
call   1060 <i2c_receive_data@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <i2c_stop@plt>
mov    $0x0,%al
call   1070 <i2c_trx_resume@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebx
mov    $0x50,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1090 <i2c_select_device@plt>
test   %eax,%eax
je     11a7 <i2c_read_array+0x37>
mov    %eax,%ebp
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1030 <__s5p_printf@plt>
mov    %ebp,%r15d
jmp    120f <i2c_read_array+0x9f>
movzbl %bl,%ebx
lea    0xe69(%rip),%rdi
lea    0xe75(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <__s5p_printf@plt>
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <i2c_send_addr@plt>
test   %eax,%eax
je     11d9 <i2c_read_array+0x69>
mov    %eax,%ebx
lea    0xe65(%rip),%rdi
jmp    11f7 <i2c_read_array+0x87>
mov    $0x50,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <i2c_restart@plt>
test   %eax,%eax
je     121b <i2c_read_array+0xab>
mov    %eax,%ebx
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1030 <__s5p_printf@plt>
mov    %ebx,%r15d
xor    %eax,%eax
call   1080 <i2c_stop@plt>
xor    %eax,%eax
call   1070 <i2c_trx_resume@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x2,%r15d
jl     124f <i2c_read_array+0xdf>
lea    -0x1(%r15),%eax
mov    %eax,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1060 <i2c_receive_data@plt>
test   %eax,%eax
jne    1263 <i2c_read_array+0xf3>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1230 <i2c_read_array+0xc0>
jmp    1252 <i2c_read_array+0xe2>
xor    %r12d,%r12d
add    %r12,%r14
xor    %edi,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <i2c_receive_data@plt>
jmp    1201 <i2c_read_array+0x91>
mov    %eax,%ebp
lea    0xdfb(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <__s5p_printf@plt>
mov    %ebp,%r15d
jmp    1201 <i2c_read_array+0x91>

LLVM IR (-O0):
@.str = [26 x i8] c"i2c devices no response.\0A\00"
@.str.1 = [19 x i8] c"%p - addr is 0x%p\0A\00"
@__func__.i2c_read_array = [15 x i8] c"i2c_read_array\00"
@.str.2 = [24 x i8] c"i2c send address fail.\0A\00"
@.str.3 = [19 x i8] c"i2c restart fail.\0A\00"
@.str.4 = [43 x i8] c"i2c receive data fail, 0x%p bytes success\0A\00"
define i32 @i2c_read_array(i8 zeroext %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8 %0, i8* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i32 (i32, i32, ...) bitcast (i32 (...)* @i2c_select_device to i32 (i32, i32, ...)*)(i32 80, i32 0)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %62
14: ; preds = %3
%15 = load i8, i8* %4
%16 = zext i8 %15 to i32
%17 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__func__.i2c_read_array, i64 0, i64 0), i32 %16)
%18 = load i8, i8* %4
%19 = zext i8 %18 to i32
%20 = call i32 (i32, ...) bitcast (i32 (...)* @i2c_send_addr to i32 (i32, ...)*)(i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %59
25: ; preds = %14
%26 = call i32 (i32, i32, ...) bitcast (i32 (...)* @i2c_restart to i32 (i32, i32, ...)*)(i32 80, i32 1)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %59
31: ; preds = %25
store i32 0, i32* %8
br label %32
32: ; preds = %49, %31
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, 1
%36 = icmp slt i32 %33, %35
br i1 %36, label %37, label %52
37: ; preds = %32
%38 = load i8*, i8** %5
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @i2c_receive_data to i32 (i32, i8*, ...)*)(i32 1, i8* %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* %8
%47 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %59
48: ; preds = %37
br label %49
49: ; preds = %48
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
br label %32
52: ; preds = %32
%53 = load i8*, i8** %5
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = call i32 (i32, i8*, ...) bitcast (i32 (...)* @i2c_receive_data to i32 (i32, i8*, ...)*)(i32 0, i8* %56)
%58 = load i32, i32* %6
store i32 %58, i32* %7
br label %59
59: ; preds = %52, %45, %29, %23
%60 = call i32 (...) @i2c_stop()
%61 = call i32 (...) @i2c_trx_resume()
br label %62
62: ; preds = %59, %12
%63 = load i32, i32* %7
ret i32 %63
}

LLVM IR (-O2):
@.str = [26 x i8] c"i2c devices no response.\0A\00"
@.str.1 = [19 x i8] c"%p - addr is 0x%p\0A\00"
@__func__.i2c_read_array = [15 x i8] c"i2c_read_array\00"
@.str.2 = [24 x i8] c"i2c send address fail.\0A\00"
@.str.3 = [19 x i8] c"i2c restart fail.\0A\00"
@.str.4 = [43 x i8] c"i2c receive data fail, 0x%p bytes success\0A\00"
define i32 @i2c_read_array(i8 zeroext %0, i8* %1, i32 %2) {
%4 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @i2c_select_device to i32 (i32, i32, ...)*)(i32 80, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %46
8: ; preds = %3
%9 = zext i8 %0 to i32
%10 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @__func__.i2c_read_array, i64 0, i64 0), i32 %9)
%11 = tail call i32 (i32, ...) bitcast (i32 (...)* @i2c_send_addr to i32 (i32, ...)*)(i32 %9)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = tail call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %42
15: ; preds = %8
%16 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @i2c_restart to i32 (i32, i32, ...)*)(i32 80, i32 1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = add i32 %2, -1
%20 = icmp sgt i32 %2, 1
br i1 %20, label %21, label %38
21: ; preds = %18
%22 = zext i32 %19 to i64
br label %25
23: ; preds = %15
%24 = tail call i32 (i8*, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %42
25: ; preds = %21, %33
%26 = phi i64 [ 0, %21 ], [ %34, %33 ]
%27 = getelementptr inbounds i8, i8* %1, i64 %26
%28 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @i2c_receive_data to i32 (i32, i8*, ...)*)(i32 1, i8* %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = trunc i64 %26 to i32
%32 = tail call i32 (i8*, i32, ...) bitcast (i32 (...)* @__s5p_printf to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %42
33: ; preds = %25
%34 = add nuw nsw i64 %26, 1
%35 = icmp eq i64 %34, %22
br i1 %35, label %36, label %25
36: ; preds = %33
%37 = zext i32 %19 to i64
br label %38
38: ; preds = %18, %36
%39 = phi i64 [ %37, %36 ], [ 0, %18 ]
%40 = getelementptr inbounds i8, i8* %1, i64 %39
%41 = tail call i32 (i32, i8*, ...) bitcast (i32 (...)* @i2c_receive_data to i32 (i32, i8*, ...)*)(i32 0, i8* %40)
br label %42
42: ; preds = %38, %30, %23, %13
%43 = phi i32 [ %11, %13 ], [ %16, %23 ], [ %28, %30 ], [ %2, %38 ]
%44 = tail call i32 (...) @i2c_stop()
%45 = tail call i32 (...) @i2c_trx_resume()
br label %46
46: ; preds = %42, %6
%47 = phi i32 [ %4, %6 ], [ %43, %42 ]
ret i32 %47
}