#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 *);
int main(void) {
  int a[1024], b[1024];
#pragma xmp align a[i] with t(i)
#pragma xmp align b[i] with t(i)
#pragma xmp shadow a[1 : 1]
#pragma xmp shadow b[1 : 1]
  void *acc_a_0, *acc_a_1;
#pragma acc data copy(a)
  {
#pragma xmp loop(i) on t(i)
#pragma acc parallel loop
    for (int i = 0; i < 1024; i++) {
      a[i] = i;
    }
#pragma xmp reflect(a) acc
#pragma acc host_data use_device(a)
    acc_a_0 = a;
  }
#pragma acc enter data create(b)
#pragma acc enter data copyin(a)
#pragma acc data present(a, b)
  {
#pragma acc host_data use_device(a)
    acc_a_1 = a;
    if (acc_a_0 == acc_a_1) {
#pragma xmp task on p(1)
      printf(
          "Cannot test reflect because device addresses of array 'a' are same");
      return 2;
    }
#pragma xmp loop(i) on t(i)
#pragma acc parallel loop
    for (int i = 0; i < 1024; i++) {
      a[i] += i;
    }
#pragma xmp reflect(a) acc
#pragma xmp loop(i) on t(i)
#pragma acc parallel loop
    for (int i = 1; i < 1024 - 1; i++) {
      b[i] = a[i - 1] + a[i + 1];
    }
  }
#pragma acc exit data copyout(b) delete (a)
  int err = 0;
#pragma xmp loop(i) on t(i) reduction(+ : err)
  for (int i = 1; i < 1024 - 1; i++) {
    if (b[i] != i * 4)
      err++;
  }
  if (err > 0) {
#pragma xmp task on p(1)
    printf("Failed\n");
    return 1;
  }
#pragma xmp task on p(1)
  printf("PASS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x2024(%rbp)
cmpl   $0x400,-0x2024(%rbp)
jge    1164 <main+0x54>
mov    -0x2024(%rbp),%ecx
movslq -0x2024(%rbp),%rax
mov    %ecx,-0x1010(%rbp,%rax,4)
mov    -0x2024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2024(%rbp)
jmp    112c <main+0x1c>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x2018(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x2020(%rbp)
mov    -0x2018(%rbp),%rax
cmp    -0x2020(%rbp),%rax
jne    11ac <main+0x9c>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12f8 <main+0x1e8>
movl   $0x0,-0x2028(%rbp)
cmpl   $0x400,-0x2028(%rbp)
jge    11f5 <main+0xe5>
mov    -0x2028(%rbp),%ecx
movslq -0x2028(%rbp),%rax
add    -0x1010(%rbp,%rax,4),%ecx
mov    %ecx,-0x1010(%rbp,%rax,4)
mov    -0x2028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2028(%rbp)
jmp    11b6 <main+0xa6>
movl   $0x1,-0x202c(%rbp)
cmpl   $0x3ff,-0x202c(%rbp)
jge    1255 <main+0x145>
mov    -0x202c(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x1010(%rbp,%rax,4),%ecx
mov    -0x202c(%rbp),%eax
add    $0x1,%eax
cltq
add    -0x1010(%rbp,%rax,4),%ecx
movslq -0x202c(%rbp),%rax
mov    %ecx,-0x2010(%rbp,%rax,4)
mov    -0x202c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x202c(%rbp)
jmp    11ff <main+0xef>
movl   $0x0,-0x2030(%rbp)
movl   $0x1,-0x2034(%rbp)
cmpl   $0x3ff,-0x2034(%rbp)
jge    12c0 <main+0x1b0>
movslq -0x2034(%rbp),%rax
mov    -0x2010(%rbp,%rax,4),%eax
mov    -0x2034(%rbp),%ecx
shl    $0x2,%ecx
cmp    %ecx,%eax
je     12a7 <main+0x197>
mov    -0x2030(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2030(%rbp)
jmp    12ac <main+0x19c>
mov    -0x2034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2034(%rbp)
jmp    1269 <main+0x159>
cmpl   $0x0,-0x2030(%rbp)
jle    12e5 <main+0x1d5>
lea    0xd6f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f8 <main+0x1e8>
lea    0xd5f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee8(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [67 x i8] c"Cannot test reflect because device addresses of array 'a' are same\00"
@.str.1 = [8 x i8] c"Failed\0A\00"
@.str.2 = [6 x i8] c"PASS\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i32]
%3 = alloca [1024 x i32]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
br label %11
11: ; preds = %19, %0
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 1024
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %17
store i32 %15, i32* %18
br label %19
19: ; preds = %14
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
br label %11
22: ; preds = %11
%23 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 0
%24 = bitcast i32* %23 to i8*
store i8* %24, i8** %4
%25 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 0
%26 = bitcast i32* %25 to i8*
store i8* %26, i8** %5
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %94
32: ; preds = %22
store i32 0, i32* %7
br label %33
33: ; preds = %43, %32
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 1024
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = load i32, i32* %7
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = add nsw i32 %41, %37
store i32 %42, i32* %40
br label %43
43: ; preds = %36
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %33
46: ; preds = %33
store i32 1, i32* %8
br label %47
47: ; preds = %65, %46
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 1023
br i1 %49, label %50, label %68
50: ; preds = %47
%51 = load i32, i32* %8
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %53
%55 = load i32, i32* %54
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = add nsw i32 %55, %60
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 %63
store i32 %61, i32* %64
br label %65
65: ; preds = %50
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %47
68: ; preds = %47
store i32 0, i32* %9
store i32 1, i32* %10
br label %69
69: ; preds = %84, %68
%70 = load i32, i32* %10
%71 = icmp slt i32 %70, 1023
br i1 %71, label %72, label %87
72: ; preds = %69
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 %74
%76 = load i32, i32* %75
%77 = load i32, i32* %10
%78 = mul nsw i32 %77, 4
%79 = icmp ne i32 %76, %78
br i1 %79, label %80, label %83
80: ; preds = %72
%81 = load i32, i32* %9
%82 = add nsw i32 %81, 1
store i32 %82, i32* %9
br label %83
83: ; preds = %80, %72
br label %84
84: ; preds = %83
%85 = load i32, i32* %10
%86 = add nsw i32 %85, 1
store i32 %86, i32* %10
br label %69
87: ; preds = %69
%88 = load i32, i32* %9
%89 = icmp sgt i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %87
%91 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %94
92: ; preds = %87
%93 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %94
94: ; preds = %92, %90, %30
%95 = load i32, i32* %1
ret i32 %95
}

LLVM IR (-O2):
@.str = [67 x i8] c"Cannot test reflect because device addresses of array 'a' are same\00"
define i32 @main() {
%1 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0))
ret i32 2
}