; LLVM IR from square/subzero :: core/trezor-crypto/monero (base58)

; 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"

@decoded_block_sizes = external dso_local constant [12 x i32], align 16
@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [45 x i8], align 1
@__PRETTY_FUNCTION__.decode_block = external hidden unnamed_addr constant [49 x i8], align 1
@b58digits_map = external constant [0 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr 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: nounwind uwtable
declare dso_local void @uint_64_to_8be(i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @decode_block(ptr noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %16 = load i64, ptr %6, align 8, !tbaa !9
  %17 = icmp ule i64 1, %16
  br i1 %17, label %18, label %22

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

21:                                               ; preds = %18
  br label %23

22:                                               ; preds = %18, %3
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.1, i32 noundef 95, ptr noundef @__PRETTY_FUNCTION__.decode_block) #4
  unreachable

23:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = getelementptr inbounds [12 x i32], ptr @decoded_block_sizes, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !11
  store i32 %26, ptr %8, align 4, !tbaa !11
  %27 = load i32, ptr %8, align 4, !tbaa !11
  %28 = icmp sle i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %23
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %96

30:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  store i64 0, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  store i64 1, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = sub i64 %31, 1
  store i64 %32, ptr %12, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %73, %30
  %34 = load i64, ptr %12, align 8, !tbaa !9
  %35 = load i64, ptr %6, align 8, !tbaa !9
  %36 = icmp ult i64 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 2, ptr %9, align 4
  br label %76

38:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = getelementptr inbounds i8, ptr %39, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !13
  %43 = sext i8 %42 to i32
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [0 x i8], ptr @b58digits_map, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !13
  %47 = sext i8 %46 to i32
  store i32 %47, ptr %13, align 4, !tbaa !11
  %48 = load i32, ptr %13, align 4, !tbaa !11
  %49 = icmp slt i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %38
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %70

51:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %52 = load i64, ptr %10, align 8, !tbaa !9
  %53 = load i64, ptr %11, align 8, !tbaa !9
  %54 = load i32, ptr %13, align 4, !tbaa !11
  %55 = sext i32 %54 to i64
  %56 = call i64 @mul128(i64 noundef %53, i64 noundef %55, ptr noundef %14)
  %57 = add i64 %52, %56
  store i64 %57, ptr %15, align 8, !tbaa !9
  %58 = load i64, ptr %15, align 8, !tbaa !9
  %59 = load i64, ptr %10, align 8, !tbaa !9
  %60 = icmp ult i64 %58, %59
  br i1 %60, label %64, label %61

61:                                               ; preds = %51
  %62 = load i64, ptr %14, align 8, !tbaa !9
  %63 = icmp ne i64 0, %62
  br i1 %63, label %64, label %65

64:                                               ; preds = %61, %51
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %69

65:                                               ; preds = %61
  %66 = load i64, ptr %15, align 8, !tbaa !9
  store i64 %66, ptr %10, align 8, !tbaa !9
  %67 = load i64, ptr %11, align 8, !tbaa !9
  %68 = mul i64 %67, 58
  store i64 %68, ptr %11, align 8, !tbaa !9
  store i32 0, ptr %9, align 4
  br label %69

69:                                               ; preds = %65, %64
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  br label %70

70:                                               ; preds = %69, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  %71 = load i32, ptr %9, align 4
  switch i32 %71, label %76 [
    i32 0, label %72
  ]

72:                                               ; preds = %70
  br label %73

73:                                               ; preds = %72
  %74 = load i64, ptr %12, align 8, !tbaa !9
  %75 = add i64 %74, -1
  store i64 %75, ptr %12, align 8, !tbaa !9
  br label %33, !llvm.loop !14

76:                                               ; preds = %70, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  %77 = load i32, ptr %9, align 4
  switch i32 %77, label %95 [
    i32 2, label %78
  ]

78:                                               ; preds = %76
  %79 = load i32, ptr %8, align 4, !tbaa !11
  %80 = sext i32 %79 to i64
  %81 = icmp ult i64 %80, 8
  br i1 %81, label %82, label %90

82:                                               ; preds = %78
  %83 = load i32, ptr %8, align 4, !tbaa !11
  %84 = mul nsw i32 8, %83
  %85 = zext i32 %84 to i64
  %86 = shl i64 1, %85
  %87 = load i64, ptr %10, align 8, !tbaa !9
  %88 = icmp ule i64 %86, %87
  br i1 %88, label %89, label %90

89:                                               ; preds = %82
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %95

90:                                               ; preds = %82, %78
  %91 = load i64, ptr %10, align 8, !tbaa !9
  %92 = load i32, ptr %8, align 4, !tbaa !11
  %93 = sext i32 %92 to i64
  %94 = load ptr, ptr %7, align 8, !tbaa !5
  call void @uint_64_to_8be(i64 noundef %91, i64 noundef %93, ptr noundef %94)
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %95

95:                                               ; preds = %90, %89, %76
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %96

96:                                               ; preds = %95, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %97 = load i1, ptr %4, align 1
  ret i1 %97
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @mul128(i64 noundef, i64 noundef, ptr noundef) #3

attributes #0 = { noreturn nounwind "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 = { 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 = { 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 #4 = { noreturn nounwind }
attributes #5 = { 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !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"

@decoded_block_sizes = external dso_local local_unnamed_addr constant [12 x i32], align 16
@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [45 x i8], align 1
@__PRETTY_FUNCTION__.decode_block = external hidden unnamed_addr constant [49 x i8], align 1
@b58digits_map = external local_unnamed_addr constant [0 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr 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: nounwind uwtable
declare dso_local void @uint_64_to_8be(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @decode_block(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = alloca i64, align 8
  %5 = add i64 %1, -1
  %or.cond = icmp ult i64 %5, 11
  br i1 %or.cond, label %7, label %6

6:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i32 noundef 95, ptr noundef nonnull @__PRETTY_FUNCTION__.decode_block) #4
  unreachable

7:                                                ; preds = %3
  %8 = getelementptr inbounds [12 x i32], ptr @decoded_block_sizes, i64 0, i64 %1
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 1
  br i1 %10, label %.thread50, label %.preheader

.preheader:                                       ; preds = %7, %18
  %.037 = phi i64 [ %21, %18 ], [ 0, %7 ]
  %.034 = phi i64 [ %25, %18 ], [ 1, %7 ]
  %.033.in = phi i64 [ %.033, %18 ], [ %1, %7 ]
  %.033 = add nsw i64 %.033.in, -1
  %.not = icmp ult i64 %.033, %1
  br i1 %.not, label %11, label %26

11:                                               ; preds = %.preheader
  %12 = getelementptr inbounds i8, ptr %0, i64 %.033
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = sext i8 %13 to i64
  %15 = getelementptr inbounds [0 x i8], ptr @b58digits_map, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = icmp slt i8 %16, 0
  br i1 %17, label %.thread50, label %18

18:                                               ; preds = %11
  %19 = zext nneg i8 %16 to i64
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  %20 = call i64 @mul128(i64 noundef %.034, i64 noundef %19, ptr noundef nonnull %4)
  %21 = add i64 %20, %.037
  %22 = icmp ult i64 %21, %.037
  %23 = load i64, ptr %4, align 8
  %24 = icmp ne i64 %23, 0
  %or.cond4 = select i1 %22, i1 true, i1 %24
  %25 = mul i64 %.034, 58
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  br i1 %or.cond4, label %.thread50, label %.preheader, !llvm.loop !10

26:                                               ; preds = %.preheader
  %27 = zext nneg i32 %9 to i64
  %28 = icmp ult i32 %9, 8
  br i1 %28, label %29, label %32

29:                                               ; preds = %26
  %30 = shl nuw nsw i32 %9, 3
  %31 = zext nneg i32 %30 to i64
  %.340.highbits = lshr i64 %.037, %31
  %.not45 = icmp eq i64 %.340.highbits, 0
  br i1 %.not45, label %32, label %.thread50

32:                                               ; preds = %29, %26
  call void @uint_64_to_8be(i64 noundef %.037, i64 noundef %27, ptr noundef %2)
  br label %.thread50

.thread50:                                        ; preds = %11, %18, %32, %29, %7
  %.5 = phi i1 [ false, %7 ], [ true, %32 ], [ false, %29 ], [ false, %18 ], [ false, %11 ]
  ret i1 %.5
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @mul128(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

attributes #0 = { noreturn nounwind "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 = { 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 = { 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 #4 = { noreturn nounwind }
attributes #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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