; LLVM IR from tangjz/acm-icpc :: 51nod/1295.ll (Z5queryiiii)

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

@seg = external dso_local global [1500030 x %struct.Trie], align 16

; 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 uwtable
define dso_local noundef i32 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr %8, align 4, !tbaa !5
  %13 = icmp eq i32 %12, -1
  br i1 %13, label %14, label %15

14:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  br label %95

15:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = ashr i32 %16, %17
  %19 = and i32 %18, 1
  store i32 %19, ptr %10, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.Trie, ptr %22, i32 0, i32 1
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = xor i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [2 x i32], ptr %23, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.Trie, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.Trie, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %10, align 4, !tbaa !5
  %38 = xor i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [2 x i32], ptr %36, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.Trie, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = icmp slt i32 %32, %45
  br i1 %46, label %47, label %73

47:                                               ; preds = %15
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.Trie, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = xor i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [2 x i32], ptr %51, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %58
  %60 = getelementptr inbounds %struct.Trie, ptr %59, i32 0, i32 1
  %61 = load i32, ptr %10, align 4, !tbaa !5
  %62 = xor i32 %61, 1
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [2 x i32], ptr %60, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load i32, ptr %8, align 4, !tbaa !5
  %67 = sub nsw i32 %66, 1
  %68 = load i32, ptr %9, align 4, !tbaa !5
  %69 = call noundef i32 @_Z5queryiiii(i32 noundef %56, i32 noundef %65, i32 noundef %67, i32 noundef %68)
  %70 = load i32, ptr %8, align 4, !tbaa !5
  %71 = shl i32 1, %70
  %72 = or i32 %69, %71
  store i32 %72, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %94

73:                                               ; preds = %15
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.Trie, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %10, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [2 x i32], ptr %77, i64 0, i64 %79
  %81 = load i32, ptr %80, align 4, !tbaa !5
  %82 = load i32, ptr %7, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %83
  %85 = getelementptr inbounds %struct.Trie, ptr %84, i32 0, i32 1
  %86 = load i32, ptr %10, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [2 x i32], ptr %85, i64 0, i64 %87
  %89 = load i32, ptr %88, align 4, !tbaa !5
  %90 = load i32, ptr %8, align 4, !tbaa !5
  %91 = sub nsw i32 %90, 1
  %92 = load i32, ptr %9, align 4, !tbaa !5
  %93 = call noundef i32 @_Z5queryiiii(i32 noundef %81, i32 noundef %89, i32 noundef %91, i32 noundef %92)
  store i32 %93, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %94

94:                                               ; preds = %73, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %95

95:                                               ; preds = %94, %14
  %96 = load i32, ptr %5, align 4
  ret i32 %96
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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" }
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 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4Trie", !6, i64 0, !7, 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.Trie = type { i32, [2 x i32] }

@seg = external dso_local local_unnamed_addr global [1500030 x %struct.Trie], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %2, -1
  br i1 %5, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr23.ph34 = phi i32 [ %24, %tailrecurse.outer ], [ %2, %4 ]
  %.tr22.ph33 = phi i32 [ %19, %tailrecurse.outer ], [ %1, %4 ]
  %.tr.ph32 = phi i32 [ %13, %tailrecurse.outer ], [ %0, %4 ]
  %accumulator.tr.ph31 = phi i32 [ %26, %tailrecurse.outer ], [ 0, %4 ]
  br label %6

6:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr2329 = phi i32 [ %.tr23.ph34, %.lr.ph ], [ %33, %tailrecurse ]
  %.tr2228 = phi i32 [ %.tr22.ph33, %.lr.ph ], [ %32, %tailrecurse ]
  %.tr27 = phi i32 [ %.tr.ph32, %.lr.ph ], [ %30, %tailrecurse ]
  %7 = lshr i32 %3, %.tr2329
  %8 = and i32 %7, 1
  %9 = sext i32 %.tr27 to i64
  %10 = xor i32 %8, 1
  %11 = zext nneg i32 %10 to i64
  %12 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %9, i32 1, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !9
  %17 = sext i32 %.tr2228 to i64
  %18 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %17, i32 1, i64 %11
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = icmp slt i32 %16, %22
  br i1 %23, label %tailrecurse.outer, label %tailrecurse

tailrecurse.outer:                                ; preds = %6
  %24 = add nsw i32 %.tr2329, -1
  %25 = shl nuw i32 1, %.tr2329
  %26 = or i32 %25, %accumulator.tr.ph31
  %27 = icmp eq i32 %.tr2329, 0
  br i1 %27, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse:                                      ; preds = %6
  %28 = zext nneg i32 %8 to i64
  %29 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %9, i32 1, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = getelementptr inbounds [1500030 x %struct.Trie], ptr @seg, i64 0, i64 %17, i32 1, i64 %28
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %.tr2329, -1
  %34 = icmp eq i32 %.tr2329, 0
  br i1 %34, label %tailrecurse.outer._crit_edge, label %6

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  %accumulator.tr.ph.lcssa = phi i32 [ 0, %4 ], [ %accumulator.tr.ph31, %tailrecurse ], [ %26, %tailrecurse.outer ]
  ret i32 %accumulator.tr.ph.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4Trie", !6, i64 0, !7, i64 4}


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