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

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

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

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

; 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) #1

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

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

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

18:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #4
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [50003 x i64], ptr @q, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [50003 x i64], ptr @q, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [50003 x i64], ptr @q, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [50003 x i64], ptr @q, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !9
  call void @_Z8makeNodellll(ptr dead_on_unwind writable sret(%struct.node) align 8 %8, i64 noundef %22, i64 noundef %26, i64 noundef %30, i64 noundef %34)
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %36
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %37, ptr align 8 %8, i64 32, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #4
  %38 = load i32, ptr %7, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %39
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 16 %40, i64 32, i1 false), !tbaa.struct !11
  br label %62

41:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = add nsw i32 %42, %43
  %45 = sdiv i32 %44, 2
  store i32 %45, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #4
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = shl i32 %48, 1
  call void @_Z5buildiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %10, i32 noundef %46, i32 noundef %47, i32 noundef %49)
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #4
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = shl i32 %53, 1
  %55 = add nsw i32 %54, 1
  call void @_Z5buildiii(ptr dead_on_unwind writable sret(%struct.node) align 8 %11, i32 noundef %51, i32 noundef %52, i32 noundef %55)
  call void @llvm.lifetime.start.p0(i64 32, ptr %12) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %13, ptr align 8 %10, i64 32, i1 false), !tbaa.struct !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %14, ptr align 8 %11, i64 32, i1 false), !tbaa.struct !11
  call void @_Z7combine4nodeS_(ptr dead_on_unwind writable sret(%struct.node) align 8 %12, ptr noundef byval(%struct.node) align 8 %13, ptr noundef byval(%struct.node) align 8 %14)
  %56 = load i32, ptr %7, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %57
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %58, ptr align 8 %12, i64 32, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 32, ptr %12) #4
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %60
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %0, ptr align 16 %61, i64 32, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %62

62:                                               ; preds = %41, %18
  ret void
}

; 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) #3

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


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

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

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

; 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 #1

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiii(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.node) align 8 %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.node, align 8
  %6 = alloca %struct.node, align 8
  %7 = alloca %struct.node, align 8
  %8 = alloca %struct.node, align 8
  %9 = icmp eq i32 %1, %2
  br i1 %9, label %10, label %16

10:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  %11 = sext i32 %1 to i64
  %12 = getelementptr inbounds [50003 x i64], ptr @q, i64 0, i64 %11
  %13 = load i64, ptr %12, align 8, !tbaa !5
  call void @_Z8makeNodellll(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %5, i64 noundef %13, i64 noundef %13, i64 noundef %13, i64 noundef %13)
  %14 = sext i32 %3 to i64
  %15 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %14
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %15, ptr noundef nonnull align 8 dereferenceable(32) %5, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef nonnull align 16 dereferenceable(32) %15, i64 32, i1 false), !tbaa.struct !9
  br label %24

16:                                               ; preds = %4
  %17 = add nsw i32 %2, %1
  %18 = sdiv i32 %17, 2
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %6) #4
  %19 = shl i32 %3, 1
  call void @_Z5buildiii(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %6, i32 noundef %1, i32 noundef %18, i32 noundef %19)
  %20 = add nsw i32 %18, 1
  %21 = or disjoint i32 %19, 1
  call void @_Z5buildiii(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %8, i32 noundef %20, i32 noundef %2, i32 noundef %21)
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %7) #4
  call void @_Z7combine4nodeS_(ptr dead_on_unwind nonnull writable sret(%struct.node) align 8 %7, ptr noundef nonnull byval(%struct.node) align 8 %6, ptr noundef nonnull byval(%struct.node) align 8 %8)
  %22 = sext i32 %3 to i64
  %23 = getelementptr inbounds [200003 x %struct.node], ptr @tree, i64 0, i64 %22
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %23, ptr noundef nonnull align 8 dereferenceable(32) %7, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %0, ptr noundef nonnull align 16 dereferenceable(32) %23, i64 32, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %6) #4
  br label %24

24:                                               ; preds = %16, %10
  ret void
}

; 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) #3

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


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass