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

; 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: nounwind uwtable
define dso_local i64 @cashaddr_polymod_step(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i8, align 1
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = lshr i64 %4, 35
  %6 = trunc i64 %5 to i8
  store i8 %6, ptr %3, align 1, !tbaa !9
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = and i64 %7, 34359738367
  %9 = shl i64 %8, 5
  %10 = load i8, ptr %3, align 1, !tbaa !9
  %11 = zext i8 %10 to i32
  %12 = ashr i32 %11, 0
  %13 = and i32 %12, 1
  %14 = sub nsw i32 0, %13
  %15 = sext i32 %14 to i64
  %16 = and i64 %15, 656907472481
  %17 = xor i64 %9, %16
  %18 = load i8, ptr %3, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = ashr i32 %19, 1
  %21 = and i32 %20, 1
  %22 = sub nsw i32 0, %21
  %23 = sext i32 %22 to i64
  %24 = and i64 %23, 522768456162
  %25 = xor i64 %17, %24
  %26 = load i8, ptr %3, align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = ashr i32 %27, 2
  %29 = and i32 %28, 1
  %30 = sub nsw i32 0, %29
  %31 = sext i32 %30 to i64
  %32 = and i64 %31, 1044723512260
  %33 = xor i64 %25, %32
  %34 = load i8, ptr %3, align 1, !tbaa !9
  %35 = zext i8 %34 to i32
  %36 = ashr i32 %35, 3
  %37 = and i32 %36, 1
  %38 = sub nsw i32 0, %37
  %39 = sext i32 %38 to i64
  %40 = and i64 %39, 748107326120
  %41 = xor i64 %33, %40
  %42 = load i8, ptr %3, align 1, !tbaa !9
  %43 = zext i8 %42 to i32
  %44 = ashr i32 %43, 4
  %45 = and i32 %44, 1
  %46 = sub nsw i32 0, %45
  %47 = sext i32 %46 to i64
  %48 = and i64 %47, 130178868336
  %49 = xor i64 %41, %48
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  ret i64 %49
}

; 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

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 }

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


; 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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local range(i64 0, 1099511627776) i64 @cashaddr_polymod_step(i64 noundef %0) local_unnamed_addr #0 {
  %2 = lshr i64 %0, 35
  %3 = trunc nuw nsw i64 %2 to i32
  %4 = shl i64 %0, 5
  %5 = and i64 %4, 1099511627744
  %6 = and i64 %0, 34359738368
  %7 = icmp eq i64 %6, 0
  %8 = select i1 %7, i64 0, i64 656907472481
  %9 = insertelement <4 x i32> poison, i32 %3, i64 0
  %10 = shufflevector <4 x i32> %9, <4 x i32> poison, <4 x i32> zeroinitializer
  %11 = and <4 x i32> %10, <i32 2, i32 4, i32 8, i32 16>
  %12 = icmp eq <4 x i32> %11, zeroinitializer
  %13 = select <4 x i1> %12, <4 x i64> zeroinitializer, <4 x i64> <i64 522768456162, i64 1044723512260, i64 748107326120, i64 130178868336>
  %14 = tail call i64 @llvm.vector.reduce.xor.v4i64(<4 x i64> %13)
  %op.rdx = xor i64 %14, %8
  %op.rdx13 = xor i64 %op.rdx, %5
  ret i64 %op.rdx13
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.xor.v4i64(<4 x i64>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

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


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