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

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

%union.nano_address_encoded = type { [65 x i8] }
%union.nano_address_raw = type { [40 x i8] }
%struct.__blake2b_state = type { [8 x i64], [2 x i64], [2 x i64], [128 x i8], i64, i64, i8 }
%struct.anon.0 = type { [4 x i8], [60 x i8], i8 }
%struct.anon = type { [3 x i8], [32 x i8], [5 x i8] }

@BASE32_ALPHABET_NANO = external dso_local global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

declare i32 @tc_blake2b_Init(ptr noundef, i64 noundef) #2

declare i32 @tc_blake2b_Update(ptr noundef, ptr noundef, i64 noundef) #2

declare i32 @tc_blake2b_Final(ptr noundef, ptr noundef, i64 noundef) #2

; 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) #3

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @nano_validate_address(ptr noundef %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #4 {
  %6 = alloca i1, align 1
  %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 %union.nano_address_encoded, align 1
  %13 = alloca %union.nano_address_raw, align 1
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca [5 x i8], align 1
  %17 = alloca %struct.__blake2b_state, align 8
  %18 = 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
  %19 = load i64, ptr %10, align 8, !tbaa !9
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = add i64 %20, 60
  %22 = icmp ne i64 %19, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %5
  store i1 false, ptr %6, align 1
  br label %92

24:                                               ; preds = %5
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = call i32 @memcmp(ptr noundef %25, ptr noundef %26, i64 noundef %27) #6
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %24
  store i1 false, ptr %6, align 1
  br label %92

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 65, ptr %12) #7
  %32 = getelementptr inbounds %struct.anon.0, ptr %12, i32 0, i32 0
  %33 = getelementptr inbounds [4 x i8], ptr %32, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 @.str.1, i64 4, i1 false)
  %34 = getelementptr inbounds %struct.anon.0, ptr %12, i32 0, i32 1
  %35 = getelementptr inbounds [60 x i8], ptr %34, i64 0, i64 0
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = load i64, ptr %8, align 8, !tbaa !9
  %38 = getelementptr inbounds i8, ptr %36, i64 %37
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %35, ptr align 1 %38, i64 60, i1 false)
  %39 = getelementptr inbounds %struct.anon.0, ptr %12, i32 0, i32 2
  store i8 0, ptr %39, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 40, ptr %13) #7
  call void @llvm.memset.p0.i64(ptr align 1 %13, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #7
  %40 = getelementptr inbounds [65 x i8], ptr %12, i64 0, i64 0
  %41 = getelementptr inbounds [65 x i8], ptr %12, i64 0, i64 0
  %42 = call i64 @strlen(ptr noundef %41) #6
  %43 = getelementptr inbounds [40 x i8], ptr %13, i64 0, i64 0
  %44 = load ptr, ptr @BASE32_ALPHABET_NANO, align 8, !tbaa !5
  %45 = call ptr @base32_decode(ptr noundef %40, i64 noundef %42, ptr noundef %43, i64 noundef 40, ptr noundef %44)
  store ptr %45, ptr %14, align 8, !tbaa !5
  %46 = load ptr, ptr %14, align 8, !tbaa !5
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %31
  store i1 false, ptr %6, align 1
  store i32 1, ptr %15, align 4
  br label %91

49:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 5, ptr %16) #7
  call void @llvm.lifetime.start.p0(i64 248, ptr %17) #7
  %50 = call i32 @tc_blake2b_Init(ptr noundef %17, i64 noundef 5)
  %51 = getelementptr inbounds %struct.anon, ptr %13, i32 0, i32 1
  %52 = getelementptr inbounds [32 x i8], ptr %51, i64 0, i64 0
  %53 = call i32 @tc_blake2b_Update(ptr noundef %17, ptr noundef %52, i64 noundef 32)
  %54 = getelementptr inbounds [5 x i8], ptr %16, i64 0, i64 0
  %55 = call i32 @tc_blake2b_Final(ptr noundef %17, ptr noundef %54, i64 noundef 5)
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #7
  store i32 0, ptr %18, align 4, !tbaa !12
  br label %56

56:                                               ; preds = %77, %49
  %57 = load i32, ptr %18, align 4, !tbaa !12
  %58 = icmp slt i32 %57, 5
  br i1 %58, label %60, label %59

