#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 */
char *buildString(char **, int);
char *genMaps(int dimension) {
  char *oneD_to_oneD[9] = {
      "int NumThreadsPerBlock = blockDim.x;",
      "int GlobalID_1D = blockIdx.x * blockDim.x + threadIdx.x;",
      "int stride = gridDim.x * NumThreadsPerBlock;",
      "int idx = GlobalID_1D;",
      "",
      "while( idx < num_elms){",
      "	c[idx] = a[idx] + b[idx];",
      "	idx += stride;",
      "}"};
  char *twoD_to_oneD[12] = {
      "int NumThreadsPerBlock = blockDim.x * blockDim.y;",
      "int globalID_2D = blockIdx.y * gridDim.x * NumThreadsPerBlock",
      "		+ blockIdx.x * NumThreadsPerBlock",
      "		+ threadIdx.y * blockDim.x",
      "		+ threadIdx.x;",
      "int stride = gridDim.x * gridDim.y * NumThreadsPerBlock;",
      "int idx = globalID_2D;",
      "",
      "while( idx < num_elms){",
      "	c[idx] = a[idx] + b[idx];",
      "	idx += stride;",
      "}"};
  char *threeD_to_oneD[14] = {
      "int NumThreadsPerBlock = blockDim.x * blockDim.y * blockDim.z;",
      "int globalID_3D =  blockIdx.z * gridDim.x * gridDim.y * "
      "NumThreadsPerBlock",
      "		+ blockIdx.y * gridDim.x * NumThreadsPerBlock",
      "		+ blockIdx.x * NumThreadsPerBlock",
      "		+ threadIdx.z * blockDim.x * blockDim.y",
      "		+ threadIdx.y * blockDim.x",
      "		+ threadIdx.x;",
      "int stride = gridDim.x * gridDim.y * gridDim.z * NumThreadsPerBlock;",
      "int idx = globalID_3D;",
      "",
      "while( idx < num_elms){",
      "	c[idx] = a[idx] + b[idx];",
      "	idx += stride;",
      "}"};
  switch (dimension) {
  case 1:
    return buildString(oneD_to_oneD, 9);
  case 2:
    return buildString(twoD_to_oneD, 12);
  case 3:
    return buildString(threeD_to_oneD, 14);
  default:
    return "Invalid dimension!\n";
  }
}


