#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 */
int /*<<< orphan*/ construct_SA(unsigned char const *, int *, int *, int *, int,
                                int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int sort_typeBstar(unsigned char const *, int *, int *, int *, int);
int divsufsort(const unsigned char *T, int *SA, int n) {
  int *bucket_A, *bucket_B;
  int m;
  int err = 0;
  if ((T == NULL) || (SA == NULL) || (n < 0)) {
    return -1;
  } else if (n == 0) {
    return 0;
  } else if (n == 1) {
    SA[0] = 0;
    return 0;
  } else if (n == 2) {
    m = (T[0] < T[1]);
    SA[m ^ 1] = 0, SA[m] = 1;
    return 0;
  }
  bucket_A = (int *)malloc(((256)) * sizeof(int));
  bucket_B = (int *)malloc(((256) * (256)) * sizeof(int));
  if ((bucket_A != NULL) && (bucket_B != NULL)) {
    m = sort_typeBstar(T, SA, bucket_A, bucket_B, n);
    construct_SA(T, SA, bucket_A, bucket_B, n, m);
  } else {
    err = -2;
  }
  free(bucket_B);
  free(bucket_A);
  return err;
}


x86 assembly (clang, -O0):
<divsufsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117a <divsufsort+0x3a>
cmpq   $0x0,-0x18(%rbp)
je     117a <divsufsort+0x3a>
cmpl   $0x0,-0x1c(%rbp)
jge    1186 <divsufsort+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <divsufsort+0x16f>
cmpl   $0x0,-0x1c(%rbp)
jne    119c <divsufsort+0x5c>
movl   $0x0,-0x4(%rbp)
jmp    12af <divsufsort+0x16f>
cmpl   $0x1,-0x1c(%rbp)
jne    11bc <divsufsort+0x7c>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12af <divsufsort+0x16f>
cmpl   $0x2,-0x1c(%rbp)
jne    1211 <divsufsort+0xd1>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
cmp    %ecx,%eax
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
xor    $0x1,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
movl   $0x0,-0x4(%rbp)
jmp    12af <divsufsort+0x16f>
jmp    1216 <divsufsort+0xd6>
jmp    121b <divsufsort+0xdb>
jmp    1220 <divsufsort+0xe0>
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x40000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1290 <divsufsort+0x150>
cmpq   $0x0,-0x30(%rbp)
je     1290 <divsufsort+0x150>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1060 <sort_typeBstar@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    -0x34(%rbp),%r9d
call   1040 <construct_SA@plt>
jmp    1297 <divsufsort+0x157>
movl   $0xfffffffe,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<divsufsort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     121e <divsufsort+0xde>
mov    %rsi,%r14
test   %rsi,%rsi
je     121e <divsufsort+0xde>
mov    %edx,%ebx
test   %edx,%edx
js     121e <divsufsort+0xde>
je     118c <divsufsort+0x4c>
mov    %rdi,%r15
cmp    $0x2,%ebx
je     1193 <divsufsort+0x53>
cmp    $0x1,%ebx
jne    11b8 <divsufsort+0x78>
movl   $0x0,(%r14)
xor    %ebp,%ebp
jmp    121e <divsufsort+0xde>
mov    %ebx,%ebp
jmp    121e <divsufsort+0xde>
mov    (%r15),%al
xor    %ecx,%ecx
xor    %edx,%edx
cmp    0x1(%r15),%al
setb   %cl
setae  %dl
movl   $0x0,(%r14,%rdx,4)
movl   $0x1,(%r14,%rcx,4)
xor    %ebp,%ebp
jmp    121e <divsufsort+0xde>
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    $0x40000,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
mov    $0xfffffffe,%ebp
test   %r12,%r12
je     120e <divsufsort+0xce>
test   %r13,%r13
je     120e <divsufsort+0xce>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %ebx,%r8d
call   1060 <sort_typeBstar@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %ebx,%r8d
mov    %eax,%r9d
call   1040 <construct_SA@plt>
xor    %ebp,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @divsufsort(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %20, label %14
14: ; preds = %3
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17, %14, %3
store i32 -1, i32* %4
br label %88
21: ; preds = %17
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 0, i32* %4
br label %88
25: ; preds = %21
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %6
%30 = getelementptr inbounds i32, i32* %29, i64 0
store i32 0, i32* %30
store i32 0, i32* %4
br label %88
31: ; preds = %25
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %54
34: ; preds = %31
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = icmp slt i32 %38, %42
%44 = zext i1 %43 to i32
store i32 %44, i32* %10
%45 = load i32*, i32** %6
%46 = load i32, i32* %10
%47 = xor i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %45, i64 %48
store i32 0, i32* %49
%50 = load i32*, i32** %6
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 1, i32* %53
store i32 0, i32* %4
br label %88
54: ; preds = %31
br label %55
55: ; preds = %54
br label %56
56: ; preds = %55
br label %57
57: ; preds = %56
%58 = call i64 @malloc(i32 1024)
%59 = inttoptr i64 %58 to i32*
store i32* %59, i32** %8
%60 = call i64 @malloc(i32 262144)
%61 = inttoptr i64 %60 to i32*
store i32* %61, i32** %9
%62 = load i32*, i32** %8
%63 = icmp ne i32* %62, null
br i1 %63, label %64, label %81
64: ; preds = %57
%65 = load i32*, i32** %9
%66 = icmp ne i32* %65, null
br i1 %66, label %67, label %81
67: ; preds = %64
%68 = load i8*, i8** %5
%69 = load i32*, i32** %6
%70 = load i32*, i32** %8
%71 = load i32*, i32** %9
%72 = load i32, i32* %7
%73 = call i32 @sort_typeBstar(i8* %68, i32* %69, i32* %70, i32* %71, i32 %72)
store i32 %73, i32* %10
%74 = load i8*, i8** %5
%75 = load i32*, i32** %6
%76 = load i32*, i32** %8
%77 = load i32*, i32** %9
%78 = load i32, i32* %7
%79 = load i32, i32* %10
%80 = call i32 @construct_SA(i8* %74, i32* %75, i32* %76, i32* %77, i32 %78, i32 %79)
br label %82
81: ; preds = %64, %57
store i32 -2, i32* %11
br label %82
82: ; preds = %81, %67
%83 = load i32*, i32** %9
%84 = call i32 @free(i32* %83)
%85 = load i32*, i32** %8
%86 = call i32 @free(i32* %85)
%87 = load i32, i32* %11
store i32 %87, i32* %4
br label %88
88: ; preds = %82, %34, %28, %24, %20
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
define i32 @divsufsort(i8* %0, i32* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i32* %1, null
%6 = or i1 %4, %5
%7 = icmp slt i32 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %36, label %9
9: ; preds = %3
switch i32 %2, label %21 [
i32 0, label %36
i32 1, label %10
i32 2, label %11
]
10: ; preds = %9
store i32 0, i32* %1
br label %36
11: ; preds = %9
%12 = load i8, i8* %0
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = load i8, i8* %13
%15 = icmp ult i8 %12, %14
%16 = xor i1 %15, true
%17 = zext i1 %16 to i64
%18 = getelementptr inbounds i32, i32* %1, i64 %17
store i32 0, i32* %18
%19 = zext i1 %15 to i64
%20 = getelementptr inbounds i32, i32* %1, i64 %19
store i32 1, i32* %20
br label %36
21: ; preds = %9
%22 = tail call i64 @malloc(i32 1024)
%23 = inttoptr i64 %22 to i32*
%24 = tail call i64 @malloc(i32 262144)
%25 = inttoptr i64 %24 to i32*
%26 = icmp ne i64 %22, 0
%27 = icmp ne i64 %24, 0
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = tail call i32 @sort_typeBstar(i8* %0, i32* %1, i32* %23, i32* %25, i32 %2)
%31 = tail call i32 @construct_SA(i8* %0, i32* %1, i32* %23, i32* %25, i32 %2, i32 %30)
br label %32
32: ; preds = %21, %29
%33 = phi i32 [ 0, %29 ], [ -2, %21 ]
%34 = tail call i32 @free(i32* %25)
%35 = tail call i32 @free(i32* %23)
br label %36
36: ; preds = %9, %3, %32, %11, %10
%37 = phi i32 [ 0, %10 ], [ 0, %11 ], [ %33, %32 ], [ -1, %3 ], [ %2, %9 ]
ret i32 %37
}