#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 __builtin_acc_on_device(int);
int /*<<< orphan*/ gang(int *);
int /*<<< orphan*/ printf(char *, int, int, int);
int main() {
  int ary[(32 * 32 * 32 + 17)];
  int ix;
  int exit = 0;
  int ondev = 0;
  for (ix = 0; ix < (32 * 32 * 32 + 17); ix++)
    ary[ix] = -1;
#pragma acc parallel num_gangs(32) vector_length(32) copy(ary) copy(ondev)
  {
    ondev = __builtin_acc_on_device(5);
    gang(ary);
  }
  for (ix = 0; ix < (32 * 32 * 32 + 17); ix++) {
    int expected = ix;
    if (ondev) {
      int g = ix / (((32 * 32 * 32 + 17) + 31) / 32);
      int w = 0;
      int v = 0;
      expected = (g << 16) | (w << 8) | v;
    }
    if (ary[ix] != expected) {
      exit = 1;
      printf("ary[%d]=%x expected %x\n", ix, ary[ix], expected);
    }
  }
  return exit;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20070,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20058(%rbp)
movl   $0x0,-0x2005c(%rbp)
movl   $0x0,-0x20054(%rbp)
cmpl   $0x8011,-0x20054(%rbp)
jge    1196 <main+0x66>
movslq -0x20054(%rbp),%rax
movl   $0xffffffff,-0x20050(%rbp,%rax,4)
ff ff ff ff
mov    -0x20054(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20054(%rbp)
jmp    1160 <main+0x30>
mov    $0x5,%edi
call   1050 <__builtin_acc_on_device@plt>
mov    %eax,-0x2005c(%rbp)
lea    -0x20050(%rbp),%rdi
call   1040 <gang@plt>
movl   $0x0,-0x20054(%rbp)
cmpl   $0x8011,-0x20054(%rbp)
jge    1290 <main+0x160>
mov    -0x20054(%rbp),%eax
mov    %eax,-0x20060(%rbp)
cmpl   $0x0,-0x2005c(%rbp)
je     122d <main+0xfd>
mov    -0x20054(%rbp),%eax
mov    $0x401,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20064(%rbp)
movl   $0x0,-0x20068(%rbp)
movl   $0x0,-0x2006c(%rbp)
mov    -0x20064(%rbp),%eax
shl    $0x10,%eax
mov    -0x20068(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
or     -0x2006c(%rbp),%eax
mov    %eax,-0x20060(%rbp)
movslq -0x20054(%rbp),%rax
mov    -0x20050(%rbp,%rax,4),%eax
cmp    -0x20060(%rbp),%eax
je     1277 <main+0x147>
movl   $0x1,-0x20058(%rbp)
mov    -0x20054(%rbp),%esi
movslq -0x20054(%rbp),%rax
mov    -0x20050(%rbp,%rax,4),%edx
mov    -0x20060(%rbp),%ecx
lea    0xd8e(%rip),%rdi
call   1030 <printf@plt>
jmp    127c <main+0x14c>
mov    -0x20054(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20054(%rbp)
jmp    11bc <main+0x8c>
mov    -0x20058(%rbp),%eax
add    $0x20070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20050,%rsp
mov    %rsp,%rbx
mov    $0x20044,%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1050 <memset@plt>
mov    $0x5,%edi
call   1060 <__builtin_acc_on_device@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <gang@plt>
xor    %ebx,%ebx
lea    0xe83(%rip),%r14
xor    %eax,%eax
jmp    119d <main+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0x8011,%rbx
je     11da <main+0x9a>
test   %ebp,%ebp
je     11c0 <main+0x80>
movzwl %bx,%ecx
imul   $0xffc1,%ecx,%ecx
shr    $0xa,%ecx
and    $0x3f0000,%ecx
jmp    11c2 <main+0x82>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%ecx
mov    (%rsp,%rbx,4),%edx
cmp    %ecx,%edx
je     1190 <main+0x50>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0x1,%eax
jmp    1190 <main+0x50>
add    $0x20050,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"ary[%d]=%x expected %x\0A\00"
define i32 @main() {
%1 = alloca [32785 x i32]
%2 = bitcast [32785 x i32]* %1 to i8*
%3 = tail call i32 @__builtin_acc_on_device(i32 5)
%4 = getelementptr inbounds [32785 x i32], [32785 x i32]* %1, i64 0, i64 0
%5 = call i32 @gang(i32* %4)
%6 = icmp eq i32 %3, 0
br label %7
7: ; preds = %0, %23
%8 = phi i64 [ 0, %0 ], [ %25, %23 ]
%9 = phi i32 [ 0, %0 ], [ %24, %23 ]
%10 = trunc i64 %8 to i32
br i1 %6, label %16, label %11
11: ; preds = %7
%12 = trunc i64 %8 to i16
%13 = udiv i16 %12, 1025
%14 = zext i16 %13 to i32
%15 = shl nuw nsw i32 %14, 16
br label %16
16: ; preds = %11, %7
%17 = phi i32 [ %15, %11 ], [ %10, %7 ]
%18 = getelementptr inbounds [32785 x i32], [32785 x i32]* %1, i64 0, i64 %8
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, %17
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %19, i32 %17)
br label %23
23: ; preds = %21, %16
%24 = phi i32 [ 1, %21 ], [ %9, %16 ]
%25 = add nuw nsw i64 %8, 1
%26 = icmp eq i64 %25, 32785
br i1 %26, label %27, label %7
27: ; preds = %23
ret i32 %24
}