; LLVM IR from sbshah97/DS-Algo-Handbook :: labs/lab9/code/interval_bottom.ll (Z5printi)

; 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.interval = type { i32, i32, i32 }

@order = external dso_local global [4 x %struct.interval], align 16
@prev = external dso_local global [4 x i32], align 16
@m = external dso_local global [4 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5printi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp slt i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %1
  br label %37

6:                                                ; preds = %1
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %12

9:                                                ; preds = %6
  %10 = load i32, ptr getelementptr inbounds (%struct.interval, ptr @order, i32 0, i32 2), align 8, !tbaa !9
  %11 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %10)
  br label %37

12:                                               ; preds = %6
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [4 x i32], ptr @m, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sub nsw i32 %17, 1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [4 x i32], ptr @m, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp eq i32 %16, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %12
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  call void @_Z5printi(i32 noundef %25)
  br label %37

26:                                               ; preds = %12
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [4 x %struct.interval], ptr @order, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.interval, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %31)
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [4 x i32], ptr @prev, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  call void @_Z5printi(i32 noundef %36)
  br label %37

37:                                               ; preds = %26, %23, %9, %5
  ret void
}

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

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 = { "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, !6, i64 8}
!10 = !{!"_ZTS8interval", !6, i64 0, !6, i64 4, !6, i64 8}


; 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.interval = type { i32, i32, i32 }

@order = external dso_local local_unnamed_addr global [4 x %struct.interval], align 16
@prev = external dso_local local_unnamed_addr global [4 x i32], align 16
@m = external dso_local local_unnamed_addr global [4 x i32], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5printi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 0
  br i1 %2, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %1, %tailrecurse.backedge
  %.tr7 = phi i32 [ %.tr.be, %tailrecurse.backedge ], [ %0, %1 ]
  %3 = icmp eq i32 %.tr7, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %.lr.ph
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @order, i64 8), align 8, !tbaa !5
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %5)
  br label %.loopexit

7:                                                ; preds = %.lr.ph
  %8 = zext nneg i32 %.tr7 to i64
  %9 = getelementptr inbounds [4 x i32], ptr @m, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !10
  %11 = add nsw i32 %.tr7, -1
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [4 x i32], ptr @m, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !10
  %15 = icmp eq i32 %10, %14
  br i1 %15, label %tailrecurse.backedge, label %16

16:                                               ; preds = %7
  %17 = getelementptr inbounds [4 x %struct.interval], ptr @order, i64 0, i64 %8, i32 2
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %18)
  %20 = getelementptr inbounds [4 x i32], ptr @prev, i64 0, i64 %8
  %21 = load i32, ptr %20, align 4, !tbaa !10
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %16, %7
  %.tr.be = phi i32 [ %21, %16 ], [ %11, %7 ]
  %22 = icmp slt i32 %.tr.be, 0
  br i1 %22, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %tailrecurse.backedge, %1, %4
  ret void
}

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

attributes #0 = { mustprogress 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 = { 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, !7, i64 8}
!6 = !{!"_ZTS8interval", !7, i64 0, !7, i64 4, !7, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!7, !7, i64 0}


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