#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 */
void *calloc(int, size_t);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
size_t rand();
int /*<<< orphan*/ srand(size_t);
void shuffle(void *base, size_t count, size_t width, size_t seed) {
  void *buffer = calloc(1, width);
  srand(seed);
  for (size_t i = 0u; i < count; ++i) {
    size_t k = rand() % (i + 1);
    memcpy(buffer, (char *)base + k * width, width);
    memcpy((char *)base + k * width, (char *)base + i * width, width);
    memcpy((char *)base + i * width, buffer, width);
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<shuffle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <srand@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1225 <shuffle+0xd5>
mov    $0x0,%al
call   1070 <rand@plt>
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x30(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    118b <shuffle+0x3b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shuffle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,(%rsp)
mov    $0x1,%edi
mov    %rdx,%rsi
call   1050 <calloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <srand@plt>
test   %r14,%r14
je     120b <shuffle+0xbb>
neg    %r14
mov    $0x1,%r15d
mov    (%rsp),%rbx
jmp    11ef <shuffle+0x9f>
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %r15
mov    %rdx,%rbp
imul   %r13,%rbp
add    (%rsp),%rbp
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
add    %r13,%rbx
lea    (%r14,%r15,1),%rax
add    $0x1,%rax
add    $0x1,%r15
cmp    $0x1,%rax
je     120b <shuffle+0xbb>
xor    %eax,%eax
call   1070 <rand@plt>
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
jne    11a0 <shuffle+0x50>
xor    %edx,%edx
div    %r15d
mov    %edx,%ebp
jmp    11a8 <shuffle+0x58>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @shuffle(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%12 = load i64, i64* %7
%13 = call i8* @calloc(i32 1, i64 %12)
store i8* %13, i8** %9
%14 = load i64, i64* %8
%15 = call i32 @srand(i64 %14)
store i64 0, i64* %10
br label %16
16: ; preds = %53, %4
%17 = load i64, i64* %10
%18 = load i64, i64* %6
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %56
20: ; preds = %16
%21 = call i64 (...) @rand()
%22 = load i64, i64* %10
%23 = add i64 %22, 1
%24 = urem i64 %21, %23
store i64 %24, i64* %11
%25 = load i8*, i8** %9
%26 = load i8*, i8** %5
%27 = load i64, i64* %11
%28 = load i64, i64* %7
%29 = mul i64 %27, %28
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = load i64, i64* %7
%32 = call i32 @memcpy(i8* %25, i8* %30, i64 %31)
%33 = load i8*, i8** %5
%34 = load i64, i64* %11
%35 = load i64, i64* %7
%36 = mul i64 %34, %35
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8*, i8** %5
%39 = load i64, i64* %10
%40 = load i64, i64* %7
%41 = mul i64 %39, %40
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i64, i64* %7
%44 = call i32 @memcpy(i8* %37, i8* %42, i64 %43)
%45 = load i8*, i8** %5
%46 = load i64, i64* %10
%47 = load i64, i64* %7
%48 = mul i64 %46, %47
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8*, i8** %9
%51 = load i64, i64* %7
%52 = call i32 @memcpy(i8* %49, i8* %50, i64 %51)
br label %53
53: ; preds = %20
%54 = load i64, i64* %10
%55 = add i64 %54, 1
store i64 %55, i64* %10
br label %16
56: ; preds = %16
%57 = load i8*, i8** %9
%58 = call i32 @free(i8* %57)
ret void
}

LLVM IR (-O2):
define void @shuffle(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = tail call i8* @calloc(i32 1, i64 %2)
%6 = tail call i32 @srand(i64 %3)
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %10, %4
%9 = tail call i32 @free(i8* %5)
ret void
10: ; preds = %4, %10
%11 = phi i64 [ %13, %10 ], [ 0, %4 ]
%12 = tail call i64 (...) @rand()
%13 = add nuw i64 %11, 1
%14 = urem i64 %12, %13
%15 = mul i64 %14, %2
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @memcpy(i8* %5, i8* %16, i64 %2)
%18 = mul i64 %11, %2
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = tail call i32 @memcpy(i8* %16, i8* %19, i64 %2)
%21 = tail call i32 @memcpy(i8* %19, i8* %5, i64 %2)
%22 = icmp eq i64 %13, %1
br i1 %22, label %8, label %10
}