59:                                               ; preds = %56
  store i32 2, ptr %15, align 4
  br label %80

60:                                               ; preds = %56
  %61 = getelementptr inbounds %struct.anon, ptr %13, i32 0, i32 2
  %62 = load i32, ptr %18, align 4, !tbaa !12
  %63 = add nsw i32 %62, 1
  %64 = sub nsw i32 5, %63
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [5 x i8], ptr %61, i64 0, i64 %65
  %67 = load i8, ptr %66, align 1, !tbaa !11
  %68 = zext i8 %67 to i32
  %69 = load i32, ptr %18, align 4, !tbaa !12
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [5 x i8], ptr %16, i64 0, i64 %70
  %72 = load i8, ptr %71, align 1, !tbaa !11
  %73 = zext i8 %72 to i32
  %74 = icmp ne i32 %68, %73
  br i1 %74, label %75, label %76

75:                                               ; preds = %60
  store i1 false, ptr %6, align 1
  store i32 1, ptr %15, align 4
  br label %80

76:                                               ; preds = %60
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %18, align 4, !tbaa !12
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %18, align 4, !tbaa !12
  br label %56, !llvm.loop !14

80:                                               ; preds = %75, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #7
  %81 = load i32, ptr %15, align 4
  switch i32 %81, label %90 [
    i32 2, label %82
  ]

82:                                               ; preds = %80
  %83 = load ptr, ptr %11, align 8, !tbaa !5
  %84 = icmp ne ptr %83, null
  br i1 %84, label %85, label %89

85:                                               ; preds = %82
  %86 = load ptr, ptr %11, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.anon, ptr %13, i32 0, i32 1
  %88 = getelementptr inbounds [32 x i8], ptr %87, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %86, ptr align 1 %88, i64 32, i1 false)
  br label %89

89:                                               ; preds = %85, %82
  store i1 true, ptr %6, align 1
  store i32 1, ptr %15, align 4
  br label %90

90:                                               ; preds = %89, %80
  call void @llvm.lifetime.end.p0(i64 248, ptr %17) #7
  call void @llvm.lifetime.end.p0(i64 5, ptr %16) #7
  br label %91

91:                                               ; preds = %90, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #7
  call void @llvm.lifetime.end.p0(i64 40, ptr %13) #7
  call void @llvm.lifetime.end.p0(i64 65, ptr %12) #7
  br label %92

92:                                               ; preds = %91, %30, %23
  %93 = load i1, ptr %6, align 1
  ret i1 %93
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #5

declare ptr @base32_decode(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { "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 nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind willreturn memory(read) "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 willreturn memory(read) }
attributes #7 = { 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"

%union.nano_address_encoded = type { [65 x i8] }
%union.nano_address_raw = type { [40 x i8] }
%struct.__blake2b_state = type { [8 x i64], [2 x i64], [2 x i64], [128 x i8], i64, i64, i8 }

@BASE32_ALPHABET_NANO = external dso_local local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

declare i32 @tc_blake2b_Init(ptr noundef, i64 noundef) local_unnamed_addr #2

declare i32 @tc_blake2b_Update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare i32 @tc_blake2b_Final(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; 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) #3

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @nano_validate_address(ptr nocapture noundef readonly %0, i64 noundef %1, ptr nocapture noundef readonly %2, i64 noundef %3, ptr noundef writeonly %4) local_unnamed_addr #4 {
  %6 = alloca %union.nano_address_encoded, align 4
  %7 = alloca %union.nano_address_raw, align 1
  %8 = alloca [5 x i8], align 1
  %9 = alloca %struct.__blake2b_state, align 8
  %10 = add i64 %1, 60
  %.not = icmp eq i64 %10, %3
  br i1 %.not, label %11, label %52

11:                                               ; preds = %5
  %bcmp = tail call i32 @bcmp(ptr %2, ptr %0, i64 %1)
  %.not18 = icmp eq i32 %bcmp, 0
  br i1 %.not18, label %12, label %52

12:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 65, ptr nonnull %6) #7
  %13 = load i32, ptr @.str.1, align 1
  store i32 %13, ptr %6, align 4
  %14 = getelementptr inbounds i8, ptr %6, i64 4
  %15 = getelementptr inbounds i8, ptr %2, i64 %1
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(60) %14, ptr noundef nonnull align 1 dereferenceable(60) %15, i64 60, i1 false)
  %16 = getelementptr inbounds i8, ptr %6, i64 64
  store i8 0, ptr %16, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %7) #7
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(40) %7, i8 0, i64 40, i1 false)
  %17 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %6) #8
  %18 = load ptr, ptr @BASE32_ALPHABET_NANO, align 8, !tbaa !8
  %19 = call ptr @base32_decode(ptr noundef nonnull %6, i64 noundef %17, ptr noundef nonnull %7, i64 noundef 40, ptr noundef %18) #7
  %20 = icmp eq ptr %19, null
  br i1 %20, label %51, label %21

21:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 5, ptr nonnull %8) #7
  call void @llvm.lifetime.start.p0(i64 248, ptr nonnull %9) #7
  %22 = call i32 @tc_blake2b_Init(ptr noundef nonnull %9, i64 noundef 5) #7
  %23 = getelementptr inbounds i8, ptr %7, i64 3
  %24 = call i32 @tc_blake2b_Update(ptr noundef nonnull %9, ptr noundef nonnull %23, i64 noundef 32) #7
  %25 = call i32 @tc_blake2b_Final(ptr noundef nonnull %9, ptr noundef nonnull %8, i64 noundef 5) #7
  %26 = getelementptr inbounds i8, ptr %7, i64 35
  %27 = getelementptr inbounds i8, ptr %7, i64 39
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = load i8, ptr %8, align 1, !tbaa !5
  %.not19 = icmp eq i8 %28, %29
  br i1 %.not19, label %30, label %.loopexit

30:                                               ; preds = %21
  %31 = getelementptr inbounds i8, ptr %7, i64 38
  %32 = load i8, ptr %31, align 1, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %8, i64 1
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %.not19.1 = icmp eq i8 %32, %34
  br i1 %.not19.1, label %35, label %.loopexit

35:                                               ; preds = %30
  %36 = getelementptr inbounds i8, ptr %7, i64 37
  %37 = load i8, ptr %36, align 1, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %8, i64 2
  %39 = load i8, ptr %38, align 1, !tbaa !5
  %.not19.2 = icmp eq i8 %37, %39
  br i1 %.not19.2, label %40, label %.loopexit

40:                                               ; preds = %35
  %41 = getelementptr inbounds i8, ptr %7, i64 36
  %42 = load i8, ptr %41, align 1, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %8, i64 3
  %44 = load i8, ptr %43, align 1, !tbaa !5
  %.not19.3 = icmp eq i8 %42, %44
  br i1 %.not19.3, label %45, label %.loopexit

45:                                               ; preds = %40
  %46 = load i8, ptr %26, align 1, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %8, i64 4
  %48 = load i8, ptr %47, align 1, !tbaa !5
  %.not19.4 = icmp eq i8 %46, %48
  br i1 %.not19.4, label %.critedge, label %.loopexit

.critedge:                                        ; preds = %45
  %.not20.old = icmp eq ptr %4, null
  br i1 %.not20.old, label %.loopexit, label %49

49:                                               ; preds = %.critedge
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32) %4, ptr noundef nonnull align 1 dereferenceable(32) %23, i64 32, i1 false)
  br label %.loopexit

.loopexit:                                        ; preds = %21, %30, %35, %40, %45, %.critedge, %49
  %50 = phi i1 [ true, %.critedge ], [ true, %49 ], [ false, %45 ], [ false, %40 ], [ false, %35 ], [ false, %30 ], [ false, %21 ]
  call void @llvm.lifetime.end.p0(i64 248, ptr nonnull %9) #7
  call void @llvm.lifetime.end.p0(i64 5, ptr nonnull %8) #7
  br label %51

51:                                               ; preds = %12, %.loopexit
  %.2 = phi i1 [ %50, %.loopexit ], [ false, %12 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %7) #7
  call void @llvm.lifetime.end.p0(i64 65, ptr nonnull %6) #7
  br label %52

52:                                               ; preds = %11, %5, %51
  %.3 = phi i1 [ %.2, %51 ], [ false, %5 ], [ false, %11 ]
  ret i1 %.3
}

declare ptr @base32_decode(ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #5

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #6

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { "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 nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { nofree nounwind willreturn memory(argmem: read) }
attributes #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }

!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 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}


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