#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) num_workers(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 chunk_size =
          ((32 * 32 * 32 + 17) + 32 * 32 * 32 - 1) / (32 * 32 * 32);
      int g = ix / (chunk_size * 32 * 32);
      int w = ix / 32 % 32;
      int v = ix % 32;
      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    12bd <main+0x18d>
mov    -0x20054(%rbp),%eax
mov    %eax,-0x20060(%rbp)
cmpl   $0x0,-0x2005c(%rbp)
je     125a <main+0x12a>
movl   $0x2,-0x20064(%rbp)
mov    -0x20054(%rbp),%eax
mov    -0x20064(%rbp),%ecx
shl    $0x5,%ecx
shl    $0x5,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20068(%rbp)
mov    -0x20054(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2006c(%rbp)
mov    -0x20054(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20070(%rbp)
mov    -0x20068(%rbp),%eax
shl    $0x10,%eax
mov    -0x2006c(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
or     -0x20070(%rbp),%eax
mov    %eax,-0x20060(%rbp)
movslq -0x20054(%rbp),%rax
mov    -0x20050(%rbp,%rax,4),%eax
cmp    -0x20060(%rbp),%eax
je     12a4 <main+0x174>
movl   $0x1,-0x20058(%rbp)
mov    -0x20054(%rbp),%esi
movslq -0x20054(%rbp),%rax
mov    -0x20050(%rbp,%rax,4),%edx
mov    -0x20060(%rbp),%ecx
lea    0xd61(%rip),%rdi
call   1030 <printf@plt>
jmp    12a9 <main+0x179>
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   %r15
push   %r14
push   %r12
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,%r15d
mov    %rbx,%rdi
call   1040 <gang@plt>
xor    %r12d,%r12d
lea    0xe7d(%rip),%r14
xor    %ebp,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
jmp    11a4 <main+0x64>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
add    $0x8,%ebp
add    $0x20,%r12d
cmp    $0x8011,%rbx
je     11ea <main+0xaa>
test   %r15d,%r15d
je     11d0 <main+0x90>
mov    %ebx,%edx
and    $0x1f,%edx
mov    %r12d,%esi
and    $0x7fff0000,%esi
mov    %ebp,%ecx
and    $0x1f00,%ecx
or     %edx,%ecx
or     %esi,%ecx
jmp    11d2 <main+0x92>
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    %r12
pop    %r14
pop    %r15
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
%10 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %3
br label %11
11: ; preds = %18, %0
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 32785
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 %16
store i32 -1, i32* %17
br label %18
18: ; preds = %14
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %11
21: ; preds = %11
%22 = call i32 @__builtin_acc_on_device(i32 5)
store i32 %22, i32* %5
%23 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 0
%24 = call i32 @gang(i32* %23)
store i32 0, i32* %3
br label %25
25: ; preds = %66, %21
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, 32785
br i1 %27, label %28, label %69
28: ; preds = %25
%29 = load i32, i32* %3
store i32 %29, i32* %6
%30 = load i32, i32* %5
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %50
32: ; preds = %28
store i32 2, i32* %7
%33 = load i32, i32* %3
%34 = load i32, i32* %7
%35 = mul nsw i32 %34, 32
%36 = mul nsw i32 %35, 32
%37 = sdiv i32 %33, %36
store i32 %37, i32* %8
%38 = load i32, i32* %3
%39 = sdiv i32 %38, 32
%40 = srem i32 %39, 32
store i32 %40, i32* %9
%41 = load i32, i32* %3
%42 = srem i32 %41, 32
store i32 %42, i32* %10
%43 = load i32, i32* %8
%44 = shl i32 %43, 16
%45 = load i32, i32* %9
%46 = shl i32 %45, 8
%47 = or i32 %44, %46
%48 = load i32, i32* %10
%49 = or i32 %47, %48
store i32 %49, i32* %6
br label %50
50: ; preds = %32, %28
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = load i32, i32* %6
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %65
57: ; preds = %50
store i32 1, i32* %4
%58 = load i32, i32* %3
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 %60
%62 = load i32, i32* %61
%63 = load i32, i32* %6
%64 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %58, i32 %62, i32 %63)
br label %65
65: ; preds = %57, %50
br label %66
66: ; preds = %65
%67 = load i32, i32* %3
%68 = add nsw i32 %67, 1
store i32 %68, i32* %3
br label %25
69: ; preds = %25
%70 = load i32, i32* %4
ret i32 %70
}

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, %28
%8 = phi i64 [ 0, %0 ], [ %30, %28 ]
%9 = phi i32 [ 0, %0 ], [ %29, %28 ]
%10 = trunc i64 %8 to i32
br i1 %6, label %21, label %11
11: ; preds = %7
%12 = and i32 %10, 31
%13 = trunc i64 %8 to i32
%14 = shl i32 %13, 5
%15 = and i32 %14, 2147418112
%16 = trunc i64 %8 to i32
%17 = shl i32 %16, 3
%18 = and i32 %17, 7936
%19 = or i32 %18, %12
%20 = or i32 %19, %15
br label %21
21: ; preds = %11, %7
%22 = phi i32 [ %20, %11 ], [ %10, %7 ]
%23 = getelementptr inbounds [32785 x i32], [32785 x i32]* %1, i64 0, i64 %8
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, %22
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %24, i32 %22)
br label %28
28: ; preds = %26, %21
%29 = phi i32 [ 1, %26 ], [ %9, %21 ]
%30 = add nuw nsw i64 %8, 1
%31 = icmp eq i64 %30, 32785
br i1 %31, label %32, label %7
32: ; preds = %28
ret i32 %29
}