#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ init_rand();
int rand();
int /*<<< orphan*/ stderr;
void randperm(int *x, int n, int d) {
  init_rand();
  int i;
  if (d > n) {
    fprintf(stderr, "Error: d > n in randperm()\n");
    return;
  }
  int i0 = rand() % n;
  static const int big_primes[100] = {
      996311, 163573, 481123, 187219, 963323, 103769, 786979, 826363, 874891,
      168991, 442501, 318679, 810377, 471073, 914519, 251059, 321983, 220009,
      211877, 875339, 605603, 578483, 219619, 860089, 644911, 398819, 544927,
      444043, 161717, 301447, 201329, 252731, 301463, 458207, 140053, 906713,
      946487, 524389, 522857, 387151, 904283, 415213, 191047, 791543, 433337,
      302989, 445853, 178859, 208499, 943589, 957331, 601291, 148439, 296801,
      400657, 829637, 112337, 134707, 240047, 669667, 746287, 668243, 488329,
      575611, 350219, 758449, 257053, 704287, 252283, 414539, 647771, 791201,
      166031, 931313, 787021, 520529, 474667, 484361, 358907, 540271, 542251,
      825829, 804709, 664843, 423347, 820367, 562577, 398347, 940349, 880603,
      578267, 644783, 611833, 273001, 354329, 506101, 292837, 851017, 262103,
      288989};
  int step = big_primes[rand() % 100];
  int idx = i0;
  for (i = 0; i < d; i++) {
    x[i] = idx;
    idx = (idx + step) % n;
  }
}


