; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (base32)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @base32_decode_unsafe(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i1, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i8, align 1
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = urem i64 %15, 8
  %17 = trunc i64 %16 to i8
  store i8 %17, ptr %10, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load i8, ptr %10, align 1, !tbaa !11
  %20 = zext i8 %19 to i64
  %21 = sub i64 %18, %20
  store i64 %21, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 0, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !9
  store i64 0, ptr %12, align 8, !tbaa !9
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %37, %4
  %23 = load i64, ptr %12, align 8, !tbaa !9
  %24 = load i64, ptr %11, align 8, !tbaa !9
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %26, label %42

26:                                               ; preds = %22
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i64, ptr %12, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %13, align 8, !tbaa !9
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = call zeroext i1 @base32_8to5(ptr noundef %29, i8 noundef zeroext 8, ptr noundef %32, ptr noundef %33)
  br i1 %34, label %36, label %35

35:                                               ; preds = %26
  store i1 false, ptr %5, align 1
  store i32 1, ptr %14, align 4
  br label %58

36:                                               ; preds = %26
  br label %37

37:                                               ; preds = %36
  %38 = load i64, ptr %12, align 8, !tbaa !9
  %39 = add i64 %38, 8
  store i64 %39, ptr %12, align 8, !tbaa !9
  %40 = load i64, ptr %13, align 8, !tbaa !9
  %41 = add i64 %40, 5
  store i64 %41, ptr %13, align 8, !tbaa !9
  br label %22, !llvm.loop !12

42:                                               ; preds = %22
  %43 = load i8, ptr %10, align 1, !tbaa !11
  %44 = zext i8 %43 to i32
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %57

46:                                               ; preds = %42
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %12, align 8, !tbaa !9
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  %50 = load i8, ptr %10, align 1, !tbaa !11
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = load i64, ptr %13, align 8, !tbaa !9
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  %55 = call zeroext i1 @base32_8to5(ptr noundef %49, i8 noundef zeroext %50, ptr noundef %53, ptr noundef %54)
  br i1 %55, label %57, label %56

56:                                               ; preds = %46
  store i1 false, ptr %5, align 1
  store i32 1, ptr %14, align 4
  br label %58

57:                                               ; preds = %46, %42
  store i1 true, ptr %5, align 1
  store i32 1, ptr %14, align 4
  br label %58

58:                                               ; preds = %57, %56, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  %59 = load i1, ptr %5, align 1
  ret i1 %59
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @base32_8to5(ptr noundef, i8 noundef zeroext, ptr noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 #3 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !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: nounwind uwtable
define dso_local noundef zeroext i1 @base32_decode_unsafe(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = trunc i64 %1 to i8
  %6 = and i8 %5, 7
  %7 = and i64 %1, -8
  %.not26 = icmp eq i64 %7, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %11
  %.024 = phi i64 [ %13, %11 ], [ 0, %4 ]
  %.01923 = phi i64 [ %12, %11 ], [ 0, %4 ]
  %8 = getelementptr inbounds i8, ptr %0, i64 %.01923
  %9 = getelementptr inbounds i8, ptr %2, i64 %.024
  %10 = tail call zeroext i1 @base32_8to5(ptr noundef %8, i8 noundef zeroext 8, ptr noundef %9, ptr noundef %3)
  br i1 %10, label %11, label %.loopexit

11:                                               ; preds = %.lr.ph
  %12 = add nuw i64 %.01923, 8
  %13 = add nuw i64 %.024, 5
  %14 = icmp ult i64 %12, %7
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %11, %4
  %.019.lcssa = phi i64 [ 0, %4 ], [ %12, %11 ]
  %.0.lcssa = phi i64 [ 0, %4 ], [ %13, %11 ]
  %.not = icmp eq i8 %6, 0
  br i1 %.not, label %19, label %15

15:                                               ; preds = %._crit_edge
  %16 = getelementptr inbounds i8, ptr %0, i64 %.019.lcssa
  %17 = getelementptr inbounds i8, ptr %2, i64 %.0.lcssa
  %18 = tail call zeroext i1 @base32_8to5(ptr noundef %16, i8 noundef zeroext %6, ptr noundef %17, ptr noundef %3)
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %15, %._crit_edge
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %15, %19
  %.020 = phi i1 [ true, %19 ], [ false, %15 ], [ false, %.lr.ph ]
  ret i1 %.020
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @base32_8to5(ptr noundef, i8 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 = { inlinehint 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" }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass