#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 scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ generate_random_brute(int, int, int *);
int /*<<< orphan*/ generate_random_indicies_lcg(int, int, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int T = 10000;
  int n = 1 << 9;
  int N = 1 << 20;
  int *indicies = (int *)malloc(n * sizeof(int));
  clock_t start, stop;
  printf("Iterations: %d\n", T);
  printf("N: %d\n", N);
  printf("n: %d\n", n);
  printf("Testing LCG method\n");
  start = clock();
  for (int i = 0; i < T; i++) {
    generate_random_indicies_lcg(N, n, indicies);
  }
  stop = clock();
  printf("Total time: %0.5f\n", (double)(stop - start) / CLOCKS_PER_SEC);
  printf("Time per iteration: %0.5f\n",
         (double)(stop - start) / CLOCKS_PER_SEC / T);
  printf("\n");
  printf("Testing brute method\n");
  start = clock();
  for (int i = 0; i < T; i++) {
    generate_random_brute(N, n, indicies);
  }
  stop = clock();
  printf("Total time: %0.5f\n", (double)(stop - start) / CLOCKS_PER_SEC);
  printf("Time per iteration: %0.5f\n",
         (double)(stop - start) / CLOCKS_PER_SEC / T);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2710,-0x8(%rbp)
movl   $0x200,-0xc(%rbp)
movl   $0x100000,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1203 <main+0xb3>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <generate_random_indicies_lcg@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11da <main+0x8a>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2dc6(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe05(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2da0(%rip),%rax
divsd  (%rax),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xde9(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12ba <main+0x16a>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <generate_random_brute@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1291 <main+0x141>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d0f(%rip),%rax
divsd  (%rax),%xmm0
lea    0xd4e(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2ce9(%rip),%rax
divsd  (%rax),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xd32(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x800,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0xe7d(%rip),%rdi
mov    $0x2710,%ebp
mov    $0x2710,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe75(%rip),%rdi
mov    $0x100000,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe69(%rip),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%r14
nopl   0x0(%rax,%rax,1)
mov    $0x100000,%edi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1080 <generate_random_indicies_lcg@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x70>
xor    %eax,%eax
call   1050 <clock@plt>
sub    %r14,%rax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
mov    0x2dd6(%rip),%r15
divsd  (%r15),%xmm0
lea    0xe08(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
movsd  (%rsp),%xmm0
divsd  (%r15),%xmm0
divsd  0xdc9(%rip),%xmm0
lea    0xdfb(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe11(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <clock@plt>
mov    %rax,%r14
mov    $0x2710,%ebp
nopw   0x0(%rax,%rax,1)
mov    $0x100000,%edi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1070 <generate_random_brute@plt>
add    $0xffffffff,%ebp
jne    1270 <main+0x100>
xor    %eax,%eax
call   1050 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
divsd  (%r15),%xmm0
lea    0xd7c(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
movsd  (%rsp),%xmm0
divsd  (%r15),%xmm0
divsd  0xd3d(%rip),%xmm0
lea    0xd6f(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Iterations: %d\0A\00"
@.str.1 = [7 x i8] c"N: %d\0A\00"
@.str.2 = [7 x i8] c"n: %d\0A\00"
@.str.3 = [20 x i8] c"Testing LCG method\0A\00"
@.str.4 = [19 x i8] c"Total time: %0.5f\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.5 = [27 x i8] c"Time per iteration: %0.5f\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [22 x i8] c"Testing brute method\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
store i32 10000, i32* %2
store i32 512, i32* %3
store i32 1048576, i32* %4
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 4
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
store i32* %15, i32** %5
%16 = load i32, i32* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%23 = call i64 (...) @clock()
store i64 %23, i64* %6
store i32 0, i32* %8
br label %24
24: ; preds = %33, %0
%25 = load i32, i32* %8
%26 = load i32, i32* %2
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = load i32, i32* %4
%30 = load i32, i32* %3
%31 = load i32*, i32** %5
%32 = call i32 @generate_random_indicies_lcg(i32 %29, i32 %30, i32* %31)
br label %33
33: ; preds = %28
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %24
36: ; preds = %24
%37 = call i64 (...) @clock()
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = sub nsw i64 %38, %39
%41 = sitofp i64 %40 to double
%42 = load double, double* @CLOCKS_PER_SEC
%43 = fdiv double %41, %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %43)
%45 = load i64, i64* %7
%46 = load i64, i64* %6
%47 = sub nsw i64 %45, %46
%48 = sitofp i64 %47 to double
%49 = load double, double* @CLOCKS_PER_SEC
%50 = fdiv double %48, %49
%51 = load i32, i32* %2
%52 = sitofp i32 %51 to double
%53 = fdiv double %50, %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), double %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%57 = call i64 (...) @clock()
store i64 %57, i64* %6
store i32 0, i32* %9
br label %58
58: ; preds = %67, %36
%59 = load i32, i32* %9
%60 = load i32, i32* %2
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %70
62: ; preds = %58
%63 = load i32, i32* %4
%64 = load i32, i32* %3
%65 = load i32*, i32** %5
%66 = call i32 @generate_random_brute(i32 %63, i32 %64, i32* %65)
br label %67
67: ; preds = %62
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %58
70: ; preds = %58
%71 = call i64 (...) @clock()
store i64 %71, i64* %7
%72 = load i64, i64* %7
%73 = load i64, i64* %6
%74 = sub nsw i64 %72, %73
%75 = sitofp i64 %74 to double
%76 = load double, double* @CLOCKS_PER_SEC
%77 = fdiv double %75, %76
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %77)
%79 = load i64, i64* %7
%80 = load i64, i64* %6
%81 = sub nsw i64 %79, %80
%82 = sitofp i64 %81 to double
%83 = load double, double* @CLOCKS_PER_SEC
%84 = fdiv double %82, %83
%85 = load i32, i32* %2
%86 = sitofp i32 %85 to double
%87 = fdiv double %84, %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), double %87)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Iterations: %d\0A\00"
@.str.1 = [7 x i8] c"N: %d\0A\00"
@.str.2 = [7 x i8] c"n: %d\0A\00"
@.str.4 = [19 x i8] c"Total time: %0.5f\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.5 = [27 x i8] c"Time per iteration: %0.5f\0A\00"
@str = [19 x i8] c"Testing LCG method\00"
@str.8 = [21 x i8] c"Testing brute method\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 2048)
%2 = inttoptr i64 %1 to i32*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 10000)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1048576)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 512)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%7 = tail call i64 (...) @clock()
br label %22
8: ; preds = %22
%9 = tail call i64 (...) @clock()
%10 = sub nsw i64 %9, %7
%11 = sitofp i64 %10 to double
%12 = load double, double* @CLOCKS_PER_SEC
%13 = fdiv double %11, %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %13)
%15 = load double, double* @CLOCKS_PER_SEC
%16 = fdiv double %11, %15
%17 = fdiv double %16, 1.000000e+04
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), double %17)
%19 = tail call i32 @putchar(i32 10)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%21 = tail call i64 (...) @clock()
br label %38
22: ; preds = %0, %22
%23 = phi i32 [ 0, %0 ], [ %25, %22 ]
%24 = tail call i32 @generate_random_indicies_lcg(i32 1048576, i32 512, i32* %2)
%25 = add nuw nsw i32 %23, 1
%26 = icmp eq i32 %25, 10000
br i1 %26, label %8, label %22
27: ; preds = %38
%28 = tail call i64 (...) @clock()
%29 = sub nsw i64 %28, %21
%30 = sitofp i64 %29 to double
%31 = load double, double* @CLOCKS_PER_SEC
%32 = fdiv double %30, %31
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), double %32)
%34 = load double, double* @CLOCKS_PER_SEC
%35 = fdiv double %30, %34
%36 = fdiv double %35, 1.000000e+04
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), double %36)
ret i32 0
38: ; preds = %8, %38
%39 = phi i32 [ 0, %8 ], [ %41, %38 ]
%40 = tail call i32 @generate_random_brute(i32 1048576, i32 512, i32* %2)
%41 = add nuw nsw i32 %39, 1
%42 = icmp eq i32 %41, 10000
br i1 %42, label %27, label %38
}