; 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: nounwind uwtable
define dso_local ptr @base32_encode(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %16 = load i64, ptr %8, align 8, !tbaa !9
  %17 = call i64 @base32_encoded_length(i64 noundef %16)
  store i64 %17, ptr %12, align 8, !tbaa !9
  %18 = load i64, ptr %10, align 8, !tbaa !9
  %19 = load i64, ptr %12, align 8, !tbaa !9
  %20 = icmp ule i64 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %5
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %63

22:                                               ; preds = %5
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  call void @base32_encode_unsafe(ptr noundef %23, i64 noundef %24, ptr noundef %25)
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  store i64 0, ptr %14, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %51, %22
  %27 = load i64, ptr %14, align 8, !tbaa !9
  %28 = load i64, ptr %12, align 8, !tbaa !9
  %29 = icmp ult i64 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %13, align 4
  br label %54

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = load i64, ptr %14, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %32, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = call i32 @base32_encode_character(i8 noundef zeroext %35, ptr noundef %36)
  store i32 %37, ptr %15, align 4, !tbaa !12
  %38 = load i32, ptr %15, align 4, !tbaa !12
  %39 = icmp eq i32 %38, -1
  br i1 %39, label %40, label %41

40:                                               ; preds = %31
  store ptr null, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %48

41:                                               ; preds = %31
  %42 = load i32, ptr %15, align 4, !tbaa !12
  %43 = trunc i32 %42 to i8
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = load i64, ptr %14, align 8, !tbaa !9
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  store i8 %43, ptr %46, align 1, !tbaa !11
  br label %47

47:                                               ; preds = %41
  store i32 0, ptr %13, align 4
  br label %48

48:                                               ; preds = %47, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  %49 = load i32, ptr %13, align 4
  switch i32 %49, label %54 [
    i32 0, label %50
  ]

50:                                               ; preds = %48
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %14, align 8, !tbaa !9
  %53 = add i64 %52, 1
  store i64 %53, ptr %14, align 8, !tbaa !9
  br label %26, !llvm.loop !14

54:                                               ; preds = %48, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  %55 = load i32, ptr %13, align 4
  switch i32 %55, label %63 [
    i32 2, label %56
  ]

56:                                               ; preds = %54
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i64, ptr %12, align 8, !tbaa !9
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  store i8 0, ptr %59, align 1, !tbaa !11
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = load i64, ptr %12, align 8, !tbaa !9
  %62 = getelementptr inbounds i8, ptr %60, i64 %61
  store ptr %62, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %63

63:                                               ; preds = %56, %54, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %64 = load ptr, ptr %6, align 8
  ret ptr %64
}

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @base32_encoded_length(i64 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @base32_encode_unsafe(ptr noundef, i64 noundef, ptr noundef) #0

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 ptr @base32_encode(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = tail call i64 @base32_encoded_length(i64 noundef %1)
  %.not = icmp ult i64 %6, %3
  br i1 %.not, label %7, label %.critedge

7:                                                ; preds = %5
  tail call void @base32_encode_unsafe(ptr noundef %0, i64 noundef %1, ptr noundef %2)
  %.not2931.not = icmp eq i64 %6, 0
  br i1 %.not2931.not, label %.critedge30, label %.lr.ph

.lr.ph:                                           ; preds = %7, %11
  %.02332 = phi i64 [ %13, %11 ], [ 0, %7 ]
  %8 = getelementptr inbounds i8, ptr %2, i64 %.02332
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = tail call i32 @base32_encode_character(i8 noundef zeroext %9, ptr noundef %4)
  %.not28 = icmp eq i32 %10, -1
  br i1 %.not28, label %.critedge, label %11

11:                                               ; preds = %.lr.ph
  %12 = trunc i32 %10 to i8
  store i8 %12, ptr %8, align 1, !tbaa !5
  %13 = add nuw i64 %.02332, 1
  %exitcond.not = icmp eq i64 %13, %6
  br i1 %exitcond.not, label %.critedge30, label %.lr.ph, !llvm.loop !8

.critedge30:                                      ; preds = %11, %7
  %14 = getelementptr inbounds i8, ptr %2, i64 %6
  store i8 0, ptr %14, align 1, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.lr.ph, %5, %.critedge30
  %.3 = phi ptr [ %14, %.critedge30 ], [ null, %5 ], [ null, %.lr.ph ]
  ret ptr %.3
}

; Function Attrs: nounwind uwtable
declare dso_local i64 @base32_encoded_length(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @base32_encode_unsafe(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @base32_encode_character(i8 noundef zeroext, 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 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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