#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fill_square_matrix(double *, size_t const,
                                      int /*<<< orphan*/);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ init_vector_list(double *, size_t const, int);
double *malloc_nofail(size_t const);
int omp_get_max_threads();
int omp_get_num_threads();
int omp_get_thread_num();
int /*<<< orphan*/ print_nobuf(char *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ sq_euclidean_distance1(double *, double *, size_t const,
                                          size_t const);
int /*<<< orphan*/ sq_euclidean_distance2(double *, double *, size_t const,
                                          size_t const);
int /*<<< orphan*/ stdout;
double sum_square_matrix(double *, size_t const);
int main(void) {
  const size_t size = 5000, dim = 2;
  double *matrix;
  double *vectors;
  printf("OpenMP max threads: %d\n", omp_get_max_threads());
  fflush(stdout);
  print_nobuf("OpenMP threads:");
#pragma omp parallel
  {
    int this_thread = omp_get_thread_num();
    int num_threads = omp_get_num_threads();
    char buffer[sizeof(" [/]") + 2 * 11];
    snprintf(buffer, sizeof(buffer), " [%d/%d]", this_thread, num_threads);
    print_nobuf(buffer);
  }
  print_nobuf("\n");
  matrix = malloc_nofail(size * size * sizeof(double));
  vectors = malloc_nofail(size * dim * sizeof(double));
  fill_square_matrix(matrix, size, 0);
  init_vector_list(vectors, size, 2);
  sq_euclidean_distance1(matrix, vectors, size, dim);
  printf("1: sum(eucl_dist(vects)) = %f\n", sum_square_matrix(matrix, size));
  sq_euclidean_distance2(matrix, vectors, size, dim);
  printf("2: sum(eucl_dist(vects)) = %f\n", sum_square_matrix(matrix, size));
  free(matrix);
  free(vectors);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x1388,-0x10(%rbp)
movq   $0x2,-0x18(%rbp)
mov    $0x0,%al
call   10e0 <omp_get_max_threads@plt>
cvtsi2sd %eax,%xmm0
lea    0xdef(%rip),%rdi
call   1080 <printf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
lea    0xded(%rip),%rdi
call   10c0 <print_nobuf@plt>
mov    $0x0,%al
call   1050 <omp_get_thread_num@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   10b0 <omp_get_num_threads@plt>
mov    %eax,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    $0x1b,%esi
lea    0xdcd(%rip),%rdx
call   1090 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   10c0 <print_nobuf@plt>
lea    0xda6(%rip),%rdi
call   10c0 <print_nobuf@plt>
mov    $0xbebc200,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,-0x20(%rbp)
mov    $0x13880,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1388,%esi
xor    %edx,%edx
call   1060 <fill_square_matrix@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1388,%esi
mov    $0x2,%edx
call   1100 <init_vector_list@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x1388,%edx
mov    $0x2,%ecx
call   1070 <sq_euclidean_distance1@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1388,%esi
call   10a0 <sum_square_matrix@plt>
lea    0xd51(%rip),%rdi
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x1388,%edx
mov    $0x2,%ecx
call   10f0 <sq_euclidean_distance2@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1388,%esi
call   10a0 <sum_square_matrix@plt>
lea    0xd3f(%rip),%rdi
call   1080 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   10e0 <omp_get_max_threads@plt>
cvtsi2sd %eax,%xmm0
lea    0xe07(%rip),%rdi
call   1080 <printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
lea    0xe05(%rip),%rdi
call   10c0 <print_nobuf@plt>
xor    %eax,%eax
call   1050 <omp_get_thread_num@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <omp_get_num_threads@plt>
lea    0xdf9(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1b,%esi
mov    %ebx,%ecx
mov    %eax,%r8d
call   1090 <snprintf@plt>
mov    %r14,%rdi
call   10c0 <print_nobuf@plt>
lea    0xdc3(%rip),%rdi
call   10c0 <print_nobuf@plt>
mov    $0xbebc200,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,%rbx
mov    $0x13880,%edi
call   1030 <malloc_nofail@plt>
mov    %rax,%r14
mov    $0x1388,%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1060 <fill_square_matrix@plt>
mov    $0x1388,%esi
mov    %r14,%rdi
mov    $0x2,%edx
call   1100 <init_vector_list@plt>
mov    $0x1388,%edx
mov    $0x2,%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <sq_euclidean_distance1@plt>
mov    $0x1388,%esi
mov    %rbx,%rdi
call   10a0 <sum_square_matrix@plt>
lea    0xd75(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1388,%edx
mov    $0x2,%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10f0 <sq_euclidean_distance2@plt>
mov    $0x1388,%esi
mov    %rbx,%rdi
call   10a0 <sum_square_matrix@plt>
lea    0xd66(%rip),%rdi
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [24 x i8] c"OpenMP max threads: %d\0A\00"
@stdout = global i32 0
@.str.1 = [16 x i8] c"OpenMP threads:\00"
@.str.2 = [9 x i8] c" [%d/%d]\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [31 x i8] c"1: sum(eucl_dist(vects)) = %f\0A\00"
@.str.5 = [31 x i8] c"2: sum(eucl_dist(vects)) = %f\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca double*
%5 = alloca double*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [27 x i8]
store i32 0, i32* %1
store i64 5000, i64* %2
store i64 2, i64* %3
%9 = call i32 (...) @omp_get_max_threads()
%10 = sitofp i32 %9 to double
%11 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %10)
%12 = load i32, i32* @stdout
%13 = call i32 @fflush(i32 %12)
%14 = call i32 @print_nobuf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (...) @omp_get_thread_num()
store i32 %15, i32* %6
%16 = call i32 (...) @omp_get_num_threads()
store i32 %16, i32* %7
%17 = getelementptr inbounds [27 x i8], [27 x i8]* %8, i64 0, i64 0
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = call i32 @snprintf(i8* %17, i32 27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
%21 = getelementptr inbounds [27 x i8], [27 x i8]* %8, i64 0, i64 0
%22 = call i32 @print_nobuf(i8* %21)
%23 = call i32 @print_nobuf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = call double* @malloc_nofail(i64 200000000)
store double* %24, double** %4
%25 = call double* @malloc_nofail(i64 80000)
store double* %25, double** %5
%26 = load double*, double** %4
%27 = call i32 @fill_square_matrix(double* %26, i64 5000, i32 0)
%28 = load double*, double** %5
%29 = call i32 @init_vector_list(double* %28, i64 5000, i32 2)
%30 = load double*, double** %4
%31 = load double*, double** %5
%32 = call i32 @sq_euclidean_distance1(double* %30, double* %31, i64 5000, i64 2)
%33 = load double*, double** %4
%34 = call double @sum_square_matrix(double* %33, i64 5000)
%35 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), double %34)
%36 = load double*, double** %4
%37 = load double*, double** %5
%38 = call i32 @sq_euclidean_distance2(double* %36, double* %37, i64 5000, i64 2)
%39 = load double*, double** %4
%40 = call double @sum_square_matrix(double* %39, i64 5000)
%41 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), double %40)
%42 = load double*, double** %4
%43 = call i32 @free(double* %42)
%44 = load double*, double** %5
%45 = call i32 @free(double* %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"OpenMP max threads: %d\0A\00"
@stdout = global i32 0
@.str.1 = [16 x i8] c"OpenMP threads:\00"
@.str.2 = [9 x i8] c" [%d/%d]\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [31 x i8] c"1: sum(eucl_dist(vects)) = %f\0A\00"
@.str.5 = [31 x i8] c"2: sum(eucl_dist(vects)) = %f\0A\00"
define i32 @main() {
%1 = alloca [27 x i8]
%2 = tail call i32 (...) @omp_get_max_threads()
%3 = sitofp i32 %2 to double
%4 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %3)
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = tail call i32 @print_nobuf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 (...) @omp_get_thread_num()
%9 = tail call i32 (...) @omp_get_num_threads()
%10 = getelementptr inbounds [27 x i8], [27 x i8]* %1, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %9)
%12 = call i32 @print_nobuf(i8* %10)
%13 = call i32 @print_nobuf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = call double* @malloc_nofail(i64 200000000)
%15 = call double* @malloc_nofail(i64 80000)
%16 = call i32 @fill_square_matrix(double* %14, i64 5000, i32 0)
%17 = call i32 @init_vector_list(double* %15, i64 5000, i32 2)
%18 = call i32 @sq_euclidean_distance1(double* %14, double* %15, i64 5000, i64 2)
%19 = call double @sum_square_matrix(double* %14, i64 5000)
%20 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), double %19)
%21 = call i32 @sq_euclidean_distance2(double* %14, double* %15, i64 5000, i64 2)
%22 = call double @sum_square_matrix(double* %14, i64 5000)
%23 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), double %22)
%24 = call i32 @free(double* %14)
%25 = call i32 @free(double* %15)
ret i32 0
}