; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions/GSS1.ll (Z3ansiiiii)

; 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 { i64, i64, i64, i64 }

@tree = external dso_local global [200003 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7combine4nodeS_(ptr dead_on_unwind noalias writable sret(%struct.node) align 8, ptr noundef byval(%struct.node) align 8, ptr noundef byval(%struct.node) align 8) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3ansiiiii(ptr dead_on_unwind noalias writable sret(%struct.node) align 8 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #0 {
  %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
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca %struct.node, align 8
  %15 = alloca %struct.node, align 8
  %16 = alloca %struct.node, align 8
  %17 = alloca %struct.node, align 8
  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
  store i32 %4, ptr %10, align 4, !tbaa !5
  store i32 %5, ptr %11, align 4, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %10, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %21, label %29

21:                                               ; preds = %6
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = load i32, ptr %11, align 4, !tbaa !5
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %25, label %29

25:                                               ; preds = %21
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 16 %28, i64 32, i1 false), !tbaa.struct !9
  br label %74

29:                                               ; preds = %21, %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  %33 = sdiv i32 %32, 2
  store i32 %33, ptr %12, align 4, !tbaa !5
  %34 = load i32, ptr %11, align 4, !tbaa !5
  %35 = load i32, ptr %12, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %37, label %44

37:                                               ; preds = %29
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = shl i32 %38, 1
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = load i32, ptr %12, align 4, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !5
  %43 = load i32, ptr %11, align 4, !tbaa !5
  call void @_Z3ansiiiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %0, i32 noundef %39, i32 noundef %40, i32 noundef %41, i32 noundef %42, i32 noundef %43)
  store i32 1, ptr %13, align 4
  br label %73

44:                                               ; preds = %29
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = load i32, ptr %12, align 4, !tbaa !5
  %47 = icmp sgt i32 %45, %46
  br i1 %47, label %48, label %57

48:                                               ; preds = %44
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = shl i32 %49, 1
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %12, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  %54 = load i32, ptr %9, align 4, !tbaa !5
  %55 = load i32, ptr %10, align 4, !tbaa !5
  %56 = load i32, ptr %11, align 4, !tbaa !5
  call void @_Z3ansiiiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %0, i32 noundef %51, i32 noundef %53, i32 noundef %54, i32 noundef %55, i32 noundef %56)
  store i32 1, ptr %13, align 4
  br label %73

57:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 32, ptr %14) #3
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = shl i32 %58, 1
  %60 = load i32, ptr %8, align 4, !tbaa !5
  %61 = load i32, ptr %12, align 4, !tbaa !5
  %62 = load i32, ptr %10, align 4, !tbaa !5
  %63 = load i32, ptr %12, align 4, !tbaa !5
  call void @_Z3ansiiiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %14, i32 noundef %59, i32 noundef %60, i32 noundef %61, i32 noundef %62, i32 noundef %63)
  call void @llvm.lifetime.start.p0(i64 32, ptr %15) #3
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = shl i32 %64, 1
  %66 = add nsw i32 %65, 1
  %67 = load i32, ptr %12, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  %69 = load i32, ptr %9, align 4, !tbaa !5
  %70 = load i32, ptr %12, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  %72 = load i32, ptr %11, align 4, !tbaa !5
  call void @_Z3ansiiiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %15, i32 noundef %66, i32 noundef %68, i32 noundef %69, i32 noundef %71, i32 noundef %72)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %16, ptr align 8 %14, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %17, ptr align 8 %15, i64 32, i1 false), !tbaa.struct !9
  call void @_Z7combine4nodeS_(ptr dead_on_unwind writable sret(%struct.node) align 8 %0, ptr noundef byval(%struct.node) align 8 %16, ptr noundef byval(%struct.node) align 8 %17)
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %14) #3
  br label %73

73:                                               ; preds = %57, %48, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %74

74:                                               ; preds = %73, %25
  ret void
}

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{i64 0, i64 8, !10, i64 8, i64 8, !10, i64 16, i64 8, !10, i64 24, i64 8, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}


; 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 { i64, i64, i64, i64 }

