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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i32 @block_idx() #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i32 @grid_dim() #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 nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

; Function Attrs: mustprogress uwtable
define dso_local void @gpu_parallel_mesh_for(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i64 noundef %5) #3 {
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  store i64 %5, ptr %12, align 8, !tbaa !11
  %17 = load i64, ptr %12, align 8, !tbaa !11
  %18 = call ptr @llvm.stacksave.p0()
  store ptr %18, ptr %13, align 8
  %19 = alloca i8, i64 %17, align 8
  store i64 %17, ptr %14, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %20 = getelementptr inbounds i8, ptr %19, i64 0
  store ptr %20, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #4
  %21 = call i32 @block_idx()
  store i32 %21, ptr %16, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %48, %6
  %23 = load i32, ptr %16, align 4, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #4
  br label %52

27:                                               ; preds = %22
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %15, align 8, !tbaa !5
  %34 = load i32, ptr %16, align 4, !tbaa !9
  call void %31(ptr noundef %32, ptr noundef %33, i32 noundef %34)
  br label %35

35:                                               ; preds = %30, %27
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = load ptr, ptr %15, align 8, !tbaa !5
  %39 = load i32, ptr %16, align 4, !tbaa !9
  call void %36(ptr noundef %37, ptr noundef %38, i32 noundef %39)
  %40 = load ptr, ptr %11, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %47

42:                                               ; preds = %35
  %43 = load ptr, ptr %11, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = load ptr, ptr %15, align 8, !tbaa !5
  %46 = load i32, ptr %16, align 4, !tbaa !9
  call void %43(ptr noundef %44, ptr noundef %45, i32 noundef %46)
  br label %47

47:                                               ; preds = %42, %35
  br label %48

48:                                               ; preds = %47
  %49 = call i32 @grid_dim()
  %50 = load i32, ptr %16, align 4, !tbaa !9
  %51 = add nsw i32 %50, %49
  store i32 %51, ptr %16, align 4, !tbaa !9
  br label %22, !llvm.loop !13

52:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  %53 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %53)
  ret void
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 #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 = !{!"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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 nounwind uwtable
declare dso_local i32 @block_idx() local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i32 @grid_dim() local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @gpu_parallel_mesh_for(ptr noundef %0, i32 noundef %1, ptr noundef readonly %2, ptr nocapture noundef readonly %3, ptr noundef readonly %4, i64 noundef %5) local_unnamed_addr #1 {
  %7 = alloca i8, i64 %5, align 8
  %8 = tail call i32 @block_idx()
  %9 = icmp slt i32 %8, %1
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %.not = icmp eq ptr %2, null
  %.not20 = icmp eq ptr %4, null
  br i1 %.not, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph
  br i1 %.not20, label %.lr.ph.split.us.split.us, label %.lr.ph.split.us.split

.lr.ph.split.us.split.us:                         ; preds = %.lr.ph.split.us, %.lr.ph.split.us.split.us
  %.021.us.us = phi i32 [ %11, %.lr.ph.split.us.split.us ], [ %8, %.lr.ph.split.us ]
  call void %3(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021.us.us)
  %10 = call i32 @grid_dim()
  %11 = add nsw i32 %10, %.021.us.us
  %12 = icmp slt i32 %11, %1
  br i1 %12, label %.lr.ph.split.us.split.us, label %._crit_edge, !llvm.loop !5

.lr.ph.split.us.split:                            ; preds = %.lr.ph.split.us, %.lr.ph.split.us.split
  %.021.us = phi i32 [ %14, %.lr.ph.split.us.split ], [ %8, %.lr.ph.split.us ]
  call void %3(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021.us)
  call void %4(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021.us)
  %13 = call i32 @grid_dim()
  %14 = add nsw i32 %13, %.021.us
  %15 = icmp slt i32 %14, %1
  br i1 %15, label %.lr.ph.split.us.split, label %._crit_edge, !llvm.loop !5

.lr.ph.split:                                     ; preds = %.lr.ph
  br i1 %.not20, label %.lr.ph.split.split.us, label %.lr.ph.split.split

.lr.ph.split.split.us:                            ; preds = %.lr.ph.split, %.lr.ph.split.split.us
  %.021.us22 = phi i32 [ %17, %.lr.ph.split.split.us ], [ %8, %.lr.ph.split ]
  call void %2(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021.us22)
  call void %3(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021.us22)
  %16 = call i32 @grid_dim()
  %17 = add nsw i32 %16, %.021.us22
  %18 = icmp slt i32 %17, %1
  br i1 %18, label %.lr.ph.split.split.us, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph.split.split, %.lr.ph.split.split.us, %.lr.ph.split.us.split, %.lr.ph.split.us.split.us, %6
  ret void

.lr.ph.split.split:                               ; preds = %.lr.ph.split, %.lr.ph.split.split
  %.021 = phi i32 [ %20, %.lr.ph.split.split ], [ %8, %.lr.ph.split ]
  call void %2(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021)
  call void %3(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021)
  call void %4(ptr noundef %0, ptr noundef nonnull %7, i32 noundef %.021)
  %19 = call i32 @grid_dim()
  %20 = add nsw i32 %19, %.021
  %21 = icmp slt i32 %20, %1
  br i1 %21, label %.lr.ph.split.split, label %._crit_edge, !llvm.loop !5
}

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass