; LLVM IR from IlyaGrebnov/libbsc :: libbsc/bwt (bwt)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z18bsc_bwt_gpu_encodePhiS_Pii(ptr noundef, i32 noundef, ptr noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress uwtable
define dso_local i32 @bsc_bwt_encode(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca [256 x i32], align 16
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = load i32, ptr %11, align 4, !tbaa !9
  %23 = call noundef i32 @_Z18bsc_bwt_gpu_encodePhiS_Pii(ptr noundef %18, i32 noundef %19, ptr noundef %20, ptr noundef %21, i32 noundef %22)
  store i32 %23, ptr %12, align 4, !tbaa !9
  %24 = load i32, ptr %12, align 4, !tbaa !9
  %25 = icmp sge i32 %24, 0
  br i1 %25, label %26, label %28

26:                                               ; preds = %5
  %27 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %27, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %128

28:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = mul i64 %30, 4
  %32 = call ptr @bsc_malloc(i64 noundef %31)
  store ptr %32, ptr %14, align 8, !tbaa !5
  %33 = load ptr, ptr %14, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %124

35:                                               ; preds = %28
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %111

38:                                               ; preds = %35
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %111

41:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 1024, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = sdiv i32 %42, 8
  store i32 %43, ptr %16, align 4, !tbaa !9
  %44 = load i32, ptr %16, align 4, !tbaa !9
  %45 = ashr i32 %44, 1
  %46 = load i32, ptr %16, align 4, !tbaa !9
  %47 = or i32 %46, %45
  store i32 %47, ptr %16, align 4, !tbaa !9
  %48 = load i32, ptr %16, align 4, !tbaa !9
  %49 = ashr i32 %48, 2
  %50 = load i32, ptr %16, align 4, !tbaa !9
  %51 = or i32 %50, %49
  store i32 %51, ptr %16, align 4, !tbaa !9
  %52 = load i32, ptr %16, align 4, !tbaa !9
  %53 = ashr i32 %52, 4
  %54 = load i32, ptr %16, align 4, !tbaa !9
  %55 = or i32 %54, %53
  store i32 %55, ptr %16, align 4, !tbaa !9
  %56 = load i32, ptr %16, align 4, !tbaa !9
  %57 = ashr i32 %56, 8
  %58 = load i32, ptr %16, align 4, !tbaa !9
  %59 = or i32 %58, %57
  store i32 %59, ptr %16, align 4, !tbaa !9
  %60 = load i32, ptr %16, align 4, !tbaa !9
  %61 = ashr i32 %60, 16
  %62 = load i32, ptr %16, align 4, !tbaa !9
  %63 = or i32 %62, %61
  store i32 %63, ptr %16, align 4, !tbaa !9
  %64 = load i32, ptr %16, align 4, !tbaa !9
  %65 = ashr i32 %64, 1
  store i32 %65, ptr %16, align 4, !tbaa !9
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = load ptr, ptr %14, align 8, !tbaa !5
  %69 = load i32, ptr %8, align 4, !tbaa !9
  %70 = load i32, ptr %16, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  %72 = getelementptr inbounds [256 x i32], ptr %15, i64 0, i64 0
  %73 = call i32 @libsais_bwt_aux(ptr noundef %66, ptr noundef %67, ptr noundef %68, i32 noundef %69, i32 noundef 0, ptr noundef null, i32 noundef %71, ptr noundef %72)
  store i32 %73, ptr %12, align 4, !tbaa !9
  %74 = load i32, ptr %12, align 4, !tbaa !9
  %75 = icmp eq i32 %74, 0
  br i1 %75, label %76, label %110

76:                                               ; preds = %41
  %77 = load i32, ptr %8, align 4, !tbaa !9
  %78 = sub nsw i32 %77, 1
  %79 = load i32, ptr %16, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  %81 = sdiv i32 %78, %80
  %82 = trunc i32 %81 to i8
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = getelementptr inbounds i8, ptr %83, i64 0
  store i8 %82, ptr %84, align 1, !tbaa !11
  %85 = getelementptr inbounds [256 x i32], ptr %15, i64 0, i64 0
  %86 = load i32, ptr %85, align 16, !tbaa !9
  store i32 %86, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %106, %76
  %88 = load i32, ptr %17, align 4, !tbaa !9
  %89 = load ptr, ptr %9, align 8, !tbaa !5
  %90 = getelementptr inbounds i8, ptr %89, i64 0
  %91 = load i8, ptr %90, align 1, !tbaa !11
  %92 = zext i8 %91 to i32
  %93 = icmp slt i32 %88, %92
  br i1 %93, label %95, label %94

94:                                               ; preds = %87
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %109

95:                                               ; preds = %87
  %96 = load i32, ptr %17, align 4, !tbaa !9
  %97 = add nsw i32 %96, 1
  %98 = sext i32 %97 to i64
  %99 = getelementptr inbounds [256 x i32], ptr %15, i64 0, i64 %98
  %100 = load i32, ptr %99, align 4, !tbaa !9
  %101 = sub nsw i32 %100, 1
  %102 = load ptr, ptr %10, align 8, !tbaa !5
  %103 = load i32, ptr %17, align 4, !tbaa !9
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds i32, ptr %102, i64 %104
  store i32 %101, ptr %105, align 4, !tbaa !9
  br label %106

106:                                              ; preds = %95
  %107 = load i32, ptr %17, align 4, !tbaa !9
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %17, align 4, !tbaa !9
  br label %87, !llvm.loop !12

109:                                              ; preds = %94
  br label %110

110:                                              ; preds = %109, %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 1024, ptr %15) #4
  br label %117

111:                                              ; preds = %38, %35
  %112 = load ptr, ptr %7, align 8, !tbaa !5
  %113 = load ptr, ptr %7, align 8, !tbaa !5
  %114 = load ptr, ptr %14, align 8, !tbaa !5
  %115 = load i32, ptr %8, align 4, !tbaa !9
  %116 = call i32 @libsais_bwt(ptr noundef %112, ptr noundef %113, ptr noundef %114, i32 noundef %115, i32 noundef 0, ptr noundef null)
  store i32 %116, ptr %12, align 4, !tbaa !9
  br label %117

117:                                              ; preds = %111, %110
  %118 = load ptr, ptr %14, align 8, !tbaa !5
  call void @bsc_free(ptr noundef %118)
  %119 = load i32, ptr %12, align 4, !tbaa !9
  switch i32 %119, label %122 [
    i32 -1, label %120
    i32 -2, label %121
  ]

120:                                              ; preds = %117
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %125

121:                                              ; preds = %117
  store i32 -2, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %125

122:                                              ; preds = %117
  %123 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %123, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %125

124:                                              ; preds = %28
  store i32 0, ptr %13, align 4
  br label %125

125:                                              ; preds = %124, %122, %121, %120
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  %126 = load i32, ptr %13, align 4
  switch i32 %126, label %128 [
    i32 0, label %127
  ]

127:                                              ; preds = %125
  store i32 -2, ptr %6, align 4
  store i32 1, ptr %13, align 4
  br label %128

128:                                              ; preds = %127, %125, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  %129 = load i32, ptr %6, align 4
  ret i32 %129
}

