; LLVM IR from taichi-dev/taichi :: taichi/runtime/llvm/runtime_module (runtime)

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

%class.anon.3 = type { ptr, ptr }
%struct.DynamicMeta = type <{ %struct.StructMeta, i32, [4 x i8] }>
%struct.StructMeta = type { i32, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.DynamicNode = type { i32, i32, 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
declare dso_local i32 @atomic_add_i32(ptr noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local ptr @Dynamic_allocate(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca %class.anon.3, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %14, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %15, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.DynamicMeta, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !9
  store i32 %18, ptr %9, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.DynamicNode, ptr %19, i32 0, i32 1
  %21 = call i32 @atomic_add_i32(ptr noundef %20, i32 noundef 1)
  store i32 %21, ptr %10, align 4, !tbaa !14
  %22 = load i32, ptr %10, align 4, !tbaa !14
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %22, ptr %23, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.DynamicNode, ptr %24, i32 0, i32 2
  store ptr %25, ptr %12, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %55, %3
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr %12, align 8, !tbaa !5
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %36

31:                                               ; preds = %27
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.DynamicNode, ptr %32, i32 0, i32 0
  call void @llvm.lifetime.start.p0(i64 16, ptr %13) #3
  %34 = getelementptr inbounds %class.anon.3, ptr %13, i32 0, i32 0
  store ptr %12, ptr %34, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.anon.3, ptr %13, i32 0, i32 1
  store ptr %7, ptr %35, align 8, !tbaa !5
  call void @"_Z11locked_taskIZ16Dynamic_allocateE3$_0EvPvRKT_"(ptr noundef %33, ptr noundef nonnull align 8 dereferenceable(16) %13)
  call void @llvm.lifetime.end.p0(i64 16, ptr %13) #3
  br label %36

36:                                               ; preds = %31, %27
  %37 = load i32, ptr %10, align 4, !tbaa !14
  %38 = load i32, ptr %11, align 4, !tbaa !14
  %39 = load i32, ptr %9, align 4, !tbaa !14
  %40 = add nsw i32 %38, %39
  %41 = icmp slt i32 %37, %40
  br i1 %41, label %42, label %55

42:                                               ; preds = %36
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i64 8
  %46 = load i32, ptr %10, align 4, !tbaa !14
  %47 = load i32, ptr %11, align 4, !tbaa !14
  %48 = sub nsw i32 %46, %47
  %49 = sext i32 %48 to i64
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.StructMeta, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !15
  %53 = mul i64 %49, %52
  %54 = getelementptr inbounds i8, ptr %45, i64 %53
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %54

55:                                               ; preds = %36
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  store ptr %57, ptr %12, align 8, !tbaa !5
  %58 = load i32, ptr %9, align 4, !tbaa !14
  %59 = load i32, ptr %11, align 4, !tbaa !14
  %60 = add nsw i32 %59, %58
  store i32 %60, ptr %11, align 4, !tbaa !14
  br label %26, !llvm.loop !16
}

; Function Attrs: mustprogress uwtable
declare hidden void @"_Z11locked_taskIZ16Dynamic_allocateE3$_0EvPvRKT_"(ptr noundef, ptr noundef nonnull align 8 dereferenceable(16)) #2

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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !12, i64 72}
!10 = !{!"_ZTS11DynamicMeta", !11, i64 0, !12, i64 72}
!11 = !{!"_ZTS10StructMeta", !12, i64 0, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!11, !13, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%class.anon.3 = type { ptr, ptr }

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i32 @atomic_add_i32(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local nonnull ptr @Dynamic_allocate(ptr noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %class.anon.3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  store ptr %0, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 72
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %1, i64 4
  %10 = tail call i32 @atomic_add_i32(ptr noundef nonnull %9, i32 noundef 1)
  store i32 %10, ptr %2, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %11 = getelementptr inbounds i8, ptr %1, i64 8
  %12 = getelementptr inbounds i8, ptr %6, i64 8
  br label %13

13:                                               ; preds = %17, %3
  %storemerge = phi ptr [ %11, %3 ], [ %21, %17 ]
  %.0 = phi i32 [ 0, %3 ], [ %18, %17 ]
  store ptr %storemerge, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %storemerge, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #3
  store ptr %5, ptr %6, align 8, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !5
  call void @"_Z11locked_taskIZ16Dynamic_allocateE3$_0EvPvRKT_"(ptr noundef %1, ptr noundef nonnull align 8 dereferenceable(16) %6)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #3
  br label %17

17:                                               ; preds = %16, %13
  %18 = add nsw i32 %.0, %8
  %19 = icmp slt i32 %10, %18
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  br i1 %19, label %22, label %13, !llvm.loop !15

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %21, i64 8
  %24 = sub nsw i32 %10, %.0
  %25 = sext i32 %24 to i64
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  %28 = load i64, ptr %27, align 8, !tbaa !17
  %29 = mul i64 %28, %25
  %30 = getelementptr inbounds i8, ptr %23, i64 %29
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret ptr %30
}

; Function Attrs: mustprogress uwtable
declare hidden void @"_Z11locked_taskIZ16Dynamic_allocateE3$_0EvPvRKT_"(ptr noundef, ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !12, i64 72}
!10 = !{!"_ZTS11DynamicMeta", !11, i64 0, !12, i64 72}
!11 = !{!"_ZTS10StructMeta", !12, i64 0, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!11, !13, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass