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

/* Variables and functions */
int SMBReadBlock(int, int, unsigned char *);
int SMBWriteBlock(int, int, unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int readFlash(int address, int len, unsigned char *buf) {
  int status, i;
  unsigned char chunk[0x10];
  unsigned char setAddr[2];
  if (len % 0x10 != 0)
    return -99;
  for (i = 0; i < len / 0x10; i++) {
    setAddr[0] = (address + i * 0x10) & 0xFF;
    setAddr[1] = ((address + i * 0x10) >> 8) & 0xFF;
    status = SMBWriteBlock(0x16, 0xFF, setAddr, 2);
    if (status != 2)
      return -1;
    status = SMBReadBlock(0x16, 0xFE, chunk);
    if (status != 0x10)
      return status;
    memcpy((buf + (i * 0x10)), chunk, 0x10);
  }
  return len;
}


x86 assembly (clang, -O0):
<readFlash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1162 <readFlash+0x32>
movl   $0xffffff9d,-0x4(%rbp)
jmp    123d <readFlash+0x10d>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jge    1237 <readFlash+0x107>
mov    -0x8(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
and    $0xff,%eax
mov    %al,-0x32(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x31(%rbp)
lea    -0x32(%rbp),%rdx
mov    $0x16,%edi
mov    $0xff,%esi
mov    $0x2,%ecx
call   1030 <SMBWriteBlock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
je     11e1 <readFlash+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <readFlash+0x10d>
lea    -0x30(%rbp),%rdx
mov    $0x16,%edi
mov    $0xfe,%esi
call   1040 <SMBReadBlock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x10,-0x1c(%rbp)
je     120c <readFlash+0xdc>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123d <readFlash+0x10d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%eax
shl    $0x4,%eax
cltq
add    %rax,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1169 <readFlash+0x39>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFlash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    $0xffffff9d,%eax
test   $0xf,%r14b
jne    11cf <readFlash+0x9f>
cmp    $0x10,%r14d
jl     11c5 <readFlash+0x95>
mov    %rdx,%rbx
mov    %edi,%ebp
mov    %r14d,%r13d
shr    $0x4,%r13d
lea    0xe(%rsp),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
mov    %bp,0xe(%rsp)
mov    $0x16,%edi
mov    $0xff,%esi
mov    %r15,%rdx
mov    $0x2,%ecx
call   1030 <SMBWriteBlock@plt>
cmp    $0x2,%eax
jne    11ca <readFlash+0x9a>
mov    $0x16,%edi
mov    $0xfe,%esi
mov    %r12,%rdx
call   1040 <SMBReadBlock@plt>
cmp    $0x10,%eax
jne    11cf <readFlash+0x9f>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
add    $0x10,%ebp
add    $0x10,%rbx
add    $0xffffffffffffffff,%r13
jne    1170 <readFlash+0x40>
mov    %r14d,%eax
jmp    11cf <readFlash+0x9f>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readFlash(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [16 x i8]
%11 = alloca [2 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i32, i32* %6
%13 = srem i32 %12, 16
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -99, i32* %4
br label %63
16: ; preds = %3
store i32 0, i32* %9
br label %17
17: ; preds = %58, %16
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = sdiv i32 %19, 16
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %61
22: ; preds = %17
%23 = load i32, i32* %5
%24 = load i32, i32* %9
%25 = mul nsw i32 %24, 16
%26 = add nsw i32 %23, %25
%27 = and i32 %26, 255
%28 = trunc i32 %27 to i8
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
store i8 %28, i8* %29
%30 = load i32, i32* %5
%31 = load i32, i32* %9
%32 = mul nsw i32 %31, 16
%33 = add nsw i32 %30, %32
%34 = ashr i32 %33, 8
%35 = and i32 %34, 255
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 1
store i8 %36, i8* %37
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%39 = call i32 @SMBWriteBlock(i32 22, i32 255, i8* %38, i32 2)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 2
br i1 %41, label %42, label %43
42: ; preds = %22
store i32 -1, i32* %4
br label %63
43: ; preds = %22
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%45 = call i32 @SMBReadBlock(i32 22, i32 254, i8* %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 16
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %63
50: ; preds = %43
%51 = load i8*, i8** %7
%52 = load i32, i32* %9
%53 = mul nsw i32 %52, 16
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%57 = call i32 @memcpy(i8* %55, i8* %56, i32 16)
br label %58
58: ; preds = %50
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %17
61: ; preds = %17
%62 = load i32, i32* %6
store i32 %62, i32* %4
br label %63
63: ; preds = %61, %48, %42, %15
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
define i32 @readFlash(i32 %0, i32 %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%8 = and i32 %1, 15
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %3
%11 = icmp sgt i32 %1, 15
br i1 %11, label %12, label %34
12: ; preds = %10
%13 = lshr i32 %1, 4
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%15 = zext i32 %13 to i64
br label %16
16: ; preds = %12, %29
%17 = phi i64 [ 0, %12 ], [ %32, %29 ]
%18 = shl nsw i64 %17, 4
%19 = trunc i64 %18 to i32
%20 = add i32 %19, %0
%21 = trunc i32 %20 to i8
store i8 %21, i8* %7
%22 = lshr i32 %20, 8
%23 = trunc i32 %22 to i8
store i8 %23, i8* %14
%24 = call i32 @SMBWriteBlock(i32 22, i32 255, i8* %7, i32 2)
%25 = icmp eq i32 %24, 2
br i1 %25, label %26, label %34
26: ; preds = %16
%27 = call i32 @SMBReadBlock(i32 22, i32 254, i8* %6)
%28 = icmp eq i32 %27, 16
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = getelementptr inbounds i8, i8* %2, i64 %18
%31 = call i32 @memcpy(i8* %30, i8* %6, i32 16)
%32 = add nuw nsw i64 %17, 1
%33 = icmp eq i64 %32, %15
br i1 %33, label %34, label %16
34: ; preds = %16, %26, %29, %10, %3
%35 = phi i32 [ -99, %3 ], [ %1, %10 ], [ -1, %16 ], [ %27, %26 ], [ %1, %29 ]
ret i32 %35
}