; LLVM IR from trustwallet/wallet-core :: trezor-crypto/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"

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @xmr_base58_encode(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #2

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

19:                                               ; preds = %5
  %20 = load i64, ptr %7, align 8, !tbaa !5
  %21 = icmp ugt i64 %20, 127
  br i1 %21, label %22, label %23

22:                                               ; preds = %19, %5
  store i32 0, ptr %6, align 4
  br label %57

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %24 = load i64, ptr %11, align 8, !tbaa !5
  store i64 %24, ptr %12, align 8, !tbaa !5
  %25 = load i64, ptr %9, align 8, !tbaa !5
  %26 = add i64 %25, 1
  %27 = add i64 %26, 32
  %28 = call ptr @llvm.stacksave.p0()
  store ptr %28, ptr %13, align 8
  %29 = alloca i8, i64 %27, align 16
  store i64 %27, ptr %14, align 8
  call void @llvm.memset.p0.i64(ptr align 16 %29, i8 0, i64 %27, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  %30 = load i64, ptr %9, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %29, i64 %30
  %32 = getelementptr inbounds i8, ptr %31, i64 1
  store ptr %32, ptr %15, align 8, !tbaa !9
  %33 = load i64, ptr %7, align 8, !tbaa !5
  %34 = trunc i64 %33 to i8
  %35 = getelementptr inbounds i8, ptr %29, i64 0
  store i8 %34, ptr %35, align 16, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %29, i64 1
  %37 = load ptr, ptr %8, align 8, !tbaa !9
  %38 = load i64, ptr %9, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %36, ptr align 1 %37, i64 %38, i1 false)
  %39 = load i64, ptr %9, align 8, !tbaa !5
  %40 = add i64 %39, 1
  %41 = load ptr, ptr %15, align 8, !tbaa !9
  call void @hasher_Raw(i32 noundef 4, ptr noundef %29, i64 noundef %40, ptr noundef %41)
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #6
  %42 = load ptr, ptr %10, align 8, !tbaa !9
  %43 = load i64, ptr %9, align 8, !tbaa !5
  %44 = add i64 %43, 1
  %45 = add i64 %44, 4
  %46 = call zeroext i1 @xmr_base58_encode(ptr noundef %42, ptr noundef %12, ptr noundef %29, i64 noundef %45)
  %47 = zext i1 %46 to i8
  store i8 %47, ptr %16, align 1, !tbaa !12
  %48 = load i8, ptr %16, align 1, !tbaa !12, !range !14, !noundef !15
  %49 = trunc i8 %48 to i1
  br i1 %49, label %51, label %50

50:                                               ; preds = %23
  br label %53

51:                                               ; preds = %23
  %52 = load i64, ptr %12, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %51, %50
  %54 = phi i64 [ 0, %50 ], [ %52, %51 ]
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  %56 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %56)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  br label %57

57:                                               ; preds = %53, %22
  %58 = load i32, ptr %6, align 4
  ret i32 %58
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

declare void @hasher_Raw(i32 noundef, ptr noundef, i64 noundef, ptr noundef) #5

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { "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 #6 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}


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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @xmr_base58_encode(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @xmr_base58_addr_encode_check(i64 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = alloca i64, align 8
  %7 = icmp ugt i64 %2, 128
  %8 = icmp ugt i64 %0, 127
  %or.cond = or i1 %8, %7
  br i1 %or.cond, label %23, label %9

9:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #6
  store i64 %4, ptr %6, align 8, !tbaa !5
  %10 = add nuw nsw i64 %2, 1
  %11 = add nuw nsw i64 %2, 33
  %12 = tail call ptr @llvm.stacksave.p0()
  %13 = alloca i8, i64 %11, align 16
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1) %13, i8 0, i64 %11, i1 false)
  %14 = getelementptr inbounds i8, ptr %13, i64 %2
  %15 = getelementptr inbounds i8, ptr %14, i64 1
  %16 = trunc nuw nsw i64 %0 to i8
  store i8 %16, ptr %13, align 16, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %13, i64 1
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %17, ptr align 1 %1, i64 %2, i1 false)
  call void @hasher_Raw(i32 noundef 4, ptr noundef nonnull %13, i64 noundef %10, ptr noundef nonnull %15) #6
  %18 = add nuw nsw i64 %2, 5
  %19 = call zeroext i1 @xmr_base58_encode(ptr noundef %3, ptr noundef nonnull %6, ptr noundef nonnull %13, i64 noundef %18)
  %20 = load i64, ptr %6, align 8
  %21 = trunc i64 %20 to i32
  %22 = select i1 %19, i32 %21, i32 0
  call void @llvm.stackrestore.p0(ptr %12)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #6
  br label %23

23:                                               ; preds = %5, %9
  %.0 = phi i32 [ %22, %9 ], [ 0, %5 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

declare void @hasher_Raw(i32 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #5

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { "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 #6 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass