; LLVM IR from square/subzero :: core/trezor-crypto (segwit_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 i32 @bech32_polymod_step(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i8, align 1
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = lshr i32 %4, 25
  %6 = trunc i32 %5 to i8
  store i8 %6, ptr %3, align 1, !tbaa !9
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = and i32 %7, 33554431
  %9 = shl i32 %8, 5
  %10 = zext i32 %9 to i64
  %11 = load i8, ptr %3, align 1, !tbaa !9
  %12 = zext i8 %11 to i32
  %13 = ashr i32 %12, 0
  %14 = and i32 %13, 1
  %15 = sub nsw i32 0, %14
  %16 = sext i32 %15 to i64
  %17 = and i64 %16, 996825010
  %18 = xor i64 %10, %17
  %19 = load i8, ptr %3, align 1, !tbaa !9
  %20 = zext i8 %19 to i32
  %21 = ashr i32 %20, 1
  %22 = and i32 %21, 1
  %23 = sub nsw i32 0, %22
  %24 = sext i32 %23 to i64
  %25 = and i64 %24, 642813549
  %26 = xor i64 %18, %25
  %27 = load i8, ptr %3, align 1, !tbaa !9
  %28 = zext i8 %27 to i32
  %29 = ashr i32 %28, 2
  %30 = and i32 %29, 1
  %31 = sub nsw i32 0, %30
  %32 = sext i32 %31 to i64
  %33 = and i64 %32, 513874426
  %34 = xor i64 %26, %33
  %35 = load i8, ptr %3, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = ashr i32 %36, 3
  %38 = and i32 %37, 1
  %39 = sub nsw i32 0, %38
  %40 = sext i32 %39 to i64
  %41 = and i64 %40, 1027748829
  %42 = xor i64 %34, %41
  %43 = load i8, ptr %3, align 1, !tbaa !9
  %44 = zext i8 %43 to i32
  %45 = ashr i32 %44, 4
  %46 = and i32 %45, 1
  %47 = sub nsw i32 0, %46
  %48 = sext i32 %47 to i64
  %49 = and i64 %48, 705979059
  %50 = xor i64 %42, %49
  %51 = trunc i64 %50 to i32
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  ret i32 %51
}

; 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 = !{!"int", !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(i32 0, 1073741824) i32 @bech32_polymod_step(i32 noundef %0) local_unnamed_addr #0 {
  %2 = shl i32 %0, 5
  %3 = and i32 %2, 1073741792
  %4 = insertelement <4 x i32> poison, i32 %0, i64 0
  %5 = shufflevector <4 x i32> %4, <4 x i32> poison, <4 x i32> zeroinitializer
  %6 = and <4 x i32> %5, <i32 33554432, i32 67108864, i32 134217728, i32 268435456>
  %7 = icmp eq <4 x i32> %6, zeroinitializer
  %8 = select <4 x i1> %7, <4 x i32> zeroinitializer, <4 x i32> <i32 996825010, i32 642813549, i32 513874426, i32 1027748829>
  %.mask11 = and i32 %0, 536870912
  %isneg.not12 = icmp eq i32 %.mask11, 0
  %9 = select i1 %isneg.not12, i32 0, i32 705979059
  %10 = tail call i32 @llvm.vector.reduce.xor.v4i32(<4 x i32> %8)
  %op.rdx = xor i32 %10, %9
  %op.rdx13 = xor i32 %op.rdx, %3
  ret i32 %op.rdx13
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.xor.v4i32(<4 x i32>) #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,SLPVectorizerPass,SROAPass,TailCallElimPass