; LLVM IR from freddiev4/dailyprogrammerchallenges :: Intermediate Challenges/Challenge 0004 Intermediate/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.AST_Node = type { ptr, ptr, i32, double }

@stderr = external global ptr, align 8
@.str.25 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [65 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @TYPEtostring(i32 noundef) #1

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @AST_insertoperator(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %10

8:                                                ; preds = %2
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %9, ptr %3, align 8
  br label %75

10:                                               ; preds = %2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %15

13:                                               ; preds = %10
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %14, ptr %3, align 8
  br label %75

15:                                               ; preds = %10
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call zeroext i1 @bindsTighterThan(ptr noundef %16, ptr noundef %17)
  br i1 %18, label %19, label %42

19:                                               ; preds = %15
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.AST_Node, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = call zeroext i1 @bindsTighterThan(ptr noundef %20, ptr noundef %23)
  br i1 %24, label %25, label %32

25:                                               ; preds = %19
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.AST_Node, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !9
  %30 = call ptr @AST_insertoperator(ptr noundef %26, ptr noundef %29)
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %31, ptr %3, align 8
  br label %75

32:                                               ; preds = %19
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.AST_Node, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.AST_Node, ptr %36, i32 0, i32 0
  store ptr %35, ptr %37, align 8, !tbaa !13
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.AST_Node, ptr %39, i32 0, i32 1
  store ptr %38, ptr %40, align 8, !tbaa !9
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %41, ptr %3, align 8
  br label %75

42:                                               ; preds = %15
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.AST_Node, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !9
  %47 = call zeroext i1 @bindsTighterThan(ptr noundef %43, ptr noundef %46)
  br i1 %47, label %48, label %70

48:                                               ; preds = %42
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.AST_Node, ptr %49, i32 0, i32 2
  %51 = load i32, ptr %50, align 8, !tbaa !14
  %52 = icmp ne i32 %51, 1
  br i1 %52, label %53, label %70

53:                                               ; preds = %48
  %54 = load ptr, ptr @stderr, align 8, !tbaa !5
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.AST_Node, ptr %55, i32 0, i32 2
  %57 = load i32, ptr %56, align 8, !tbaa !14
  %58 = call ptr @TYPEtostring(i32 noundef %57)
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.AST_Node, ptr %59, i32 0, i32 2
  %61 = load i32, ptr %60, align 8, !tbaa !14
  %62 = call ptr @TYPEtostring(i32 noundef %61)
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.AST_Node, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.AST_Node, ptr %65, i32 0, i32 2
  %67 = load i32, ptr %66, align 8, !tbaa !14
  %68 = call ptr @TYPEtostring(i32 noundef %67)
  %69 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %54, ptr noundef @.str.25, ptr noundef @.str.26, ptr noundef %58, ptr noundef @.str.27, ptr noundef %62, ptr noundef %68) #3
  call void @exit(i32 noundef 1) #4
  unreachable

70:                                               ; preds = %48, %42
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.AST_Node, ptr %72, i32 0, i32 0
  store ptr %71, ptr %73, align 8, !tbaa !13
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %74, ptr %3, align 8
  br label %75

75:                                               ; preds = %70, %32, %25, %13, %8
  %76 = load ptr, ptr %3, align 8
  ret ptr %76
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bindsTighterThan(ptr noundef, ptr noundef) #1

attributes #0 = { 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 = { 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 = { noreturn 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 #3 = { nounwind }
attributes #4 = { noreturn 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 8}
!10 = !{!"AST_Node", !6, i64 0, !6, i64 8, !11, i64 16, !12, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!10, !6, i64 0}
!14 = !{!10, !11, i64 16}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.25 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.26 = external hidden unnamed_addr constant [65 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [24 x i8], align 1

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

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @AST_insertoperator(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %.loopexit.thread77, label %.lr.ph.split

.lr.ph.split:                                     ; preds = %2
  %4 = icmp eq ptr %1, null
  br i1 %4, label %.loopexit.thread77, label %.lr.ph63.preheader

.lr.ph63.preheader:                               ; preds = %.lr.ph.split
  %5 = tail call zeroext i1 @bindsTighterThan(ptr noundef nonnull %0, ptr noundef nonnull %1)
  br i1 %5, label %6, label %.split

6:                                                ; preds = %.lr.ph63.preheader
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = tail call zeroext i1 @bindsTighterThan(ptr noundef nonnull %0, ptr noundef %8)
  %10 = load ptr, ptr %7, align 8, !tbaa !5
  br i1 %9, label %tailrecurse.peel, label %.split52.thread

.split52.thread:                                  ; preds = %6
  store ptr %10, ptr %0, align 8, !tbaa !12
  store ptr %0, ptr %7, align 8, !tbaa !5
  br label %.loopexit.thread77

tailrecurse.peel:                                 ; preds = %6
  %11 = icmp eq ptr %10, null
  br i1 %11, label %.loopexit.thread, label %.lr.ph63

.lr.ph63:                                         ; preds = %tailrecurse.peel, %tailrecurse
  %.tr283862 = phi ptr [ %17, %tailrecurse ], [ %10, %tailrecurse.peel ]
  %12 = tail call zeroext i1 @bindsTighterThan(ptr noundef nonnull %0, ptr noundef nonnull %.tr283862)
  br i1 %12, label %13, label %.split

13:                                               ; preds = %.lr.ph63
  %14 = getelementptr inbounds i8, ptr %.tr283862, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = tail call zeroext i1 @bindsTighterThan(ptr noundef nonnull %0, ptr noundef %15)
  %17 = load ptr, ptr %14, align 8, !tbaa !5
  br i1 %16, label %tailrecurse, label %.split52

tailrecurse:                                      ; preds = %13
  %18 = icmp eq ptr %17, null
  br i1 %18, label %.loopexit.thread, label %.lr.ph63, !llvm.loop !13

.split52:                                         ; preds = %13
  %19 = getelementptr inbounds i8, ptr %.tr283862, i64 8
  store ptr %17, ptr %0, align 8, !tbaa !12
  store ptr %0, ptr %19, align 8, !tbaa !5
  br i1 %9, label %.loopexit.thread, label %.loopexit.thread77

.split:                                           ; preds = %.lr.ph63, %.lr.ph63.preheader
  %.tr283862.lcssa = phi ptr [ %1, %.lr.ph63.preheader ], [ %.tr283862, %.lr.ph63 ]
  %20 = getelementptr inbounds i8, ptr %.tr283862.lcssa, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = tail call zeroext i1 @bindsTighterThan(ptr noundef nonnull %0, ptr noundef %21)
  br i1 %22, label %23, label %.loopexit

23:                                               ; preds = %.split
  %24 = getelementptr inbounds i8, ptr %.tr283862.lcssa, i64 16
  %25 = load i32, ptr %24, align 8, !tbaa !15
  %.not = icmp eq i32 %25, 1
  br i1 %.not, label %.loopexit, label %26

26:                                               ; preds = %23
  %27 = load ptr, ptr @stderr, align 8, !tbaa !16
  %28 = getelementptr inbounds i8, ptr %0, i64 16
  %29 = load i32, ptr %28, align 8, !tbaa !15
  %30 = tail call ptr @TYPEtostring(i32 noundef %29)
  %31 = load i32, ptr %24, align 8, !tbaa !15
  %32 = tail call ptr @TYPEtostring(i32 noundef %31)
  %33 = load ptr, ptr %20, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i64 16
  %35 = load i32, ptr %34, align 8, !tbaa !15
  %36 = tail call ptr @TYPEtostring(i32 noundef %35)
  %37 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef nonnull @.str.25, ptr noundef nonnull @.str.26, ptr noundef %30, ptr noundef nonnull @.str.27, ptr noundef %32, ptr noundef %36) #3
  tail call void @exit(i32 noundef 1) #4
  unreachable

.loopexit:                                        ; preds = %.split, %23
  store ptr %.tr283862.lcssa, ptr %0, align 8, !tbaa !12
  br i1 %5, label %.loopexit.thread, label %.loopexit.thread77

.loopexit.thread:                                 ; preds = %tailrecurse, %tailrecurse.peel, %.split52, %.loopexit
  br label %.loopexit.thread77

.loopexit.thread77:                               ; preds = %.lr.ph.split, %2, %.split52.thread, %.split52, %.loopexit, %.loopexit.thread
  %38 = phi ptr [ %1, %.loopexit.thread ], [ %0, %.loopexit ], [ %.tr283862, %.split52 ], [ %1, %.split52.thread ], [ %0, %.lr.ph.split ], [ %1, %2 ]
  ret ptr %38
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @bindsTighterThan(ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { 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 = { noreturn 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 #3 = { cold nounwind }
attributes #4 = { noreturn 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, !7, i64 8}
!6 = !{!"AST_Node", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.peeled.count", i32 1}
!15 = !{!6, !10, i64 16}
!16 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass