#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 SMBRead(unsigned int, unsigned char *, int);
int SMBWrite(int, int, int /*<<< orphan*/, unsigned char *, int);
int readRam(int address, unsigned int size, unsigned char *buf) {
  int status, i;
  unsigned char block[0xFF];
  block[0] = 0x16;
  block[1] = 0xFC;
  block[2] = address & 0xFF;
  block[3] = (address >> 8) & 0xFF;
  block[4] = (address >> 16) & 0xFF;
  block[5] = size & 0xFF;
  block[6] = (size >> 8) & 0xFF;
  status = SMBWrite(1, 0, 0, block, 7);
  if (status < 0)
    return status;
  block[0] = 0x17;
  status = SMBWrite(0, 1, 0, block, 1);
  return SMBRead(size, buf, 1);
}


x86 assembly (clang, -O0):
<readRam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x16,-0x120(%rbp)
movb   $0xfc,-0x11f(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x11e(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x11d(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x11c(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x11b(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x11a(%rbp)
lea    -0x120(%rbp),%rcx
mov    $0x1,%edi
xor    %edx,%edx
mov    $0x7,%r8d
mov    %edx,%esi
call   1030 <SMBWrite@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c5 <readRam+0xa5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <readRam+0xdc>
movb   $0x17,-0x120(%rbp)
lea    -0x120(%rbp),%rcx
xor    %edx,%edx
mov    $0x1,%r8d
mov    %edx,%edi
mov    %r8d,%esi
call   1030 <SMBWrite@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <SMBRead@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readRam>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%eax
movw   $0xfc16,(%rsp)
mov    %al,0x2(%rsp)
mov    %ah,0x3(%rsp)
shr    $0x10,%eax
mov    %al,0x4(%rsp)
mov    %bl,0x5(%rsp)
mov    %bh,0x6(%rsp)
mov    %rsp,%rcx
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x7,%r8d
call   1030 <SMBWrite@plt>
test   %eax,%eax
js     1193 <readRam+0x73>
movb   $0x17,(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    $0x1,%esi
xor    %edx,%edx
mov    $0x1,%r8d
call   1030 <SMBWrite@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <SMBRead@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @readRam(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 [255 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
store i8 22, i8* %11
%12 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 1
store i8 -4, i8* %12
%13 = load i32, i32* %5
%14 = and i32 %13, 255
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 2
store i8 %15, i8* %16
%17 = load i32, i32* %5
%18 = ashr i32 %17, 8
%19 = and i32 %18, 255
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 3
store i8 %20, i8* %21
%22 = load i32, i32* %5
%23 = ashr i32 %22, 16
%24 = and i32 %23, 255
%25 = trunc i32 %24 to i8
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 4
store i8 %25, i8* %26
%27 = load i32, i32* %6
%28 = and i32 %27, 255
%29 = trunc i32 %28 to i8
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 5
store i8 %29, i8* %30
%31 = load i32, i32* %6
%32 = lshr i32 %31, 8
%33 = and i32 %32, 255
%34 = trunc i32 %33 to i8
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 6
store i8 %34, i8* %35
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%37 = call i32 @SMBWrite(i32 1, i32 0, i32 0, i8* %36, i32 7)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %3
%41 = load i32, i32* %8
store i32 %41, i32* %4
br label %49
42: ; preds = %3
%43 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
store i8 23, i8* %43
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%45 = call i32 @SMBWrite(i32 0, i32 1, i32 0, i8* %44, i32 1)
store i32 %45, i32* %8
%46 = load i32, i32* %6
%47 = load i8*, i8** %7
%48 = call i32 @SMBRead(i32 %46, i8* %47, i32 1)
store i32 %48, i32* %4
br label %49
49: ; preds = %42, %40
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @readRam(i32 %0, i32 %1, i8* %2) {
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
store i8 22, i8* %5
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 1
store i8 -4, i8* %6
%7 = trunc i32 %0 to i8
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 2
store i8 %7, i8* %8
%9 = lshr i32 %0, 8
%10 = trunc i32 %9 to i8
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 3
store i8 %10, i8* %11
%12 = lshr i32 %0, 16
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 4
store i8 %13, i8* %14
%15 = trunc i32 %1 to i8
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 5
store i8 %15, i8* %16
%17 = lshr i32 %1, 8
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 6
store i8 %18, i8* %19
%20 = call i32 @SMBWrite(i32 1, i32 0, i32 0, i8* %5, i32 7)
%21 = icmp slt i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %3
store i8 23, i8* %5
%23 = call i32 @SMBWrite(i32 0, i32 1, i32 0, i8* %5, i32 1)
%24 = call i32 @SMBRead(i32 %1, i8* %2, i32 1)
br label %25
25: ; preds = %3, %22
%26 = phi i32 [ %24, %22 ], [ %20, %3 ]
ret i32 %26
}