#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_wr(int argc, char **argv) {
  unsigned long addr;
  unsigned long val;
  unsigned long nr_words;
  unsigned long addr_bak;
  if (argc < 2) {
    wsh_printf("not enough arguments\n");
    wsh_printf("usage: wr ADDR VALUE [LENGTH]\n");
    return -1;
  }
  addr = extract_val(argv[0]);
  val = extract_val(argv[1]);
  nr_words = 1;
  addr &= ~0x3;
  addr_bak = addr;
  if (argc == 3)
    nr_words = extract_val(argv[2]);
  for (int i = 0; i < nr_words; i++) {
    *(unsigned long *)addr = val;
    addr += 4;
  }
  wsh_cache_data_flush((unsigned long *)addr_bak, nr_words * 4);
  return 0;
}


x86 assembly (clang, -O0):
<wsh_cmd_wr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    116d <wsh_cmd_wr+0x3d>
lea    0xeb0(%rip),%rdi
call   1040 <wsh_printf@plt>
lea    0xeba(%rip),%rdi
call   1040 <wsh_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <wsh_cmd_wr+0xe7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <extract_val@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <extract_val@plt>
mov    %rax,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
and    $0xfffffffffffffffc,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11c5 <wsh_cmd_wr+0x95>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <extract_val@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11ff <wsh_cmd_wr+0xcf>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11cc <wsh_cmd_wr+0x9c>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
shl    $0x2,%rsi
call   1050 <wsh_cache_data_flush@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wsh_cmd_wr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     115e <wsh_cmd_wr+0x2e>
lea    0xebd(%rip),%rdi
call   1040 <wsh_printf@plt>
lea    0xec7(%rip),%rdi
call   1040 <wsh_printf@plt>
mov    $0xffffffff,%eax
jmp    120f <wsh_cmd_wr+0xdf>
mov    %rsi,%r15
mov    %edi,%ebp
mov    (%rsi),%rdi
call   1030 <extract_val@plt>
mov    %rax,%r14
mov    0x8(%r15),%rdi
call   1030 <extract_val@plt>
mov    %rax,%rbx
and    $0xfffffffffffffffc,%r14
mov    $0x1,%esi
cmp    $0x3,%ebp
jne    1199 <wsh_cmd_wr+0x69>
mov    0x10(%r15),%rdi
call   1030 <extract_val@plt>
mov    %rax,%rsi
test   %rax,%rax
je     11ff <wsh_cmd_wr+0xcf>
lea    -0x1(%rsi),%rdx
mov    %esi,%eax
and    $0x7,%eax
mov    %r14,%rcx
cmp    $0x7,%rdx
jb     11e9 <wsh_cmd_wr+0xb9>
mov    %rsi,%rdx
and    $0xfffffffffffffff8,%rdx
mov    %r14,%rcx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,(%rcx)
mov    %rbx,0x4(%rcx)
mov    %rbx,0x8(%rcx)
mov    %rbx,0xc(%rcx)
mov    %rbx,0x10(%rcx)
mov    %rbx,0x14(%rcx)
mov    %rbx,0x18(%rcx)
mov    %rbx,0x1c(%rcx)
add    $0x20,%rcx
add    $0xfffffffffffffff8,%rdx
jne    11c0 <wsh_cmd_wr+0x90>
test   %rax,%rax
je     1201 <wsh_cmd_wr+0xd1>
xchg   %ax,%ax
mov    %rbx,(%rcx)
add    $0x4,%rcx
add    $0xffffffffffffffff,%rax
jne    11f0 <wsh_cmd_wr+0xc0>
jmp    1201 <wsh_cmd_wr+0xd1>
xor    %esi,%esi
shl    $0x2,%rsi
mov    %r14,%rdi
call   1050 <wsh_cache_data_flush@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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