x86 assembly (clang, -O0):
<randperm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1040 <init_rand@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    116f <randperm+0x3f>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11de <randperm+0xae>
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
lea    0xe8a(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11de <randperm+0xae>
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a9 <randperm+0x79>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randperm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <init_rand@plt>
cmp    %ebp,%r15d
jle    116a <randperm+0x3a>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
lea    0xea5(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fprintf@plt>
xor    %eax,%eax
call   1050 <rand@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <rand@plt>
test   %r15d,%r15d
jle    11f0 <randperm+0xc0>
mov    %r15d,%r8d
cmp    $0x1,%r15d
jne    118c <randperm+0x5c>
xor    %esi,%esi
jmp    11e1 <randperm+0xb1>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
cltq
lea    0xe70(%rip),%rcx
mov    (%rcx,%rax,4),%edi
mov    %r8d,%ecx
and    $0xfffffffe,%ecx
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
cltd
idiv   %ebp
mov    %edx,(%r14,%rsi,4)
lea    (%rdx,%rdi,1),%eax
cltd
idiv   %ebp
mov    %edx,%ebx
mov    %edx,0x4(%r14,%rsi,4)
add    %edi,%ebx
add    $0x2,%rsi
cmp    %rsi,%rcx
jne    11c0 <randperm+0x90>
test   $0x1,%r8b
je     11f0 <randperm+0xc0>
mov    %ebx,%eax
cltd
idiv   %ebp
mov    %edx,(%r14,%rsi,4)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Error: d > n in randperm()\0A\00"
@randperm.big_primes = internal constant [100 x i32] [i32 996311, i32 163573, i32 481123, i32 187219, i32 963323, i32 103769, i32 786979, i32 826363, i32 874891, i32 168991, i32 442501, i32 318679, i32 810377, i32 471073, i32 914519, i32 251059, i32 321983, i32 220009, i32 211877, i32 875339, i32 605603, i32 578483, i32 219619, i32 860089, i32 644911, i32 398819, i32 544927, i32 444043, i32 161717, i32 301447, i32 201329, i32 252731, i32 301463, i32 458207, i32 140053, i32 906713, i32 946487, i32 524389, i32 522857, i32 387151, i32 904283, i32 415213, i32 191047, i32 791543, i32 433337, i32 302989, i32 445853, i32 178859, i32 208499, i32 943589, i32 957331, i32 601291, i32 148439, i32 296801, i32 400657, i32 829637, i32 112337, i32 134707, i32 240047, i32 669667, i32 746287, i32 668243, i32 488329, i32 575611, i32 350219, i32 758449, i32 257053, i32 704287, i32 252283, i32 414539, i32 647771, i32 791201, i32 166031, i32 931313, i32 787021, i32 520529, i32 474667, i32 484361, i32 358907, i32 540271, i32 542251, i32 825829, i32 804709, i32 664843, i32 423347, i32 820367, i32 562577, i32 398347, i32 940349, i32 880603, i32 578267, i32 644783, i32 611833, i32 273001, i32 354329, i32 506101, i32 292837, i32 851017, i32 262103, i32 288989]
define void @randperm(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = call i32 (...) @init_rand()
%12 = load i32, i32* %6
%13 = load i32, i32* %5
%14 = icmp sgt i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %46
18: ; preds = %3
%19 = call i32 (...) @rand()
%20 = load i32, i32* %5
%21 = srem i32 %19, %20
store i32 %21, i32* %8
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 100
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [100 x i32], [100 x i32]* @randperm.big_primes, i64 0, i64 %24
%26 = load i32, i32* %25
store i32 %26, i32* %9
%27 = load i32, i32* %8
store i32 %27, i32* %10
store i32 0, i32* %7
br label %28
28: ; preds = %43, %18
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %46
32: ; preds = %28
%33 = load i32, i32* %10
%34 = load i32*, i32** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 %33, i32* %37
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = add nsw i32 %38, %39
%41 = load i32, i32* %5
%42 = srem i32 %40, %41
store i32 %42, i32* %10
br label %43
43: ; preds = %32
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %28
46: ; preds = %15, %28
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Error: d > n in randperm()\0A\00"
@randperm.big_primes = internal unnamed_addr constant [100 x i32] [i32 996311, i32 163573, i32 481123, i32 187219, i32 963323, i32 103769, i32 786979, i32 826363, i32 874891, i32 168991, i32 442501, i32 318679, i32 810377, i32 471073, i32 914519, i32 251059, i32 321983, i32 220009, i32 211877, i32 875339, i32 605603, i32 578483, i32 219619, i32 860089, i32 644911, i32 398819, i32 544927, i32 444043, i32 161717, i32 301447, i32 201329, i32 252731, i32 301463, i32 458207, i32 140053, i32 906713, i32 946487, i32 524389, i32 522857, i32 387151, i32 904283, i32 415213, i32 191047, i32 791543, i32 433337, i32 302989, i32 445853, i32 178859, i32 208499, i32 943589, i32 957331, i32 601291, i32 148439, i32 296801, i32 400657, i32 829637, i32 112337, i32 134707, i32 240047, i32 669667, i32 746287, i32 668243, i32 488329, i32 575611, i32 350219, i32 758449, i32 257053, i32 704287, i32 252283, i32 414539, i32 647771, i32 791201, i32 166031, i32 931313, i32 787021, i32 520529, i32 474667, i32 484361, i32 358907, i32 540271, i32 542251, i32 825829, i32 804709, i32 664843, i32 423347, i32 820367, i32 562577, i32 398347, i32 940349, i32 880603, i32 578267, i32 644783, i32 611833, i32 273001, i32 354329, i32 506101, i32 292837, i32 851017, i32 262103, i32 288989]
define void @randperm(i32* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = tail call i32 (...) @init_rand()
%5 = icmp sgt i32 %2, %1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %44
9: ; preds = %3
%10 = tail call i32 (...) @rand()
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 100
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [100 x i32], [100 x i32]* @randperm.big_primes, i64 0, i64 %13
%15 = load i32, i32* %14
%16 = icmp sgt i32 %2, 0
br i1 %16, label %17, label %44
17: ; preds = %9
%18 = zext i32 %2 to i64
%19 = and i64 %18, 1
%20 = icmp eq i32 %2, 1
br i1 %20, label %37, label %21
21: ; preds = %17
%22 = and i64 %18, 4294967294
br label %23
23: ; preds = %23, %21
%24 = phi i64 [ 0, %21 ], [ %34, %23 ]
%25 = phi i32 [ %10, %21 ], [ %33, %23 ]
%26 = phi i64 [ 0, %21 ], [ %35, %23 ]
%27 = srem i32 %25, %1
%28 = getelementptr inbounds i32, i32* %0, i64 %24
store i32 %27, i32* %28
%29 = add nsw i32 %27, %15
%30 = or i64 %24, 1
%31 = srem i32 %29, %1
%32 = getelementptr inbounds i32, i32* %0, i64 %30
store i32 %31, i32* %32
%33 = add nsw i32 %31, %15
%34 = add nuw nsw i64 %24, 2
%35 = add i64 %26, 2
%36 = icmp eq i64 %35, %22
br i1 %36, label %37, label %23
37: ; preds = %23, %17
%38 = phi i64 [ 0, %17 ], [ %34, %23 ]
%39 = phi i32 [ %10, %17 ], [ %33, %23 ]
%40 = icmp eq i64 %19, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = srem i32 %39, %1
%43 = getelementptr inbounds i32, i32* %0, i64 %38
store i32 %42, i32* %43
br label %44
44: ; preds = %41, %37, %9, %6
ret void
}