#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 */
char *compute_chunk_start_addr(unsigned int);
unsigned int compute_store_pattern(unsigned int, unsigned int *, unsigned int);
int /*<<< orphan*/ dcbf(unsigned int volatile *);
int /*<<< orphan*/ end_verification_log(unsigned int, int);
int /*<<< orphan*/ log_anamoly(unsigned int, unsigned int *, unsigned int,
                               unsigned int);
int /*<<< orphan*/ start_verification_log(unsigned int, unsigned int *,
                                          unsigned int, unsigned int);
void verify_chunk(unsigned int tid, unsigned int *next_store_addr,
                  unsigned int cur_sweep_id, unsigned int prev_sweep_id) {
  unsigned int *iter_ptr;
  unsigned int size = 1024;
  unsigned int expected;
  unsigned int observed;
  char *chunk_start = compute_chunk_start_addr(tid);
  int nr_anamolies = 0;
  start_verification_log(tid, next_store_addr, cur_sweep_id, prev_sweep_id);
  for (iter_ptr = (unsigned int *)chunk_start;
       (unsigned long)iter_ptr < (unsigned long)chunk_start + size;
       iter_ptr++) {
    unsigned int expected_sweep_id;
    if (iter_ptr < next_store_addr) {
      expected_sweep_id = cur_sweep_id;
    } else {
      expected_sweep_id = prev_sweep_id;
    }
    expected = compute_store_pattern(tid, iter_ptr, expected_sweep_id);
    dcbf((volatile unsigned int *)iter_ptr);
    observed = *iter_ptr;
    if (observed != expected) {
      nr_anamolies++;
      log_anamoly(tid, iter_ptr, expected, observed);
    }
  }
  end_verification_log(tid, nr_anamolies);
}


