#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 int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int next_seq(int);
int /*<<< orphan*/ printf(char *, void *, int, int);
void verify_memory(void *addr) {
  assert(addr);
  uint32_t *wordaddr = (uint32_t *)addr;
  uint32_t numwords = wordaddr[0];
  assert(numwords >= 1);
  for (int i = 1; i < numwords; i++) {
    if ((i == 1 && wordaddr[1] == ((uint32_t)addr % 1000)) ||
        wordaddr[i] == next_seq(wordaddr[i - 1])) {
    } else {
      printf("Memory verification error. Buffer starting at %p, index %u: %u\n",
             addr, i, wordaddr[i]);
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<verify_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1227 <verify_memory+0xe7>
cmpl   $0x1,-0x18(%rbp)
jne    11b7 <verify_memory+0x77>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    -0x1c(%rbp),%eax
cmp    %edx,%eax
je     11e7 <verify_memory+0xa7>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edi
call   1060 <next_seq@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jne    11ec <verify_memory+0xac>
jmp    1214 <verify_memory+0xd4>
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    1219 <verify_memory+0xd9>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <verify_memory+0x40>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <assert@plt>
mov    (%r14),%r15d
xor    %edi,%edi
test   %r15d,%r15d
setg   %dil
call   1040 <assert@plt>
cmp    $0x2,%r15d
jl     11cb <verify_memory+0x8b>
movslq %r14d,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %r14d,%ecx
sub    %eax,%ecx
mov    0x4(%r14),%ebx
cmp    %ecx,%ebx
je     119c <verify_memory+0x5c>
mov    (%r14),%edi
call   1060 <next_seq@plt>
cmp    %eax,%ebx
jne    11d6 <verify_memory+0x96>
cmp    $0x2,%r15d
je     11cb <verify_memory+0x8b>
mov    $0x2,%ebx
nopw   0x0(%rax,%rax,1)
mov    -0x4(%r14,%rbx,4),%edi
mov    (%r14,%rbx,4),%ebp
call   1060 <next_seq@plt>
cmp    %eax,%ebp
jne    11db <verify_memory+0x9b>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11b0 <verify_memory+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
mov    %ebx,%eax
mov    (%r14,%rax,4),%ecx
lea    0xe18(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [64 x i8] c"Memory verification error. Buffer starting at %p, index %u: %u\0A\00"
define void @verify_memory(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 1
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
store i32 1, i32* %5
br label %18
18: ; preds = %59, %1
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %62
22: ; preds = %18
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i32*, i32** %3
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %2
%30 = ptrtoint i8* %29 to i32
%31 = srem i32 %30, 1000
%32 = icmp eq i32 %28, %31
br i1 %32, label %47, label %33
33: ; preds = %25, %22
%34 = load i32*, i32** %3
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = load i32*, i32** %3
%40 = load i32, i32* %5
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %39, i64 %42
%44 = load i32, i32* %43
%45 = call i32 @next_seq(i32 %44)
%46 = icmp eq i32 %38, %45
br i1 %46, label %47, label %48
47: ; preds = %33, %25
br label %58
48: ; preds = %33
%49 = load i8*, i8** %2
%50 = load i32, i32* %5
%51 = load i32*, i32** %3
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %49, i32 %50, i32 %55)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %47
br label %59
59: ; preds = %58
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %18
62: ; preds = %18
ret void
}

LLVM IR (-O2):
@.str = [64 x i8] c"Memory verification error. Buffer starting at %p, index %u: %u\0A\00"
define void @verify_memory(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = trunc i64 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = icmp sgt i32 %6, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp sgt i32 %6, 1
br i1 %10, label %11, label %24
11: ; preds = %1
%12 = zext i32 %6 to i64
%13 = srem i32 %3, 1000
%14 = getelementptr inbounds i8, i8* %0, i64 4
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, %13
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = load i32, i32* %5
%20 = tail call i32 @next_seq(i32 %19)
%21 = icmp eq i32 %16, %20
br i1 %21, label %22, label %34
22: ; preds = %18, %11
%23 = icmp eq i32 %6, 2
br i1 %23, label %24, label %25
24: ; preds = %42, %22, %1
ret void
25: ; preds = %22, %42
%26 = phi i64 [ %43, %42 ], [ 2, %22 ]
%27 = getelementptr inbounds i32, i32* %5, i64 %26
%28 = load i32, i32* %27
%29 = add nsw i64 %26, -1
%30 = getelementptr inbounds i32, i32* %5, i64 %29
%31 = load i32, i32* %30
%32 = tail call i32 @next_seq(i32 %31)
%33 = icmp eq i32 %28, %32
br i1 %33, label %42, label %34
34: ; preds = %25, %18
%35 = phi i64 [ 1, %18 ], [ %26, %25 ]
%36 = trunc i64 %35 to i32
%37 = and i64 %35, 4294967295
%38 = getelementptr inbounds i32, i32* %5, i64 %37
%39 = load i32, i32* %38
%40 = tail call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %36, i32 %39)
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %25
%43 = add nuw nsw i64 %26, 1
%44 = icmp eq i64 %43, %12
br i1 %44, label %24, label %25
}