; 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.4 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [59 x i8], align 1

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

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

6:                                                ; preds = %1
  %7 = load ptr, ptr @stderr, align 8, !tbaa !5
  %8 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef @.str.4, ptr noundef @.str.32) #3
  call void @exit(i32 noundef 1) #4
  unreachable

9:                                                ; preds = %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.AST_Node, ptr %10, i32 0, i32 2
  %12 = load i32, ptr %11, align 8, !tbaa !9
  switch i32 %12, label %106 [
    i32 0, label %13
    i32 1, label %17
    i32 2, label %22
    i32 3, label %32
    i32 4, label %44
    i32 5, label %54
    i32 6, label %66
    i32 7, label %76
    i32 8, label %86
    i32 9, label %96
  ]

13:                                               ; preds = %9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.AST_Node, ptr %14, i32 0, i32 3
  %16 = load double, ptr %15, align 8, !tbaa !13
  store double %16, ptr %2, align 8
  br label %107

17:                                               ; preds = %9
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.AST_Node, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = call double @AST_eval(ptr noundef %20)
  store double %21, ptr %2, align 8
  br label %107

22:                                               ; preds = %9
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.AST_Node, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = call double @AST_eval(ptr noundef %25)
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.AST_Node, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = call double @AST_eval(ptr noundef %29)
  %31 = call double @pow(double noundef %26, double noundef %30) #3
  store double %31, ptr %2, align 8
  br label %107

32:                                               ; preds = %9
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.AST_Node, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = call double @AST_eval(ptr noundef %35)
  %37 = call double @log(double noundef %36) #3
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.AST_Node, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = call double @AST_eval(ptr noundef %40)
  %42 = call double @log(double noundef %41) #3
  %43 = fdiv double %37, %42
  store double %43, ptr %2, align 8
  br label %107

44:                                               ; preds = %9
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.AST_Node, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !15
  %48 = call double @AST_eval(ptr noundef %47)
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.AST_Node, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !14
  %52 = call double @AST_eval(ptr noundef %51)
  %53 = call double @fmod(double noundef %48, double noundef %52) #3
  store double %53, ptr %2, align 8
  br label %107

54:                                               ; preds = %9
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.AST_Node, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !15
  %58 = call double @AST_eval(ptr noundef %57)
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.AST_Node, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  %62 = call double @AST_eval(ptr noundef %61)
  %63 = fdiv double %58, %62
  %64 = fptosi double %63 to i32
  %65 = sitofp i32 %64 to double
  store double %65, ptr %2, align 8
  br label %107

66:                                               ; preds = %9
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.AST_Node, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !15
  %70 = call double @AST_eval(ptr noundef %69)
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.AST_Node, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = call double @AST_eval(ptr noundef %73)
  %75 = fmul double %70, %74
  store double %75, ptr %2, align 8
  br label %107

76:                                               ; preds = %9
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.AST_Node, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !15
  %80 = call double @AST_eval(ptr noundef %79)
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.AST_Node, ptr %81, i32 0, i32 1
  %83 = load ptr, ptr %82, align 8, !tbaa !14
  %84 = call double @AST_eval(ptr noundef %83)
  %85 = fdiv double %80, %84
  store double %85, ptr %2, align 8
  br label %107

86:                                               ; preds = %9
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.AST_Node, ptr %87, i32 0, i32 0
  %89 = load ptr, ptr %88, align 8, !tbaa !15
  %90 = call double @AST_eval(ptr noundef %89)
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.AST_Node, ptr %91, i32 0, i32 1
  %93 = load ptr, ptr %92, align 8, !tbaa !14
  %94 = call double @AST_eval(ptr noundef %93)
  %95 = fadd double %90, %94
  store double %95, ptr %2, align 8
  br label %107

96:                                               ; preds = %9
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.AST_Node, ptr %97, i32 0, i32 0
  %99 = load ptr, ptr %98, align 8, !tbaa !15
  %100 = call double @AST_eval(ptr noundef %99)
  %101 = load ptr, ptr %3, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.AST_Node, ptr %101, i32 0, i32 1
  %103 = load ptr, ptr %102, align 8, !tbaa !14
  %104 = call double @AST_eval(ptr noundef %103)
  %105 = fsub double %100, %104
  store double %105, ptr %2, align 8
  br label %107

106:                                              ; preds = %9
  store double 0.000000e+00, ptr %2, align 8
  br label %107

107:                                              ; preds = %106, %96, %86, %76, %66, %54, %44, %32, %22, %17, %13
  %108 = load double, ptr %2, align 8
  ret double %108
}

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

; Function Attrs: nounwind
declare double @pow(double noundef, double noundef) #0

; Function Attrs: nounwind
declare double @log(double noundef) #0

