; LLVM IR from Tunghohin/Competitive_Coding :: Poj/poj2528.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"

%struct.Node = type { i32, i32, i32 }

@tr = external dso_local global [1000050 x %struct.Node], align 16
@vis = external dso_local global [200010 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5queryi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %5
  %7 = getelementptr inbounds %struct.Node, ptr %6, i32 0, i32 2
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %10, label %29

10:                                               ; preds = %1
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %12
  %14 = getelementptr inbounds %struct.Node, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [200010 x i8], ptr @vis, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !11, !range !13, !noundef !14
  %19 = trunc i8 %18 to i1
  br i1 %19, label %28, label %20

20:                                               ; preds = %10
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.Node, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [200010 x i8], ptr @vis, i64 0, i64 %26
  store i8 1, ptr %27, align 1, !tbaa !11
  store i32 1, ptr %2, align 4
  br label %51

28:                                               ; preds = %10
  store i32 0, ptr %2, align 4
  br label %51

29:                                               ; preds = %1
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.Node, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 4, !tbaa !15
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = icmp eq i32 %34, %39
  br i1 %40, label %41, label %42

41:                                               ; preds = %29
  store i32 0, ptr %2, align 4
  br label %51

42:                                               ; preds = %29
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = shl i32 %43, 1
  %45 = call noundef i32 @_Z5queryi(i32 noundef %44)
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = shl i32 %46, 1
  %48 = or i32 %47, 1
  %49 = call noundef i32 @_Z5queryi(i32 noundef %48)
  %50 = add nsw i32 %45, %49
  store i32 %50, ptr %2, align 4
  br label %51

51:                                               ; preds = %42, %41, %28, %20
  %52 = load i32, ptr %2, align 4
  ret i32 %52
}

attributes #0 = { mustprogress 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 = !{!10, !6, i64 8}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!10, !6, i64 0}
!16 = !{!10, !6, i64 4}


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

%struct.Node = type { i32, i32, i32 }

@tr = external dso_local local_unnamed_addr global [1000050 x %struct.Node], align 16
@vis = external dso_local local_unnamed_addr global [200010 x i8], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %2
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not12 = icmp eq i32 %5, 0
  br i1 %.not12, label %.lr.ph, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %1
  %accumulator.tr.lcssa = phi i32 [ 0, %1 ], [ %19, %tailrecurse ]
  %.lcssa = phi i32 [ %5, %1 ], [ %23, %tailrecurse ]
  %6 = sext i32 %.lcssa to i64
  %7 = getelementptr inbounds [200010 x i8], ptr @vis, i64 0, i64 %6
  %8 = load i8, ptr %7, align 1, !tbaa !10, !range !12, !noundef !13
  %9 = trunc nuw i8 %8 to i1
  br i1 %9, label %.loopexit, label %10

10:                                               ; preds = %tailrecurse._crit_edge
  store i8 1, ptr %7, align 1, !tbaa !10
  br label %.loopexit

.lr.ph:                                           ; preds = %1, %tailrecurse
  %11 = phi ptr [ %21, %tailrecurse ], [ %3, %1 ]
  %.tr14 = phi i32 [ %18, %tailrecurse ], [ %0, %1 ]
  %accumulator.tr13 = phi i32 [ %19, %tailrecurse ], [ 0, %1 ]
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %11, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !15
  %15 = icmp eq i32 %12, %14
  br i1 %15, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph
  %16 = shl i32 %.tr14, 1
  %17 = tail call noundef i32 @_Z5queryi(i32 noundef %16)
  %18 = or disjoint i32 %16, 1
  %19 = add nsw i32 %17, %accumulator.tr13
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [1000050 x %struct.Node], ptr @tr, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %.lr.ph, label %tailrecurse._crit_edge

.loopexit:                                        ; preds = %.lr.ph, %tailrecurse._crit_edge, %10
  %accumulator.tr11 = phi i32 [ %accumulator.tr.lcssa, %10 ], [ %accumulator.tr.lcssa, %tailrecurse._crit_edge ], [ %accumulator.tr13, %.lr.ph ]
  %.0 = phi i32 [ 1, %10 ], [ 0, %tailrecurse._crit_edge ], [ 0, %.lr.ph ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr11
  ret i32 %accumulator.ret.tr
}

attributes #0 = { mustprogress nofree 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, !7, i64 8}
!6 = !{!"_ZTS4Node", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"bool", !8, i64 0}
!12 = !{i8 0, i8 2}
!13 = !{}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 4}


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