#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 */
unsigned long extract_val(char *);
int /*<<< orphan*/ wsh_cache_data_flush(unsigned long *, unsigned long);
int /*<<< orphan*/ wsh_printf(char *, ...);
int wsh_cmd_rd(int argc, char **argv) {
  unsigned long addr;
  unsigned long nr_words;
  if (!argc) {
    wsh_printf("not enough arguments\n");
    wsh_printf("usage: rd ADDR [LENGTH]\n");
    return -1;
  }
  addr = extract_val(argv[0]);
  nr_words = 1;
  addr &= ~0x3;
  if (argc > 1)
    nr_words = extract_val(argv[1]);
  wsh_cache_data_flush((unsigned long *)addr, nr_words * 4);
  for (int i = 0; i < nr_words; i += 4) {
    wsh_printf("[0x%08lx] ", addr);
    for (int j = i; j < i + 4 && j < nr_words; j++) {
      wsh_printf("0x%08lx ", *(unsigned long *)addr);
      addr += 4;
    }
    wsh_printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<wsh_cmd_rd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1171 <wsh_cmd_rd+0x41>
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1040 <wsh_printf@plt>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <wsh_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <wsh_cmd_rd+0x144>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <extract_val@plt>
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    $0xfffffffffffffffc,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b0 <wsh_cmd_rd+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <extract_val@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x2,%rsi
call   1050 <wsh_cache_data_flush@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    126d <wsh_cmd_rd+0x13d>
mov    -0x18(%rbp),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <wsh_printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%edx
add    $0x4,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x29(%rbp)
jge    1212 <wsh_cmd_rd+0xe2>
movslq -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1222 <wsh_cmd_rd+0xf2>
jmp    1251 <wsh_cmd_rd+0x121>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <wsh_printf@plt>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ee <wsh_cmd_rd+0xbe>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <wsh_printf@plt>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
jmp    11c8 <wsh_cmd_rd+0x98>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wsh_cmd_rd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     117d <wsh_cmd_rd+0x4d>
mov    %rsi,%r14
mov    %edi,%ebp
mov    (%rsi),%rdi
call   1030 <extract_val@plt>
mov    %rax,%rbx
and    $0xfffffffffffffffc,%rbx
cmp    $0x1,%ebp
jle    11af <wsh_cmd_rd+0x7f>
mov    0x8(%r14),%rdi
call   1030 <extract_val@plt>
mov    %rax,%r14
lea    0x0(,%rax,4),%rsi
mov    %rbx,%rdi
call   1050 <wsh_cache_data_flush@plt>
test   %r14,%r14
jne    11c2 <wsh_cmd_rd+0x92>
xor    %ebp,%ebp
jmp    119e <wsh_cmd_rd+0x6e>
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1040 <wsh_printf@plt>
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1040 <wsh_printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x4,%esi
mov    %rbx,%rdi
call   1050 <wsh_cache_data_flush@plt>
mov    $0x1,%r14d
mov    $0x1,%r15d
lea    0xe6b(%rip),%r12
xor    %r13d,%r13d
jmp    11f9 <wsh_cmd_rd+0xc9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %ebp,%ebp
lea    0xe44(%rip),%rdi
xor    %eax,%eax
call   1040 <wsh_printf@plt>
add    $0x4,%r15
cmp    %r13,%r14
jbe    119e <wsh_cmd_rd+0x6e>
lea    0xe2f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <wsh_printf@plt>
add    $0x4,%r13
mov    %r15,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <wsh_printf@plt>
add    $0x4,%rbx
cmp    %r13,%rbp
jae    11e0 <wsh_cmd_rd+0xb0>
lea    0x1(%rbp),%rax
cmp    %rbp,%r14
mov    %rax,%rbp
ja     1220 <wsh_cmd_rd+0xf0>
jmp    11e0 <wsh_cmd_rd+0xb0>

LLVM IR (-O0):
@.str = [22 x i8] c"not enough arguments\0A\00"
@.str.1 = [25 x i8] c"usage: rd ADDR [LENGTH]\0A\00"
@.str.2 = [11 x i8] c"[0x%08lx] \00"
@.str.3 = [9 x i8] c"0x%08lx \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @wsh_cmd_rd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i64 @extract_val(i8* %18)
store i64 %19, i64* %6
store i64 1, i64* %7
%20 = load i64, i64* %6
%21 = and i64 %20, -4
store i64 %21, i64* %6
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %15
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @extract_val(i8* %27)
store i64 %28, i64* %7
br label %29
29: ; preds = %24, %15
%30 = load i64, i64* %6
%31 = inttoptr i64 %30 to i64*
%32 = load i64, i64* %7
%33 = mul i64 %32, 4
%34 = call i32 @wsh_cache_data_flush(i64* %31, i64 %33)
store i32 0, i32* %8
br label %35
35: ; preds = %68, %29
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = load i64, i64* %7
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %71
40: ; preds = %35
%41 = load i64, i64* %6
%42 = call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %41)
%43 = load i32, i32* %8
store i32 %43, i32* %9
br label %44
44: ; preds = %63, %40
%45 = load i32, i32* %9
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 4
%48 = icmp slt i32 %45, %47
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = load i64, i64* %7
%53 = icmp ult i64 %51, %52
br label %54
54: ; preds = %49, %44
%55 = phi i1 [ false, %44 ], [ %53, %49 ]
br i1 %55, label %56, label %66
56: ; preds = %54
%57 = load i64, i64* %6
%58 = inttoptr i64 %57 to i64*
%59 = load i64, i64* %58
%60 = call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 %59)
%61 = load i64, i64* %6
%62 = add i64 %61, 4
store i64 %62, i64* %6
br label %63
63: ; preds = %56
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %44
66: ; preds = %54
%67 = call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66
%69 = load i32, i32* %8
%70 = add nsw i32 %69, 4
store i32 %70, i32* %8
br label %35
71: ; preds = %35
store i32 0, i32* %3
br label %72
72: ; preds = %71, %12
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [22 x i8] c"not enough arguments\0A\00"
@.str.1 = [25 x i8] c"usage: rd ADDR [LENGTH]\0A\00"
@.str.2 = [11 x i8] c"[0x%08lx] \00"
@.str.3 = [9 x i8] c"0x%08lx \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @wsh_cmd_rd(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i64 @extract_val(i8* %8)
%10 = and i64 %9, -4
%11 = icmp sgt i32 %0, 1
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = inttoptr i64 %10 to i64*
%14 = tail call i32 @wsh_cache_data_flush(i64* %13, i64 4)
br label %23
15: ; preds = %7
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i64 @extract_val(i8* %17)
%19 = inttoptr i64 %10 to i64*
%20 = shl i64 %18, 2
%21 = tail call i32 @wsh_cache_data_flush(i64* %19, i64 %20)
%22 = icmp eq i64 %18, 0
br i1 %22, label %44, label %23
23: ; preds = %12, %15
%24 = phi i64 [ 1, %12 ], [ %18, %15 ]
br label %25
25: ; preds = %30, %23
%26 = phi i64 [ 0, %23 ], [ %29, %30 ]
%27 = phi i64 [ %10, %23 ], [ %39, %30 ]
%28 = tail call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %27)
%29 = add nuw i64 %26, 4
br label %33
30: ; preds = %33
%31 = tail call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp ugt i64 %24, %29
br i1 %32, label %25, label %44
33: ; preds = %25, %33
%34 = phi i64 [ %26, %25 ], [ %40, %33 ]
%35 = phi i64 [ %27, %25 ], [ %39, %33 ]
%36 = inttoptr i64 %35 to i64*
%37 = load i64, i64* %36
%38 = tail call i32 (i8*, ...) @wsh_printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 %37)
%39 = add i64 %35, 4
%40 = add nuw i64 %34, 1
%41 = icmp ult i64 %40, %29
%42 = icmp ugt i64 %24, %40
%43 = select i1 %41, i1 %42, i1 false
br i1 %43, label %33, label %30
44: ; preds = %30, %15, %4
%45 = phi i32 [ -1, %4 ], [ 0, %15 ], [ 0, %30 ]
ret i32 %45
}