; LLVM IR from freddiev4/dailyprogrammerchallenges :: Hard Challenges/Challenge 0104 Hard - (Stack Attack)/solutions (solution)

; 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.Operators = type { i8, i32, i32, ptr }
%struct.Token = type { i32, %union.Data_type }
%union.Data_type = type { double }
%struct.result_stack = type { double, ptr }

@operators = external dso_local global [5 x %struct.Operators], align 16

; 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: nounwind uwtable
declare dso_local void @rstack_push(ptr noundef, double noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local double @rstack_pop(ptr noundef) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare dso_local { i32, i64 } @deque_pop(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local double @eval_deque(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = alloca double, align 8
  %6 = alloca ptr, align 8
  %7 = alloca %struct.Token, align 8
  %8 = alloca %struct.Token, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  br label %9

9:                                                ; preds = %44, %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %45

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #3
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = call { i32, i64 } @deque_pop(ptr noundef %14)
  %16 = getelementptr inbounds { i32, i64 }, ptr %8, i32 0, i32 0
  %17 = extractvalue { i32, i64 } %15, 0
  store i32 %17, ptr %16, align 8
  %18 = getelementptr inbounds { i32, i64 }, ptr %8, i32 0, i32 1
  %19 = extractvalue { i32, i64 } %15, 1
  store i64 %19, ptr %18, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 8 %8, i64 16, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #3
  %20 = getelementptr inbounds %struct.Token, ptr %7, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !13
  %22 = icmp eq i32 %21, 1
  br i1 %22, label %23, label %36

23:                                               ; preds = %13
  %24 = call double @rstack_pop(ptr noundef %6)
  store double %24, ptr %3, align 8, !tbaa !15
  %25 = call double @rstack_pop(ptr noundef %6)
  store double %25, ptr %4, align 8, !tbaa !15
  %26 = getelementptr inbounds %struct.Token, ptr %7, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [5 x %struct.Operators], ptr @operators, i64 0, i64 %28
  %30 = getelementptr inbounds %struct.Operators, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = load double, ptr %3, align 8, !tbaa !15
  %33 = load double, ptr %4, align 8, !tbaa !15
  %34 = call double %31(double noundef %32, double noundef %33)
  store double %34, ptr %5, align 8, !tbaa !15
  %35 = load double, ptr %5, align 8, !tbaa !15
  call void @rstack_push(ptr noundef %6, double noundef %35)
  br label %44

36:                                               ; preds = %13
  %37 = getelementptr inbounds %struct.Token, ptr %7, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !13
  %39 = icmp eq i32 %38, 2
  br i1 %39, label %40, label %43

40:                                               ; preds = %36
  %41 = getelementptr inbounds %struct.Token, ptr %7, i32 0, i32 1
  %42 = load double, ptr %41, align 8, !tbaa !12
  call void @rstack_push(ptr noundef %6, double noundef %42)
  br label %43

43:                                               ; preds = %40, %36
  br label %44

44:                                               ; preds = %43, %23
  br label %9, !llvm.loop !19

45:                                               ; preds = %9
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.result_stack, ptr %46, i32 0, i32 0
  %48 = load double, ptr %47, align 8, !tbaa !21
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret double %48
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{i64 0, i64 4, !10, i64 8, i64 8, !12}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !11, i64 0}
!14 = !{!"Token", !11, i64 0, !7, i64 8}
!15 = !{!16, !16, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = !{!18, !6, i64 16}
!18 = !{!"Operators", !7, i64 0, !11, i64 4, !11, i64 8, !6, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!22, !16, i64 0}
!22 = !{!"result_stack", !16, i64 0, !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.Operators = type { i8, i32, i32, ptr }

@operators = external dso_local local_unnamed_addr global [5 x %struct.Operators], align 16

; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local void @rstack_push(ptr noundef, double noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local double @rstack_pop(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local { i32, i64 } @deque_pop(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local double @eval_deque(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  store ptr null, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %.not8 = icmp ne ptr %3, null
  tail call void @llvm.assume(i1 %.not8)
  br label %.lr.ph

.lr.ph:                                           ; preds = %1, %16
  %4 = call { i32, i64 } @deque_pop(ptr noundef nonnull %0)
  %5 = extractvalue { i32, i64 } %4, 0
  %6 = extractvalue { i32, i64 } %4, 1
  switch i32 %5, label %16 [
    i32 1, label %7
    i32 2, label %14
  ]

7:                                                ; preds = %.lr.ph
  %8 = call double @rstack_pop(ptr noundef nonnull %2)
  %9 = call double @rstack_pop(ptr noundef nonnull %2)
  %sext = shl i64 %6, 32
  %10 = ashr exact i64 %sext, 32
  %11 = getelementptr inbounds [5 x %struct.Operators], ptr @operators, i64 0, i64 %10, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = call double %12(double noundef %8, double noundef %9) #3
  br label %.sink.split

14:                                               ; preds = %.lr.ph
  %15 = bitcast i64 %6 to double
  br label %.sink.split

.sink.split:                                      ; preds = %7, %14
  %.sink = phi double [ %15, %14 ], [ %13, %7 ]
  call void @rstack_push(ptr noundef nonnull %2, double noundef %.sink)
  br label %16

16:                                               ; preds = %.sink.split, %.lr.ph
  %17 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %17, null
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !12

._crit_edge.loopexit:                             ; preds = %16
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  %18 = load double, ptr %.pre, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret double %18
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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(inaccessiblemem: write) }
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, !6, i64 16}
!10 = !{!"Operators", !7, i64 0, !11, i64 4, !11, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !16, i64 0}
!15 = !{!"result_stack", !16, i64 0, !6, i64 8}
!16 = !{!"double", !7, i64 0}


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