declare ptr @bsc_malloc(i64 noundef) #3

declare i32 @libsais_bwt_aux(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) #3

declare i32 @libsais_bwt(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) #3

declare void @bsc_free(ptr noundef) #3

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z18bsc_bwt_gpu_encodePhiS_Pii(ptr noundef, i32 noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress uwtable
define dso_local i32 @bsc_bwt_encode(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #2 {
  %6 = alloca [256 x i32], align 16
  %7 = tail call noundef i32 @_Z18bsc_bwt_gpu_encodePhiS_Pii(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4)
  %8 = icmp sgt i32 %7, -1
  br i1 %8, label %48, label %9

9:                                                ; preds = %5
  %10 = sext i32 %1 to i64
  %11 = shl nsw i64 %10, 2
  %12 = tail call ptr @bsc_malloc(i64 noundef %11)
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %48, label %13

13:                                               ; preds = %9
  %14 = icmp ne ptr %2, null
  %15 = icmp ne ptr %3, null
  %or.cond = and i1 %14, %15
  br i1 %or.cond, label %16, label %45

16:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %6) #4
  %17 = sdiv i32 %1, 8
  %18 = ashr i32 %17, 1
  %19 = or i32 %18, %17
  %20 = ashr i32 %19, 2
  %21 = or i32 %20, %19
  %22 = ashr i32 %21, 4
  %23 = or i32 %22, %21
  %24 = ashr i32 %23, 8
  %25 = or i32 %24, %23
  %26 = ashr i32 %25, 17
  %27 = ashr i32 %25, 1
  %28 = or i32 %26, %27
  %29 = add nsw i32 %28, 1
  %30 = call i32 @libsais_bwt_aux(ptr noundef %0, ptr noundef %0, ptr noundef nonnull %12, i32 noundef %1, i32 noundef 0, ptr noundef null, i32 noundef %29, ptr noundef nonnull %6)
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %.loopexit

32:                                               ; preds = %16
  %33 = add nsw i32 %1, -1
  %34 = sdiv i32 %33, %29
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %2, align 1, !tbaa !5
  %36 = load i32, ptr %6, align 16, !tbaa !8
  %37 = and i32 %34, 255
  %.not57 = icmp eq i32 %37, 0
  br i1 %.not57, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %32, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %32 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %38 = getelementptr inbounds [256 x i32], ptr %6, i64 0, i64 %indvars.iv.next
  %39 = load i32, ptr %38, align 4, !tbaa !8
  %40 = add nsw i32 %39, -1
  %41 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  store i32 %40, ptr %41, align 4, !tbaa !8
  %42 = load i8, ptr %2, align 1, !tbaa !5
  %43 = zext i8 %42 to i64
  %44 = icmp ult i64 %indvars.iv.next, %43
  br i1 %44, label %.lr.ph, label %.loopexit, !llvm.loop !10

.loopexit:                                        ; preds = %.lr.ph, %32, %16
  %.049 = phi i32 [ %30, %16 ], [ %36, %32 ], [ %36, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %6) #4
  br label %47

45:                                               ; preds = %13
  %46 = tail call i32 @libsais_bwt(ptr noundef %0, ptr noundef %0, ptr noundef nonnull %12, i32 noundef %1, i32 noundef 0, ptr noundef null)
  br label %47

47:                                               ; preds = %45, %.loopexit
  %.150 = phi i32 [ %.049, %.loopexit ], [ %46, %45 ]
  call void @bsc_free(ptr noundef nonnull %12)
  br label %48

48:                                               ; preds = %47, %9, %5
  %.1 = phi i32 [ %7, %5 ], [ -2, %9 ], [ %.150, %47 ]
  ret i32 %.1
}

declare ptr @bsc_malloc(i64 noundef) local_unnamed_addr #3

declare i32 @libsais_bwt_aux(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

declare i32 @libsais_bwt(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

declare void @bsc_free(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass