#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 *occupancy() {
  char *string[46] = {
      "#include \"stdio.h\"",
      "#if __CUDA_ARCH__ >= 650",
      "",
      "__global__ void MyKernel(int *array, int arrayCount)",
      "{ ",
      "	int idx = threadIdx.x + blockIdx.x * blockDim.x; ",
      "	if (idx < arrayCount) ",
      "	{ ",
      "		array[idx] *= array[idx]; ",
      "	} ",
      "}",
      "",
      "void main(int *array, int arrayCount) ",
      "{ ",
      "	int blockSize;   // The launch configurator returned block size ",
      "	int minGridSize; // The minimum grid size needed to achieve the ",
      "			 // maximum occupancy for a full device launch ",
      "	int gridSize;    // The actual grid size needed, based on input size ",
      "",
      "	cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, ",
      "                                      MyKernel, 0, arrayCount); ",
      "	// Round up according to array size ",
      "	gridSize = (arrayCount + blockSize - 1) / blockSize; ",
      "",
      "	MyKernel<<< gridSize, blockSize >>>(array, arrayCount); ",
      "",
      "	cudaDeviceSynchronize(); ",
      "",
      "	// calculate theoretical occupancy",
      "	int maxActiveBlocks;",
      "	cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, ",
      "                                                 MyKernel, blockSize, ",
      "                                                 0);",
      "",
      "	int device;",
      "	cudaDeviceProp props;",
      "	cudaGetDevice(&device);",
      "	cudaGetDeviceProperties(&props, device);",
      "",
      "	float occupancy = (maxActiveBlocks * blockSize / props.warpSize) / ",
      "                         (float)(props.maxThreadsPerMultiProcessor / ",
      "			  props.warpSize);",
      "",
      "	printf(\"Launched blocks of size %d. Theoretical occupancy: %f\\n\", "
      "blockSize, occupancy);",
      "}",
      "#endif"};
  return buildString(string, 46);
}


x86 assembly (clang, -O0):
<occupancy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
lea    -0x170(%rbp),%rdi
lea    0x2b77(%rip),%rsi
mov    $0x170,%edx
call   1040 <memcpy@plt>
lea    -0x170(%rbp),%rdi
mov    $0x2e,%esi
call   1030 <buildString@plt>
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<occupancy>:
push   %rbx
sub    $0x170,%rsp
lea    0x2b81(%rip),%rsi
mov    %rsp,%rbx
mov    $0x170,%edx
mov    %rbx,%rdi
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1030 <buildString@plt>
add    $0x170,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"#include \22stdio.h\22\00"
@.str.1 = [25 x i8] c"#if __CUDA_ARCH__ >= 650\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [53 x i8] c"__global__ void MyKernel(int *array, int arrayCount)\00"
@.str.4 = [3 x i8] c"{ \00"
@.str.5 = [51 x i8] c"\09int idx = threadIdx.x + blockIdx.x * blockDim.x; \00"
@.str.6 = [24 x i8] c"\09if (idx < arrayCount) \00"
@.str.7 = [4 x i8] c"\09{ \00"
@.str.8 = [29 x i8] c"\09\09array[idx] *= array[idx]; \00"
@.str.9 = [4 x i8] c"\09} \00"
@.str.10 = [2 x i8] c"}\00"
@.str.11 = [39 x i8] c"void main(int *array, int arrayCount) \00"
@.str.12 = [66 x i8] c"\09int blockSize; // The launch configurator returned block size \00"
@.str.13 = [66 x i8] c"\09int minGridSize; // The minimum grid size needed to achieve the \00"
@.str.14 = [51 x i8] c"\09\09\09 // maximum occupancy for a full device launch \00"
@.str.15 = [71 x i8] c"\09int gridSize; // The actual grid size needed, based on input size \00"
@.str.16 = [64 x i8] c"\09cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, \00"
@.str.17 = [65 x i8] c" MyKernel, 0, arrayCount); \00"
@.str.18 = [38 x i8] c"\09// Round up according to array size \00"
@.str.19 = [55 x i8] c"\09gridSize = (arrayCount + blockSize - 1) / blockSize; \00"
@.str.20 = [58 x i8] c"\09MyKernel<<< gridSize, blockSize >>>(array, arrayCount); \00"
@.str.21 = [27 x i8] c"\09cudaDeviceSynchronize(); \00"
@.str.22 = [36 x i8] c"\09// calculate theoretical occupancy\00"
@.str.23 = [22 x i8] c"\09int maxActiveBlocks;\00"
@.str.24 = [67 x i8] c"\09cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, \00"
@.str.25 = [71 x i8] c" MyKernel, blockSize, \00"
@.str.26 = [53 x i8] c" 0);\00"
@.str.27 = [13 x i8] c"\09int device;\00"
@.str.28 = [23 x i8] c"\09cudaDeviceProp props;\00"
@.str.29 = [25 x i8] c"\09cudaGetDevice(&device);\00"
@.str.30 = [42 x i8] c"\09cudaGetDeviceProperties(&props, device);\00"
@.str.31 = [69 x i8] c"\09float occupancy = (maxActiveBlocks * blockSize / props.warpSize) / \00"
@.str.32 = [70 x i8] c" (float)(props.maxThreadsPerMultiProcessor / \00"
@.str.33 = [22 x i8] c"\09\09\09 props.warpSize);\00"
@.str.34 = [90 x i8] c"\09printf(\22Launched blocks of size %d. Theoretical occupancy: %f\\n\22, blockSize, occupancy);\00"
@.str.35 = [7 x i8] c"#endif\00"
@__const.occupancy.string = [46 x i8*] [i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.22, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.23, i32 0, i32 0), i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.24, i32 0, i32 0), i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.25, i32 0, i32 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.26, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.27, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.28, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.29, i32 0, i32 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.30, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.31, i32 0, i32 0), i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.32, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.33, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.34, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.35, i32 0, i32 0)]
define i8* @occupancy() {
%1 = alloca [46 x i8*]
%2 = bitcast [46 x i8*]* %1 to i8*
%3 = getelementptr inbounds [46 x i8*], [46 x i8*]* %1, i64 0, i64 0
%4 = call i8* @buildString(i8** %3, i32 46)
ret i8* %4
}

LLVM IR (-O2):
@.str = [19 x i8] c"#include \22stdio.h\22\00"
@.str.1 = [25 x i8] c"#if __CUDA_ARCH__ >= 650\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [53 x i8] c"__global__ void MyKernel(int *array, int arrayCount)\00"
@.str.4 = [3 x i8] c"{ \00"
@.str.5 = [51 x i8] c"\09int idx = threadIdx.x + blockIdx.x * blockDim.x; \00"
@.str.6 = [24 x i8] c"\09if (idx < arrayCount) \00"
@.str.7 = [4 x i8] c"\09{ \00"
@.str.8 = [29 x i8] c"\09\09array[idx] *= array[idx]; \00"
@.str.9 = [4 x i8] c"\09} \00"
@.str.10 = [2 x i8] c"}\00"
@.str.11 = [39 x i8] c"void main(int *array, int arrayCount) \00"
@.str.12 = [66 x i8] c"\09int blockSize; // The launch configurator returned block size \00"
@.str.13 = [66 x i8] c"\09int minGridSize; // The minimum grid size needed to achieve the \00"
@.str.14 = [51 x i8] c"\09\09\09 // maximum occupancy for a full device launch \00"
@.str.15 = [71 x i8] c"\09int gridSize; // The actual grid size needed, based on input size \00"
@.str.16 = [64 x i8] c"\09cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, \00"
@.str.17 = [65 x i8] c" MyKernel, 0, arrayCount); \00"
@.str.18 = [38 x i8] c"\09// Round up according to array size \00"
@.str.19 = [55 x i8] c"\09gridSize = (arrayCount + blockSize - 1) / blockSize; \00"
@.str.20 = [58 x i8] c"\09MyKernel<<< gridSize, blockSize >>>(array, arrayCount); \00"
@.str.21 = [27 x i8] c"\09cudaDeviceSynchronize(); \00"
@.str.22 = [36 x i8] c"\09// calculate theoretical occupancy\00"
@.str.23 = [22 x i8] c"\09int maxActiveBlocks;\00"
@.str.24 = [67 x i8] c"\09cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, \00"
@.str.25 = [71 x i8] c" MyKernel, blockSize, \00"
@.str.26 = [53 x i8] c" 0);\00"
@.str.27 = [13 x i8] c"\09int device;\00"
@.str.28 = [23 x i8] c"\09cudaDeviceProp props;\00"
@.str.29 = [25 x i8] c"\09cudaGetDevice(&device);\00"
@.str.30 = [42 x i8] c"\09cudaGetDeviceProperties(&props, device);\00"
@.str.31 = [69 x i8] c"\09float occupancy = (maxActiveBlocks * blockSize / props.warpSize) / \00"
@.str.32 = [70 x i8] c" (float)(props.maxThreadsPerMultiProcessor / \00"
@.str.33 = [22 x i8] c"\09\09\09 props.warpSize);\00"
@.str.34 = [90 x i8] c"\09printf(\22Launched blocks of size %d. Theoretical occupancy: %f\\n\22, blockSize, occupancy);\00"
@.str.35 = [7 x i8] c"#endif\00"
@__const.occupancy.string = [46 x i8*] [i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.11, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.12, i32 0, i32 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.13, i32 0, i32 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.14, i32 0, i32 0), i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.15, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.16, i32 0, i32 0), i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.17, i32 0, i32 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.18, i32 0, i32 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.19, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.20, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.21, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.22, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.23, i32 0, i32 0), i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.24, i32 0, i32 0), i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.25, i32 0, i32 0), i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.26, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.27, i32 0, i32 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.28, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.29, i32 0, i32 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.30, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.31, i32 0, i32 0), i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.32, i32 0, i32 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.33, i32 0, i32 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.34, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.35, i32 0, i32 0)]
define i8* @occupancy() {
%1 = alloca [46 x i8*]
%2 = bitcast [46 x i8*]* %1 to i8*
%3 = getelementptr inbounds [46 x i8*], [46 x i8*]* %1, i64 0, i64 0
%4 = call i8* @buildString(i8** %3, i32 46)
ret i8* %4
}