@tree = external dso_local local_unnamed_addr global [200003 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7combine4nodeS_(ptr dead_on_unwind noalias writable sret(%struct.node) align 8, ptr noundef byval(%struct.node) align 8, ptr noundef byval(%struct.node) align 8) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3ansiiiii(ptr dead_on_unwind noalias writable sret(%struct.node) align 8 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #0 {
  %7 = alloca %struct.node, align 8
  %8 = alloca %struct.node, align 8
  %9 = icmp eq i32 %2, %4
  %10 = icmp eq i32 %3, %5
  %or.cond4349 = and i1 %9, %10
  br i1 %or.cond4349, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %6, %tailrecurse.outer
  %11 = phi i1 [ %25, %tailrecurse.outer ], [ %9, %6 ]
  %.tr37.ph52 = phi i32 [ %.us-phi48, %tailrecurse.outer ], [ %3, %6 ]
  %.tr36.ph51 = phi i32 [ %24, %tailrecurse.outer ], [ %2, %6 ]
  %.tr35.ph50 = phi i32 [ %23, %tailrecurse.outer ], [ %1, %6 ]
  br i1 %11, label %.lr.ph.split, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %.lr.ph, %tailrecurse.us
  %.tr3745.us = phi i32 [ %13, %tailrecurse.us ], [ %.tr37.ph52, %.lr.ph ]
  %.tr3544.us = phi i32 [ %14, %tailrecurse.us ], [ %.tr35.ph50, %.lr.ph ]
  %12 = add i32 %.tr3745.us, %.tr36.ph51
  %13 = sdiv i32 %12, 2
  %.not.us = icmp slt i32 %13, %5
  %14 = shl i32 %.tr3544.us, 1
  br i1 %.not.us, label %.split.us, label %tailrecurse.us

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %6
  %.tr35.lcssa = phi i32 [ %1, %6 ], [ %19, %tailrecurse ], [ %23, %tailrecurse.outer ]
  %15 = sext i32 %.tr35.lcssa to i64
  %16 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %15
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef nonnull align 16 dereferenceable(32) %16, i64 32, i1 false), !tbaa.struct !5
  br label %31

.lr.ph.split:                                     ; preds = %.lr.ph, %tailrecurse
  %.tr3745 = phi i32 [ %18, %tailrecurse ], [ %.tr37.ph52, %.lr.ph ]
  %.tr3544 = phi i32 [ %19, %tailrecurse ], [ %.tr35.ph50, %.lr.ph ]
  %17 = add i32 %.tr3745, %.tr36.ph51
  %18 = sdiv i32 %17, 2
  %.not = icmp slt i32 %18, %5
  br i1 %.not, label %.split.us, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph.split
  %19 = shl i32 %.tr3544, 1
  %20 = icmp eq i32 %18, %5
  br i1 %20, label %tailrecurse.outer._crit_edge, label %.lr.ph.split

.split.us:                                        ; preds = %tailrecurse.us, %.lr.ph.split
  %.us-phi = phi i32 [ %18, %.lr.ph.split ], [ %13, %tailrecurse.us ]
  %.us-phi47 = phi i32 [ %.tr3544, %.lr.ph.split ], [ %.tr3544.us, %tailrecurse.us ]
  %.us-phi48 = phi i32 [ %.tr3745, %.lr.ph.split ], [ %.tr3745.us, %tailrecurse.us ]
  %21 = icmp slt i32 %.us-phi, %4
  br i1 %21, label %tailrecurse.outer, label %27

tailrecurse.outer:                                ; preds = %.split.us
  %22 = shl i32 %.us-phi47, 1
  %23 = or disjoint i32 %22, 1
  %24 = add nsw i32 %.us-phi, 1
  %25 = icmp eq i32 %24, %4
  %26 = icmp eq i32 %.us-phi48, %5
  %or.cond43 = and i1 %25, %26
  br i1 %or.cond43, label %tailrecurse.outer._crit_edge, label %.lr.ph

27:                                               ; preds = %.split.us
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #3
  %28 = shl i32 %.us-phi47, 1
  call void @_Z3ansiiiii(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %7, i32 noundef %28, i32 noundef %.tr36.ph51, i32 noundef %.us-phi, i32 noundef %4, i32 noundef %.us-phi)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %8) #3
  %29 = or disjoint i32 %28, 1
  %30 = add nsw i32 %.us-phi, 1
  call void @_Z3ansiiiii(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %8, i32 noundef %29, i32 noundef %30, i32 noundef %.us-phi48, i32 noundef %30, i32 noundef %5)
  call void @_Z7combine4nodeS_(ptr dead_on_unwind writable sret(%struct.node) align 8 %0, ptr noundef nonnull byval(%struct.node) align 8 %7, ptr noundef nonnull byval(%struct.node) align 8 %8)
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #3
  br label %31

31:                                               ; preds = %27, %tailrecurse.outer._crit_edge
  ret void
}

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


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