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

@operators = external dso_local global [5 x %struct.Operators], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 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: nounwind uwtable
define dso_local void @print_deque(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %4, ptr %3, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %40, %1
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %44

8:                                                ; preds = %5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.deque, ptr %9, i32 0, i32 0
  %11 = getelementptr inbounds %struct.Token, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %11, align 8, !tbaa !9
  %13 = icmp eq i32 %12, 1
  br i1 %13, label %14, label %25

14:                                               ; preds = %8
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.deque, ptr %15, i32 0, i32 0
  %17 = getelementptr inbounds %struct.Token, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [5 x %struct.Operators], ptr @operators, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.Operators, ptr %20, i32 0, i32 0
  %22 = load i8, ptr %21, align 8, !tbaa !14
  %23 = sext i8 %22 to i32
  %24 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %23)
  br label %40

25:                                               ; preds = %8
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.deque, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds %struct.Token, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !9
  %30 = icmp eq i32 %29, 2
  br i1 %30, label %31, label %37

31:                                               ; preds = %25
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.deque, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds %struct.Token, ptr %33, i32 0, i32 1
  %35 = load double, ptr %34, align 8, !tbaa !13
  %36 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, double noundef %35)
  br label %39

37:                                               ; preds = %25
  %38 = call i32 @puts(ptr noundef @.str.2)
  br label %39

39:                                               ; preds = %37, %31
  br label %40

40:                                               ; preds = %39, %14
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.deque, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !16
  store ptr %43, ptr %3, align 8, !tbaa !5
  br label %5, !llvm.loop !17

44:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

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

declare i32 @puts(ptr noundef) #2

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 = { "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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 0}
!10 = !{!"deque", !11, i64 0, !6, i64 16}
!11 = !{!"Token", !12, i64 0, !7, i64 8}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"Operators", !7, i64 0, !12, i64 4, !12, i64 8, !6, i64 16}
!16 = !{!10, !6, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.Operators = type { i8, i32, i32, ptr }

@operators = external dso_local local_unnamed_addr global [5 x %struct.Operators], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @print_deque(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not8 = icmp eq ptr %0, null
  br i1 %.not8, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %17
  %.09 = phi ptr [ %19, %17 ], [ %0, %1 ]
  %2 = load i32, ptr %.09, align 8, !tbaa !5
  switch i32 %2, label %15 [
    i32 1, label %3
    i32 2, label %11
  ]

3:                                                ; preds = %.lr.ph
  %4 = getelementptr inbounds i8, ptr %.09, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !12
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [5 x %struct.Operators], ptr @operators, i64 0, i64 %6
  %8 = load i8, ptr %7, align 8, !tbaa !13
  %9 = sext i8 %8 to i32
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %9)
  br label %17

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %.09, i64 8
  %13 = load double, ptr %12, align 8, !tbaa !12
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, double noundef %13)
  br label %17

15:                                               ; preds = %.lr.ph
  %16 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %17

17:                                               ; preds = %11, %15, %3
  %18 = getelementptr inbounds i8, ptr %.09, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !15
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %17, %1
  ret void
}

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

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

attributes #0 = { 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, !8, i64 0}
!6 = !{!"deque", !7, i64 0, !11, i64 16}
!7 = !{!"Token", !8, i64 0, !9, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!9, !9, i64 0}
!13 = !{!14, !9, i64 0}
!14 = !{!"Operators", !9, i64 0, !8, i64 4, !8, i64 8, !11, i64 16}
!15 = !{!6, !11, i64 16}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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