#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 *reduction() {
  char *string[33] = {"__global__ void TODO:kernel_name(float *in, float *out, "
                      "unsigned int num_elms) {",
                      "",
                      "	int GlobalID = blockIdx.x * blockDim.x + threadIdx.x;",
                      "	int stride = gridDim.x * blockDim.x;",
                      "",
                      "	extern __shared__ float reduction_share[];",
                      "	float thread_sum = 0.0;",
                      "",
                      "	int idx = GlobalID;",
                      "",
                      "	while( idx < num_elms ){",
                      "		thread_sum += in[idx];",
                      "		idx = idx + stride;",
                      "	}",
                      "",
                      "	reduction_share[threadIdx.x] = thread_sum;",
                      "	__syncthreads();",
                      "",
                      "	int num_threads_on = blockDim.x / 2;",
                      "",
                      "	while(num_threads_on > 0) {",
                      "",
                      "		if(threadIdx.x < num_threads_on) {",
                      "			reduction_share[threadIdx.x] += "
                      "reduction_share[threadIdx.x + num_threads_on];",
                      "		}",
                      "",
                      "		num_threads_on = num_threads_on / 2;",
                      "		__syncthreads();",
                      "	}",
                      "",
                      "	if(threadIdx.x == 0)",
                      "		out[blockIdx.x] = reduction_share[0];",
                      "}"};
  return buildString(string, 33);
}


x86 assembly (clang, -O0):
<reduction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    -0x110(%rbp),%rdi
lea    0x2bd7(%rip),%rsi
mov    $0x108,%edx
call   1040 <memcpy@plt>
lea    -0x110(%rbp),%rdi
mov    $0x21,%esi
call   1030 <buildString@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reduction>:
push   %rbx
sub    $0x110,%rsp
lea    0x2be1(%rip),%rsi
mov    %rsp,%rbx
mov    $0x108,%edx
mov    %rbx,%rdi
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x21,%esi
call   1030 <buildString@plt>
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [81 x i8] c"__global__ void TODO:kernel_name(float *in, float *out, unsigned int num_elms) {\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [55 x i8] c"\09int GlobalID = blockIdx.x * blockDim.x + threadIdx.x;\00"
@.str.3 = [38 x i8] c"\09int stride = gridDim.x * blockDim.x;\00"
@.str.4 = [44 x i8] c"\09extern __shared__ float reduction_share[];\00"
@.str.5 = [25 x i8] c"\09float thread_sum = 0.0;\00"
@.str.6 = [21 x i8] c"\09int idx = GlobalID;\00"
@.str.7 = [26 x i8] c"\09while( idx < num_elms ){\00"
@.str.8 = [25 x i8] c"\09\09thread_sum += in[idx];\00"
@.str.9 = [22 x i8] c"\09\09idx = idx + stride;\00"
@.str.10 = [3 x i8] c"\09}\00"
@.str.11 = [44 x i8] c"\09reduction_share[threadIdx.x] = thread_sum;\00"
@.str.12 = [18 x i8] c"\09__syncthreads();\00"
@.str.13 = [38 x i8] c"\09int num_threads_on = blockDim.x / 2;\00"
@.str.14 = [29 x i8] c"\09while(num_threads_on > 0) {\00"
@.str.15 = [37 x i8] c"\09\09if(threadIdx.x < num_threads_on) {\00"
@.str.16 = [82 x i8] c"\09\09\09reduction_share[threadIdx.x] += reduction_share[threadIdx.x + num_threads_on];\00"
@.str.17 = [4 x i8] c"\09\09}\00"
@.str.18 = [39 x i8] c"\09\09num_threads_on = num_threads_on / 2;\00"
@.str.19 = [19 x i8] c"\09\09__syncthreads();\00"
@.str.20 = [22 x i8] c"\09if(threadIdx.x == 0)\00"
@.str.21 = [40 x i8] c"\09\09out[blockIdx.x] = reduction_share[0];\00"
@.str.22 = [2 x i8] c"}\00"
@__const.reduction.string = [33 x i8*] [i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.22, i32 0, i32 0)]
define i8* @reduction() {
%1 = alloca [33 x i8*]
%2 = bitcast [33 x i8*]* %1 to i8*
%3 = getelementptr inbounds [33 x i8*], [33 x i8*]* %1, i64 0, i64 0
%4 = call i8* @buildString(i8** %3, i32 33)
ret i8* %4
}

LLVM IR (-O2):
@.str = [81 x i8] c"__global__ void TODO:kernel_name(float *in, float *out, unsigned int num_elms) {\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [55 x i8] c"\09int GlobalID = blockIdx.x * blockDim.x + threadIdx.x;\00"
@.str.3 = [38 x i8] c"\09int stride = gridDim.x * blockDim.x;\00"
@.str.4 = [44 x i8] c"\09extern __shared__ float reduction_share[];\00"
@.str.5 = [25 x i8] c"\09float thread_sum = 0.0;\00"
@.str.6 = [21 x i8] c"\09int idx = GlobalID;\00"
@.str.7 = [26 x i8] c"\09while( idx < num_elms ){\00"
@.str.8 = [25 x i8] c"\09\09thread_sum += in[idx];\00"
@.str.9 = [22 x i8] c"\09\09idx = idx + stride;\00"
@.str.10 = [3 x i8] c"\09}\00"
@.str.11 = [44 x i8] c"\09reduction_share[threadIdx.x] = thread_sum;\00"
@.str.12 = [18 x i8] c"\09__syncthreads();\00"
@.str.13 = [38 x i8] c"\09int num_threads_on = blockDim.x / 2;\00"
@.str.14 = [29 x i8] c"\09while(num_threads_on > 0) {\00"
@.str.15 = [37 x i8] c"\09\09if(threadIdx.x < num_threads_on) {\00"
@.str.16 = [82 x i8] c"\09\09\09reduction_share[threadIdx.x] += reduction_share[threadIdx.x + num_threads_on];\00"
@.str.17 = [4 x i8] c"\09\09}\00"
@.str.18 = [39 x i8] c"\09\09num_threads_on = num_threads_on / 2;\00"
@.str.19 = [19 x i8] c"\09\09__syncthreads();\00"
@.str.20 = [22 x i8] c"\09if(threadIdx.x == 0)\00"
@.str.21 = [40 x i8] c"\09\09out[blockIdx.x] = reduction_share[0];\00"
@.str.22 = [2 x i8] c"}\00"
@__const.reduction.string = [33 x i8*] [i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.22, i32 0, i32 0)]
define i8* @reduction() {
%1 = alloca [33 x i8*]
%2 = bitcast [33 x i8*]* %1 to i8*
%3 = getelementptr inbounds [33 x i8*], [33 x i8*]* %1, i64 0, i64 0
%4 = call i8* @buildString(i8** %3, i32 33)
ret i8* %4
}