#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();
size_t strlen(char *);
int time(int /*<<< orphan*/ *);
char *strfry(char *string) {
  static int init;
  static struct random_data rdata;
  if (!init) {
    static char state[32];
    rdata.state = NULL;
    __initstate_r(time((time_t *)NULL) ^ getpid(), state, sizeof(state),
                  &rdata);
    init = 1;
  }
  size_t len = strlen(string);
  if (len > 0)
    for (size_t i = 0; i < len - 1; ++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>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    1254 <strfry+0x104>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    124f <strfry+0xff>
lea    0x2e7e(%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    11ce <strfry+0x7e>
jmp    1254 <strfry+0x104>
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>
test   %rax,%rax
je     121b <strfry+0xcb>
mov    %rax,%rbx
cmp    $0x1,%rax
je     121b <strfry+0xcb>
xor    %r13d,%r13d
lea    0x2ea0(%rip),%r15
mov    %rsp,%r12
mov    %r14,%rbp
jmp    11fa <strfry+0xaa>
xor    %edx,%edx
div    %rbx
add    %r13,%rdx
movzbl 0x0(%rbp),%eax
movzbl (%r14,%rdx,1),%ecx
mov    %cl,0x0(%rbp)
mov    %al,(%r14,%rdx,1)
add    $0x1,%r13
add    $0xffffffffffffffff,%rbx
add    $0x1,%rbp
cmp    $0x1,%rbx
je     121b <strfry+0xcb>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <__random_r@plt>
mov    (%rsp),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
jne    11d0 <strfry+0x80>
xor    %edx,%edx
div    %ebx
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 i64 @strlen(i8* %15)
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = icmp ugt i64 %17, 0
br i1 %18, label %19, label %53
19: ; preds = %14
store i64 0, i64* %4
br label %20
20: ; preds = %49, %19
%21 = load i64, i64* %4
%22 = load i64, i64* %3
%23 = sub i64 %22, 1
%24 = icmp ult i64 %21, %23
br i1 %24, label %25, label %52
25: ; preds = %20
%26 = call i32 @__random_r(%struct.random_data* @strfry.rdata, i64* %5)
%27 = load i64, i64* %5
%28 = load i64, i64* %3
%29 = load i64, i64* %4
%30 = sub i64 %28, %29
%31 = urem i64 %27, %30
%32 = load i64, i64* %4
%33 = add i64 %31, %32
store i64 %33, i64* %5
%34 = load i8*, i8** %2
%35 = load i64, i64* %4
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i8, i8* %36
store i8 %37, i8* %6
%38 = load i8*, i8** %2
%39 = load i64, i64* %5
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i8, i8* %40
%42 = load i8*, i8** %2
%43 = load i64, i64* %4
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 %41, i8* %44
%45 = load i8, i8* %6
%46 = load i8*, i8** %2
%47 = load i64, i64* %5
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 %45, i8* %48
br label %49
49: ; preds = %25
%50 = load i64, i64* %4
%51 = add i64 %50, 1
store i64 %51, i64* %4
br label %20
52: ; preds = %20
br label %53
53: ; preds = %52, %14
%54 = load i8*, i8** %2
ret i8* %54
}

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 i64 @strlen(i8* dereferenceable(1) %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %9
%13 = add i64 %10, -1
%14 = icmp eq i64 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %12
%16 = bitcast i64* %2 to i8*
br label %17
17: ; preds = %15, %17
%18 = phi i64 [ 0, %15 ], [ %28, %17 ]
%19 = call i32 @__random_r(%struct.random_data* @strfry.rdata, i64* %2)
%20 = load i64, i64* %2
%21 = sub i64 %10, %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, %13
br i1 %29, label %30, label %17
30: ; preds = %17, %12, %9
ret i8* %0
}