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

@slip39_wordlist = external hidden constant [1024 x ptr], align 16

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @word_index(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i8 %2, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  store i16 0, ptr %8, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #3
  store i16 1024, ptr %9, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  store i16 0, ptr %10, align 2, !tbaa !10
  br label %12

12:                                               ; preds = %40, %3
  %13 = load i16, ptr %9, align 2, !tbaa !10
  %14 = zext i16 %13 to i32
  %15 = load i16, ptr %8, align 2, !tbaa !10
  %16 = zext i16 %15 to i32
  %17 = sub nsw i32 %14, %16
  %18 = icmp sgt i32 %17, 1
  br i1 %18, label %19, label %41

19:                                               ; preds = %12
  %20 = load i16, ptr %9, align 2, !tbaa !10
  %21 = zext i16 %20 to i32
  %22 = load i16, ptr %8, align 2, !tbaa !10
  %23 = zext i16 %22 to i32
  %24 = add nsw i32 %21, %23
  %25 = sdiv i32 %24, 2
  %26 = trunc i32 %25 to i16
  store i16 %26, ptr %10, align 2, !tbaa !10
  %27 = load i16, ptr %10, align 2, !tbaa !10
  %28 = zext i16 %27 to i64
  %29 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i8, ptr %7, align 1, !tbaa !9
  %33 = zext i8 %32 to i64
  %34 = call i32 @strncmp(ptr noundef %30, ptr noundef %31, i64 noundef %33) #4
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %38

36:                                               ; preds = %19
  %37 = load i16, ptr %10, align 2, !tbaa !10
  store i16 %37, ptr %9, align 2, !tbaa !10
  br label %40

38:                                               ; preds = %19
  %39 = load i16, ptr %10, align 2, !tbaa !10
  store i16 %39, ptr %8, align 2, !tbaa !10
  br label %40

40:                                               ; preds = %38, %36
  br label %12, !llvm.loop !12

41:                                               ; preds = %12
  %42 = load i16, ptr %8, align 2, !tbaa !10
  %43 = zext i16 %42 to i64
  %44 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %43
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i8, ptr %7, align 1, !tbaa !9
  %48 = zext i8 %47 to i64
  %49 = call i32 @strncmp(ptr noundef %45, ptr noundef %46, i64 noundef %48) #4
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %52

51:                                               ; preds = %41
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %55

52:                                               ; preds = %41
  %53 = load i16, ptr %8, align 2, !tbaa !10
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  store i16 %53, ptr %54, align 2, !tbaa !10
  store i1 true, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %55

55:                                               ; preds = %52, %51
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  %56 = load i1, ptr %4, align 1
  ret i1 %56
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strncmp(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) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind }
attributes #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !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"

@slip39_wordlist = external hidden local_unnamed_addr constant [1024 x ptr], align 16

; Function Attrs: nofree nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @word_index(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = zext i8 %2 to i64
  br label %5

5:                                                ; preds = %3, %5
  %6 = phi i32 [ 0, %3 ], [ %17, %5 ]
  %7 = phi i32 [ 1024, %3 ], [ %16, %5 ]
  %.018 = phi i16 [ 1024, %3 ], [ %..0, %5 ]
  %.01417 = phi i16 [ 0, %3 ], [ %.014., %5 ]
  %8 = add nuw nsw i32 %7, %6
  %9 = lshr i32 %8, 1
  %10 = trunc nuw i32 %9 to i16
  %11 = zext nneg i32 %9 to i64
  %12 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = tail call i32 @strncmp(ptr noundef %13, ptr noundef %1, i64 noundef %4) #2
  %15 = icmp sgt i32 %14, 0
  %.014. = select i1 %15, i16 %.01417, i16 %10
  %..0 = select i1 %15, i16 %10, i16 %.018
  %16 = zext i16 %..0 to i32
  %17 = zext i16 %.014. to i32
  %18 = sub nsw i32 %16, %17
  %19 = icmp sgt i32 %18, 1
  br i1 %19, label %5, label %20, !llvm.loop !9

20:                                               ; preds = %5
  %21 = zext i16 %.014. to i64
  %22 = getelementptr inbounds [1024 x ptr], ptr @slip39_wordlist, i64 0, i64 %21
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %24 = tail call i32 @strncmp(ptr noundef %23, ptr noundef %1, i64 noundef %4) #2
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %25, label %26

25:                                               ; preds = %20
  store i16 %.014., ptr %0, align 2, !tbaa !11
  br label %26

26:                                               ; preds = %20, %25
  ret i1 %.not
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strncmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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 = { 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 #2 = { 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 = !{!"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, !12, i64 0}
!12 = !{!"short", !7, i64 0}


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