; LLVM IR from taichi-dev/taichi :: misc/baselines (atomics)

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

@glo = external dso_local global i32, align 4
@tot = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  store i32 0, ptr @glo, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %22, %0
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp slt i32 %16, 100000000
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %25

19:                                               ; preds = %15
  %20 = load i32, ptr @glo, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @glo, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %19
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %2, align 4, !tbaa !5
  br label %15, !llvm.loop !9

25:                                               ; preds = %18
  %26 = load i32, ptr @glo, align 4, !tbaa !5
  %27 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %26)
  store i32 0, ptr @glo, align 4, !tbaa !5
  store i64 0, ptr @tot, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %43, %25
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = icmp slt i32 %29, 100000000
  br i1 %30, label %32, label %31

31:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %46

32:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4
  %34 = atomicrmw add ptr @glo, i32 %33 monotonic, align 4
  %35 = add i32 %34, %33
  store i32 %35, ptr %6, align 4
  %36 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %36, ptr %4, align 4, !tbaa !5
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  store i64 %38, ptr %7, align 8, !tbaa !11
  %39 = load i64, ptr %7, align 8
  %40 = atomicrmw add ptr @tot, i64 %39 monotonic, align 8
  %41 = add i64 %40, %39
  store i64 %41, ptr %8, align 8
  %42 = load i64, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %43

43:                                               ; preds = %32
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %3, align 4, !tbaa !5
  br label %28, !llvm.loop !13

46:                                               ; preds = %31
  %47 = load i32, ptr @glo, align 4, !tbaa !5
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %47)
  %49 = load i64, ptr @tot, align 8, !tbaa !11
  %50 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %49)
  store i32 0, ptr @glo, align 4, !tbaa !5
  store i64 0, ptr @tot, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %66, %46
  %52 = load i32, ptr %9, align 4, !tbaa !5
  %53 = icmp slt i32 %52, 100000000
  br i1 %53, label %55, label %54

54:                                               ; preds = %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %69

55:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 1, ptr %11, align 4, !tbaa !5
  %56 = load i32, ptr %11, align 4
  %57 = atomicrmw add ptr @glo, i32 %56 seq_cst, align 4
  %58 = add i32 %57, %56
  store i32 %58, ptr %12, align 4
  %59 = load i32, ptr %12, align 4, !tbaa !5
  store i32 %59, ptr %10, align 4, !tbaa !5
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  store i64 %61, ptr %13, align 8, !tbaa !11
  %62 = load i64, ptr %13, align 8
  %63 = atomicrmw add ptr @tot, i64 %62 seq_cst, align 8
  %64 = add i64 %63, %62
  store i64 %64, ptr %14, align 8
  %65 = load i64, ptr %14, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %66

66:                                               ; preds = %55
  %67 = load i32, ptr %9, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %9, align 4, !tbaa !5
  br label %51, !llvm.loop !14

69:                                               ; preds = %54
  %70 = load i32, ptr @glo, align 4, !tbaa !5
  %71 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %70)
  %72 = load i64, ptr @tot, align 8, !tbaa !11
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %72)
  ret i32 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

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { mustprogress norecurse 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


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

@glo = external dso_local global i32, align 4
@tot = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  store i32 100000000, ptr @glo, align 4, !tbaa !5
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 100000000)
  store i32 0, ptr @glo, align 4, !tbaa !5
  store i64 0, ptr @tot, align 8, !tbaa !9
  br label %7

2:                                                ; preds = %7
  %3 = load i32, ptr @glo, align 4, !tbaa !5
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %3)
  %5 = load i64, ptr @tot, align 8, !tbaa !9
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %5)
  store i32 0, ptr @glo, align 4, !tbaa !5
  store i64 0, ptr @tot, align 8, !tbaa !9
  br label %34

7:                                                ; preds = %7, %0
  %.01619 = phi i32 [ 0, %0 ], [ %28, %7 ]
  %8 = atomicrmw add ptr @glo, i32 1 monotonic, align 4
  %9 = add i32 %8, 1
  %10 = sext i32 %9 to i64
  %11 = atomicrmw add ptr @tot, i64 %10 monotonic, align 8
  %12 = atomicrmw add ptr @glo, i32 1 monotonic, align 4
  %13 = add i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = atomicrmw add ptr @tot, i64 %14 monotonic, align 8
  %16 = atomicrmw add ptr @glo, i32 1 monotonic, align 4
  %17 = add i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = atomicrmw add ptr @tot, i64 %18 monotonic, align 8
  %20 = atomicrmw add ptr @glo, i32 1 monotonic, align 4
  %21 = add i32 %20, 1
  %22 = sext i32 %21 to i64
  %23 = atomicrmw add ptr @tot, i64 %22 monotonic, align 8
  %24 = atomicrmw add ptr @glo, i32 1 monotonic, align 4
  %25 = add i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = atomicrmw add ptr @tot, i64 %26 monotonic, align 8
  %28 = add nuw nsw i32 %.01619, 5
  %exitcond.not.4 = icmp eq i32 %28, 100000000
  br i1 %exitcond.not.4, label %2, label %7, !llvm.loop !11

29:                                               ; preds = %34
  %30 = load i32, ptr @glo, align 4, !tbaa !5
  %31 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %30)
  %32 = load i64, ptr @tot, align 8, !tbaa !9
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %32)
  ret i32 0

34:                                               ; preds = %34, %2
  %.01720 = phi i32 [ 0, %2 ], [ %55, %34 ]
  %35 = atomicrmw add ptr @glo, i32 1 seq_cst, align 4
  %36 = add i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = atomicrmw add ptr @tot, i64 %37 seq_cst, align 8
  %39 = atomicrmw add ptr @glo, i32 1 seq_cst, align 4
  %40 = add i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = atomicrmw add ptr @tot, i64 %41 seq_cst, align 8
  %43 = atomicrmw add ptr @glo, i32 1 seq_cst, align 4
  %44 = add i32 %43, 1
  %45 = sext i32 %44 to i64
  %46 = atomicrmw add ptr @tot, i64 %45 seq_cst, align 8
  %47 = atomicrmw add ptr @glo, i32 1 seq_cst, align 4
  %48 = add i32 %47, 1
  %49 = sext i32 %48 to i64
  %50 = atomicrmw add ptr @tot, i64 %49 seq_cst, align 8
  %51 = atomicrmw add ptr @glo, i32 1 seq_cst, align 4
  %52 = add i32 %51, 1
  %53 = sext i32 %52 to i64
  %54 = atomicrmw add ptr @tot, i64 %53 seq_cst, align 8
  %55 = add nuw nsw i32 %.01720, 5
  %exitcond21.not.4 = icmp eq i32 %55, 100000000
  br i1 %exitcond21.not.4, label %29, label %34, !llvm.loop !13
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree norecurse 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 = { nofree nounwind "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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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