#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 */
struct timeval {
  int tv_sec;
  double tv_usec;
};

/* Variables and functions */
scalar_t__ RAND_MAX;
int /*<<< orphan*/ acc_device_default;
int /*<<< orphan*/ acc_init(int /*<<< orphan*/);
double fabs(double);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/);
int main() {
  int i, j, k;
  double sum, known_sum;
  int NI;
  int error;
  double *input;
  double rounding_error = 1.E-9;
  struct timeval tim;
  double start, end;
  NI = 1 << 20;
  error = 0;
  input = (double *)malloc(NI * sizeof(double));
  acc_init(acc_device_default);
  srand((unsigned)time(0));
  for (i = 0; i < NI; i++) {
    input[i] = (double)rand() / (double)RAND_MAX + 0.1;
  }
  sum = 0;
  gettimeofday(&tim, NULL);
  start = tim.tv_sec * 1000 + (tim.tv_usec / 1000.0);
#pragma acc parallel copyin(input [0:NI]) num_gangs(192) num_workers(8)        \
    vector_length(32)
  {
#pragma acc loop gang worker reduction(+ : sum)
    for (i = 0; i < NI; i++)
      sum += input[i];
  }
  gettimeofday(&tim, NULL);
  end = tim.tv_sec * 1000 + (tim.tv_usec / 1000.0);
  known_sum = 0;
  for (i = 0; i < NI; i++) {
    known_sum += input[i];
  }
  if (fabs(sum - known_sum) > rounding_error) {
    error++;
    printf("same_gang_worker + FAILED! sum=%d, known_sum=%d\n", sum, known_sum);
  }
  printf("same_gang_worker + execution time is :%.2lf: ms\n", end - start);
  if (error == 0)
    printf("same_gang_worker + SUCCESS!\n");
  free(input);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movsd  0xe69(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x100000,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1090 <acc_init@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    122f <main+0xaf>
mov    $0x0,%al
call   10a0 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2de9(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe00(%rip),%xmm1
addsd  %xmm1,%xmm0
mov    -0x30(%rbp),%rax
movslq -0x8(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e0 <main+0x60>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
imul   $0x3e8,-0x48(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  -0x40(%rbp),%xmm1
movsd  0xdac(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12a1 <main+0x121>
mov    -0x30(%rbp),%rax
movslq -0x8(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1270 <main+0xf0>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
imul   $0x3e8,-0x48(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  -0x40(%rbp),%xmm1
movsd  0xd42(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1313 <main+0x193>
mov    -0x30(%rbp),%rax
movslq -0x8(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12e2 <main+0x162>
movsd  -0x18(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movaps 0xcfc(%rip),%xmm1
pand   %xmm1,%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    1354 <main+0x1d4>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
lea    0xce3(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movsd  -0x58(%rbp),%xmm0
subsd  -0x50(%rbp),%xmm0
lea    0xcfc(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1384 <main+0x204>
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    $0x800000,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   10a0 <acc_init@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    0x2e1f(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq (%r14),%xmm1
divsd  %xmm1,%xmm0
addsd  0xe0d(%rip),%xmm0
movsd  %xmm0,(%r15,%rbx,8)
add    $0x1,%rbx
cmp    $0x100000,%rbx
jne    11d0 <main+0x40>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0x3e8,0x18(%rsp),%eax
movsd  0x20(%rsp),%xmm0
divsd  0xde0(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm0,%xmm0
mov    $0x7,%ecx
nopw   0x0(%rax,%rax,1)
addsd  -0x38(%r15,%rcx,8),%xmm0
addsd  -0x30(%r15,%rcx,8),%xmm0
addsd  -0x28(%r15,%rcx,8),%xmm0
addsd  -0x20(%r15,%rcx,8),%xmm0
addsd  -0x18(%r15,%rcx,8),%xmm0
addsd  -0x10(%r15,%rcx,8),%xmm0
addsd  -0x8(%r15,%rcx,8),%xmm0
addsd  (%r15,%rcx,8),%xmm0
add    $0x8,%rcx
cmp    $0x100007,%rcx
jne    1240 <main+0xb0>
movsd  %xmm0,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x28(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
imul   $0x3e8,0x18(%rsp),%eax
movsd  0x20(%rsp),%xmm5
divsd  0xd4f(%rip),%xmm5
xorpd  %xmm1,%xmm1
mov    $0x7,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
addsd  -0x38(%r15,%rcx,8),%xmm1
addsd  -0x30(%r15,%rcx,8),%xmm1
addsd  -0x28(%r15,%rcx,8),%xmm1
addsd  -0x20(%r15,%rcx,8),%xmm1
addsd  -0x18(%r15,%rcx,8),%xmm1
addsd  -0x10(%r15,%rcx,8),%xmm1
addsd  -0x8(%r15,%rcx,8),%xmm1
addsd  (%r15,%rcx,8),%xmm1
add    $0x8,%rcx
cmp    $0x100007,%rcx
jne    12d0 <main+0x140>
cvtsi2sd %eax,%xmm4
movsd  0x10(%rsp),%xmm3
addsd  0x28(%rsp),%xmm3
movsd  0x8(%rsp),%xmm0
movapd %xmm0,%xmm2
subsd  %xmm1,%xmm2
andpd  0xce6(%rip),%xmm2
addsd  %xmm4,%xmm5
ucomisd 0xcca(%rip),%xmm2
jbe    137e <main+0x1ee>
lea    0xce1(%rip),%rdi
mov    $0x2,%al
movsd  %xmm3,0x10(%rsp)
movsd  %xmm5,0x8(%rsp)
call   1050 <printf@plt>
movsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
lea    0xcec(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
jmp    13a0 <main+0x210>
subsd  %xmm3,%xmm5
lea    0xcd8(%rip),%rdi
movapd %xmm5,%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xcf7(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, double }
@acc_device_default = global i32 0
@RAND_MAX = global i64 0
@.str = [49 x i8] c"same_gang_worker + FAILED! sum=%d, known_sum=%d\0A\00"
@.str.1 = [49 x i8] c"same_gang_worker + execution time is :%.2lf: ms\0A\00"
@.str.2 = [29 x i8] c"same_gang_worker + SUCCESS!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double*
%10 = alloca double
%11 = alloca %struct.timeval
%12 = alloca double
%13 = alloca double
store i32 0, i32* %1
store double 1.000000e-09, double* %10
store i32 1048576, i32* %7
store i32 0, i32* %8
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 8
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to double*
store double* %19, double** %9
%20 = load i32, i32* @acc_device_default
%21 = call i32 @acc_init(i32 %20)
%22 = call i64 @time(i32 0)
%23 = trunc i64 %22 to i32
%24 = call i32 @srand(i32 %23)
store i32 0, i32* %2
br label %25
25: ; preds = %40, %0
%26 = load i32, i32* %2
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = call i64 (...) @rand()
%31 = sitofp i64 %30 to double
%32 = load i64, i64* @RAND_MAX
%33 = sitofp i64 %32 to double
%34 = fdiv double %31, %33
%35 = fadd double %34, 1.000000e-01
%36 = load double*, double** %9
%37 = load i32, i32* %2
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds double, double* %36, i64 %38
store double %35, double* %39
br label %40
40: ; preds = %29
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
br label %25
43: ; preds = %25
store double 0.000000e+00, double* %5
%44 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%46 = load i32, i32* %45
%47 = mul nsw i32 %46, 1000
%48 = sitofp i32 %47 to double
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%50 = load double, double* %49
%51 = fdiv double %50, 1.000000e+03
%52 = fadd double %48, %51
store double %52, double* %12
store i32 0, i32* %2
br label %53
53: ; preds = %65, %43
%54 = load i32, i32* %2
%55 = load i32, i32* %7
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %53
%58 = load double*, double** %9
%59 = load i32, i32* %2
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds double, double* %58, i64 %60
%62 = load double, double* %61
%63 = load double, double* %5
%64 = fadd double %63, %62
store double %64, double* %5
br label %65
65: ; preds = %57
%66 = load i32, i32* %2
%67 = add nsw i32 %66, 1
store i32 %67, i32* %2
br label %53
68: ; preds = %53
%69 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
%71 = load i32, i32* %70
%72 = mul nsw i32 %71, 1000
%73 = sitofp i32 %72 to double
%74 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%75 = load double, double* %74
%76 = fdiv double %75, 1.000000e+03
%77 = fadd double %73, %76
store double %77, double* %13
store double 0.000000e+00, double* %6
store i32 0, i32* %2
br label %78
78: ; preds = %90, %68
%79 = load i32, i32* %2
%80 = load i32, i32* %7
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %93
82: ; preds = %78
%83 = load double*, double** %9
%84 = load i32, i32* %2
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds double, double* %83, i64 %85
%87 = load double, double* %86
%88 = load double, double* %6
%89 = fadd double %88, %87
store double %89, double* %6
br label %90
90: ; preds = %82
%91 = load i32, i32* %2
%92 = add nsw i32 %91, 1
store i32 %92, i32* %2
br label %78
93: ; preds = %78
%94 = load double, double* %5
%95 = load double, double* %6
%96 = fsub double %94, %95
%97 = call double @llvm.fabs.f64(double %96)
%98 = load double, double* %10
%99 = fcmp ogt double %97, %98
br i1 %99, label %100, label %106
100: ; preds = %93
%101 = load i32, i32* %8
%102 = add nsw i32 %101, 1
store i32 %102, i32* %8
%103 = load double, double* %5
%104 = load double, double* %6
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), double %103, double %104)
br label %106
106: ; preds = %100, %93
%107 = load double, double* %13
%108 = load double, double* %12
%109 = fsub double %107, %108
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), double %109)
%111 = load i32, i32* %8
%112 = icmp eq i32 %111, 0
br i1 %112, label %113, label %115
113: ; preds = %106
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %115
115: ; preds = %113, %106
%116 = load double*, double** %9
%117 = call i32 @free(double* %116)
%118 = load i32, i32* %1
ret i32 %118
}

LLVM IR (-O2):
%struct.timeval = type { i32, double }
@acc_device_default = global i32 0
@RAND_MAX = global i64 0
@.str = [49 x i8] c"same_gang_worker + FAILED! sum=%d, known_sum=%d\0A\00"
@.str.1 = [49 x i8] c"same_gang_worker + execution time is :%.2lf: ms\0A\00"
@str = [28 x i8] c"same_gang_worker + SUCCESS!\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = tail call i64 @malloc(i32 8388608)
%4 = inttoptr i64 %3 to double*
%5 = load i32, i32* @acc_device_default
%6 = tail call i32 @acc_init(i32 %5)
%7 = tail call i64 @time(i32 0)
%8 = trunc i64 %7 to i32
%9 = tail call i32 @srand(i32 %8)
br label %10
10: ; preds = %0, %10
%11 = phi i64 [ 0, %0 ], [ %19, %10 ]
%12 = tail call i64 (...) @rand()
%13 = sitofp i64 %12 to double
%14 = load i64, i64* @RAND_MAX
%15 = sitofp i64 %14 to double
%16 = fdiv double %13, %15
%17 = fadd double %16, 1.000000e-01
%18 = getelementptr inbounds double, double* %4, i64 %11
store double %17, double* %18
%19 = add nuw nsw i64 %11, 1
%20 = icmp eq i64 %19, 1048576
br i1 %20, label %21, label %10
21: ; preds = %10
%22 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = mul nsw i32 %24, 1000
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%27 = load double, double* %26
%28 = fdiv double %27, 1.000000e+03
br label %29
29: ; preds = %29, %21
%30 = phi i64 [ 0, %21 ], [ %63, %29 ]
%31 = phi double [ 0.000000e+00, %21 ], [ %62, %29 ]
%32 = getelementptr inbounds double, double* %4, i64 %30
%33 = load double, double* %32
%34 = fadd double %31, %33
%35 = or i64 %30, 1
%36 = getelementptr inbounds double, double* %4, i64 %35
%37 = load double, double* %36
%38 = fadd double %34, %37
%39 = or i64 %30, 2
%40 = getelementptr inbounds double, double* %4, i64 %39
%41 = load double, double* %40
%42 = fadd double %38, %41
%43 = or i64 %30, 3
%44 = getelementptr inbounds double, double* %4, i64 %43
%45 = load double, double* %44
%46 = fadd double %42, %45
%47 = or i64 %30, 4
%48 = getelementptr inbounds double, double* %4, i64 %47
%49 = load double, double* %48
%50 = fadd double %46, %49
%51 = or i64 %30, 5
%52 = getelementptr inbounds double, double* %4, i64 %51
%53 = load double, double* %52
%54 = fadd double %50, %53
%55 = or i64 %30, 6
%56 = getelementptr inbounds double, double* %4, i64 %55
%57 = load double, double* %56
%58 = fadd double %54, %57
%59 = or i64 %30, 7
%60 = getelementptr inbounds double, double* %4, i64 %59
%61 = load double, double* %60
%62 = fadd double %58, %61
%63 = add nuw nsw i64 %30, 8
%64 = icmp eq i64 %63, 1048576
br i1 %64, label %65, label %29
65: ; preds = %29
%66 = sitofp i32 %25 to double
%67 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%68 = load i32, i32* %23
%69 = mul nsw i32 %68, 1000
%70 = load double, double* %26
%71 = fdiv double %70, 1.000000e+03
br label %72
72: ; preds = %72, %65
%73 = phi i64 [ 0, %65 ], [ %106, %72 ]
%74 = phi double [ 0.000000e+00, %65 ], [ %105, %72 ]
%75 = getelementptr inbounds double, double* %4, i64 %73
%76 = load double, double* %75
%77 = fadd double %74, %76
%78 = or i64 %73, 1
%79 = getelementptr inbounds double, double* %4, i64 %78
%80 = load double, double* %79
%81 = fadd double %77, %80
%82 = or i64 %73, 2
%83 = getelementptr inbounds double, double* %4, i64 %82
%84 = load double, double* %83
%85 = fadd double %81, %84
%86 = or i64 %73, 3
%87 = getelementptr inbounds double, double* %4, i64 %86
%88 = load double, double* %87
%89 = fadd double %85, %88
%90 = or i64 %73, 4
%91 = getelementptr inbounds double, double* %4, i64 %90
%92 = load double, double* %91
%93 = fadd double %89, %92
%94 = or i64 %73, 5
%95 = getelementptr inbounds double, double* %4, i64 %94
%96 = load double, double* %95
%97 = fadd double %93, %96
%98 = or i64 %73, 6
%99 = getelementptr inbounds double, double* %4, i64 %98
%100 = load double, double* %99
%101 = fadd double %97, %100
%102 = or i64 %73, 7
%103 = getelementptr inbounds double, double* %4, i64 %102
%104 = load double, double* %103
%105 = fadd double %101, %104
%106 = add nuw nsw i64 %73, 8
%107 = icmp eq i64 %106, 1048576
br i1 %107, label %108, label %72
108: ; preds = %72
%109 = fadd double %28, %66
%110 = sitofp i32 %69 to double
%111 = fadd double %71, %110
%112 = fsub double %62, %105
%113 = tail call double @llvm.fabs.f64(double %112)
%114 = fcmp ogt double %113, 1.000000e-09
br i1 %114, label %115, label %119
115: ; preds = %108
%116 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), double %62, double %105)
%117 = fsub double %111, %109
%118 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), double %117)
br label %123
119: ; preds = %108
%120 = fsub double %111, %109
%121 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), double %120)
%122 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %123
123: ; preds = %115, %119
%124 = tail call i32 @free(double* %4)
ret i32 0
}