#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 */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ shellsort(char *, int /*<<< orphan*/, int);
int strlen(char *);
char *sort(char text[]) {
  int l = strlen(text);
  char *newstring;
  if ((newstring = malloc(l + 1))) {
    memcpy(newstring, text, l + 1);
    if (l > 1) {
      shellsort(newstring, 0, l - 1);
    }
  } else {
    newstring = NULL;
  }
  return newstring;
}


x86 assembly (clang, -O0):
<sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a4 <sort+0x64>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0x1,%edx
call   1040 <memcpy@plt>
cmpl   $0x1,-0xc(%rbp)
jle    119f <sort+0x5f>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1060 <shellsort@plt>
jmp    11ac <sort+0x6c>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1189 <sort+0x49>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
cmp    $0x2,%r14d
jl     1189 <sort+0x49>
add    $0xffffffff,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1060 <shellsort@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @sort(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %25
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8*, i8** %2
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
%16 = call i32 @memcpy(i8* %12, i8* %13, i32 %15)
%17 = load i32, i32* %3
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i32, i32* %3
%22 = sub nsw i32 %21, 1
%23 = call i32 @shellsort(i8* %20, i32 0, i32 %22)
br label %24
24: ; preds = %19, %11
br label %26
25: ; preds = %1
store i8* null, i8** %4
br label %26
26: ; preds = %25, %24
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
define i8* @sort(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = tail call i32 @memcpy(i8* %4, i8* %0, i32 %3)
%8 = icmp sgt i32 %2, 1
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = add nsw i32 %2, -1
%11 = tail call i32 @shellsort(i8* %4, i32 0, i32 %10)
br label %12
12: ; preds = %1, %6, %9
ret i8* %4
}