#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 */
int get_random_value(int, int);
int /*<<< orphan*/ printf(char *, int);
int *reservoir_sample(int *);
int main() {
  int population[2000];
  for (int i = 0; i < 2000; i++) {
    population[i] = get_random_value(1, 2000);
  }
  int *sample = reservoir_sample(population);
  for (int i = 0; i < 100; i++) {
    printf("%d ", sample[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1f54(%rbp)
cmpl   $0x7d0,-0x1f54(%rbp)
jge    118f <main+0x5f>
mov    $0x1,%edi
mov    $0x7d0,%esi
call   1050 <get_random_value@plt>
mov    %eax,%ecx
movslq -0x1f54(%rbp),%rax
mov    %ecx,-0x1f50(%rbp,%rax,4)
mov    -0x1f54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f54(%rbp)
jmp    114c <main+0x1c>
lea    -0x1f50(%rbp),%rdi
call   1030 <reservoir_sample@plt>
mov    %rax,-0x1f60(%rbp)
movl   $0x0,-0x1f64(%rbp)
cmpl   $0x64,-0x1f64(%rbp)
jge    11ea <main+0xba>
mov    -0x1f60(%rbp),%rax
movslq -0x1f64(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1f64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f64(%rbp)
jmp    11ac <main+0x7c>
xor    %eax,%eax
add    $0x1f70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1f40,%rsp
xor    %ebx,%ebx
xchg   %ax,%ax
mov    $0x1,%edi
mov    $0x7d0,%esi
call   1050 <get_random_value@plt>
mov    %eax,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x7d0,%rbx
jne    1140 <main+0x10>
mov    %rsp,%rdi
call   1030 <reservoir_sample@plt>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xe8d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x64,%rbx
jne    1180 <main+0x50>
xor    %eax,%eax
add    $0x1f40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2000 x i32]
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %6
6: ; preds = %14, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 2000
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = call i32 @get_random_value(i32 1, i32 2000)
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [2000 x i32], [2000 x i32]* %2, i64 0, i64 %12
store i32 %10, i32* %13
br label %14
14: ; preds = %9
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
br label %6
17: ; preds = %6
%18 = getelementptr inbounds [2000 x i32], [2000 x i32]* %2, i64 0, i64 0
%19 = call i32* @reservoir_sample(i32* %18)
store i32* %19, i32** %4
store i32 0, i32* %5
br label %20
20: ; preds = %30, %17
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 100
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32*, i32** %4
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %23
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %20
33: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d \00"
define i32 @main() {
%1 = alloca [2000 x i32]
%2 = bitcast [2000 x i32]* %1 to i8*
br label %6
3: ; preds = %6
%4 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 0
%5 = call i32* @reservoir_sample(i32* %4)
br label %13
6: ; preds = %0, %6
%7 = phi i64 [ 0, %0 ], [ %10, %6 ]
%8 = tail call i32 @get_random_value(i32 1, i32 2000)
%9 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 %7
store i32 %8, i32* %9
%10 = add nuw nsw i64 %7, 1
%11 = icmp eq i64 %10, 2000
br i1 %11, label %3, label %6
12: ; preds = %13
ret i32 0
13: ; preds = %3, %13
%14 = phi i64 [ 0, %3 ], [ %18, %13 ]
%15 = getelementptr inbounds i32, i32* %5, i64 %14
%16 = load i32, i32* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, 100
br i1 %19, label %12, label %13
}