; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Sorting/Bubble_Sort (Bubble_Sort)

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

@.str = external hidden unnamed_addr constant [42 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local 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 [30 x i32], align 16
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 120, ptr %6) #3
  %7 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %2)
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %9)
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %20, %0
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %23

15:                                               ; preds = %11
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %17
  %19 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %18)
  br label %20

20:                                               ; preds = %15
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %4, align 4, !tbaa !5
  br label %11, !llvm.loop !9

23:                                               ; preds = %11
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 2
  store i32 %25, ptr %4, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %68, %23
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = icmp sge i32 %27, 0
  br i1 %28, label %29, label %71

29:                                               ; preds = %26
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %64, %29
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = icmp sle i32 %31, %32
  br i1 %33, label %34, label %67

34:                                               ; preds = %30
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp sgt i32 %38, %43
  br i1 %44, label %45, label %63

45:                                               ; preds = %34
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  store i32 %49, ptr %3, align 4, !tbaa !5
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %56
  store i32 %54, ptr %57, align 4, !tbaa !5
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %61
  store i32 %58, ptr %62, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %45, %34
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %5, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %5, align 4, !tbaa !5
  br label %30, !llvm.loop !11

67:                                               ; preds = %30
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = add nsw i32 %69, -1
  store i32 %70, ptr %4, align 4, !tbaa !5
  br label %26, !llvm.loop !12

71:                                               ; preds = %26
  %72 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %73

73:                                               ; preds = %83, %71
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = load i32, ptr %2, align 4, !tbaa !5
  %76 = icmp slt i32 %74, %75
  br i1 %76, label %77, label %86

77:                                               ; preds = %73
  %78 = load i32, ptr %4, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [30 x i32], ptr %6, i64 0, i64 %79
  %81 = load i32, ptr %80, align 4, !tbaa !5
  %82 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %81)
  br label %83

83:                                               ; preds = %77
  %84 = load i32, ptr %4, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %4, align 4, !tbaa !5
  br label %73, !llvm.loop !13

86:                                               ; preds = %73
  call void @llvm.lifetime.end.p0(i64 120, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  ret i32 0
}

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

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

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

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

attributes #0 = { 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 = { "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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !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"

@.str = external hidden unnamed_addr constant [42 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca [30 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  call void @llvm.lifetime.start.p0(i64 120, ptr nonnull %2) #3
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1)
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %5)
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge25

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %9 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %9)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = icmp slt i64 %indvars.iv.next, %12
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph
  %14 = icmp sgt i32 %11, 1
  br i1 %14, label %.preheader.preheader, label %._crit_edge25

.preheader.preheader:                             ; preds = %._crit_edge
  %15 = add nsw i32 %11, -2
  %16 = add nsw i32 %11, -1
  %17 = zext i32 %16 to i64
  %18 = add nsw i64 %17, -1
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.epilog-lcssa
  %indvar = phi i64 [ 0, %.preheader.preheader ], [ %indvar.next, %.epilog-lcssa ]
  %indvars.iv37 = phi i64 [ %17, %.preheader.preheader ], [ %indvars.iv.next38, %.epilog-lcssa ]
  %.124 = phi i32 [ %15, %.preheader.preheader ], [ %41, %.epilog-lcssa ]
  %.pre = load i32, ptr %2, align 16, !tbaa !5
  %xtraiter = and i64 %indvars.iv37, 1
  %19 = icmp eq i64 %indvar, %18
  br i1 %19, label %.unr-lcssa, label %.preheader.new

.preheader.new:                                   ; preds = %.preheader
  %unroll_iter = and i64 %indvars.iv37, -2
  br label %20

20:                                               ; preds = %34, %.preheader.new
  %21 = phi i32 [ %.pre, %.preheader.new ], [ %35, %34 ]
  %indvars.iv32 = phi i64 [ 0, %.preheader.new ], [ %indvars.iv.next33.1, %34 ]
  %niter = phi i64 [ 0, %.preheader.new ], [ %niter.next.1, %34 ]
  %indvars.iv.next33 = or disjoint i64 %indvars.iv32, 1
  %22 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv.next33
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = icmp sgt i32 %21, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %20
  %26 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv32
  store i32 %23, ptr %26, align 8, !tbaa !5
  store i32 %21, ptr %22, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %20, %25
  %28 = phi i32 [ %23, %20 ], [ %21, %25 ]
  %indvars.iv.next33.1 = add nuw nsw i64 %indvars.iv32, 2
  %29 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv.next33.1
  %30 = load i32, ptr %29, align 8, !tbaa !5
  %31 = icmp sgt i32 %28, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %27
  %33 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv.next33
  store i32 %30, ptr %33, align 4, !tbaa !5
  store i32 %28, ptr %29, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %32, %27
  %35 = phi i32 [ %30, %27 ], [ %28, %32 ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %20, !llvm.loop !11

.unr-lcssa:                                       ; preds = %34, %.preheader
  %.unr = phi i32 [ %.pre, %.preheader ], [ %35, %34 ]
  %indvars.iv32.unr = phi i64 [ 0, %.preheader ], [ %indvars.iv.next33.1, %34 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.epil.preheader

.epil.preheader:                                  ; preds = %.unr-lcssa
  %indvars.iv.next33.epil = add nuw nsw i64 %indvars.iv32.unr, 1
  %36 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv.next33.epil
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp sgt i32 %.unr, %37
  br i1 %38, label %39, label %.epilog-lcssa

39:                                               ; preds = %.epil.preheader
  %40 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv32.unr
  store i32 %37, ptr %40, align 4, !tbaa !5
  store i32 %.unr, ptr %36, align 4, !tbaa !5
  br label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.epil.preheader, %39, %.unr-lcssa
  %41 = add nsw i32 %.124, -1
  %42 = icmp sgt i32 %.124, 0
  %indvars.iv.next38 = add nsw i64 %indvars.iv37, -1
  %indvar.next = add i64 %indvar, 1
  br i1 %42, label %.preheader, label %._crit_edge25, !llvm.loop !12

._crit_edge25:                                    ; preds = %.epilog-lcssa, %0, %._crit_edge
  %43 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %44 = load i32, ptr %1, align 4, !tbaa !5
  %45 = icmp sgt i32 %44, 0
  br i1 %45, label %.lr.ph28, label %._crit_edge29

.lr.ph28:                                         ; preds = %._crit_edge25, %.lr.ph28
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %.lr.ph28 ], [ 0, %._crit_edge25 ]
  %46 = getelementptr inbounds [30 x i32], ptr %2, i64 0, i64 %indvars.iv40
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %47)
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = icmp slt i64 %indvars.iv.next41, %50
  br i1 %51, label %.lr.ph28, label %._crit_edge29, !llvm.loop !13

._crit_edge29:                                    ; preds = %.lr.ph28, %._crit_edge25
  call void @llvm.lifetime.end.p0(i64 120, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret i32 0
}

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

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

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

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

attributes #0 = { nofree 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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" }
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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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