; LLVM IR from KishanBGajera/2024-366-Days-Of-DSA :: Data Structures/Linear/Stack/Applications of Stack (evaluatePostfix)

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

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #2

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

; Function Attrs: nounwind uwtable
define dso_local double @evaluatePostfix(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca [1000 x double], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  %8 = alloca double, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = call i64 @strlen(ptr noundef %9) #5
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8000, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 -1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %96, %1
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

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

17:                                               ; preds = %12
  %18 = call ptr @__ctype_b_loc() #6
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = sext i8 %24 to i32
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i16, ptr %19, i64 %26
  %28 = load i16, ptr %27, align 2, !tbaa !12
  %29 = zext i16 %28 to i32
  %30 = and i32 %29, 2048
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %45

32:                                               ; preds = %17
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = sext i8 %37 to i32
  %39 = sub nsw i32 %38, 48
  %40 = sitofp i32 %39 to double
  %41 = load i32, ptr %5, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %5, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %43
  store double %40, ptr %44, align 8, !tbaa !14
  br label %95

45:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = add nsw i32 %46, -1
  store i32 %47, ptr %5, align 4, !tbaa !9
  %48 = sext i32 %46 to i64
  %49 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %48
  %50 = load double, ptr %49, align 8, !tbaa !14
  store double %50, ptr %7, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr %5, align 4, !tbaa !9
  %53 = sext i32 %51 to i64
  %54 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %53
  %55 = load double, ptr %54, align 8, !tbaa !14
  store double %55, ptr %8, align 8, !tbaa !14
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !11
  %61 = sext i8 %60 to i32
  switch i32 %61, label %94 [
    i32 43, label %62
    i32 45, label %70
    i32 42, label %78
    i32 47, label %86
  ]

62:                                               ; preds = %45
  %63 = load double, ptr %8, align 8, !tbaa !14
  %64 = load double, ptr %7, align 8, !tbaa !14
  %65 = fadd double %63, %64
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %5, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %68
  store double %65, ptr %69, align 8, !tbaa !14
  br label %94

70:                                               ; preds = %45
  %71 = load double, ptr %8, align 8, !tbaa !14
  %72 = load double, ptr %7, align 8, !tbaa !14
  %73 = fsub double %71, %72
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %5, align 4, !tbaa !9
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %76
  store double %73, ptr %77, align 8, !tbaa !14
  br label %94

78:                                               ; preds = %45
  %79 = load double, ptr %8, align 8, !tbaa !14
  %80 = load double, ptr %7, align 8, !tbaa !14
  %81 = fmul double %79, %80
  %82 = load i32, ptr %5, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %5, align 4, !tbaa !9
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %84
  store double %81, ptr %85, align 8, !tbaa !14
  br label %94

86:                                               ; preds = %45
  %87 = load double, ptr %8, align 8, !tbaa !14
  %88 = load double, ptr %7, align 8, !tbaa !14
  %89 = fdiv double %87, %88
  %90 = load i32, ptr %5, align 4, !tbaa !9
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %5, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 %92
  store double %89, ptr %93, align 8, !tbaa !14
  br label %94

94:                                               ; preds = %86, %78, %70, %62, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %95

95:                                               ; preds = %94, %32
  br label %96

96:                                               ; preds = %95
  %97 = load i32, ptr %6, align 4, !tbaa !9
  %98 = add nsw i32 %97, 1
  store i32 %98, ptr %6, align 4, !tbaa !9
  br label %12, !llvm.loop !16

99:                                               ; preds = %16
  %100 = getelementptr inbounds [1000 x double], ptr %4, i64 0, i64 0
  %101 = load double, ptr %100, align 16, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8000, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret double %101
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind willreturn memory(read) "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 = { nounwind willreturn memory(none) "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 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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { nounwind willreturn memory(none) }

!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}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() 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) #0

; Function Attrs: nofree nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local double @evaluatePostfix(ptr nocapture noundef readonly %0) local_unnamed_addr #3 {
  %2 = alloca [1000 x double], align 16
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %4 = trunc i64 %3 to i32
  call void @llvm.lifetime.start.p0(i64 8000, ptr nonnull %2) #5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %6 = tail call ptr @__ctype_b_loc() #6
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %wide.trip.count = and i64 %3, 2147483647
  br label %9

._crit_edge.loopexit:                             ; preds = %40
  %.pre = load double, ptr %2, align 16, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %1
  %8 = phi double [ %.pre, %._crit_edge.loopexit ], [ undef, %1 ]
  call void @llvm.lifetime.end.p0(i64 8000, ptr nonnull %2) #5
  ret double %8

9:                                                ; preds = %.lr.ph, %40
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %40 ]
  %.026 = phi i32 [ -1, %.lr.ph ], [ %.2, %40 ]
  %10 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %11 = load i8, ptr %10, align 1, !tbaa !11
  %12 = sext i8 %11 to i64
  %13 = getelementptr inbounds i16, ptr %7, i64 %12
  %14 = load i16, ptr %13, align 2, !tbaa !12
  %15 = and i16 %14, 2048
  %.not = icmp eq i16 %15, 0
  br i1 %.not, label %23, label %16

16:                                               ; preds = %9
  %17 = sext i8 %11 to i32
  %18 = add nsw i32 %17, -48
  %19 = sitofp i32 %18 to double
  %20 = add nsw i32 %.026, 1
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1000 x double], ptr %2, i64 0, i64 %21
  store double %19, ptr %22, align 8, !tbaa !9
  br label %40

23:                                               ; preds = %9
  %24 = add nsw i32 %.026, -1
  %25 = sext i32 %.026 to i64
  %26 = getelementptr inbounds [1000 x double], ptr %2, i64 0, i64 %25
  %27 = load double, ptr %26, align 8, !tbaa !9
  %28 = add nsw i32 %.026, -2
  %29 = sext i32 %24 to i64
  %30 = getelementptr inbounds [1000 x double], ptr %2, i64 0, i64 %29
  %31 = load double, ptr %30, align 8, !tbaa !9
  switch i8 %11, label %40 [
    i8 43, label %32
    i8 45, label %34
    i8 42, label %36
    i8 47, label %38
  ]

32:                                               ; preds = %23
  %33 = fadd double %27, %31
  store double %33, ptr %30, align 8, !tbaa !9
  br label %40

34:                                               ; preds = %23
  %35 = fsub double %31, %27
  store double %35, ptr %30, align 8, !tbaa !9
  br label %40

36:                                               ; preds = %23
  %37 = fmul double %27, %31
  store double %37, ptr %30, align 8, !tbaa !9
  br label %40

38:                                               ; preds = %23
  %39 = fdiv double %31, %27
  store double %39, ptr %30, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %23, %32, %34, %36, %38, %16
  %.2 = phi i32 [ %20, %16 ], [ %28, %23 ], [ %24, %38 ], [ %24, %36 ], [ %24, %34 ], [ %24, %32 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %9, !llvm.loop !14
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { nofree nounwind memory(read, inaccessiblemem: none) 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 #4 = { nounwind willreturn memory(read) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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 = !{!"double", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass