#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;
      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   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    1290 <main+0x160>
mov    -0x20054(%rbp),%eax
mov    %eax,-0x20060(%rbp)
cmpl   $0x0,-0x2005c(%rbp)
je     122d <main+0xfd>
movl   $0x0,-0x20064(%rbp)
mov    -0x20054(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-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   %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    11a3 <main+0x63>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%rbx
add    $0x100,%ebp
cmp    $0x8011,%rbx
je     11cb <main+0x8b>
mov    %ebp,%edx
and    $0x1f00,%edx
test   %r15d,%r15d
mov    %ebx,%ecx
cmovne %edx,%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 = %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
store i32 0, i32* %7
%32 = load i32, i32* %3
%33 = srem i32 %32, 32
store i32 %33, 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 @worker(i32* %4)
%6 = icmp eq i32 %3, 0
br label %7
7: ; preds = %0, %20
%8 = phi i64 [ 0, %0 ], [ %22, %20 ]
%9 = phi i32 [ 0, %0 ], [ %21, %20 ]
%10 = trunc i64 %8 to i32
%11 = shl i32 %10, 8
%12 = and i32 %11, 7936
%13 = trunc i64 %8 to i32
%14 = select i1 %6, i32 %13, i32 %12
%15 = getelementptr inbounds [32785 x i32], [32785 x i32]* %1, i64 0, i64 %8
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, %14
br i1 %17, label %20, label %18
18: ; preds = %7
%19 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16, i32 %14)
br label %20
20: ; preds = %18, %7
%21 = phi i32 [ 1, %18 ], [ %9, %7 ]
%22 = add nuw nsw i64 %8, 1
%23 = icmp eq i64 %22, 32785
br i1 %23, label %24, label %7
24: ; preds = %20
ret i32 %21
}