; LLVM IR from gargarchit/Data_Structure_n_Algorithms :: Stacks (Implementing_two_stack_in_one_array)

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

@top1 = external dso_local global i32, align 4
@top2 = external dso_local global i32, align 4
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local void @display(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp eq i32 %7, 1
  br i1 %8, label %9, label %24

9:                                                ; preds = %2
  %10 = load i32, ptr @top1, align 4, !tbaa !9
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store i32 1, ptr %6, align 4
  br label %43

13:                                               ; preds = %9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load i32, ptr @top1, align 4, !tbaa !9
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr @top1, align 4, !tbaa !9
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds i32, ptr %14, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %19)
  %21 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  call void @display(ptr noundef %22, i32 noundef 1)
  br label %23

23:                                               ; preds = %13
  br label %24

24:                                               ; preds = %23, %2
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = icmp eq i32 %25, 2
  br i1 %26, label %27, label %42

27:                                               ; preds = %24
  %28 = load i32, ptr @top2, align 4, !tbaa !9
  %29 = icmp eq i32 %28, 5
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  store i32 1, ptr %6, align 4
  br label %43

31:                                               ; preds = %27
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr @top2, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr @top2, align 4, !tbaa !9
  %35 = sext i32 %33 to i64
  %36 = getelementptr inbounds i32, ptr %32, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %37)
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  call void @display(ptr noundef %40, i32 noundef 2)
  br label %41

41:                                               ; preds = %31
  br label %42

42:                                               ; preds = %41, %24
  store i32 0, ptr %6, align 4
  br label %43

43:                                               ; preds = %42, %30, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %44 = load i32, ptr %6, align 4
  switch i32 %44, label %46 [
    i32 0, label %45
    i32 1, label %45
  ]

45:                                               ; preds = %43, %43
  ret void

46:                                               ; preds = %43
  unreachable
}

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

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

attributes #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 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


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

@top1 = external dso_local local_unnamed_addr global i32, align 4
@top2 = external dso_local local_unnamed_addr global i32, align 4
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1

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

; Function Attrs: nofree nounwind uwtable
define dso_local void @display(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #1 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.backedge, %2
  switch i32 %1, label %18 [
    i32 1, label %3
    i32 2, label %13
  ]

3:                                                ; preds = %tailrecurse
  %4 = load i32, ptr @top1, align 4, !tbaa !5
  %5 = icmp eq i32 %4, -1
  br i1 %5, label %18, label %6

6:                                                ; preds = %3
  %7 = add nsw i32 %4, -1
  store i32 %7, ptr @top1, align 4, !tbaa !5
  br label %tailrecurse.backedge

tailrecurse.backedge:                             ; preds = %6, %16
  %.sink8 = phi i32 [ %4, %6 ], [ %14, %16 ]
  %8 = sext i32 %.sink8 to i64
  %9 = getelementptr inbounds i32, ptr %0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %10)
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  br label %tailrecurse

13:                                               ; preds = %tailrecurse
  %14 = load i32, ptr @top2, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 5
  br i1 %15, label %18, label %16

16:                                               ; preds = %13
  %17 = add nsw i32 %14, 1
  store i32 %17, ptr @top2, align 4, !tbaa !5
  br label %tailrecurse.backedge

18:                                               ; preds = %tailrecurse, %13, %3
  ret void
}

attributes #0 = { 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 #1 = { 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" }

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


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