; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 21. Trie/Maximum_Subarray_XOR.ll (Z5queryP8TrieNodei)

; 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.TrieNode = type { i32, [2 x ptr] }

; 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 @_Z5queryP8TrieNodei(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 31, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %59, %2
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = icmp sge i32 %10, 0
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = shl i32 1, %15
  %17 = and i32 %14, %16
  %18 = icmp ne i32 %17, 0
  %19 = zext i1 %18 to i8
  store i8 %19, ptr %7, align 1, !tbaa !11
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.TrieNode, ptr %20, i32 0, i32 1
  %22 = load i8, ptr %7, align 1, !tbaa !11, !range !13, !noundef !14
  %23 = trunc i8 %22 to i1
  %24 = zext i1 %23 to i32
  %25 = sub nsw i32 1, %24
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [2 x ptr], ptr %21, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %40

30:                                               ; preds = %13
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TrieNode, ptr %31, i32 0, i32 1
  %33 = load i8, ptr %7, align 1, !tbaa !11, !range !13, !noundef !14
  %34 = trunc i8 %33 to i1
  %35 = zext i1 %34 to i32
  %36 = sub nsw i32 1, %35
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [2 x ptr], ptr %32, i64 0, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  store ptr %39, ptr %5, align 8, !tbaa !5
  br label %58

40:                                               ; preds = %13
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.TrieNode, ptr %41, i32 0, i32 1
  %43 = load i8, ptr %7, align 1, !tbaa !11, !range !13, !noundef !14
  %44 = trunc i8 %43 to i1
  %45 = zext i1 %44 to i64
  %46 = getelementptr inbounds [2 x ptr], ptr %42, i64 0, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %57

49:                                               ; preds = %40
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TrieNode, ptr %50, i32 0, i32 1
  %52 = load i8, ptr %7, align 1, !tbaa !11, !range !13, !noundef !14
  %53 = trunc i8 %52 to i1
  %54 = zext i1 %53 to i64
  %55 = getelementptr inbounds [2 x ptr], ptr %51, i64 0, i64 %54
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  store ptr %56, ptr %5, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %49, %40
  br label %58

58:                                               ; preds = %57, %30
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = add nsw i32 %60, -1
  store i32 %61, ptr %6, align 4, !tbaa !9
  br label %9, !llvm.loop !15

62:                                               ; preds = %12
  %63 = load i32, ptr %4, align 4, !tbaa !9
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TrieNode, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 8, !tbaa !17
  %67 = xor i32 %63, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i32 %67
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !10, i64 0}
!18 = !{!"_ZTS8TrieNode", !10, i64 0, !7, i64 8}


; 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(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryP8TrieNodei(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %6

3:                                                ; preds = %18
  %4 = load i32, ptr %.1, align 8, !tbaa !5
  %5 = xor i32 %4, %1
  ret i32 %5

6:                                                ; preds = %2, %18
  %.019 = phi ptr [ %0, %2 ], [ %.1, %18 ]
  %.01418 = phi i32 [ 31, %2 ], [ %19, %18 ]
  %7 = shl nuw i32 1, %.01418
  %8 = and i32 %7, %1
  %9 = icmp ne i32 %8, 0
  %10 = getelementptr inbounds i8, ptr %.019, i64 8
  %not. = xor i1 %9, true
  %11 = zext i1 %not. to i64
  %12 = getelementptr inbounds [2 x ptr], ptr %10, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !10
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %14, label %18

14:                                               ; preds = %6
  %15 = zext i1 %9 to i64
  %16 = getelementptr inbounds [2 x ptr], ptr %10, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !10
  %.not17 = icmp eq ptr %17, null
  %spec.select = select i1 %.not17, ptr %.019, ptr %17
  br label %18

18:                                               ; preds = %14, %6
  %.1 = phi ptr [ %13, %6 ], [ %spec.select, %14 ]
  %19 = add nsw i32 %.01418, -1
  %.not20 = icmp eq i32 %.01418, 0
  br i1 %.not20, label %3, label %6, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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, !7, i64 0}
!6 = !{!"_ZTS8TrieNode", !7, i64 0, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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