#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 int /*<<< orphan*/ time_t;
struct random_data {
  int /*<<< orphan*/ *state;
};
typedef int /*<<< orphan*/ state;
typedef size_t int32_t;

/* Variables and functions */
int /*<<< orphan*/ __initstate_r(int, char *, int, struct random_data *);
int /*<<< orphan*/ __random_r(struct random_data *, size_t *);
int getpid();
int strlen(char *);
int time(int /*<<< orphan*/ *);
char *strfry(char *string) {
  static int init;
  static struct random_data rdata;
  size_t len, i;
  if (!init) {
    static char state[32];
    rdata.state = NULL;
    __initstate_r(time((time_t *)NULL) ^ getpid(), state, sizeof(state),
                  &rdata);
    init = 1;
  }
  len = strlen(string) - 1;
  for (i = 0; i < len; ++i) {
    int32_t j;
    __random_r(&rdata, &j);
    j = j % (len - i) + i;
    char c = string[i];
    string[i] = string[j];
    string[j] = c;
  }
  return string;
}


x86 assembly (clang, -O0):
<strfry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpl   $0x0,0x2efd(%rip)
jne    11ae <strfry+0x5e>
movq   $0x0,0x2ef4(%rip)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x28(%rbp),%edi
xor    %eax,%edi
lea    0x2edd(%rip),%rsi
mov    $0x20,%edx
lea    0x2ec9(%rip),%rcx
call   1060 <__initstate_r@plt>
movl   $0x1,0x2eb2(%rip)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1242 <strfry+0xf2>
lea    0x2e8b(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <__random_r@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    -0x18(%rbp),%rcx
xor    %edx,%edx
div    %rcx
add    -0x18(%rbp),%rdx
mov    %rdx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x21(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11c8 <strfry+0x78>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strfry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
cmpb   $0x0,0x2efb(%rip)
jne    11a8 <strfry+0x58>
movq   $0x0,0x2ef6(%rip)
lea    0x2eef(%rip),%rbx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %eax,%ebp
lea    0x2ede(%rip),%rsi
mov    %ebp,%edi
mov    $0x20,%edx
mov    %rbx,%rcx
call   1060 <__initstate_r@plt>
movb   $0x1,0x2eb8(%rip)
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0xffffffff,%eax
je     1221 <strfry+0xd1>
movslq %eax,%rbp
cmp    $0x2,%rbp
mov    $0x1,%r13d
cmovae %rbp,%r13
xor    %ebx,%ebx
mov    %rsp,%r15
mov    %r14,%r12
jmp    11fc <strfry+0xac>
xor    %edx,%edx
div    %rbp
add    %rbx,%rdx
movzbl (%r12),%eax
movzbl (%r14,%rdx,1),%ecx
mov    %cl,(%r12)
mov    %al,(%r14,%rdx,1)
add    $0x1,%rbx
add    $0x1,%r12
add    $0xffffffffffffffff,%rbp
add    $0xffffffffffffffff,%r13
je     1221 <strfry+0xd1>
lea    0x2e65(%rip),%rdi
mov    %r15,%rsi
call   1050 <__random_r@plt>
mov    (%rsp),%rax
mov    %rax,%rcx
or     %rbp,%rcx
shr    $0x20,%rcx
jne    11d0 <strfry+0x80>
xor    %edx,%edx
div    %ebp
jmp    11d5 <strfry+0x85>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.random_data = type { i32* }
@strfry.init = internal global i32 0
@strfry.rdata = internal global %struct.random_data zeroinitializer
@strfry.state = internal global [32 x i8] zeroinitializer
define i8* @strfry(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %2
%7 = load i32, i32* @strfry.init
%8 = icmp ne i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %1
store i32* null, i32** getelementptr inbounds (%struct.random_data, %struct.random_data* @strfry.rdata, i32 0, i32 0)
%10 = call i32 @time(i32* null)
%11 = call i32 (...) @getpid()
%12 = xor i32 %10, %11
%13 = call i32 @__initstate_r(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @strfry.state, i64 0, i64 0), i32 32, %struct.random_data* @strfry.rdata)
store i32 1, i32* @strfry.init
br label %14
14: ; preds = %9, %1
%15 = load i8*, i8** %2
%16 = call i32 @strlen(i8* %15)
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
store i64 %18, i64* %3
store i64 0, i64* %4
br label %19
19: ; preds = %47, %14
%20 = load i64, i64* %4
%21 = load i64, i64* %3
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %50
23: ; preds = %19
%24 = call i32 @__random_r(%struct.random_data* @strfry.rdata, i64* %5)
%25 = load i64, i64* %5
%26 = load i64, i64* %3
%27 = load i64, i64* %4
%28 = sub i64 %26, %27
%29 = urem i64 %25, %28
%30 = load i64, i64* %4
%31 = add i64 %29, %30
store i64 %31, i64* %5
%32 = load i8*, i8** %2
%33 = load i64, i64* %4
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* %6
%36 = load i8*, i8** %2
%37 = load i64, i64* %5
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %2
%41 = load i64, i64* %4
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 %39, i8* %42
%43 = load i8, i8* %6
%44 = load i8*, i8** %2
%45 = load i64, i64* %5
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8 %43, i8* %46
br label %47
47: ; preds = %23
%48 = load i64, i64* %4
%49 = add i64 %48, 1
store i64 %49, i64* %4
br label %19
50: ; preds = %19
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
%struct.random_data = type { i32* }
@strfry.init = internal unnamed_addr global i1 false
@strfry.rdata = internal global %struct.random_data zeroinitializer
@strfry.state = internal global [32 x i8] zeroinitializer
define i8* @strfry(i8* returned %0) {
%2 = alloca i64
%3 = load i1, i1* @strfry.init
br i1 %3, label %9, label %4
4: ; preds = %1
store i32* null, i32** getelementptr inbounds (%struct.random_data, %struct.random_data* @strfry.rdata, i64 0, i32 0)
%5 = tail call i32 @time(i32* null)
%6 = tail call i32 (...) @getpid()
%7 = xor i32 %6, %5
%8 = tail call i32 @__initstate_r(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @strfry.state, i64 0, i64 0), i32 32, %struct.random_data* @strfry.rdata)
store i1 true, i1* @strfry.init
br label %9
9: ; preds = %4, %1
%10 = tail call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = icmp eq i32 %11, 0
br i1 %13, label %30, label %14
14: ; preds = %9
%15 = bitcast i64* %2 to i8*
%16 = call i64 @llvm.umax.i64(i64 %12, i64 1)
br label %17
17: ; preds = %14, %17
%18 = phi i64 [ 0, %14 ], [ %28, %17 ]
%19 = call i32 @__random_r(%struct.random_data* @strfry.rdata, i64* %2)
%20 = load i64, i64* %2
%21 = sub i64 %12, %18
%22 = urem i64 %20, %21
%23 = add i64 %22, %18
%24 = getelementptr inbounds i8, i8* %0, i64 %18
%25 = load i8, i8* %24
%26 = getelementptr inbounds i8, i8* %0, i64 %23
%27 = load i8, i8* %26
store i8 %27, i8* %24
store i8 %25, i8* %26
%28 = add nuw i64 %18, 1
%29 = icmp eq i64 %28, %16
br i1 %29, label %30, label %17
30: ; preds = %17, %9
ret i8* %0
}