#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);
int divsufsort(const unsigned char *T, int *SA, int n, int openMP) {
  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, openMP);
    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)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117d <divsufsort+0x3d>
cmpq   $0x0,-0x18(%rbp)
je     117d <divsufsort+0x3d>
cmpl   $0x0,-0x1c(%rbp)
jge    1189 <divsufsort+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <divsufsort+0x176>
cmpl   $0x0,-0x1c(%rbp)
jne    119f <divsufsort+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <divsufsort+0x176>
cmpl   $0x1,-0x1c(%rbp)
jne    11bf <divsufsort+0x7f>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12b6 <divsufsort+0x176>
cmpl   $0x2,-0x1c(%rbp)
jne    1214 <divsufsort+0xd4>
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    12b6 <divsufsort+0x176>
jmp    1219 <divsufsort+0xd9>
jmp    121e <divsufsort+0xde>
jmp    1223 <divsufsort+0xe3>
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     1297 <divsufsort+0x157>
cmpq   $0x0,-0x30(%rbp)
je     1297 <divsufsort+0x157>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
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    129e <divsufsort+0x15e>
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     1231 <divsufsort+0xf1>
mov    %rsi,%r14
test   %rsi,%rsi
je     1231 <divsufsort+0xf1>
mov    %edx,%ebx
test   %edx,%edx
js     1231 <divsufsort+0xf1>
je     118c <divsufsort+0x4c>
mov    %rdi,%r13
cmp    $0x2,%ebx
je     1193 <divsufsort+0x53>
cmp    $0x1,%ebx
jne    11b9 <divsufsort+0x79>
movl   $0x0,(%r14)
xor    %ebp,%ebp
jmp    1231 <divsufsort+0xf1>
mov    %ebx,%ebp
jmp    1231 <divsufsort+0xf1>
mov    0x0(%r13),%al
xor    %ecx,%ecx
xor    %edx,%edx
cmp    0x1(%r13),%al
setb   %cl
setae  %dl
movl   $0x0,(%r14,%rdx,4)
movl   $0x1,(%r14,%rcx,4)
xor    %ebp,%ebp
jmp    1231 <divsufsort+0xf1>
mov    %ecx,%r15d
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    $0x40000,%edi
call   1050 <malloc@plt>
mov    $0xfffffffe,%ebp
mov    %r12,(%rsp)
test   %r12,%r12
je     1220 <divsufsort+0xe0>
test   %rax,%rax
je     1220 <divsufsort+0xe0>
mov    %r13,%rdi
mov    %r14,%rsi
mov    (%rsp),%rbp
mov    %rbp,%rdx
mov    %rax,%rcx
mov    %ebx,%r8d
mov    %r15d,%r9d
mov    %rax,%r15
call   1060 <sort_typeBstar@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
mov    %eax,%r9d
call   1040 <construct_SA@plt>
mov    %r15,%rax
xor    %ebp,%ebp
mov    %rax,%rdi
call   1030 <free@plt>
mov    (%rsp),%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, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %13
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %4
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19, %16, %4
store i32 -1, i32* %5
br label %91
23: ; preds = %19
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %5
br label %91
27: ; preds = %23
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32*, i32** %7
%32 = getelementptr inbounds i32, i32* %31, i64 0
store i32 0, i32* %32
store i32 0, i32* %5
br label %91
33: ; preds = %27
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp slt i32 %40, %44
%46 = zext i1 %45 to i32
store i32 %46, i32* %12
%47 = load i32*, i32** %7
%48 = load i32, i32* %12
%49 = xor i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %47, i64 %50
store i32 0, i32* %51
%52 = load i32*, i32** %7
%53 = load i32, i32* %12
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
store i32 1, i32* %55
store i32 0, i32* %5
br label %91
56: ; preds = %33
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57
br label %59
59: ; preds = %58
%60 = call i64 @malloc(i32 1024)
%61 = inttoptr i64 %60 to i32*
store i32* %61, i32** %10
%62 = call i64 @malloc(i32 262144)
%63 = inttoptr i64 %62 to i32*
store i32* %63, i32** %11
%64 = load i32*, i32** %10
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %84
66: ; preds = %59
%67 = load i32*, i32** %11
%68 = icmp ne i32* %67, null
br i1 %68, label %69, label %84
69: ; preds = %66
%70 = load i8*, i8** %6
%71 = load i32*, i32** %7
%72 = load i32*, i32** %10
%73 = load i32*, i32** %11
%74 = load i32, i32* %8
%75 = load i32, i32* %9
%76 = call i32 @sort_typeBstar(i8* %70, i32* %71, i32* %72, i32* %73, i32 %74, i32 %75)
store i32 %76, i32* %12
%77 = load i8*, i8** %6
%78 = load i32*, i32** %7
%79 = load i32*, i32** %10
%80 = load i32*, i32** %11
%81 = load i32, i32* %8
%82 = load i32, i32* %12
%83 = call i32 @construct_SA(i8* %77, i32* %78, i32* %79, i32* %80, i32 %81, i32 %82)
br label %85
84: ; preds = %66, %59
store i32 -2, i32* %13
br label %85
85: ; preds = %84, %69
%86 = load i32*, i32** %11
%87 = call i32 @free(i32* %86)
%88 = load i32*, i32** %10
%89 = call i32 @free(i32* %88)
%90 = load i32, i32* %13
store i32 %90, i32* %5
br label %91
91: ; preds = %85, %36, %30, %26, %22
%92 = load i32, i32* %5
ret i32 %92
}

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