; LLVM IR from square/subzero :: core/trezor-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: nounwind uwtable
define dso_local void @base32_encode_unsafe(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  %11 = load i64, ptr %5, align 8, !tbaa !9
  %12 = urem i64 %11, 5
  %13 = trunc i64 %12 to i8
  store i8 %13, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = load i8, ptr %7, align 1, !tbaa !11
  %16 = zext i8 %15 to i64
  %17 = sub i64 %14, %16
  store i64 %17, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %29, %3
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = icmp ult i64 %19, %20
  br i1 %21, label %22, label %34

22:                                               ; preds = %18
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load i64, ptr %10, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %26, i64 %27
  call void @base32_5to8(ptr noundef %25, i8 noundef zeroext 5, ptr noundef %28)
  br label %29

29:                                               ; preds = %22
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = add i64 %30, 5
  store i64 %31, ptr %9, align 8, !tbaa !9
  %32 = load i64, ptr %10, align 8, !tbaa !9
  %33 = add i64 %32, 8
  store i64 %33, ptr %10, align 8, !tbaa !9
  br label %18, !llvm.loop !12

34:                                               ; preds = %18
  %35 = load i8, ptr %7, align 1, !tbaa !11
  %36 = icmp ne i8 %35, 0
  br i1 %36, label %37, label %45

37:                                               ; preds = %34
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i64, ptr %9, align 8, !tbaa !9
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  %41 = load i8, ptr %7, align 1, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  call void @base32_5to8(ptr noundef %40, i8 noundef zeroext %41, ptr noundef %44)
  br label %45

45:                                               ; preds = %37, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @base32_5to8(ptr noundef, i8 noundef zeroext, 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 void @base32_encode_unsafe(ptr noundef %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = urem i64 %1, 5
  %5 = trunc nuw nsw i64 %4 to i8
  %6 = sub nuw i64 %1, %4
  %.not21 = icmp eq i64 %4, %1
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.019 = phi i64 [ %10, %.lr.ph ], [ 0, %3 ]
  %.01618 = phi i64 [ %9, %.lr.ph ], [ 0, %3 ]
  %7 = getelementptr inbounds i8, ptr %0, i64 %.01618
  %8 = getelementptr inbounds i8, ptr %2, i64 %.019
  tail call void @base32_5to8(ptr noundef %7, i8 noundef zeroext 5, ptr noundef %8)
  %9 = add i64 %.01618, 5
  %10 = add i64 %.019, 8
  %11 = icmp ult i64 %9, %6
  br i1 %11, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.016.lcssa = phi i64 [ 0, %3 ], [ %9, %.lr.ph ]
  %.0.lcssa = phi i64 [ 0, %3 ], [ %10, %.lr.ph ]
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %15, label %12

12:                                               ; preds = %._crit_edge
  %13 = getelementptr inbounds i8, ptr %0, i64 %.016.lcssa
  %14 = getelementptr inbounds i8, ptr %2, i64 %.0.lcssa
  tail call void @base32_5to8(ptr noundef %13, i8 noundef zeroext %5, ptr noundef %14)
  br label %15

15:                                               ; preds = %12, %._crit_edge
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @base32_5to8(ptr noundef, 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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