#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__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exch(void *, scalar_t__, scalar_t__, scalar_t__, scalar_t__);
scalar_t__ rand();
int shuffle(void *base, ssize_t nmemb, ssize_t size) {
  ssize_t i;
  ssize_t r;
  if (base == NULL) {
    return -1;
  }
  if (nmemb <= 0) {
    return -1;
  }
  if (size <= 0) {
    return -1;
  }
  for (i = 0; i < nmemb; i++) {
    r = rand() % (i + 1);
    exch(base, nmemb, size, i, r);
  }
  return 0;
}


x86 assembly (clang, -O0):
<shuffle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114b <shuffle+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <shuffle+0xb8>
cmpq   $0x0,-0x18(%rbp)
jg     1162 <shuffle+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <shuffle+0xb8>
cmpq   $0x0,-0x20(%rbp)
jg     1179 <shuffle+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <shuffle+0xb8>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11d1 <shuffle+0xb1>
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
cqto
idiv   %rcx
mov    %rdx,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1030 <exch@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1181 <shuffle+0x61>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shuffle>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1193 <shuffle+0x73>
mov    %rsi,%r12
test   %rsi,%rsi
jle    1193 <shuffle+0x73>
mov    %rdx,%r14
test   %rdx,%rdx
jle    1193 <shuffle+0x73>
mov    %rdi,%r15
xor    %r13d,%r13d
jmp    1171 <shuffle+0x51>
nopl   0x0(%rax,%rax,1)
cqto
idiv   %rbx
mov    %rdx,%r8
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r13,%rcx
call   1030 <exch@plt>
mov    %rbx,%r13
cmp    %rbx,%r12
je     1191 <shuffle+0x71>
xor    %eax,%eax
call   1040 <rand@plt>
lea    0x1(%r13),%rbx
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
jne    1150 <shuffle+0x30>
xor    %edx,%edx
div    %ebx
mov    %edx,%r8d
jmp    1158 <shuffle+0x38>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @shuffle(i8* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %41
13: ; preds = %3
%14 = load i64, i64* %6
%15 = icmp sle i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %4
br label %41
17: ; preds = %13
%18 = load i64, i64* %7
%19 = icmp sle i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 -1, i32* %4
br label %41
21: ; preds = %17
store i64 0, i64* %8
br label %22
22: ; preds = %37, %21
%23 = load i64, i64* %8
%24 = load i64, i64* %6
%25 = icmp slt i64 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = call i64 (...) @rand()
%28 = load i64, i64* %8
%29 = add nsw i64 %28, 1
%30 = srem i64 %27, %29
store i64 %30, i64* %9
%31 = load i8*, i8** %5
%32 = load i64, i64* %6
%33 = load i64, i64* %7
%34 = load i64, i64* %8
%35 = load i64, i64* %9
%36 = call i32 @exch(i8* %31, i64 %32, i64 %33, i64 %34, i64 %35)
br label %37
37: ; preds = %26
%38 = load i64, i64* %8
%39 = add nsw i64 %38, 1
store i64 %39, i64* %8
br label %22
40: ; preds = %22
store i32 0, i32* %4
br label %41
41: ; preds = %40, %20, %16, %12
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @shuffle(i8* %0, i64 %1, i64 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp slt i64 %1, 1
%6 = or i1 %4, %5
%7 = icmp slt i64 %2, 1
%8 = or i1 %6, %7
br i1 %8, label %16, label %9
9: ; preds = %3, %9
%10 = phi i64 [ %12, %9 ], [ 0, %3 ]
%11 = tail call i64 (...) @rand()
%12 = add nuw nsw i64 %10, 1
%13 = srem i64 %11, %12
%14 = tail call i32 @exch(i8* %0, i64 %1, i64 %2, i64 %10, i64 %13)
%15 = icmp eq i64 %12, %1
br i1 %15, label %16, label %9
16: ; preds = %9, %3
%17 = phi i32 [ -1, %3 ], [ 0, %9 ]
ret i32 %17
}