#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, unsigned int,
                           unsigned char *);
scalar_t__ readData(int, unsigned char *, int);
scalar_t__ sendAddress(int, unsigned int);
scalar_t__ sendCommand(int, int);
scalar_t__ sendLength(int, int);
int /*<<< orphan*/ stderr;
int readMemory(int fd, unsigned addr, void *ptr, int len) {
  unsigned char *data = ptr;
  while (len > 0) {
    int xfer = (len > 256) ? 256 : len;
    fprintf(stderr, "read %04x at %08x -> %p\n", xfer, addr, data);
    if (sendCommand(fd, 0x11))
      return -1;
    if (sendAddress(fd, addr))
      return -1;
    if (sendLength(fd, xfer))
      return -1;
    if (readData(fd, data, xfer))
      return -1;
    data += xfer;
    len -= xfer;
    addr += xfer;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1270 <readMemory+0x120>
cmpl   $0x100,-0x1c(%rbp)
jle    1191 <readMemory+0x41>
mov    $0x100,%eax
mov    %eax,-0x30(%rbp)
jmp    1197 <readMemory+0x47>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x28(%rbp),%r8
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    $0x11,%esi
call   1030 <sendCommand@plt>
cmp    $0x0,%rax
je     11df <readMemory+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <readMemory+0x127>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <sendAddress@plt>
cmp    $0x0,%rax
je     1200 <readMemory+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <readMemory+0x127>
mov    -0x8(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1070 <sendLength@plt>
cmp    $0x0,%rax
je     1221 <readMemory+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <readMemory+0x127>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <readData@plt>
cmp    $0x0,%rax
je     1246 <readMemory+0xf6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <readMemory+0x127>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    116d <readMemory+0x1d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMemory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    11fa <readMemory+0xaa>
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r13d
mov    %edi,%r15d
mov    $0xffffffff,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x100,%ebx
mov    $0x100,%ebp
cmovb  %ebx,%ebp
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %ebp,%edx
mov    %r13d,%ecx
mov    %r14,%r8
call   1060 <fprintf@plt>
mov    %r15d,%edi
mov    $0x11,%esi
call   1030 <sendCommand@plt>
test   %rax,%rax
jne    11fd <readMemory+0xad>
mov    %r15d,%edi
mov    %r13d,%esi
call   1040 <sendAddress@plt>
test   %rax,%rax
jne    11fd <readMemory+0xad>
mov    %r15d,%edi
mov    %ebp,%esi
call   1070 <sendLength@plt>
test   %rax,%rax
jne    11fd <readMemory+0xad>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <readData@plt>
test   %rax,%rax
jne    11fd <readMemory+0xad>
mov    %ebp,%eax
add    %rax,%r14
add    %ebp,%r13d
sub    %ebp,%ebx
jg     1180 <readMemory+0x30>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"read %04x at %08x -> %p\0A\00"
define i32 @readMemory(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %8
store i8* %12, i8** %10
br label %13
13: ; preds = %52, %4
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %63
16: ; preds = %13
%17 = load i32, i32* %9
%18 = icmp sgt i32 %17, 256
br i1 %18, label %19, label %20
19: ; preds = %16
br label %22
20: ; preds = %16
%21 = load i32, i32* %9
br label %22
22: ; preds = %20, %19
%23 = phi i32 [ 256, %19 ], [ %21, %20 ]
store i32 %23, i32* %11
%24 = load i32, i32* @stderr
%25 = load i32, i32* %11
%26 = load i32, i32* %7
%27 = load i8*, i8** %10
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i8* %27)
%29 = load i32, i32* %6
%30 = call i64 @sendCommand(i32 %29, i32 17)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 -1, i32* %5
br label %64
33: ; preds = %22
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = call i64 @sendAddress(i32 %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %5
br label %64
39: ; preds = %33
%40 = load i32, i32* %6
%41 = load i32, i32* %11
%42 = call i64 @sendLength(i32 %40, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 -1, i32* %5
br label %64
45: ; preds = %39
%46 = load i32, i32* %6
%47 = load i8*, i8** %10
%48 = load i32, i32* %11
%49 = call i64 @readData(i32 %46, i8* %47, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
store i32 -1, i32* %5
br label %64
52: ; preds = %45
%53 = load i32, i32* %11
%54 = load i8*, i8** %10
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8* %56, i8** %10
%57 = load i32, i32* %11
%58 = load i32, i32* %9
%59 = sub nsw i32 %58, %57
store i32 %59, i32* %9
%60 = load i32, i32* %11
%61 = load i32, i32* %7
%62 = add i32 %61, %60
store i32 %62, i32* %7
br label %13
63: ; preds = %13
store i32 0, i32* %5
br label %64
64: ; preds = %63, %51, %44, %38, %32
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"read %04x at %08x -> %p\0A\00"
define i32 @readMemory(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %31
6: ; preds = %4, %25
%7 = phi i8* [ %27, %25 ], [ %2, %4 ]
%8 = phi i32 [ %28, %25 ], [ %3, %4 ]
%9 = phi i32 [ %29, %25 ], [ %1, %4 ]
%10 = icmp ult i32 %8, 256
%11 = select i1 %10, i32 %8, i32 256
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %9, i8* %7)
%14 = tail call i64 @sendCommand(i32 %0, i32 17)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %6
%17 = tail call i64 @sendAddress(i32 %0, i32 %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = tail call i64 @sendLength(i32 %0, i32 %11)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = tail call i64 @readData(i32 %0, i8* %7, i32 %11)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = zext i32 %11 to i64
%27 = getelementptr inbounds i8, i8* %7, i64 %26
%28 = sub nsw i32 %8, %11
%29 = add i32 %11, %9
%30 = icmp sgt i32 %28, 0
br i1 %30, label %6, label %31
31: ; preds = %25, %6, %16, %19, %22, %4
%32 = phi i32 [ 0, %4 ], [ -1, %22 ], [ -1, %19 ], [ -1, %16 ], [ -1, %6 ], [ 0, %25 ]
ret i32 %32
}