#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 */
typedef scalar_t__ uint32_t;

/* Variables and functions */
int conv_endian(scalar_t__);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fpga_readl(scalar_t__);
int /*<<< orphan*/ fpga_writel(int, scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
void copy_lm32(uint32_t *buf, int buf_nwords, uint32_t base_addr) {
  int i;
  printf("Writing memory: ");
  for (i = 0; i < buf_nwords; i++) {
    fpga_writel(conv_endian(buf[i]), base_addr + i * 4);
    if (!(i & 0xfff)) {
      printf(".");
      fflush(stdout);
    }
  }
  printf("\nVerifing memory: ");
  for (i = 0; i < buf_nwords; i++) {
    uint32_t x = fpga_readl(base_addr + i * 4);
    if (conv_endian(buf[i]) != x) {
      printf("Verify failed (%x vs %x)\n", conv_endian(buf[i]), x);
      return;
    }
    if (!(i & 0xfff)) {
      printf(".");
      fflush(stdout);
    }
  }
  printf("OK.\n");
}


x86 assembly (clang, -O0):
<copy_lm32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11eb <copy_lm32+0x9b>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <conv_endian@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rsi
call   1060 <fpga_writel@plt>
mov    -0x1c(%rbp),%eax
and    $0xfff,%eax
cmp    $0x0,%eax
jne    11d8 <copy_lm32+0x88>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    11dd <copy_lm32+0x8d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1178 <copy_lm32+0x28>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12ab <copy_lm32+0x15b>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
call   1070 <fpga_readl@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <conv_endian@plt>
cltq
cmp    -0x28(%rbp),%rax
je     126b <copy_lm32+0x11b>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <conv_endian@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b9 <copy_lm32+0x169>
mov    -0x1c(%rbp),%eax
and    $0xfff,%eax
cmp    $0x0,%eax
jne    1298 <copy_lm32+0x148>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    129d <copy_lm32+0x14d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1200 <copy_lm32+0xb0>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_lm32>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    1255 <copy_lm32+0xe5>
mov    %ebp,%r15d
xor    %ebx,%ebx
mov    0x2e3b(%rip),%r12
jmp    11b9 <copy_lm32+0x49>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %r15,%rbx
je     11f0 <copy_lm32+0x80>
mov    (%r14,%rbx,8),%rdi
call   1060 <conv_endian@plt>
lea    0x0(,%rbx,4),%esi
add    %r13,%rsi
mov    %eax,%edi
call   1080 <fpga_writel@plt>
test   $0xfff,%ebx
jne    11b0 <copy_lm32+0x40>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
jmp    11b0 <copy_lm32+0x40>
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    1263 <copy_lm32+0xf3>
xor    %ebx,%ebx
jmp    121d <copy_lm32+0xad>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
add    $0x4,%r13
cmp    %rbx,%r15
je     1263 <copy_lm32+0xf3>
mov    %r13,%rdi
call   1090 <fpga_readl@plt>
mov    %rax,%rbp
mov    (%r14,%rbx,8),%rdi
call   1060 <conv_endian@plt>
cltq
cmp    %rax,%rbp
jne    127d <copy_lm32+0x10d>
test   $0xfff,%ebx
jne    1210 <copy_lm32+0xa0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
jmp    1210 <copy_lm32+0xa0>
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd4(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
mov    (%r14,%rbx,8),%rdi
call   1060 <conv_endian@plt>
lea    0xd97(%rip),%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Writing memory: \00"
@.str.1 = [2 x i8] c".\00"
@stdout = global i32 0
@.str.2 = [19 x i8] c"\0AVerifing memory: \00"
@.str.3 = [26 x i8] c"Verify failed (%x vs %x)\0A\00"
@.str.4 = [5 x i8] c"OK.\0A\00"
define void @copy_lm32(i64* %0, i32 %1, i64 %2) {
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i64* %0, i64** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %10
10: ; preds = %35, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %38
14: ; preds = %10
%15 = load i64*, i64** %4
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i64, i64* %15, i64 %17
%19 = load i64, i64* %18
%20 = call i32 @conv_endian(i64 %19)
%21 = load i64, i64* %6
%22 = load i32, i32* %7
%23 = mul nsw i32 %22, 4
%24 = sext i32 %23 to i64
%25 = add nsw i64 %21, %24
%26 = call i32 @fpga_writel(i32 %20, i64 %25)
%27 = load i32, i32* %7
%28 = and i32 %27, 4095
%29 = icmp ne i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %14
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @stdout
%33 = call i32 @fflush(i32 %32)
br label %34
34: ; preds = %30, %14
br label %35
35: ; preds = %34
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %10
38: ; preds = %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %40
40: ; preds = %78, %38
%41 = load i32, i32* %7
%42 = load i32, i32* %5
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %81
44: ; preds = %40
%45 = load i64, i64* %6
%46 = load i32, i32* %7
%47 = mul nsw i32 %46, 4
%48 = sext i32 %47 to i64
%49 = add nsw i64 %45, %48
%50 = call i64 @fpga_readl(i64 %49)
store i64 %50, i64* %8
%51 = load i64*, i64** %4
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i64, i64* %51, i64 %53
%55 = load i64, i64* %54
%56 = call i32 @conv_endian(i64 %55)
%57 = sext i32 %56 to i64
%58 = load i64, i64* %8
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %69
60: ; preds = %44
%61 = load i64*, i64** %4
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i64, i64* %61, i64 %63
%65 = load i64, i64* %64
%66 = call i32 @conv_endian(i64 %65)
%67 = load i64, i64* %8
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %66, i64 %67)
br label %83
69: ; preds = %44
%70 = load i32, i32* %7
%71 = and i32 %70, 4095
%72 = icmp ne i32 %71, 0
br i1 %72, label %77, label %73
73: ; preds = %69
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%75 = load i32, i32* @stdout
%76 = call i32 @fflush(i32 %75)
br label %77
77: ; preds = %73, %69
br label %78
78: ; preds = %77
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
store i32 %80, i32* %7
br label %40
81: ; preds = %40
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %83
83: ; preds = %81, %60
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"Writing memory: \00"
@stdout = global i32 0
@.str.2 = [19 x i8] c"\0AVerifing memory: \00"
@.str.3 = [26 x i8] c"Verify failed (%x vs %x)\0A\00"
@str = [4 x i8] c"OK.\00"
define void @copy_lm32(i64* nocapture %0, i32 %1, i64 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%5 = icmp sgt i32 %1, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %58
8: ; preds = %3
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %25
%11 = phi i64 [ 0, %8 ], [ %26, %25 ]
%12 = getelementptr inbounds i64, i64* %0, i64 %11
%13 = load i64, i64* %12
%14 = tail call i32 @conv_endian(i64 %13)
%15 = shl i64 %11, 2
%16 = and i64 %15, 4294967292
%17 = add nsw i64 %16, %2
%18 = tail call i32 @fpga_writel(i32 %14, i64 %17)
%19 = and i64 %11, 4095
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %10
%22 = tail call i32 @putchar(i32 46)
%23 = load i32, i32* @stdout
%24 = tail call i32 @fflush(i32 %23)
br label %25
25: ; preds = %10, %21
%26 = add nuw nsw i64 %11, 1
%27 = icmp eq i64 %26, %9
br i1 %27, label %28, label %10
28: ; preds = %25
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br i1 %5, label %30, label %58
30: ; preds = %28
%31 = zext i32 %1 to i64
br label %32
32: ; preds = %30, %55
%33 = phi i64 [ 0, %30 ], [ %56, %55 ]
%34 = shl nsw i64 %33, 2
%35 = add nsw i64 %34, %2
%36 = tail call i64 @fpga_readl(i64 %35)
%37 = getelementptr inbounds i64, i64* %0, i64 %33
%38 = load i64, i64* %37
%39 = tail call i32 @conv_endian(i64 %38)
%40 = sext i32 %39 to i64
%41 = icmp eq i64 %36, %40
br i1 %41, label %42, label %49
42: ; preds = %32
%43 = and i64 %33, 4095
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = tail call i32 @putchar(i32 46)
%47 = load i32, i32* @stdout
%48 = tail call i32 @fflush(i32 %47)
br label %55
49: ; preds = %32
%50 = and i64 %33, 4294967295
%51 = getelementptr inbounds i64, i64* %0, i64 %50
%52 = load i64, i64* %51
%53 = tail call i32 @conv_endian(i64 %52)
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %53, i64 %36)
br label %60
55: ; preds = %42, %45
%56 = add nuw nsw i64 %33, 1
%57 = icmp eq i64 %56, %31
br i1 %57, label %58, label %32
58: ; preds = %55, %6, %28
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
br label %60
60: ; preds = %49, %58
ret void
}