; LLVM IR from viveakrt/Competitive-programing :: SPOJ/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 { i32, i32, i32, i32 }

@s = external dso_local global [50001 x i32], align 16
@tree = external dso_local global [200004 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %struct.node, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.node, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %37

13:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  %14 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 0
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [50001 x i32], ptr @s, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  store i32 %18, ptr %14, align 4, !tbaa !9
  %19 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 1
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [50001 x i32], ptr @s, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  store i32 %23, ptr %19, align 4, !tbaa !11
  %24 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 2
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [50001 x i32], ptr @s, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr %24, align 4, !tbaa !12
  %29 = getelementptr inbounds %struct.node, ptr %7, i32 0, i32 3
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [50001 x i32], ptr @s, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  store i32 %33, ptr %29, align 4, !tbaa !13
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %35
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %36, ptr align 4 %7, i64 16, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  br label %69

37:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = add nsw i32 %38, %39
  %41 = sdiv i32 %40, 2
  store i32 %41, ptr %8, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = mul nsw i32 %44, 2
  call void @_Z5buildiii(i32 noundef %42, i32 noundef %43, i32 noundef %45)
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = mul nsw i32 %49, 2
  %51 = add nsw i32 %50, 1
  call void @_Z5buildiii(i32 noundef %47, i32 noundef %48, i32 noundef %51)
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #3
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = mul nsw i32 %52, 2
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %54
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = mul nsw i32 %56, 2
  %58 = add nsw i32 %57, 1
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %59
  %61 = call { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 4 dereferenceable(16) %55, ptr noundef nonnull align 4 dereferenceable(16) %60)
  %62 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 0
  %63 = extractvalue { i64, i64 } %61, 0
  store i64 %63, ptr %62, align 4
  %64 = getelementptr inbounds { i64, i64 }, ptr %9, i32 0, i32 1
  %65 = extractvalue { i64, i64 } %61, 1
  store i64 %65, ptr %64, align 4
  %66 = load i32, ptr %6, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %67
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %68, ptr align 4 %9, i64 16, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %69

69:                                               ; preds = %37, %13
  ret void
}

; 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 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 4 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(16)) #0

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 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4node", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, i64 8}
!13 = !{!10, !6, i64 12}
!14 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 4, !5, i64 12, i64 4, !5}


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

@s = external dso_local local_unnamed_addr global [50001 x i32], align 16
@tree = external dso_local global [200004 x %struct.node], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %0, %1
  br i1 %4, label %common.ret, label %12

common.ret:                                       ; preds = %3
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [50001 x i32], ptr @s, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = sext i32 %2 to i64
  %9 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %8
  %10 = insertelement <4 x i32> poison, i32 %7, i64 0
  %11 = shufflevector <4 x i32> %10, <4 x i32> poison, <4 x i32> zeroinitializer
  store <4 x i32> %11, ptr %9, align 16, !tbaa !5
  br label %common.ret21

common.ret21:                                     ; preds = %12, %common.ret
  ret void

12:                                               ; preds = %3
  %13 = add nsw i32 %1, %0
  %14 = sdiv i32 %13, 2
  %15 = shl nsw i32 %2, 1
  tail call void @_Z5buildiii(i32 noundef %0, i32 noundef %14, i32 noundef %15)
  %16 = add nsw i32 %14, 1
  %17 = or disjoint i32 %15, 1
  tail call void @_Z5buildiii(i32 noundef %16, i32 noundef %1, i32 noundef %17)
  %18 = sext i32 %15 to i64
  %19 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %18
  %20 = sext i32 %17 to i64
  %21 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %20
  %22 = tail call { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 4 dereferenceable(16) %19, ptr noundef nonnull align 4 dereferenceable(16) %21)
  %23 = extractvalue { i64, i64 } %22, 0
  %24 = extractvalue { i64, i64 } %22, 1
  %25 = sext i32 %2 to i64
  %26 = getelementptr inbounds [200004 x %struct.node], ptr @tree, i64 0, i64 %25
  store i64 %23, ptr %26, align 16
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %26, i64 8
  store i64 %24, ptr %.sroa.4.0..sroa_idx, align 8
  br label %common.ret21
}

; Function Attrs: mustprogress uwtable
declare dso_local { i64, i64 } @_ZplRK4nodeS1_(ptr noundef nonnull align 4 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(16)) local_unnamed_addr #0

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass