; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/ds/Double_Ended_Queue.ll (Z7displayv)

; 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.dequeue = type { [10 x i32], i32, i32 }

@dq = external dso_local global %struct.dequeue, align 4
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; 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 uwtable
define dso_local void @_Z7displayv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 1), align 4, !tbaa !5
  %5 = icmp ne i32 %4, -1
  br i1 %5, label %6, label %60

6:                                                ; preds = %0
  %7 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 1), align 4, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 2), align 4, !tbaa !10
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %10, label %27

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %11 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 1), align 4, !tbaa !5
  store i32 %11, ptr %1, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %23, %10
  %13 = load i32, ptr %1, align 4, !tbaa !11
  %14 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 2), align 4, !tbaa !10
  %15 = icmp sle i32 %13, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %12
  %18 = load i32, ptr %1, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %21)
  br label %23

23:                                               ; preds = %17
  %24 = load i32, ptr %1, align 4, !tbaa !11
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %1, align 4, !tbaa !11
  br label %12, !llvm.loop !12

26:                                               ; preds = %16
  br label %58

27:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %28 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 1), align 4, !tbaa !5
  store i32 %28, ptr %2, align 4, !tbaa !11
  br label %29

29:                                               ; preds = %39, %27
  %30 = load i32, ptr %2, align 4, !tbaa !11
  %31 = icmp sle i32 %30, 9
  br i1 %31, label %33, label %32

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

33:                                               ; preds = %29
  %34 = load i32, ptr %2, align 4, !tbaa !11
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %37)
  br label %39

39:                                               ; preds = %33
  %40 = load i32, ptr %2, align 4, !tbaa !11
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %2, align 4, !tbaa !11
  br label %29, !llvm.loop !14

42:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !11
  br label %43

43:                                               ; preds = %54, %42
  %44 = load i32, ptr %3, align 4, !tbaa !11
  %45 = load i32, ptr getelementptr inbounds (%struct.dequeue, ptr @dq, i32 0, i32 2), align 4, !tbaa !10
  %46 = icmp sle i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %57

48:                                               ; preds = %43
  %49 = load i32, ptr %3, align 4, !tbaa !11
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !11
  %53 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %52)
  br label %54

54:                                               ; preds = %48
  %55 = load i32, ptr %3, align 4, !tbaa !11
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %3, align 4, !tbaa !11
  br label %43, !llvm.loop !15

57:                                               ; preds = %47
  br label %58

58:                                               ; preds = %57, %26
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %62

60:                                               ; preds = %0
  %61 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  br label %62

62:                                               ; preds = %60, %58
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
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, !9, i64 40}
!6 = !{!"_ZTS7dequeue", !7, i64 0, !9, i64 40, !9, i64 44}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !9, i64 44}
!11 = !{!9, !9, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


; 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.dequeue = type { [10 x i32], i32, i32 }

@dq = external dso_local local_unnamed_addr global %struct.dequeue, align 4
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z7displayv() local_unnamed_addr #0 {
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @dq, i64 40), align 4, !tbaa !5
  %.not = icmp eq i32 %1, -1
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %0
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @dq, i64 44), align 4, !tbaa !10
  %.not12 = icmp sgt i32 %1, %3
  br i1 %.not12, label %.preheader15, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %4 = sext i32 %1 to i64
  br label %.lr.ph

.preheader15:                                     ; preds = %2
  %5 = icmp slt i32 %1, 10
  br i1 %5, label %.lr.ph21.preheader, label %.preheader

.lr.ph21.preheader:                               ; preds = %.preheader15
  %6 = sext i32 %1 to i64
  br label %.lr.ph21

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %7 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %8)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @dq, i64 44), align 4, !tbaa !10
  %11 = sext i32 %10 to i64
  %.not14.not = icmp slt i64 %indvars.iv, %11
  br i1 %.not14.not, label %.lr.ph, label %.loopexit, !llvm.loop !12

.preheader.loopexit:                              ; preds = %.lr.ph21
  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @dq, i64 44), align 4, !tbaa !10
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %.preheader15
  %12 = phi i32 [ %.pre, %.preheader.loopexit ], [ %3, %.preheader15 ]
  %.not1322 = icmp slt i32 %12, 0
  br i1 %.not1322, label %.loopexit, label %.lr.ph24

.lr.ph21:                                         ; preds = %.lr.ph21.preheader, %.lr.ph21
  %indvars.iv27 = phi i64 [ %6, %.lr.ph21.preheader ], [ %indvars.iv.next28, %.lr.ph21 ]
  %13 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %indvars.iv27
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %14)
  %indvars.iv.next28 = add nsw i64 %indvars.iv27, 1
  %16 = and i64 %indvars.iv.next28, 4294967295
  %exitcond.not = icmp eq i64 %16, 10
  br i1 %exitcond.not, label %.preheader.loopexit, label %.lr.ph21, !llvm.loop !14

.lr.ph24:                                         ; preds = %.preheader, %.lr.ph24
  %indvars.iv30 = phi i64 [ %indvars.iv.next31, %.lr.ph24 ], [ 0, %.preheader ]
  %17 = getelementptr inbounds [10 x i32], ptr @dq, i64 0, i64 %indvars.iv30
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %18)
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @dq, i64 44), align 4, !tbaa !10
  %21 = sext i32 %20 to i64
  %.not13.not = icmp slt i64 %indvars.iv30, %21
  br i1 %.not13.not, label %.lr.ph24, label %.loopexit, !llvm.loop !15

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph24, %0, %.preheader
  %.str.2.sink = phi ptr [ @.str.1, %.preheader ], [ @.str.2, %0 ], [ @.str.1, %.lr.ph24 ], [ @.str.1, %.lr.ph ]
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str.2.sink)
  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, !9, i64 40}
!6 = !{!"_ZTS7dequeue", !7, i64 0, !9, i64 40, !9, i64 44}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !9, i64 44}
!11 = !{!9, !9, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


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