x86 assembly (clang, -O0):
<genMaps>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2c1b(%rip),%rax
mov    %rax,-0x20(%rbp)
movaps 0x2c00(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movaps 0x2be5(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movaps 0x2bca(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0x2baf(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0x2c44(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movaps 0x2c29(%rip),%xmm0
movaps %xmm0,-0x80(%rbp)
movaps 0x2c0e(%rip),%xmm0
movaps %xmm0,-0x90(%rbp)
movaps 0x2bf0(%rip),%xmm0
movaps %xmm0,-0xa0(%rbp)
movaps 0x2bd2(%rip),%xmm0
movaps %xmm0,-0xb0(%rbp)
movaps 0x2bb4(%rip),%xmm0
movaps %xmm0,-0xc0(%rbp)
movaps 0x2c66(%rip),%xmm0
movaps %xmm0,-0xd0(%rbp)
movaps 0x2c48(%rip),%xmm0
movaps %xmm0,-0xe0(%rbp)
movaps 0x2c2a(%rip),%xmm0
movaps %xmm0,-0xf0(%rbp)
movaps 0x2c0c(%rip),%xmm0
movaps %xmm0,-0x100(%rbp)
movaps 0x2bee(%rip),%xmm0
movaps %xmm0,-0x110(%rbp)
movaps 0x2bd0(%rip),%xmm0
movaps %xmm0,-0x120(%rbp)
movaps 0x2bb2(%rip),%xmm0
movaps %xmm0,-0x130(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x134(%rbp)
sub    $0x1,%eax
je     1244 <genMaps+0x134>
jmp    121c <genMaps+0x10c>
mov    -0x134(%rbp),%eax
sub    $0x2,%eax
je     125b <genMaps+0x14b>
jmp    1230 <genMaps+0x120>
mov    -0x134(%rbp),%eax
sub    $0x3,%eax
je     1275 <genMaps+0x165>
jmp    128f <genMaps+0x17f>
lea    -0x60(%rbp),%rdi
mov    $0x9,%esi
call   1030 <buildString@plt>
mov    %rax,-0x8(%rbp)
jmp    129a <genMaps+0x18a>
lea    -0xc0(%rbp),%rdi
mov    $0xc,%esi
call   1030 <buildString@plt>
mov    %rax,-0x8(%rbp)
jmp    129a <genMaps+0x18a>
lea    -0x130(%rbp),%rdi
mov    $0xe,%esi
call   1030 <buildString@plt>
mov    %rax,-0x8(%rbp)
jmp    129a <genMaps+0x18a>
lea    0x10a3(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genMaps>:
sub    $0x128,%rsp
mov    0x2c22(%rip),%rax
mov    %rax,0x40(%rsp)
movaps 0x2c06(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2bea(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0x2bce(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2bb2(%rip),%xmm0
movaps %xmm0,(%rsp)
movaps 0x2c47(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0x2c28(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0x2c09(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movaps 0x2bea(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0x2bce(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0x2bb2(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0x2c66(%rip),%xmm0
movaps %xmm0,0x110(%rsp)
movaps 0x2c47(%rip),%xmm0
movaps %xmm0,0x100(%rsp)
movaps 0x2c28(%rip),%xmm0
movaps %xmm0,0xf0(%rsp)
movaps 0x2c09(%rip),%xmm0
movaps %xmm0,0xe0(%rsp)
movaps 0x2bea(%rip),%xmm0
movaps %xmm0,0xd0(%rsp)
movaps 0x2bcb(%rip),%xmm0
movaps %xmm0,0xc0(%rsp)
movaps 0x2bac(%rip),%xmm0
movaps %xmm0,0xb0(%rsp)
cmp    $0x3,%edi
je     1225 <genMaps+0x115>
cmp    $0x2,%edi
je     1234 <genMaps+0x124>
cmp    $0x1,%edi
jne    124b <genMaps+0x13b>
mov    %rsp,%rdi
mov    $0x9,%esi
jmp    123e <genMaps+0x12e>
lea    0xb0(%rsp),%rdi
mov    $0xe,%esi
jmp    123e <genMaps+0x12e>
lea    0x50(%rsp),%rdi
mov    $0xc,%esi
call   1030 <buildString@plt>
add    $0x128,%rsp
ret
lea    0x10e7(%rip),%rax
add    $0x128,%rsp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"int NumThreadsPerBlock = blockDim.x;\00"
@.str.1 = [57 x i8] c"int GlobalID_1D = blockIdx.x * blockDim.x + threadIdx.x;\00"
@.str.2 = [45 x i8] c"int stride = gridDim.x * NumThreadsPerBlock;\00"
@.str.3 = [23 x i8] c"int idx = GlobalID_1D;\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [24 x i8] c"while( idx < num_elms){\00"
@.str.6 = [27 x i8] c"\09c[idx] = a[idx] + b[idx];\00"
@.str.7 = [16 x i8] c"\09idx += stride;\00"
@.str.8 = [2 x i8] c"}\00"
@__const.genMaps.oneD_to_oneD = [9 x i8*] [i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [50 x i8] c"int NumThreadsPerBlock = blockDim.x * blockDim.y;\00"
@.str.10 = [62 x i8] c"int globalID_2D = blockIdx.y * gridDim.x * NumThreadsPerBlock\00"
@.str.11 = [36 x i8] c"\09\09+ blockIdx.x * NumThreadsPerBlock\00"
@.str.12 = [29 x i8] c"\09\09+ threadIdx.y * blockDim.x\00"
@.str.13 = [17 x i8] c"\09\09+ threadIdx.x;\00"
@.str.14 = [57 x i8] c"int stride = gridDim.x * gridDim.y * NumThreadsPerBlock;\00"
@.str.15 = [23 x i8] c"int idx = globalID_2D;\00"
@__const.genMaps.twoD_to_oneD = [12 x i8*] [i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.16 = [63 x i8] c"int NumThreadsPerBlock = blockDim.x * blockDim.y * blockDim.z;\00"
@.str.17 = [75 x i8] c"int globalID_3D = blockIdx.z * gridDim.x * gridDim.y * NumThreadsPerBlock\00"
@.str.18 = [48 x i8] c"\09\09+ blockIdx.y * gridDim.x * NumThreadsPerBlock\00"
@.str.19 = [42 x i8] c"\09\09+ threadIdx.z * blockDim.x * blockDim.y\00"
@.str.20 = [69 x i8] c"int stride = gridDim.x * gridDim.y * gridDim.z * NumThreadsPerBlock;\00"
@.str.21 = [23 x i8] c"int idx = globalID_3D;\00"
@__const.genMaps.threeD_to_oneD = [14 x i8*] [i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.22 = [20 x i8] c"Invalid dimension!\0A\00"
define i8* @genMaps(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [9 x i8*]
%5 = alloca [12 x i8*]
%6 = alloca [14 x i8*]
store i32 %0, i32* %3
%7 = bitcast [9 x i8*]* %4 to i8*
%8 = bitcast [12 x i8*]* %5 to i8*
%9 = bitcast [14 x i8*]* %6 to i8*
%10 = load i32, i32* %3
switch i32 %10, label %20 [
i32 1, label %11
i32 2, label %14
i32 3, label %17
]
11: ; preds = %1
%12 = getelementptr inbounds [9 x i8*], [9 x i8*]* %4, i64 0, i64 0
%13 = call i8* @buildString(i8** %12, i32 9)
store i8* %13, i8** %2
br label %21
14: ; preds = %1
%15 = getelementptr inbounds [12 x i8*], [12 x i8*]* %5, i64 0, i64 0
%16 = call i8* @buildString(i8** %15, i32 12)
store i8* %16, i8** %2
br label %21
17: ; preds = %1
%18 = getelementptr inbounds [14 x i8*], [14 x i8*]* %6, i64 0, i64 0
%19 = call i8* @buildString(i8** %18, i32 14)
store i8* %19, i8** %2
br label %21
20: ; preds = %1
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.22, i64 0, i64 0), i8** %2
br label %21
21: ; preds = %20, %17, %14, %11
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [37 x i8] c"int NumThreadsPerBlock = blockDim.x;\00"
@.str.1 = [57 x i8] c"int GlobalID_1D = blockIdx.x * blockDim.x + threadIdx.x;\00"
@.str.2 = [45 x i8] c"int stride = gridDim.x * NumThreadsPerBlock;\00"
@.str.3 = [23 x i8] c"int idx = GlobalID_1D;\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [24 x i8] c"while( idx < num_elms){\00"
@.str.6 = [27 x i8] c"\09c[idx] = a[idx] + b[idx];\00"
@.str.7 = [16 x i8] c"\09idx += stride;\00"
@.str.8 = [2 x i8] c"}\00"
@__const.genMaps.oneD_to_oneD = [9 x i8*] [i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.9 = [50 x i8] c"int NumThreadsPerBlock = blockDim.x * blockDim.y;\00"
@.str.10 = [62 x i8] c"int globalID_2D = blockIdx.y * gridDim.x * NumThreadsPerBlock\00"
@.str.11 = [36 x i8] c"\09\09+ blockIdx.x * NumThreadsPerBlock\00"
@.str.12 = [29 x i8] c"\09\09+ threadIdx.y * blockDim.x\00"
@.str.13 = [17 x i8] c"\09\09+ threadIdx.x;\00"
@.str.14 = [57 x i8] c"int stride = gridDim.x * gridDim.y * NumThreadsPerBlock;\00"
@.str.15 = [23 x i8] c"int idx = globalID_2D;\00"
@__const.genMaps.twoD_to_oneD = [12 x i8*] [i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.16 = [63 x i8] c"int NumThreadsPerBlock = blockDim.x * blockDim.y * blockDim.z;\00"
@.str.17 = [75 x i8] c"int globalID_3D = blockIdx.z * gridDim.x * gridDim.y * NumThreadsPerBlock\00"
@.str.18 = [48 x i8] c"\09\09+ blockIdx.y * gridDim.x * NumThreadsPerBlock\00"
@.str.19 = [42 x i8] c"\09\09+ threadIdx.z * blockDim.x * blockDim.y\00"
@.str.20 = [69 x i8] c"int stride = gridDim.x * gridDim.y * gridDim.z * NumThreadsPerBlock;\00"
@.str.21 = [23 x i8] c"int idx = globalID_3D;\00"
@__const.genMaps.threeD_to_oneD = [14 x i8*] [i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i32 0, i32 0)]
@.str.22 = [20 x i8] c"Invalid dimension!\0A\00"
define i8* @genMaps(i32 %0) {
%2 = alloca [9 x i8*]
%3 = alloca [12 x i8*]
%4 = alloca [14 x i8*]
%5 = bitcast [9 x i8*]* %2 to i8*
%6 = bitcast [12 x i8*]* %3 to i8*
%7 = bitcast [14 x i8*]* %4 to i8*
switch i32 %0, label %17 [
i32 1, label %8
i32 2, label %11
i32 3, label %14
]
8: ; preds = %1
%9 = getelementptr inbounds [9 x i8*], [9 x i8*]* %2, i64 0, i64 0
%10 = call i8* @buildString(i8** %9, i32 9)
br label %17
11: ; preds = %1
%12 = getelementptr inbounds [12 x i8*], [12 x i8*]* %3, i64 0, i64 0
%13 = call i8* @buildString(i8** %12, i32 12)
br label %17
14: ; preds = %1
%15 = getelementptr inbounds [14 x i8*], [14 x i8*]* %4, i64 0, i64 0
%16 = call i8* @buildString(i8** %15, i32 14)
br label %17
17: ; preds = %1, %14, %11, %8
%18 = phi i8* [ %16, %14 ], [ %13, %11 ], [ %10, %8 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.22, i64 0, i64 0), %1 ]
ret i8* %18
}