; Function Attrs: nounwind
declare double @fmod(double noundef, double noundef) #0

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, !11, i64 16}
!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, !12, i64 24}
!14 = !{!10, !6, i64 8}
!15 = !{!10, !6, 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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [59 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
define dso_local double @AST_eval(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %1
  %3 = load ptr, ptr @stderr, align 8, !tbaa !5
  %4 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %3, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.32) #4
  tail call void @exit(i32 noundef 1) #5
  unreachable

.lr.ph:                                           ; preds = %1, %tailrecurse
  %.tr32 = phi ptr [ %11, %tailrecurse ], [ %0, %1 ]
  %5 = getelementptr inbounds i8, ptr %.tr32, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !9
  switch i32 %6, label %common.ret83 [
    i32 0, label %7
    i32 1, label %tailrecurse
    i32 2, label %13
    i32 3, label %20
    i32 4, label %29
    i32 5, label %36
    i32 6, label %45
    i32 7, label %52
    i32 8, label %59
    i32 9, label %66
  ]

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds i8, ptr %.tr32, i64 24
  %9 = load double, ptr %8, align 8, !tbaa !13
  br label %common.ret83

tailrecurse:                                      ; preds = %.lr.ph
  %10 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %12 = icmp eq ptr %11, null
  br i1 %12, label %tailrecurse._crit_edge, label %.lr.ph

common.ret83:                                     ; preds = %7, %.lr.ph, %66, %59, %52, %45, %36, %29, %20, %13
  %common.ret83.op = phi double [ %19, %13 ], [ %28, %20 ], [ %35, %29 ], [ %44, %36 ], [ %51, %45 ], [ %58, %52 ], [ %65, %59 ], [ %72, %66 ], [ %9, %7 ], [ 0.000000e+00, %.lr.ph ]
  ret double %common.ret83.op

13:                                               ; preds = %.lr.ph
  %14 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %15 = tail call double @AST_eval(ptr noundef %14)
  %16 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = tail call double @AST_eval(ptr noundef %17)
  %19 = tail call double @pow(double noundef %15, double noundef %18) #6
  br label %common.ret83

20:                                               ; preds = %.lr.ph
  %21 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %22 = tail call double @AST_eval(ptr noundef %21)
  %23 = tail call double @log(double noundef %22) #6
  %24 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = tail call double @AST_eval(ptr noundef %25)
  %27 = tail call double @log(double noundef %26) #6
  %28 = fdiv double %23, %27
  br label %common.ret83

29:                                               ; preds = %.lr.ph
  %30 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %31 = tail call double @AST_eval(ptr noundef %30)
  %32 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = tail call double @AST_eval(ptr noundef %33)
  %35 = tail call double @fmod(double noundef %31, double noundef %34) #6
  br label %common.ret83

36:                                               ; preds = %.lr.ph
  %37 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %38 = tail call double @AST_eval(ptr noundef %37)
  %39 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = tail call double @AST_eval(ptr noundef %40)
  %42 = fdiv double %38, %41
  %43 = fptosi double %42 to i32
  %44 = sitofp i32 %43 to double
  br label %common.ret83

45:                                               ; preds = %.lr.ph
  %46 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %47 = tail call double @AST_eval(ptr noundef %46)
  %48 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %49 = load ptr, ptr %48, align 8, !tbaa !14
  %50 = tail call double @AST_eval(ptr noundef %49)
  %51 = fmul double %47, %50
  br label %common.ret83

52:                                               ; preds = %.lr.ph
  %53 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %54 = tail call double @AST_eval(ptr noundef %53)
  %55 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = tail call double @AST_eval(ptr noundef %56)
  %58 = fdiv double %54, %57
  br label %common.ret83

59:                                               ; preds = %.lr.ph
  %60 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %61 = tail call double @AST_eval(ptr noundef %60)
  %62 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = tail call double @AST_eval(ptr noundef %63)
  %65 = fadd double %61, %64
  br label %common.ret83

66:                                               ; preds = %.lr.ph
  %67 = load ptr, ptr %.tr32, align 8, !tbaa !15
  %68 = tail call double @AST_eval(ptr noundef %67)
  %69 = getelementptr inbounds i8, ptr %.tr32, i64 8
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  %71 = tail call double @AST_eval(ptr noundef %70)
  %72 = fsub double %68, %71
  br label %common.ret83
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @pow(double noundef, double noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @log(double noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @fmod(double noundef, double noundef) local_unnamed_addr #3

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 = { mustprogress nofree nounwind willreturn memory(write) "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 = { cold nounwind }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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, !11, i64 16}
!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, !12, i64 24}
!14 = !{!10, !6, i64 8}
!15 = !{!10, !6, i64 0}


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