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

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

%struct.anon = type { i16, i16 }

@slip39_wordlist = external hidden constant [1024 x ptr], align 16
@words_button_seq = external hidden constant [1024 x %struct.anon], align 16

; 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: nounwind uwtable
define dso_local ptr @button_sequence_to_word(i16 noundef zeroext %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  %7 = load i16, ptr %3, align 2, !tbaa !5
  %8 = zext i16 %7 to i32
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %12

10:                                               ; preds = %1
  %11 = load ptr, ptr @slip39_wordlist, align 16, !tbaa !9
  store ptr %11, ptr %2, align 8
  br label %56

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  store i16 1, ptr %4, align 2, !tbaa !5
  br label %13

13:                                               ; preds = %17, %12
  %14 = load i16, ptr %3, align 2, !tbaa !5
  %15 = zext i16 %14 to i32
  %16 = icmp slt i32 %15, 1000
  br i1 %16, label %17, label %26

17:                                               ; preds = %13
  %18 = load i16, ptr %3, align 2, !tbaa !5
  %19 = zext i16 %18 to i32
  %20 = mul nsw i32 %19, 10
  %21 = trunc i32 %20 to i16
  store i16 %21, ptr %3, align 2, !tbaa !5
  %22 = load i16, ptr %4, align 2, !tbaa !5
  %23 = zext i16 %22 to i32
  %24 = mul nsw i32 %23, 10
  %25 = trunc i32 %24 to i16
  store i16 %25, ptr %4, align 2, !tbaa !5
  br label %13, !llvm.loop !11

26:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %27 = load i16, ptr %3, align 2, !tbaa !5
  %28 = call zeroext i16 @find_sequence(i16 noundef zeroext %27)
  store i16 %28, ptr %5, align 2, !tbaa !5
  %29 = load i16, ptr %5, align 2, !tbaa !5
  %30 = zext i16 %29 to i32
  %31 = icmp sge i32 %30, 1024
  br i1 %31, label %45, label %32

32:                                               ; preds = %26
  %33 = load i16, ptr %5, align 2, !tbaa !5
  %34 = zext i16 %33 to i64
  %35 = getelementptr inbounds [1024 x %struct.anon], ptr @words_button_seq, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.anon, ptr %35, i32 0, i32 0
  %37 = load i16, ptr %36, align 4, !tbaa !13
  %38 = zext i16 %37 to i32
  %39 = load i16, ptr %3, align 2, !tbaa !5
  %40 = zext i16 %39 to i32
  %41 = sub nsw i32 %38, %40
  %42 = load i16, ptr %4, align 2, !tbaa !5
  %43 = zext i16 %42 to i32
  %44 = icmp sge i32 %41, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %32, %26
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %55

46:                                               ; preds = %32
  %47 = load i16, ptr %5, align 2, !tbaa !5
  %48 = zext i16 %47 to i64
  %49 = getelementptr inbounds [1024 x %struct.anon], ptr @words_button_seq, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.anon, ptr %49, i32 0, i32 1
  %51 = load i16, ptr %50, align 2, !tbaa !15
  %52 = zext i16 %51 to i64
  %53 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  store ptr %54, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %55

55:                                               ; preds = %46, %45
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %56

56:                                               ; preds = %55, %10
  %57 = load ptr, ptr %2, align 8
  ret ptr %57
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @find_sequence(i16 noundef zeroext) #1

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 = { 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 = !{!"short", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 2}
!15 = !{!14, !6, i64 2}


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

%struct.anon = type { i16, i16 }

@slip39_wordlist = external hidden local_unnamed_addr constant [1024 x ptr], align 16
@words_button_seq = external hidden local_unnamed_addr constant [1024 x %struct.anon], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @button_sequence_to_word(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = icmp eq i16 %0, 0
  br i1 %2, label %4, label %.preheader

.preheader:                                       ; preds = %1
  %3 = icmp ult i16 %0, 1000
  br i1 %3, label %.lr.ph, label %._crit_edge

4:                                                ; preds = %1
  %5 = load ptr, ptr @slip39_wordlist, align 16, !tbaa !5
  br label %25

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01016 = phi i16 [ %7, %.lr.ph ], [ 1, %.preheader ]
  %.01115 = phi i16 [ %6, %.lr.ph ], [ %0, %.preheader ]
  %6 = mul nuw nsw i16 %.01115, 10
  %7 = mul i16 %.01016, 10
  %8 = icmp ult i16 %.01115, 100
  br i1 %8, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %9 = zext i16 %7 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.011.lcssa14 = phi i16 [ %0, %.preheader ], [ %6, %._crit_edge.loopexit ]
  %.010.lcssa = phi i32 [ 1, %.preheader ], [ %9, %._crit_edge.loopexit ]
  %10 = tail call zeroext i16 @find_sequence(i16 noundef zeroext %.011.lcssa14)
  %11 = icmp ugt i16 %10, 1023
  br i1 %11, label %25, label %12

12:                                               ; preds = %._crit_edge
  %13 = zext i16 %.011.lcssa14 to i32
  %14 = zext nneg i16 %10 to i64
  %15 = getelementptr inbounds [1024 x %struct.anon], ptr @words_button_seq, i64 0, i64 %14
  %16 = load i16, ptr %15, align 4, !tbaa !11
  %17 = zext i16 %16 to i32
  %18 = sub nsw i32 %17, %13
  %.not = icmp slt i32 %18, %.010.lcssa
  br i1 %.not, label %19, label %25

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %15, i64 2
  %21 = load i16, ptr %20, align 2, !tbaa !14
  %22 = zext i16 %21 to i64
  %23 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %19, %12, %._crit_edge, %4
  %.1 = phi ptr [ %5, %4 ], [ %24, %19 ], [ null, %12 ], [ null, %._crit_edge ]
  ret ptr %.1
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @find_sequence(i16 noundef zeroext) local_unnamed_addr #0

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 2}


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