#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*/ printf(char *, ...);
void print_mem_changes(unsigned short *ptr, int nwords) {
  int i, cr;
  unsigned short last;
  last = 0xcafe;
  cr = 0;
  for (i = 0; i < nwords; i++) {
    if (ptr[i] != last) {
      printf("0x%x:%x ", i * 2, ptr[i]);
      cr++;
      if (cr > 0 && (cr % 8) == 0)
        printf("\n");
      last = ptr[i];
    }
  }
  if ((cr % 8) != 0)
    printf("\n");
  printf("mem_changes %d nwords\n", nwords);
}


x86 assembly (clang, -O0):
<print_mem_changes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movw   $0xcafe,-0x16(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11cf <print_mem_changes+0xbf>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzwl (%rax,%rcx,2),%eax
movzwl -0x16(%rbp),%ecx
cmp    %ecx,%eax
je     11bc <print_mem_changes+0xac>
mov    -0x10(%rbp),%esi
shl    $0x1,%esi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzwl (%rax,%rcx,2),%edx
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11ac <print_mem_changes+0x9c>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11ac <print_mem_changes+0x9c>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,2),%ax
mov    %ax,-0x16(%rbp)
jmp    11c1 <print_mem_changes+0xb1>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1133 <print_mem_changes+0x23>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11f1 <print_mem_changes+0xe1>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_mem_changes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
test   %esi,%esi
jle    11ac <print_mem_changes+0x8c>
mov    %rdi,%r13
mov    %ebx,0x4(%rsp)
mov    %ebx,%r12d
add    %r12,%r12
mov    $0xcafe,%ax
lea    0xeb7(%rip),%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    115c <print_mem_changes+0x3c>
nop
mov    %ebx,%r14d
add    $0x2,%rbp
cmp    %rbp,%r12
je     1198 <print_mem_changes+0x78>
movzwl 0x0(%r13,%rbp,1),%edx
cmp    %ax,%dx
je     1150 <print_mem_changes+0x30>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x1(%rbx),%r14d
test   %ebx,%ebx
js     118d <print_mem_changes+0x6d>
mov    %r14d,%eax
and    $0x7,%eax
jne    118d <print_mem_changes+0x6d>
mov    $0xa,%edi
call   1030 <putchar@plt>
movzwl 0x0(%r13,%rbp,1),%eax
mov    %r14d,%ebx
jmp    1153 <print_mem_changes+0x33>
test   $0x7,%r14b
mov    0x4(%rsp),%ebx
je     11ac <print_mem_changes+0x8c>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe56(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"0x%x:%x \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [23 x i8] c"mem_changes %d nwords\0A\00"
define void @print_mem_changes(i16* %0, i32 %1) {
%3 = alloca i16*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
store i16* %0, i16** %3
store i32 %1, i32* %4
store i16 -13570, i16* %7
store i32 0, i32* %6
store i32 0, i32* %5
br label %8
8: ; preds = %49, %2
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %52
12: ; preds = %8
%13 = load i16*, i16** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i16, i16* %13, i64 %15
%17 = load i16, i16* %16
%18 = zext i16 %17 to i32
%19 = load i16, i16* %7
%20 = zext i16 %19 to i32
%21 = icmp ne i32 %18, %20
br i1 %21, label %22, label %48
22: ; preds = %12
%23 = load i32, i32* %5
%24 = mul nsw i32 %23, 2
%25 = load i16*, i16** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i16, i16* %25, i64 %27
%29 = load i16, i16* %28
%30 = zext i16 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %30)
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %22
%37 = load i32, i32* %6
%38 = srem i32 %37, 8
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %36, %22
%43 = load i16*, i16** %3
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i16, i16* %43, i64 %45
%47 = load i16, i16* %46
store i16 %47, i16* %7
br label %48
48: ; preds = %42, %12
br label %49
49: ; preds = %48
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
br label %8
52: ; preds = %8
%53 = load i32, i32* %6
%54 = srem i32 %53, 8
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i32, i32* %4
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %59)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"0x%x:%x \00"
@.str.2 = [23 x i8] c"mem_changes %d nwords\0A\00"
define void @print_mem_changes(i16* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %37
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %27
%7 = phi i64 [ 0, %4 ], [ %30, %27 ]
%8 = phi i16 [ -13570, %4 ], [ %29, %27 ]
%9 = phi i32 [ 0, %4 ], [ %28, %27 ]
%10 = getelementptr inbounds i16, i16* %0, i64 %7
%11 = load i16, i16* %10
%12 = icmp eq i16 %11, %8
br i1 %12, label %27, label %13
13: ; preds = %6
%14 = zext i16 %11 to i32
%15 = trunc i64 %7 to i32
%16 = shl i32 %15, 1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %14)
%18 = add nsw i32 %9, 1
%19 = icmp sgt i32 %9, -1
%20 = and i32 %18, 7
%21 = icmp eq i32 %20, 0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = tail call i32 @putchar(i32 10)
br label %25
25: ; preds = %23, %13
%26 = load i16, i16* %10
br label %27
27: ; preds = %6, %25
%28 = phi i32 [ %18, %25 ], [ %9, %6 ]
%29 = phi i16 [ %26, %25 ], [ %8, %6 ]
%30 = add nuw nsw i64 %7, 1
%31 = icmp eq i64 %30, %5
br i1 %31, label %32, label %6
32: ; preds = %27
%33 = and i32 %28, 7
%34 = icmp eq i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = tail call i32 @putchar(i32 10)
br label %37
37: ; preds = %2, %35, %32
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %1)
ret void
}