#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 SMBWriteBlock(int, int, unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ readClearStatusRegister();
int /*<<< orphan*/ usleep(int);
int writeFlash(int address, int len, unsigned char *buf) {
  int status, i;
  unsigned char chunk[0x10 + 2];
  if (len % 0x10 != 0)
    return -99;
  for (i = 0; i < len / 0x10; i++) {
    memcpy(chunk + 2, buf + (i * 0x10), 0x10);
    chunk[0] = (address + (i * 0x10)) & 0xFF;
    chunk[1] = ((address + (i * 0x10)) >> 8) & 0xFF;
    readClearStatusRegister();
    status = SMBWriteBlock(0x16, 0x40, chunk, 0x10 + 2);
    usleep(2000);
    if (status != 0x10 + 2)
      return status;
  }
  return len;
}


x86 assembly (clang, -O0):
<writeFlash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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     1172 <writeFlash+0x32>
movl   $0xffffff9d,-0x4(%rbp)
jmp    123b <writeFlash+0xfb>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jge    1235 <writeFlash+0xf5>
lea    -0x40(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%eax
shl    $0x4,%eax
cltq
add    %rax,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
and    $0xff,%eax
mov    %al,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x4,%ecx
add    %ecx,%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x3f(%rbp)
mov    $0x0,%al
call   1040 <readClearStatusRegister@plt>
lea    -0x40(%rbp),%rdx
mov    $0x16,%edi
mov    $0x40,%esi
mov    $0x12,%ecx
call   1030 <SMBWriteBlock@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x7d0,%edi
call   1060 <usleep@plt>
cmpl   $0x12,-0x1c(%rbp)
je     1222 <writeFlash+0xe2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123b <writeFlash+0xfb>
jmp    1227 <writeFlash+0xe7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1179 <writeFlash+0x39>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFlash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    $0xffffff9d,%ebx
test   $0xf,%r14b
jne    11d5 <writeFlash+0x95>
cmp    $0x10,%r14d
jl     11d2 <writeFlash+0x92>
mov    %rdx,%r13
mov    %edi,%ebp
mov    %r14d,%r15d
shr    $0x4,%r15d
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x2(%rsp),%rdi
mov    %r13,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
mov    %bp,(%rsp)
xor    %eax,%eax
call   1040 <readClearStatusRegister@plt>
mov    $0x16,%edi
mov    $0x40,%esi
mov    %r12,%rdx
mov    $0x12,%ecx
call   1030 <SMBWriteBlock@plt>
mov    %eax,%ebx
mov    $0x7d0,%edi
call   1060 <usleep@plt>
cmp    $0x12,%ebx
jne    11d5 <writeFlash+0x95>
add    $0x10,%ebp
add    $0x10,%r13
add    $0xffffffffffffffff,%r15
jne    1180 <writeFlash+0x40>
mov    %r14d,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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