x86 assembly (clang, -O0):
<verify_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x400,-0x24(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <compute_chunk_start_addr@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1050 <start_verification_log@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    -0x24(%rbp),%edx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1240 <verify_chunk+0xe0>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11d9 <verify_chunk+0x79>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    11df <verify_chunk+0x7f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   1040 <compute_store_pattern@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <dcbf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     122a <verify_chunk+0xca>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
call   1060 <log_anamoly@plt>
jmp    122f <verify_chunk+0xcf>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    11a9 <verify_chunk+0x49>
mov    -0x4(%rbp),%edi
mov    -0x3c(%rbp),%esi
call   1030 <end_verification_log@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_chunk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1070 <compute_chunk_start_addr@plt>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rbx,0x10(%rsp)
mov    %rbx,%rsi
mov    %r15d,0x8(%rsp)
mov    %r15d,%edx
mov    %r14d,0xc(%rsp)
mov    %r14d,%ecx
call   1050 <start_verification_log@plt>
xor    %ebx,%ebx
cmp    $0xfffffffffffffbff,%r12
ja     120a <verify_chunk+0xaa>
mov    %r12,%r13
add    $0x400,%r12
xor    %ebx,%ebx
mov    %r13,%r14
jmp    11cd <verify_chunk+0x6d>
nopl   0x0(%rax)
add    $0x4,%r14
add    $0x4,%r13
cmp    %r13,%r12
jbe    120a <verify_chunk+0xaa>
cmp    0x10(%rsp),%r14
mov    0xc(%rsp),%edx
cmovb  0x8(%rsp),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <compute_store_pattern@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1080 <dcbf@plt>
mov    (%r14),%ecx
cmp    %r15d,%ecx
je     11c0 <verify_chunk+0x60>
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <log_anamoly@plt>
jmp    11c0 <verify_chunk+0x60>
mov    %ebp,%edi
mov    %ebx,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <end_verification_log@plt>

LLVM IR (-O0):
define void @verify_chunk(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 1024, i32* %10
%16 = load i32, i32* %5
%17 = call i8* @compute_chunk_start_addr(i32 %16)
store i8* %17, i8** %13
store i32 0, i32* %14
%18 = load i32, i32* %5
%19 = load i32*, i32** %6
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = call i32 @start_verification_log(i32 %18, i32* %19, i32 %20, i32 %21)
%23 = load i8*, i8** %13
%24 = bitcast i8* %23 to i32*
store i32* %24, i32** %9
br label %25
25: ; preds = %63, %4
%26 = load i32*, i32** %9
%27 = ptrtoint i32* %26 to i64
%28 = load i8*, i8** %13
%29 = ptrtoint i8* %28 to i64
%30 = load i32, i32* %10
%31 = zext i32 %30 to i64
%32 = add i64 %29, %31
%33 = icmp ult i64 %27, %32
br i1 %33, label %34, label %66
34: ; preds = %25
%35 = load i32*, i32** %9
%36 = load i32*, i32** %6
%37 = icmp ult i32* %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* %7
store i32 %39, i32* %15
br label %42
40: ; preds = %34
%41 = load i32, i32* %8
store i32 %41, i32* %15
br label %42
42: ; preds = %40, %38
%43 = load i32, i32* %5
%44 = load i32*, i32** %9
%45 = load i32, i32* %15
%46 = call i32 @compute_store_pattern(i32 %43, i32* %44, i32 %45)
store i32 %46, i32* %11
%47 = load i32*, i32** %9
%48 = call i32 @dcbf(i32* %47)
%49 = load i32*, i32** %9
%50 = load i32, i32* %49
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = load i32, i32* %11
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %62
54: ; preds = %42
%55 = load i32, i32* %14
%56 = add nsw i32 %55, 1
store i32 %56, i32* %14
%57 = load i32, i32* %5
%58 = load i32*, i32** %9
%59 = load i32, i32* %11
%60 = load i32, i32* %12
%61 = call i32 @log_anamoly(i32 %57, i32* %58, i32 %59, i32 %60)
br label %62
62: ; preds = %54, %42
br label %63
63: ; preds = %62
%64 = load i32*, i32** %9
%65 = getelementptr inbounds i32, i32* %64, i32 1
store i32* %65, i32** %9
br label %25
66: ; preds = %25
%67 = load i32, i32* %5
%68 = load i32, i32* %14
%69 = call i32 @end_verification_log(i32 %67, i32 %68)
ret void
}

LLVM IR (-O2):
define void @verify_chunk(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = tail call i8* @compute_chunk_start_addr(i32 %0)
%6 = tail call i32 @start_verification_log(i32 %0, i32* %1, i32 %2, i32 %3)
%7 = ptrtoint i8* %5 to i64
%8 = add i64 %7, 1024
%9 = icmp ult i8* %5, inttoptr (i64 -1024 to i8*)
br i1 %9, label %10, label %29
10: ; preds = %4
%11 = bitcast i8* %5 to i32*
br label %12
12: ; preds = %10, %24
%13 = phi i32 [ %25, %24 ], [ 0, %10 ]
%14 = phi i32* [ %26, %24 ], [ %11, %10 ]
%15 = icmp ult i32* %14, %1
%16 = select i1 %15, i32 %2, i32 %3
%17 = tail call i32 @compute_store_pattern(i32 %0, i32* %14, i32 %16)
%18 = tail call i32 @dcbf(i32* %14)
%19 = load i32, i32* %14
%20 = icmp eq i32 %19, %17
br i1 %20, label %24, label %21
21: ; preds = %12
%22 = add nsw i32 %13, 1
%23 = tail call i32 @log_anamoly(i32 %0, i32* %14, i32 %17, i32 %19)
br label %24
24: ; preds = %21, %12
%25 = phi i32 [ %22, %21 ], [ %13, %12 ]
%26 = getelementptr inbounds i32, i32* %14, i64 1
%27 = ptrtoint i32* %26 to i64
%28 = icmp ugt i64 %8, %27
br i1 %28, label %12, label %29
29: ; preds = %24, %4
%30 = phi i32 [ 0, %4 ], [ %25, %24 ]
%31 = tail call i32 @end_verification_log(i32 %0, i32 %30)
ret void
}