; LLVM IR from KishanBGajera/2024-366-Days-Of-DSA :: Data Structures/Linear/Stack/Applications of Stack/undo-redo demo/C (main)

; 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.17 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @push(ptr noundef, i32 noundef) #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @operate(ptr noundef %0, i32 noundef %1, i8 noundef signext %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i8 %2, ptr %8, align 1, !tbaa !11
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  %11 = load i8, ptr %8, align 1, !tbaa !11
  %12 = sext i8 %11 to i32
  %13 = icmp eq i32 %12, 114
  br i1 %13, label %14, label %21

14:                                               ; preds = %5
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %16, align 4, !tbaa !9
  call void @push(ptr noundef %15, i32 noundef %17)
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = call i32 @pop(ptr noundef %18)
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %19, ptr %20, align 4, !tbaa !9
  br label %63

21:                                               ; preds = %5
  %22 = load i8, ptr %8, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %23, 117
  br i1 %24, label %25, label %32

25:                                               ; preds = %21
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i32, ptr %27, align 4, !tbaa !9
  call void @push(ptr noundef %26, i32 noundef %28)
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = call i32 @pop(ptr noundef %29)
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %30, ptr %31, align 4, !tbaa !9
  br label %63

32:                                               ; preds = %21
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %34, align 4, !tbaa !9
  call void @push(ptr noundef %33, i32 noundef %35)
  %36 = load i8, ptr %8, align 1, !tbaa !11
  %37 = sext i8 %36 to i32
  switch i32 %37, label %58 [
    i32 43, label %38
    i32 45, label %43
    i32 42, label %48
    i32 47, label %53
  ]

38:                                               ; preds = %32
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = add nsw i32 %41, %39
  store i32 %42, ptr %40, align 4, !tbaa !9
  br label %62

43:                                               ; preds = %32
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i32, ptr %45, align 4, !tbaa !9
  %47 = sub nsw i32 %46, %44
  store i32 %47, ptr %45, align 4, !tbaa !9
  br label %62

48:                                               ; preds = %32
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = mul nsw i32 %51, %49
  store i32 %52, ptr %50, align 4, !tbaa !9
  br label %62

53:                                               ; preds = %32
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = sdiv i32 %56, %54
  store i32 %57, ptr %55, align 4, !tbaa !9
  br label %62

58:                                               ; preds = %32
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.17)
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = call i32 @pop(ptr noundef %60)
  br label %62

62:                                               ; preds = %58, %53, %48, %43, %38
  br label %63

63:                                               ; preds = %62, %25, %14
  ret void
}

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 = { "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 = !{!"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}
!11 = !{!7, !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"

@.str.17 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @push(ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @pop(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @operate(ptr nocapture noundef %0, i32 noundef %1, i8 noundef signext %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = load i32, ptr %0, align 4, !tbaa !5
  switch i8 %2, label %11 [
    i8 114, label %7
    i8 117, label %9
  ]

7:                                                ; preds = %5
  tail call void @push(ptr noundef %3, i32 noundef %6)
  %8 = tail call i32 @pop(ptr noundef %4)
  store i32 %8, ptr %0, align 4, !tbaa !5
  br label %27

9:                                                ; preds = %5
  tail call void @push(ptr noundef %4, i32 noundef %6)
  %10 = tail call i32 @pop(ptr noundef %3)
  store i32 %10, ptr %0, align 4, !tbaa !5
  br label %27

11:                                               ; preds = %5
  tail call void @push(ptr noundef %3, i32 noundef %6)
  switch i8 %2, label %24 [
    i8 43, label %12
    i8 45, label %15
    i8 42, label %18
    i8 47, label %21
  ]

12:                                               ; preds = %11
  %13 = load i32, ptr %0, align 4, !tbaa !5
  %14 = add nsw i32 %13, %1
  store i32 %14, ptr %0, align 4, !tbaa !5
  br label %27

15:                                               ; preds = %11
  %16 = load i32, ptr %0, align 4, !tbaa !5
  %17 = sub nsw i32 %16, %1
  store i32 %17, ptr %0, align 4, !tbaa !5
  br label %27

18:                                               ; preds = %11
  %19 = load i32, ptr %0, align 4, !tbaa !5
  %20 = mul nsw i32 %19, %1
  store i32 %20, ptr %0, align 4, !tbaa !5
  br label %27

21:                                               ; preds = %11
  %22 = load i32, ptr %0, align 4, !tbaa !5
  %23 = sdiv i32 %22, %1
  store i32 %23, ptr %0, align 4, !tbaa !5
  br label %27

24:                                               ; preds = %11
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.17)
  %26 = tail call i32 @pop(ptr noundef %3)
  br label %27

27:                                               ; preds = %12, %15, %18, %21, %24, %9, %7
  ret void
}

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 = { 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/C++ TBAA"}


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