#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*/ printf(char *, int, int, int);
int /*<<< orphan*/ worker(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_workers(32) vector_length(32) copy(ary) copy(ondev)
  {
    ondev = __builtin_acc_on_device(5);
    worker(ary);
  }
  for (ix = 0; ix < (32 * 32 * 32 + 17); ix++) {
    int expected = ix;
    if (ondev) {
      int g = 0;
      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   1040 <__builtin_acc_on_device@plt>
mov    %eax,-0x2005c(%rbp)
lea    -0x20050(%rbp),%rdi
call   1050 <worker@plt>
movl   $0x0,-0x20054(%rbp)
cmpl   $0x8011,-0x20054(%rbp)
jge    12a2 <main+0x172>
mov    -0x20054(%rbp),%eax
mov    %eax,-0x20060(%rbp)
cmpl   $0x0,-0x2005c(%rbp)
je     123f <main+0x10f>
movl   $0x0,-0x20064(%rbp)
mov    -0x20054(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20068(%rbp)
mov    -0x20054(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-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     1289 <main+0x159>
movl   $0x1,-0x20058(%rbp)
mov    -0x20054(%rbp),%esi
movslq -0x20054(%rbp),%rax
mov    -0x20050(%rbp,%rax,4),%edx
mov    -0x20060(%rbp),%ecx
lea    0xd7c(%rip),%rdi
call   1030 <printf@plt>
jmp    128e <main+0x15e>
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   %rbx
sub    $0x20048,%rsp
mov    %rsp,%rbx
mov    $0x20044,%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1040 <memset@plt>
mov    $0x5,%edi
call   1050 <__builtin_acc_on_device@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <worker@plt>
xor    %ebp,%ebp
lea    0xe80(%rip),%r14
xor    %ebx,%ebx
xor    %eax,%eax
jmp    11a0 <main+0x60>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
add    $0x8,%ebp
cmp    $0x8011,%rbx
je     11cd <main+0x8d>
mov    %ebx,%edx
and    $0x1f,%edx
mov    %ebp,%ecx
and    $0x1f00,%ecx
or     %edx,%ecx
test   %r15d,%r15d
cmove  %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    $0x20048,%rsp
pop    %rbx
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
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 @worker(i32* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %60, %20
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 32785
br i1 %26, label %27, label %63
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 %44
31: ; preds = %27
store i32 0, i32* %7
%32 = load i32, i32* %3
%33 = sdiv i32 %32, 32
%34 = srem i32 %33, 32
store i32 %34, i32* %8
%35 = load i32, i32* %3
%36 = srem i32 %35, 32
store i32 %36, i32* %9
%37 = load i32, i32* %7
%38 = shl i32 %37, 16
%39 = load i32, i32* %8
%40 = shl i32 %39, 8
%41 = or i32 %38, %40
%42 = load i32, i32* %9
%43 = or i32 %41, %42
store i32 %43, i32* %6
br label %44
44: ; preds = %31, %27
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = load i32, i32* %6
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %59
51: ; preds = %44
store i32 1, i32* %4
%52 = load i32, i32* %3
%53 = load i32, i32* %3
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [32785 x i32], [32785 x i32]* %2, i64 0, i64 %54
%56 = load i32, i32* %55
%57 = load i32, i32* %6
%58 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %52, i32 %56, i32 %57)
br label %59
59: ; preds = %51, %44
br label %60
60: ; preds = %59
%61 = load i32, i32* %3
%62 = add nsw i32 %61, 1
store i32 %62, i32* %3
br label %24
63: ; preds = %24
%64 = load i32, i32* %4
ret i32 %64
}

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