; LLVM IR from VastoLorde95/Competitive-Programming :: SPOJ/Random/MKTHNUM/mk2.ll (Z5queryP4nodeS0_iii)

; 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, ptr, 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 @_Z5queryP4nodeS0_iii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %58, %5
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = add nsw i32 %14, 1
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = icmp eq i32 %15, %16
  br i1 %17, label %18, label %20

18:                                               ; preds = %13
  %19 = load i32, ptr %8, align 4, !tbaa !9
  ret i32 %19

20:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.node, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = getelementptr inbounds %struct.node, ptr %23, i32 0, i32 0
  %25 = load i32, ptr %24, align 8, !tbaa !13
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.node, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = getelementptr inbounds %struct.node, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 8, !tbaa !13
  %31 = sub nsw i32 %25, %30
  store i32 %31, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = add nsw i32 %32, %33
  %35 = ashr i32 %34, 1
  store i32 %35, ptr %12, align 4, !tbaa !9
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = icmp sge i32 %36, %37
  br i1 %38, label %39, label %47

39:                                               ; preds = %20
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.node, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  store ptr %42, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  store ptr %45, ptr %7, align 8, !tbaa !5
  %46 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %46, ptr %9, align 4, !tbaa !9
  br label %58

47:                                               ; preds = %20
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.node, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !14
  store ptr %50, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.node, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !14
  store ptr %53, ptr %7, align 8, !tbaa !5
  %54 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %54, ptr %8, align 4, !tbaa !9
  %55 = load i32, ptr %11, align 4, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = sub nsw i32 %56, %55
  store i32 %57, ptr %10, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %47, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %13, !llvm.loop !15
}

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, !6, i64 8}
!12 = !{!"_ZTS4node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !10, i64 0}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z5queryP4nodeS0_iii(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = add nsw i32 %2, 1
  %7 = icmp eq i32 %6, %3
  br i1 %7, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %23, %5
  %.022.lcssa = phi i32 [ %2, %5 ], [ %.123, %23 ]
  ret i32 %.022.lcssa

.lr.ph:                                           ; preds = %5, %23
  %.029 = phi ptr [ %.1, %23 ], [ %0, %5 ]
  %.01628 = phi ptr [ %.117, %23 ], [ %1, %5 ]
  %.01827 = phi i32 [ %.119, %23 ], [ %4, %5 ]
  %.02026 = phi i32 [ %.121, %23 ], [ %3, %5 ]
  %.02225 = phi i32 [ %.123, %23 ], [ %2, %5 ]
  %8 = getelementptr inbounds i8, ptr %.029, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %.01628, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = sub nsw i32 %10, %13
  %15 = add nsw i32 %.02026, %.02225
  %16 = ashr i32 %15, 1
  %.not = icmp slt i32 %14, %.01827
  br i1 %.not, label %17, label %23

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %.029, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  %20 = getelementptr inbounds i8, ptr %.01628, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = sub nsw i32 %.01827, %14
  br label %23

23:                                               ; preds = %.lr.ph, %17
  %.123 = phi i32 [ %16, %17 ], [ %.02225, %.lr.ph ]
  %.121 = phi i32 [ %.02026, %17 ], [ %16, %.lr.ph ]
  %.119 = phi i32 [ %22, %17 ], [ %.01827, %.lr.ph ]
  %.117 = phi ptr [ %21, %17 ], [ %12, %.lr.ph ]
  %.1 = phi ptr [ %19, %17 ], [ %9, %.lr.ph ]
  %24 = add nsw i32 %.123, 1
  %25 = icmp eq i32 %24, %.121
  br i1 %25, label %._crit_edge, label %.lr.ph, !llvm.loop !13
}

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


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