; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x16-Trie/144. 最长异或值路径.ll (Z5queryi)

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

@trie = external dso_local global [3200320 x [2 x i32]], align 16
@p = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@tsize = external dso_local global i32, align 4

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z5queryi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  store i32 1, ptr @q, align 4, !tbaa !5
  store i32 1, ptr @p, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 31, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %73, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp sge i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %76

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = ashr i32 %11, %12
  %14 = and i32 %13, 1
  store i32 %14, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr @p, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %16
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [2 x i32], ptr %17, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %32

23:                                               ; preds = %10
  %24 = load i32, ptr @tsize, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr @tsize, align 4, !tbaa !5
  %26 = load i32, ptr @p, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %27
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2 x i32], ptr %28, i64 0, i64 %30
  store i32 %25, ptr %31, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %23, %10
  %33 = load i32, ptr @p, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %34
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [2 x i32], ptr %35, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  store i32 %39, ptr @p, align 4, !tbaa !5
  %40 = load i32, ptr @q, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %41
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = icmp ne i32 %43, 0
  %45 = xor i1 %44, true
  %46 = zext i1 %45 to i64
  %47 = getelementptr inbounds [2 x i32], ptr %42, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %64

50:                                               ; preds = %32
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = shl i32 1, %51
  %53 = load i32, ptr %3, align 4, !tbaa !5
  %54 = or i32 %53, %52
  store i32 %54, ptr %3, align 4, !tbaa !5
  %55 = load i32, ptr @q, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %56
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = icmp ne i32 %58, 0
  %60 = xor i1 %59, true
  %61 = zext i1 %60 to i64
  %62 = getelementptr inbounds [2 x i32], ptr %57, i64 0, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !5
  store i32 %63, ptr @q, align 4, !tbaa !5
  br label %72

64:                                               ; preds = %32
  %65 = load i32, ptr @q, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %66
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [2 x i32], ptr %67, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !5
  store i32 %71, ptr @q, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %64, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = add nsw i32 %74, -1
  store i32 %75, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !9

76:                                               ; preds = %9
  %77 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %77
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@trie = external dso_local local_unnamed_addr global [3200320 x [2 x i32]], align 16
@p = external dso_local local_unnamed_addr global i32, align 4
@q = external dso_local local_unnamed_addr global i32, align 4
@tsize = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryi(i32 noundef %0) local_unnamed_addr #0 {
  %tsize.promoted = load i32, ptr @tsize, align 4, !tbaa !5
  br label %3

2:                                                ; preds = %29
  store i32 %16, ptr @p, align 4, !tbaa !5
  store i32 %storemerge, ptr @q, align 4, !tbaa !5
  ret i32 %.1

3:                                                ; preds = %1, %29
  %.018 = phi i32 [ 0, %1 ], [ %.1, %29 ]
  %.01317 = phi i32 [ 31, %1 ], [ %30, %29 ]
  %4 = phi i32 [ 1, %1 ], [ %16, %29 ]
  %5 = phi i32 [ %tsize.promoted, %1 ], [ %17, %29 ]
  %storemerge1516 = phi i32 [ 1, %1 ], [ %storemerge, %29 ]
  %6 = lshr i32 %0, %.01317
  %7 = and i32 %6, 1
  %8 = sext i32 %4 to i64
  %9 = zext nneg i32 %7 to i64
  %10 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %8, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %3
  %14 = add nsw i32 %5, 1
  store i32 %14, ptr @tsize, align 4, !tbaa !5
  store i32 %14, ptr %10, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %13, %3
  %16 = phi i32 [ %14, %13 ], [ %11, %3 ]
  %17 = phi i32 [ %14, %13 ], [ %5, %3 ]
  %18 = sext i32 %storemerge1516 to i64
  %19 = xor i32 %7, 1
  %20 = zext nneg i32 %19 to i64
  %21 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %18, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %.not = icmp eq i32 %22, 0
  br i1 %.not, label %26, label %23

23:                                               ; preds = %15
  %24 = shl nuw i32 1, %.01317
  %25 = or i32 %.018, %24
  br label %29

26:                                               ; preds = %15
  %27 = getelementptr inbounds [3200320 x [2 x i32]], ptr @trie, i64 0, i64 %18, i64 %9
  %28 = load i32, ptr %27, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %26, %23
  %storemerge = phi i32 [ %28, %26 ], [ %22, %23 ]
  %.1 = phi i32 [ %.018, %26 ], [ %25, %23 ]
  %30 = add nsw i32 %.01317, -1
  %.not19 = icmp eq i32 %.01317, 0
  br i1 %.not19, label %2, label %3, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, 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" }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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