#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 */
scalar_t__ calloc(int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int);
int main() {
  int *array = (int *)malloc(52 * sizeof(int));
  int *out = (int *)calloc(52, sizeof(int));
  int i;
  int m = 17;
  srand(123);
  printf("\n");
  for (i = 0; i < 52; i++) {
    array[i] = i;
    printf("%d ", array[i]);
  }
  printf("\n");
  int sindex = 0;
  int temp;
  for (i = 0; i < m; i++) {
    int arIdx = (rand() % (52 - sindex)) + sindex;
    out[i] = array[arIdx];
    temp = array[arIdx];
    array[arIdx] = array[sindex];
    array[sindex] = temp;
    sindex++;
  }
  printf("\n naive");
  for (i = 0; i < 52; i++) {
    printf("%d ", out[i]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xd0,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x34,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x11,-0x20(%rbp)
mov    $0x7b,%edi
call   1040 <srand@plt>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x34,-0x1c(%rbp)
jge    11e5 <main+0x95>
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a6 <main+0x56>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1284 <main+0x134>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x34,%ecx
sub    -0x24(%rbp),%ecx
cltd
idiv   %ecx
add    -0x24(%rbp),%edx
mov    %edx,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1201 <main+0xb1>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x34,-0x1c(%rbp)
jge    12ca <main+0x17a>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1299 <main+0x149>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xd0,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    $0x34,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
mov    $0x7b,%edi
call   1050 <srand@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe5e(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,(%r12,%rbx,4)
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x34,%rbx
jne    11b0 <main+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x34,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <rand@plt>
cltd
idiv   %ebp
movslq %edx,%rax
add    %rbx,%rax
mov    (%r12,%rax,4),%ecx
mov    %ecx,(%r14,%rbx,4)
mov    (%r12,%rax,4),%ecx
mov    (%r12,%rbx,4),%edx
mov    %edx,(%r12,%rax,4)
mov    %ecx,(%r12,%rbx,4)
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0x11,%rbx
jne    11e0 <main+0x80>
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd6(%rip),%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r14,%rbp,4),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    $0x34,%rbp
jne    1230 <main+0xd0>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [8 x i8] c"\0A naive\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i64 @malloc(i32 208)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %2
%11 = call i64 @calloc(i32 52, i32 4)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %3
store i32 17, i32* %5
%13 = call i32 @srand(i32 123)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %15
15: ; preds = %30, %0
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 52
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = load i32, i32* %4
%20 = load i32*, i32** %2
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 %19, i32* %23
%24 = load i32*, i32** %2
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %18
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %15
33: ; preds = %15
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
store i32 0, i32* %4
br label %35
35: ; preds = %76, %33
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %79
39: ; preds = %35
%40 = call i32 (...) @rand()
%41 = load i32, i32* %6
%42 = sub nsw i32 52, %41
%43 = srem i32 %40, %42
%44 = load i32, i32* %6
%45 = add nsw i32 %43, %44
store i32 %45, i32* %8
%46 = load i32*, i32** %2
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32*, i32** %3
%52 = load i32, i32* %4
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
store i32 %50, i32* %54
%55 = load i32*, i32** %2
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
%59 = load i32, i32* %58
store i32 %59, i32* %7
%60 = load i32*, i32** %2
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = load i32*, i32** %2
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %65, i64 %67
store i32 %64, i32* %68
%69 = load i32, i32* %7
%70 = load i32*, i32** %2
%71 = load i32, i32* %6
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %70, i64 %72
store i32 %69, i32* %73
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
store i32 %75, i32* %6
br label %76
76: ; preds = %39
%77 = load i32, i32* %4
%78 = add nsw i32 %77, 1
store i32 %78, i32* %4
br label %35
79: ; preds = %35
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %81
81: ; preds = %91, %79
%82 = load i32, i32* %4
%83 = icmp slt i32 %82, 52
br i1 %83, label %84, label %94
84: ; preds = %81
%85 = load i32*, i32** %3
%86 = load i32, i32* %4
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i32, i32* %85, i64 %87
%89 = load i32, i32* %88
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %89)
br label %91
91: ; preds = %84
%92 = load i32, i32* %4
%93 = add nsw i32 %92, 1
store i32 %93, i32* %4
br label %81
94: ; preds = %81
%95 = load i32, i32* %1
ret i32 %95
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [8 x i8] c"\0A naive\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 208)
%2 = inttoptr i64 %1 to i32*
%3 = tail call i64 @calloc(i32 52, i32 4)
%4 = tail call i32 @srand(i32 123)
%5 = tail call i32 @putchar(i32 10)
br label %6
6: ; preds = %0, %6
%7 = phi i64 [ 0, %0 ], [ %11, %6 ]
%8 = getelementptr inbounds i32, i32* %2, i64 %7
%9 = trunc i64 %7 to i32
store i32 %9, i32* %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = add nuw nsw i64 %7, 1
%12 = icmp eq i64 %11, 52
br i1 %12, label %13, label %6
13: ; preds = %6
%14 = inttoptr i64 %3 to i32*
%15 = tail call i32 @putchar(i32 10)
br label %16
16: ; preds = %13, %16
%17 = phi i64 [ 0, %13 ], [ %31, %16 ]
%18 = tail call i32 (...) @rand()
%19 = trunc i64 %17 to i32
%20 = sub i32 52, %19
%21 = srem i32 %18, %20
%22 = trunc i64 %17 to i32
%23 = add nsw i32 %21, %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %2, i64 %24
%26 = load i32, i32* %25
%27 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 %26, i32* %27
%28 = load i32, i32* %25
%29 = getelementptr inbounds i32, i32* %2, i64 %17
%30 = load i32, i32* %29
store i32 %30, i32* %25
store i32 %28, i32* %29
%31 = add nuw nsw i64 %17, 1
%32 = icmp eq i64 %31, 17
br i1 %32, label %33, label %16
33: ; preds = %16
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %35
%36 = phi i64 [ 0, %33 ], [ %40, %35 ]
%37 = getelementptr inbounds i32, i32* %14, i64 %36
%38 = load i32, i32* %37
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = add nuw nsw i64 %36, 1
%41 = icmp eq i64 %40, 52
br i1 %41, label %42, label %35
42: ; preds = %35
ret i32 0
}